-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservoTask.c
117 lines (96 loc) · 2.5 KB
/
servoTask.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* servoTask.c
* author: Mithul Manoj
*/
#include "cy_pdl.h"
#include "cyhal.h"
#include "cybsp.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "global.h"
#include "servoTask.h"
#define PWM_FREQUENCY (50u)
#define PWM_DUTY_LEFT (2.4f)
#define PWM_DUTY_IDLE (4.5f)
#define PWM_DUTY_RIGHT (7.8f)
/* Function prototypes */
void set_servo_position(float value);
/* PWM object */
cyhal_pwm_t pwm_servo_control;
void servoTask(void *params)
{
(void)params;
cy_rslt_t result;
servo_command_t servoComm;
/* Initialize the PWM */
result = cyhal_pwm_init(&pwm_servo_control, P12_0, NULL);
if(CY_RSLT_SUCCESS != result)
{
CY_ASSERT(false);
}
set_servo_position(PWM_DUTY_IDLE);
/* Start the PWM */
result = cyhal_pwm_start(&pwm_servo_control);
for(;;)
{
xSemaphoreTake(switchSemaphore,portMAX_DELAY);
xQueueReceive(servoQueue, &servoComm, 0 );
switch(servoComm)
{
/* Gate not opened */
case SERVO_IDLE:
{
set_servo_position(PWM_DUTY_IDLE);
break;
}
/* Open gate to the right */
case SERVO_ARM_RIGHT:
{
set_servo_position(PWM_DUTY_RIGHT);
cyhal_system_delay_ms(2000);
set_servo_position(PWM_DUTY_IDLE);
break;
}
/* Invalid command */
default:
{
/* Handle invalid command here */
printf("System error");
break;
}
}
}
}
void set_servo_position(float value){
float previous_value = 0;
float i =0;
/* API return code */
cy_rslt_t result;
if(value > previous_value)
{
for(i=previous_value;i<=value;i=i+0.1)
{
/* Set the PWM output frequency and duty cycle */
result = cyhal_pwm_set_duty_cycle(&pwm_servo_control, i, PWM_FREQUENCY);
if(CY_RSLT_SUCCESS != result)
{
CY_ASSERT(false);
}
cyhal_system_delay_ms(10);
}
}
else
{
for(i=previous_value;i>=value;i=i-0.1){
/* Set the PWM output frequency and duty cycle */
result = cyhal_pwm_set_duty_cycle(&pwm_servo_control, i, PWM_FREQUENCY);
if(CY_RSLT_SUCCESS != result)
{
CY_ASSERT(false);
}
cyhal_system_delay_ms(10);
}
}
previous_value = value;
}