blob: 2ebe64376ff970d390ace160e8e9b9dbe220d5be [file] [log] [blame]
Damien George626f6b82014-03-22 15:52:33 +00001#include <stdio.h>
2
3#include "stm32f4xx_hal.h"
4
Damien George0119fc72014-03-22 18:34:16 +00005#include "nlr.h"
Damien George626f6b82014-03-22 15:52:33 +00006#include "misc.h"
7#include "mpconfig.h"
8#include "qstr.h"
9#include "obj.h"
Damien Georgeeed6f262014-03-26 22:46:03 +000010#include "runtime.h"
Damien Georgea12be912014-04-02 15:09:36 +010011#include "timer.h"
Damien George626f6b82014-03-22 15:52:33 +000012#include "servo.h"
13
Damien George8d096402014-04-29 22:55:34 +010014/// \moduleref pyb
15/// \class Servo - 3-wire hobby servo driver
16///
17/// Servo controls standard hobby servos with 3-wires (ground, power, signal).
18
Damien George626f6b82014-03-22 15:52:33 +000019// this servo driver uses hardware PWM to drive servos on PA0, PA1, PA2, PA3 = X1, X2, X3, X4
20// TIM2 and TIM5 have CH1, CH2, CH3, CH4 on PA0-PA3 respectively
21// they are both 32-bit counters with 16-bit prescaler
Damien Georgea12be912014-04-02 15:09:36 +010022// we use TIM5
Damien George626f6b82014-03-22 15:52:33 +000023
Damien George0119fc72014-03-22 18:34:16 +000024#define PYB_SERVO_NUM (4)
25
26typedef struct _pyb_servo_obj_t {
27 mp_obj_base_t base;
Damien George22934712014-04-09 00:45:45 +010028 uint8_t servo_id;
29 uint8_t pulse_min; // units of 10us
30 uint8_t pulse_max; // units of 10us
31 uint8_t pulse_centre; // units of 10us
32 uint8_t pulse_angle_90; // units of 10us; pulse at 90 degrees, minus pulse_centre
33 uint8_t pulse_speed_100; // units of 10us; pulse at 100% forward speed, minus pulse_centre
34 uint16_t pulse_cur; // units of 10us
35 uint16_t pulse_dest; // units of 10us
Damien George0119fc72014-03-22 18:34:16 +000036 int16_t pulse_accum;
Damien George22934712014-04-09 00:45:45 +010037 uint16_t time_left;
Damien George0119fc72014-03-22 18:34:16 +000038} pyb_servo_obj_t;
39
Damien George0119fc72014-03-22 18:34:16 +000040STATIC pyb_servo_obj_t pyb_servo_obj[PYB_SERVO_NUM];
41
Damien George626f6b82014-03-22 15:52:33 +000042void servo_init(void) {
Damien Georgea12be912014-04-02 15:09:36 +010043 timer_tim5_init();
Damien George0119fc72014-03-22 18:34:16 +000044
45 // reset servo objects
46 for (int i = 0; i < PYB_SERVO_NUM; i++) {
Damien Georgee90eefc2014-04-02 19:55:08 +010047 pyb_servo_obj[i].base.type = &pyb_servo_type;
Damien George0119fc72014-03-22 18:34:16 +000048 pyb_servo_obj[i].servo_id = i + 1;
Damien George22934712014-04-09 00:45:45 +010049 pyb_servo_obj[i].pulse_min = 64;
50 pyb_servo_obj[i].pulse_max = 242;
51 pyb_servo_obj[i].pulse_centre = 150;
52 pyb_servo_obj[i].pulse_angle_90 = 97;
53 pyb_servo_obj[i].pulse_speed_100 = 70;
54 pyb_servo_obj[i].pulse_cur = 150;
Damien George0119fc72014-03-22 18:34:16 +000055 pyb_servo_obj[i].pulse_dest = 0;
Damien George22934712014-04-09 00:45:45 +010056 pyb_servo_obj[i].time_left = 0;
Damien George0119fc72014-03-22 18:34:16 +000057 }
Damien George626f6b82014-03-22 15:52:33 +000058}
59
Damien George0119fc72014-03-22 18:34:16 +000060void servo_timer_irq_callback(void) {
Damien George0119fc72014-03-22 18:34:16 +000061 bool need_it = false;
62 for (int i = 0; i < PYB_SERVO_NUM; i++) {
63 pyb_servo_obj_t *s = &pyb_servo_obj[i];
64 if (s->pulse_cur != s->pulse_dest) {
Damien George22934712014-04-09 00:45:45 +010065 // clamp pulse to within min/max
66 if (s->pulse_dest < s->pulse_min) {
67 s->pulse_dest = s->pulse_min;
68 } else if (s->pulse_dest > s->pulse_max) {
69 s->pulse_dest = s->pulse_max;
70 }
71 // adjust cur to get closer to dest
Damien George0119fc72014-03-22 18:34:16 +000072 if (s->time_left <= 1) {
73 s->pulse_cur = s->pulse_dest;
74 s->time_left = 0;
75 } else {
76 s->pulse_accum += s->pulse_dest - s->pulse_cur;
77 s->pulse_cur += s->pulse_accum / s->time_left;
78 s->pulse_accum %= s->time_left;
79 s->time_left--;
80 need_it = true;
81 }
Damien George22934712014-04-09 00:45:45 +010082 // set the pulse width
Damien George0119fc72014-03-22 18:34:16 +000083 switch (s->servo_id) {
Damien Georgea12be912014-04-02 15:09:36 +010084 case 1: TIM5->CCR1 = s->pulse_cur; break;
85 case 2: TIM5->CCR2 = s->pulse_cur; break;
86 case 3: TIM5->CCR3 = s->pulse_cur; break;
87 case 4: TIM5->CCR4 = s->pulse_cur; break;
Damien George0119fc72014-03-22 18:34:16 +000088 }
89 }
90 }
91 if (need_it) {
Damien Georgea12be912014-04-02 15:09:36 +010092 __HAL_TIM_ENABLE_IT(&TIM5_Handle, TIM_IT_UPDATE);
Damien George0119fc72014-03-22 18:34:16 +000093 } else {
Damien Georgea12be912014-04-02 15:09:36 +010094 __HAL_TIM_DISABLE_IT(&TIM5_Handle, TIM_IT_UPDATE);
Damien George0119fc72014-03-22 18:34:16 +000095 }
96}
97
98STATIC void servo_init_channel(pyb_servo_obj_t *s) {
Damien George626f6b82014-03-22 15:52:33 +000099 uint32_t pin;
100 uint32_t channel;
Damien George0119fc72014-03-22 18:34:16 +0000101 switch (s->servo_id) {
Damien George626f6b82014-03-22 15:52:33 +0000102 case 1: pin = GPIO_PIN_0; channel = TIM_CHANNEL_1; break;
103 case 2: pin = GPIO_PIN_1; channel = TIM_CHANNEL_2; break;
104 case 3: pin = GPIO_PIN_2; channel = TIM_CHANNEL_3; break;
105 case 4: pin = GPIO_PIN_3; channel = TIM_CHANNEL_4; break;
106 default: return;
107 }
108
109 // GPIO configuration
110 GPIO_InitTypeDef GPIO_InitStructure;
111 GPIO_InitStructure.Pin = pin;
112 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
113 GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
114 GPIO_InitStructure.Pull = GPIO_NOPULL;
Damien Georgea12be912014-04-02 15:09:36 +0100115 GPIO_InitStructure.Alternate = GPIO_AF2_TIM5;
Damien George626f6b82014-03-22 15:52:33 +0000116 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
117
118 // PWM mode configuration
119 TIM_OC_InitTypeDef oc_init;
120 oc_init.OCMode = TIM_OCMODE_PWM1;
Damien George0119fc72014-03-22 18:34:16 +0000121 oc_init.Pulse = s->pulse_cur; // units of 10us
Damien George626f6b82014-03-22 15:52:33 +0000122 oc_init.OCPolarity = TIM_OCPOLARITY_HIGH;
123 oc_init.OCFastMode = TIM_OCFAST_DISABLE;
Damien Georgea12be912014-04-02 15:09:36 +0100124 HAL_TIM_PWM_ConfigChannel(&TIM5_Handle, &oc_init, channel);
Damien George626f6b82014-03-22 15:52:33 +0000125
126 // start PWM
Damien Georgea12be912014-04-02 15:09:36 +0100127 HAL_TIM_PWM_Start(&TIM5_Handle, channel);
Damien George626f6b82014-03-22 15:52:33 +0000128}
129
130/******************************************************************************/
131// Micro Python bindings
132
133STATIC mp_obj_t pyb_servo_set(mp_obj_t port, mp_obj_t value) {
134 int p = mp_obj_get_int(port);
135 int v = mp_obj_get_int(value);
136 if (v < 50) { v = 50; }
137 if (v > 250) { v = 250; }
138 switch (p) {
Damien Georgea12be912014-04-02 15:09:36 +0100139 case 1: TIM5->CCR1 = v; break;
140 case 2: TIM5->CCR2 = v; break;
141 case 3: TIM5->CCR3 = v; break;
142 case 4: TIM5->CCR4 = v; break;
Damien George626f6b82014-03-22 15:52:33 +0000143 }
144 return mp_const_none;
145}
146
147MP_DEFINE_CONST_FUN_OBJ_2(pyb_servo_set_obj, pyb_servo_set);
148
149STATIC mp_obj_t pyb_pwm_set(mp_obj_t period, mp_obj_t pulse) {
150 int pe = mp_obj_get_int(period);
151 int pu = mp_obj_get_int(pulse);
Damien Georgea12be912014-04-02 15:09:36 +0100152 TIM5->ARR = pe;
153 TIM5->CCR3 = pu;
Damien George626f6b82014-03-22 15:52:33 +0000154 return mp_const_none;
155}
156
157MP_DEFINE_CONST_FUN_OBJ_2(pyb_pwm_set_obj, pyb_pwm_set);
158
Damien George0e9d96f2014-03-24 11:48:39 +0000159STATIC void pyb_servo_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damien George626f6b82014-03-22 15:52:33 +0000160 pyb_servo_obj_t *self = self_in;
Damien George22934712014-04-09 00:45:45 +0100161 print(env, "<Servo %lu at %luus>", self->servo_id, 10 * self->pulse_cur);
Damien George626f6b82014-03-22 15:52:33 +0000162}
163
Damien George8d096402014-04-29 22:55:34 +0100164/// \classmethod \constructor(id)
165/// Create a servo object. `id` is 1-4.
Damien George0e9d96f2014-03-24 11:48:39 +0000166STATIC mp_obj_t pyb_servo_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
167 // check arguments
Damien Georged6894302014-04-20 00:16:30 +0100168 mp_arg_check_num(n_args, n_kw, 1, 1, false);
Damien George0e9d96f2014-03-24 11:48:39 +0000169
170 // get servo number
171 machine_int_t servo_id = mp_obj_get_int(args[0]) - 1;
172
173 // check servo number
174 if (!(0 <= servo_id && servo_id < PYB_SERVO_NUM)) {
Damien Georgeea13f402014-04-05 18:32:08 +0100175 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Servo %d does not exist", servo_id + 1));
Damien George0e9d96f2014-03-24 11:48:39 +0000176 }
177
178 // get and init servo object
179 pyb_servo_obj_t *s = &pyb_servo_obj[servo_id];
180 s->pulse_dest = s->pulse_cur;
181 s->time_left = 0;
182 servo_init_channel(s);
183
184 return s;
185}
186
Damien George8d096402014-04-29 22:55:34 +0100187/// \method pulse_width([value])
188/// Get or set the pulse width in milliseconds.
Damien George22934712014-04-09 00:45:45 +0100189STATIC mp_obj_t pyb_servo_pulse_width(uint n_args, const mp_obj_t *args) {
190 pyb_servo_obj_t *self = args[0];
191 if (n_args == 1) {
192 // get pulse width, in us
193 return mp_obj_new_int(10 * self->pulse_cur);
194 } else {
195 // set pulse width, in us
196 self->pulse_dest = mp_obj_get_int(args[1]) / 10;
197 self->time_left = 0;
198 servo_timer_irq_callback();
199 return mp_const_none;
200 }
201}
Damien George22934712014-04-09 00:45:45 +0100202STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_servo_pulse_width_obj, 1, 2, pyb_servo_pulse_width);
203
Damien George8d096402014-04-29 22:55:34 +0100204/// \method calibration([pulse_min, pulse_max, pulse_centre, [pulse_angle_90, pulse_speed_100]])
205/// Get or set the calibration of the servo timing.
Damien Georgeaf797f42014-04-21 18:21:07 +0100206STATIC mp_obj_t pyb_servo_calibration(uint n_args, const mp_obj_t *args) {
Damien George22934712014-04-09 00:45:45 +0100207 pyb_servo_obj_t *self = args[0];
208 if (n_args == 1) {
209 // get calibration values
210 mp_obj_t tuple[5];
211 tuple[0] = mp_obj_new_int(10 * self->pulse_min);
212 tuple[1] = mp_obj_new_int(10 * self->pulse_max);
213 tuple[2] = mp_obj_new_int(10 * self->pulse_centre);
214 tuple[3] = mp_obj_new_int(10 * (self->pulse_angle_90 + self->pulse_centre));
215 tuple[4] = mp_obj_new_int(10 * (self->pulse_speed_100 + self->pulse_centre));
216 return mp_obj_new_tuple(5, tuple);
217 } else if (n_args >= 4) {
218 // set min, max, centre
219 self->pulse_min = mp_obj_get_int(args[1]) / 10;
220 self->pulse_max = mp_obj_get_int(args[2]) / 10;
221 self->pulse_centre = mp_obj_get_int(args[3]) / 10;
222 if (n_args == 4) {
223 return mp_const_none;
224 } else if (n_args == 6) {
225 self->pulse_angle_90 = mp_obj_get_int(args[4]) / 10 - self->pulse_centre;
226 self->pulse_speed_100 = mp_obj_get_int(args[5]) / 10 - self->pulse_centre;
227 return mp_const_none;
228 }
229 }
230
231 // bad number of arguments
Damien Georgeaf797f42014-04-21 18:21:07 +0100232 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "calibration expecting 1, 4 or 6 arguments, got %d", n_args));
Damien George22934712014-04-09 00:45:45 +0100233}
Damien Georgeaf797f42014-04-21 18:21:07 +0100234STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_servo_calibration_obj, 1, 6, pyb_servo_calibration);
Damien George22934712014-04-09 00:45:45 +0100235
Damien George8d096402014-04-29 22:55:34 +0100236/// \method angle([angle, time=0])
237/// Get or set the angle of the servo.
238///
239/// - `angle` is the angle to move to in degrees.
240/// - `time` is the number of milliseconds to take to get to the specified angle.
Damien George0e9d96f2014-03-24 11:48:39 +0000241STATIC mp_obj_t pyb_servo_angle(uint n_args, const mp_obj_t *args) {
Damien George0119fc72014-03-22 18:34:16 +0000242 pyb_servo_obj_t *self = args[0];
243 if (n_args == 1) {
244 // get angle
Damien George22934712014-04-09 00:45:45 +0100245 return mp_obj_new_int((self->pulse_cur - self->pulse_centre) * 90 / self->pulse_angle_90);
Damien George0119fc72014-03-22 18:34:16 +0000246 } else {
Damien George626f6b82014-03-22 15:52:33 +0000247#if MICROPY_ENABLE_FLOAT
Damien George22934712014-04-09 00:45:45 +0100248 self->pulse_dest = self->pulse_centre + self->pulse_angle_90 * mp_obj_get_float(args[1]) / 90.0;
Damien George626f6b82014-03-22 15:52:33 +0000249#else
Damien George22934712014-04-09 00:45:45 +0100250 self->pulse_dest = self->pulse_centre + self->pulse_angle_90 * mp_obj_get_int(args[1]) / 90;
Damien George626f6b82014-03-22 15:52:33 +0000251#endif
Damien George0119fc72014-03-22 18:34:16 +0000252 if (n_args == 2) {
253 // set angle immediately
254 self->time_left = 0;
255 } else {
256 // set angle over a given time (given in milli seconds)
257 self->time_left = mp_obj_get_int(args[2]) / 20;
258 self->pulse_accum = 0;
259 }
260 servo_timer_irq_callback();
261 return mp_const_none;
Damien George626f6b82014-03-22 15:52:33 +0000262 }
Damien George626f6b82014-03-22 15:52:33 +0000263}
Damien George0e9d96f2014-03-24 11:48:39 +0000264STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_servo_angle_obj, 1, 3, pyb_servo_angle);
Damien George626f6b82014-03-22 15:52:33 +0000265
Damien George8d096402014-04-29 22:55:34 +0100266/// \method speed([speed, time=0])
267/// Get or set the speed of a continuous rotation servo.
268///
269/// - `speed` is the speed to move to change to, between -100 and 100.
270/// - `time` is the number of milliseconds to take to get to the specified speed.
Damien George22934712014-04-09 00:45:45 +0100271STATIC mp_obj_t pyb_servo_speed(uint n_args, const mp_obj_t *args) {
272 pyb_servo_obj_t *self = args[0];
273 if (n_args == 1) {
274 // get speed
275 return mp_obj_new_int((self->pulse_cur - self->pulse_centre) * 100 / self->pulse_speed_100);
276 } else {
277#if MICROPY_ENABLE_FLOAT
278 self->pulse_dest = self->pulse_centre + self->pulse_speed_100 * mp_obj_get_float(args[1]) / 100.0;
279#else
280 self->pulse_dest = self->pulse_centre + self->pulse_speed_100 * mp_obj_get_int(args[1]) / 100;
281#endif
282 if (n_args == 2) {
283 // set speed immediately
284 self->time_left = 0;
285 } else {
286 // set speed over a given time (given in milli seconds)
287 self->time_left = mp_obj_get_int(args[2]) / 20;
288 self->pulse_accum = 0;
289 }
290 servo_timer_irq_callback();
291 return mp_const_none;
292 }
293}
294
295STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_servo_speed_obj, 1, 3, pyb_servo_speed);
296
Damien George9b196cd2014-03-26 21:47:19 +0000297STATIC const mp_map_elem_t pyb_servo_locals_dict_table[] = {
Damien George22934712014-04-09 00:45:45 +0100298 { MP_OBJ_NEW_QSTR(MP_QSTR_pulse_width), (mp_obj_t)&pyb_servo_pulse_width_obj },
Damien Georgeaf797f42014-04-21 18:21:07 +0100299 { MP_OBJ_NEW_QSTR(MP_QSTR_calibration), (mp_obj_t)&pyb_servo_calibration_obj },
Damien George9b196cd2014-03-26 21:47:19 +0000300 { MP_OBJ_NEW_QSTR(MP_QSTR_angle), (mp_obj_t)&pyb_servo_angle_obj },
Damien George22934712014-04-09 00:45:45 +0100301 { MP_OBJ_NEW_QSTR(MP_QSTR_speed), (mp_obj_t)&pyb_servo_speed_obj },
Damien George626f6b82014-03-22 15:52:33 +0000302};
303
Damien George9b196cd2014-03-26 21:47:19 +0000304STATIC MP_DEFINE_CONST_DICT(pyb_servo_locals_dict, pyb_servo_locals_dict_table);
305
Damien George0e9d96f2014-03-24 11:48:39 +0000306const mp_obj_type_t pyb_servo_type = {
Damien George626f6b82014-03-22 15:52:33 +0000307 { &mp_type_type },
308 .name = MP_QSTR_Servo,
Damien George0e9d96f2014-03-24 11:48:39 +0000309 .print = pyb_servo_print,
310 .make_new = pyb_servo_make_new,
Damien George9b196cd2014-03-26 21:47:19 +0000311 .locals_dict = (mp_obj_t)&pyb_servo_locals_dict,
Damien George626f6b82014-03-22 15:52:33 +0000312};