Firmware SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
bc_servo.c
1 #include <bc_servo.h>
2 
3 #define _BC_SERVO_PERIOD_CYCLES 50000
4 #define _BC_SERVO_RESOLUTION 1
5 
7 {
8  self->_channel = channel;
9  self->_angle = BC_SERVO_NO_ANGLE;
10  self->_min = BC_SERVO_DEFAULT_MIN;
11  self->_max = BC_SERVO_DEFAULT_MAX;
12 
13  bc_pwm_init(channel);
14 
15  if ((channel == BC_PWM_P0) || (channel == BC_PWM_P1) || (channel == BC_PWM_P2) || (channel == BC_PWM_P3))
16  {
17  bc_pwm_tim_configure(BC_PWM_TIM2_P0_P1_P2_P3, _BC_SERVO_RESOLUTION, _BC_SERVO_PERIOD_CYCLES);
18  }
19  else if ((channel == BC_PWM_P6) || (channel == BC_PWM_P7) || (channel == BC_PWM_P8))
20  {
21  bc_pwm_tim_configure(BC_PWM_TIM3_P6_P7_P8, _BC_SERVO_RESOLUTION, _BC_SERVO_PERIOD_CYCLES);
22  }
23  else if ((channel == BC_PWM_P12) || (channel == BC_PWM_P14))
24  {
25  bc_pwm_tim_configure(BC_PWM_TIM21_P12_P14, _BC_SERVO_RESOLUTION, _BC_SERVO_PERIOD_CYCLES);
26  }
27 }
28 
29 void bc_servo_set_angle(bc_servo_t *self, uint8_t angle)
30 {
31  if (angle > 180)
32  {
33  angle = 180;
34  }
35 
36  uint16_t pwm_value = ((self->_max - self->_min) * ((float) angle / 180.f)) + self->_min;
37 
38  bc_pwm_set(self->_channel, pwm_value);
39 
40  if (self->_angle == BC_SERVO_NO_ANGLE)
41  {
42  bc_pwm_enable(self->_channel);
43  }
44 
45  self->_angle = angle;
46 }
47 
49 {
50  return self->_angle;
51 }
52 
53 void bc_servo_set_pulse_limits(bc_servo_t *self, uint16_t min_us, uint16_t max_us)
54 {
55  self->_min = min_us;
56 
57  self->_max = max_us;
58 }
59 
60 void bc_servo_set_microseconds(bc_servo_t *self, uint16_t us)
61 {
62  bc_pwm_set(self->_channel, us);
63 
64  if (self->_angle == BC_SERVO_NO_ANGLE)
65  {
66  bc_pwm_enable(self->_channel);
67  }
68 
69  self->_angle = BC_SERVO_UNKNOWN_ANGLE;
70 }
Servo instance.
Definition: bc_servo.h:17
void bc_servo_init(bc_servo_t *self, bc_pwm_channel_t channel)
Initialize Servo.
Definition: bc_servo.c:6
void bc_servo_set_pulse_limits(bc_servo_t *self, uint16_t min_us, uint16_t max_us)
Set pulse limits.
Definition: bc_servo.c:53
bc_pwm_channel_t
PWM channels.
Definition: bc_pwm.h:14
void bc_servo_set_microseconds(bc_servo_t *self, uint16_t us)
Set Microseconds.
Definition: bc_servo.c:60
uint8_t bc_servo_get_angle(bc_servo_t *self)
Get angle.
Definition: bc_servo.c:48
void bc_pwm_enable(bc_pwm_channel_t channel)
Enable PWM output on GPIO pin.
Definition: bc_pwm.c:174
void bc_servo_set_angle(bc_servo_t *self, uint8_t angle)
Set angle.
Definition: bc_servo.c:29
void bc_pwm_tim_configure(bc_pwm_tim_t tim, uint32_t resolution_us, uint32_t period_cycles)
Reconfigure TIM3.
Definition: bc_pwm.c:115
void bc_pwm_init(bc_pwm_channel_t channel)
Initialize PWM timers based on GPIO pin.
Definition: bc_pwm.c:141
void bc_pwm_set(bc_pwm_channel_t channel, uint16_t pwm_value)
Set PWM value for GPIO pin.
Definition: bc_pwm.c:193