Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | |
| 29 | #include "stm32f4xx_hal.h" |
| 30 | |
Damien George | 2cf6dfa | 2015-01-01 21:06:20 +0000 | [diff] [blame] | 31 | #include "py/nlr.h" |
| 32 | #include "py/runtime.h" |
Damien George | a12be91 | 2014-04-02 15:09:36 +0100 | [diff] [blame] | 33 | #include "timer.h" |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 34 | #include "servo.h" |
| 35 | |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 36 | /// \moduleref pyb |
| 37 | /// \class Servo - 3-wire hobby servo driver |
| 38 | /// |
| 39 | /// Servo controls standard hobby servos with 3-wires (ground, power, signal). |
| 40 | |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 41 | // this servo driver uses hardware PWM to drive servos on PA0, PA1, PA2, PA3 = X1, X2, X3, X4 |
| 42 | // TIM2 and TIM5 have CH1, CH2, CH3, CH4 on PA0-PA3 respectively |
| 43 | // they are both 32-bit counters with 16-bit prescaler |
Damien George | a12be91 | 2014-04-02 15:09:36 +0100 | [diff] [blame] | 44 | // we use TIM5 |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 45 | |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 46 | #define PYB_SERVO_NUM (4) |
| 47 | |
| 48 | typedef struct _pyb_servo_obj_t { |
| 49 | mp_obj_base_t base; |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 50 | uint8_t servo_id; |
| 51 | uint8_t pulse_min; // units of 10us |
| 52 | uint8_t pulse_max; // units of 10us |
| 53 | uint8_t pulse_centre; // units of 10us |
| 54 | uint8_t pulse_angle_90; // units of 10us; pulse at 90 degrees, minus pulse_centre |
| 55 | uint8_t pulse_speed_100; // units of 10us; pulse at 100% forward speed, minus pulse_centre |
| 56 | uint16_t pulse_cur; // units of 10us |
| 57 | uint16_t pulse_dest; // units of 10us |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 58 | int16_t pulse_accum; |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 59 | uint16_t time_left; |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 60 | } pyb_servo_obj_t; |
| 61 | |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 62 | STATIC pyb_servo_obj_t pyb_servo_obj[PYB_SERVO_NUM]; |
| 63 | |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 64 | void servo_init(void) { |
Damien George | a12be91 | 2014-04-02 15:09:36 +0100 | [diff] [blame] | 65 | timer_tim5_init(); |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 66 | |
| 67 | // reset servo objects |
| 68 | for (int i = 0; i < PYB_SERVO_NUM; i++) { |
Damien George | e90eefc | 2014-04-02 19:55:08 +0100 | [diff] [blame] | 69 | pyb_servo_obj[i].base.type = &pyb_servo_type; |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 70 | pyb_servo_obj[i].servo_id = i + 1; |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 71 | pyb_servo_obj[i].pulse_min = 64; |
| 72 | pyb_servo_obj[i].pulse_max = 242; |
| 73 | pyb_servo_obj[i].pulse_centre = 150; |
| 74 | pyb_servo_obj[i].pulse_angle_90 = 97; |
| 75 | pyb_servo_obj[i].pulse_speed_100 = 70; |
| 76 | pyb_servo_obj[i].pulse_cur = 150; |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 77 | pyb_servo_obj[i].pulse_dest = 0; |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 78 | pyb_servo_obj[i].time_left = 0; |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 79 | } |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 82 | void servo_timer_irq_callback(void) { |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 83 | bool need_it = false; |
| 84 | for (int i = 0; i < PYB_SERVO_NUM; i++) { |
| 85 | pyb_servo_obj_t *s = &pyb_servo_obj[i]; |
| 86 | if (s->pulse_cur != s->pulse_dest) { |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 87 | // clamp pulse to within min/max |
| 88 | if (s->pulse_dest < s->pulse_min) { |
| 89 | s->pulse_dest = s->pulse_min; |
| 90 | } else if (s->pulse_dest > s->pulse_max) { |
| 91 | s->pulse_dest = s->pulse_max; |
| 92 | } |
| 93 | // adjust cur to get closer to dest |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 94 | if (s->time_left <= 1) { |
| 95 | s->pulse_cur = s->pulse_dest; |
| 96 | s->time_left = 0; |
| 97 | } else { |
| 98 | s->pulse_accum += s->pulse_dest - s->pulse_cur; |
| 99 | s->pulse_cur += s->pulse_accum / s->time_left; |
| 100 | s->pulse_accum %= s->time_left; |
| 101 | s->time_left--; |
| 102 | need_it = true; |
| 103 | } |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 104 | // set the pulse width |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 105 | switch (s->servo_id) { |
Damien George | a12be91 | 2014-04-02 15:09:36 +0100 | [diff] [blame] | 106 | case 1: TIM5->CCR1 = s->pulse_cur; break; |
| 107 | case 2: TIM5->CCR2 = s->pulse_cur; break; |
| 108 | case 3: TIM5->CCR3 = s->pulse_cur; break; |
| 109 | case 4: TIM5->CCR4 = s->pulse_cur; break; |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |
| 113 | if (need_it) { |
Damien George | a12be91 | 2014-04-02 15:09:36 +0100 | [diff] [blame] | 114 | __HAL_TIM_ENABLE_IT(&TIM5_Handle, TIM_IT_UPDATE); |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 115 | } else { |
Damien George | a12be91 | 2014-04-02 15:09:36 +0100 | [diff] [blame] | 116 | __HAL_TIM_DISABLE_IT(&TIM5_Handle, TIM_IT_UPDATE); |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | STATIC void servo_init_channel(pyb_servo_obj_t *s) { |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 121 | uint32_t pin; |
| 122 | uint32_t channel; |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 123 | switch (s->servo_id) { |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 124 | case 1: pin = GPIO_PIN_0; channel = TIM_CHANNEL_1; break; |
| 125 | case 2: pin = GPIO_PIN_1; channel = TIM_CHANNEL_2; break; |
| 126 | case 3: pin = GPIO_PIN_2; channel = TIM_CHANNEL_3; break; |
| 127 | case 4: pin = GPIO_PIN_3; channel = TIM_CHANNEL_4; break; |
| 128 | default: return; |
| 129 | } |
| 130 | |
| 131 | // GPIO configuration |
| 132 | GPIO_InitTypeDef GPIO_InitStructure; |
| 133 | GPIO_InitStructure.Pin = pin; |
| 134 | GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; |
| 135 | GPIO_InitStructure.Speed = GPIO_SPEED_FAST; |
| 136 | GPIO_InitStructure.Pull = GPIO_NOPULL; |
Damien George | a12be91 | 2014-04-02 15:09:36 +0100 | [diff] [blame] | 137 | GPIO_InitStructure.Alternate = GPIO_AF2_TIM5; |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 138 | HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); |
| 139 | |
| 140 | // PWM mode configuration |
| 141 | TIM_OC_InitTypeDef oc_init; |
| 142 | oc_init.OCMode = TIM_OCMODE_PWM1; |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 143 | oc_init.Pulse = s->pulse_cur; // units of 10us |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 144 | oc_init.OCPolarity = TIM_OCPOLARITY_HIGH; |
| 145 | oc_init.OCFastMode = TIM_OCFAST_DISABLE; |
Damien George | a12be91 | 2014-04-02 15:09:36 +0100 | [diff] [blame] | 146 | HAL_TIM_PWM_ConfigChannel(&TIM5_Handle, &oc_init, channel); |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 147 | |
| 148 | // start PWM |
Damien George | a12be91 | 2014-04-02 15:09:36 +0100 | [diff] [blame] | 149 | HAL_TIM_PWM_Start(&TIM5_Handle, channel); |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /******************************************************************************/ |
| 153 | // Micro Python bindings |
| 154 | |
| 155 | STATIC mp_obj_t pyb_servo_set(mp_obj_t port, mp_obj_t value) { |
| 156 | int p = mp_obj_get_int(port); |
| 157 | int v = mp_obj_get_int(value); |
| 158 | if (v < 50) { v = 50; } |
| 159 | if (v > 250) { v = 250; } |
| 160 | switch (p) { |
Damien George | a12be91 | 2014-04-02 15:09:36 +0100 | [diff] [blame] | 161 | case 1: TIM5->CCR1 = v; break; |
| 162 | case 2: TIM5->CCR2 = v; break; |
| 163 | case 3: TIM5->CCR3 = v; break; |
| 164 | case 4: TIM5->CCR4 = v; break; |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 165 | } |
| 166 | return mp_const_none; |
| 167 | } |
| 168 | |
| 169 | MP_DEFINE_CONST_FUN_OBJ_2(pyb_servo_set_obj, pyb_servo_set); |
| 170 | |
| 171 | STATIC mp_obj_t pyb_pwm_set(mp_obj_t period, mp_obj_t pulse) { |
| 172 | int pe = mp_obj_get_int(period); |
| 173 | int pu = mp_obj_get_int(pulse); |
Damien George | a12be91 | 2014-04-02 15:09:36 +0100 | [diff] [blame] | 174 | TIM5->ARR = pe; |
| 175 | TIM5->CCR3 = pu; |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 176 | return mp_const_none; |
| 177 | } |
| 178 | |
| 179 | MP_DEFINE_CONST_FUN_OBJ_2(pyb_pwm_set_obj, pyb_pwm_set); |
| 180 | |
Damien George | 0e9d96f | 2014-03-24 11:48:39 +0000 | [diff] [blame] | 181 | STATIC void pyb_servo_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 182 | pyb_servo_obj_t *self = self_in; |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 183 | print(env, "<Servo %lu at %luus>", self->servo_id, 10 * self->pulse_cur); |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 186 | /// \classmethod \constructor(id) |
| 187 | /// Create a servo object. `id` is 1-4. |
Damien George | ecc88e9 | 2014-08-30 00:35:11 +0100 | [diff] [blame] | 188 | STATIC mp_obj_t pyb_servo_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { |
Damien George | 0e9d96f | 2014-03-24 11:48:39 +0000 | [diff] [blame] | 189 | // check arguments |
Damien George | d689430 | 2014-04-20 00:16:30 +0100 | [diff] [blame] | 190 | mp_arg_check_num(n_args, n_kw, 1, 1, false); |
Damien George | 0e9d96f | 2014-03-24 11:48:39 +0000 | [diff] [blame] | 191 | |
| 192 | // get servo number |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 193 | mp_int_t servo_id = mp_obj_get_int(args[0]) - 1; |
Damien George | 0e9d96f | 2014-03-24 11:48:39 +0000 | [diff] [blame] | 194 | |
| 195 | // check servo number |
| 196 | if (!(0 <= servo_id && servo_id < PYB_SERVO_NUM)) { |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 197 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Servo %d does not exist", servo_id + 1)); |
Damien George | 0e9d96f | 2014-03-24 11:48:39 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // get and init servo object |
| 201 | pyb_servo_obj_t *s = &pyb_servo_obj[servo_id]; |
| 202 | s->pulse_dest = s->pulse_cur; |
| 203 | s->time_left = 0; |
| 204 | servo_init_channel(s); |
| 205 | |
| 206 | return s; |
| 207 | } |
| 208 | |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 209 | /// \method pulse_width([value]) |
| 210 | /// Get or set the pulse width in milliseconds. |
Damien George | ecc88e9 | 2014-08-30 00:35:11 +0100 | [diff] [blame] | 211 | STATIC mp_obj_t pyb_servo_pulse_width(mp_uint_t n_args, const mp_obj_t *args) { |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 212 | pyb_servo_obj_t *self = args[0]; |
| 213 | if (n_args == 1) { |
| 214 | // get pulse width, in us |
| 215 | return mp_obj_new_int(10 * self->pulse_cur); |
| 216 | } else { |
| 217 | // set pulse width, in us |
| 218 | self->pulse_dest = mp_obj_get_int(args[1]) / 10; |
| 219 | self->time_left = 0; |
| 220 | servo_timer_irq_callback(); |
| 221 | return mp_const_none; |
| 222 | } |
| 223 | } |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 224 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_servo_pulse_width_obj, 1, 2, pyb_servo_pulse_width); |
| 225 | |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 226 | /// \method calibration([pulse_min, pulse_max, pulse_centre, [pulse_angle_90, pulse_speed_100]]) |
| 227 | /// Get or set the calibration of the servo timing. |
Damien George | baa2afb | 2014-05-03 16:42:27 +0100 | [diff] [blame] | 228 | // TODO should accept 1 arg, a 5-tuple of values to set |
Damien George | ecc88e9 | 2014-08-30 00:35:11 +0100 | [diff] [blame] | 229 | STATIC mp_obj_t pyb_servo_calibration(mp_uint_t n_args, const mp_obj_t *args) { |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 230 | pyb_servo_obj_t *self = args[0]; |
| 231 | if (n_args == 1) { |
| 232 | // get calibration values |
| 233 | mp_obj_t tuple[5]; |
| 234 | tuple[0] = mp_obj_new_int(10 * self->pulse_min); |
| 235 | tuple[1] = mp_obj_new_int(10 * self->pulse_max); |
| 236 | tuple[2] = mp_obj_new_int(10 * self->pulse_centre); |
| 237 | tuple[3] = mp_obj_new_int(10 * (self->pulse_angle_90 + self->pulse_centre)); |
| 238 | tuple[4] = mp_obj_new_int(10 * (self->pulse_speed_100 + self->pulse_centre)); |
| 239 | return mp_obj_new_tuple(5, tuple); |
| 240 | } else if (n_args >= 4) { |
| 241 | // set min, max, centre |
| 242 | self->pulse_min = mp_obj_get_int(args[1]) / 10; |
| 243 | self->pulse_max = mp_obj_get_int(args[2]) / 10; |
| 244 | self->pulse_centre = mp_obj_get_int(args[3]) / 10; |
| 245 | if (n_args == 4) { |
| 246 | return mp_const_none; |
| 247 | } else if (n_args == 6) { |
| 248 | self->pulse_angle_90 = mp_obj_get_int(args[4]) / 10 - self->pulse_centre; |
| 249 | self->pulse_speed_100 = mp_obj_get_int(args[5]) / 10 - self->pulse_centre; |
| 250 | return mp_const_none; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // bad number of arguments |
Damien George | af797f4 | 2014-04-21 18:21:07 +0100 | [diff] [blame] | 255 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "calibration expecting 1, 4 or 6 arguments, got %d", n_args)); |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 256 | } |
Damien George | af797f4 | 2014-04-21 18:21:07 +0100 | [diff] [blame] | 257 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_servo_calibration_obj, 1, 6, pyb_servo_calibration); |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 258 | |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 259 | /// \method angle([angle, time=0]) |
| 260 | /// Get or set the angle of the servo. |
| 261 | /// |
| 262 | /// - `angle` is the angle to move to in degrees. |
| 263 | /// - `time` is the number of milliseconds to take to get to the specified angle. |
Damien George | ecc88e9 | 2014-08-30 00:35:11 +0100 | [diff] [blame] | 264 | STATIC mp_obj_t pyb_servo_angle(mp_uint_t n_args, const mp_obj_t *args) { |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 265 | pyb_servo_obj_t *self = args[0]; |
| 266 | if (n_args == 1) { |
| 267 | // get angle |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 268 | return mp_obj_new_int((self->pulse_cur - self->pulse_centre) * 90 / self->pulse_angle_90); |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 269 | } else { |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 270 | #if MICROPY_PY_BUILTINS_FLOAT |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 271 | self->pulse_dest = self->pulse_centre + self->pulse_angle_90 * mp_obj_get_float(args[1]) / 90.0; |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 272 | #else |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 273 | self->pulse_dest = self->pulse_centre + self->pulse_angle_90 * mp_obj_get_int(args[1]) / 90; |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 274 | #endif |
Damien George | 0119fc7 | 2014-03-22 18:34:16 +0000 | [diff] [blame] | 275 | if (n_args == 2) { |
| 276 | // set angle immediately |
| 277 | self->time_left = 0; |
| 278 | } else { |
| 279 | // set angle over a given time (given in milli seconds) |
| 280 | self->time_left = mp_obj_get_int(args[2]) / 20; |
| 281 | self->pulse_accum = 0; |
| 282 | } |
| 283 | servo_timer_irq_callback(); |
| 284 | return mp_const_none; |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 285 | } |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 286 | } |
Damien George | 0e9d96f | 2014-03-24 11:48:39 +0000 | [diff] [blame] | 287 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_servo_angle_obj, 1, 3, pyb_servo_angle); |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 288 | |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 289 | /// \method speed([speed, time=0]) |
| 290 | /// Get or set the speed of a continuous rotation servo. |
| 291 | /// |
| 292 | /// - `speed` is the speed to move to change to, between -100 and 100. |
| 293 | /// - `time` is the number of milliseconds to take to get to the specified speed. |
Damien George | ecc88e9 | 2014-08-30 00:35:11 +0100 | [diff] [blame] | 294 | STATIC mp_obj_t pyb_servo_speed(mp_uint_t n_args, const mp_obj_t *args) { |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 295 | pyb_servo_obj_t *self = args[0]; |
| 296 | if (n_args == 1) { |
| 297 | // get speed |
| 298 | return mp_obj_new_int((self->pulse_cur - self->pulse_centre) * 100 / self->pulse_speed_100); |
| 299 | } else { |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 300 | #if MICROPY_PY_BUILTINS_FLOAT |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 301 | self->pulse_dest = self->pulse_centre + self->pulse_speed_100 * mp_obj_get_float(args[1]) / 100.0; |
| 302 | #else |
| 303 | self->pulse_dest = self->pulse_centre + self->pulse_speed_100 * mp_obj_get_int(args[1]) / 100; |
| 304 | #endif |
| 305 | if (n_args == 2) { |
| 306 | // set speed immediately |
| 307 | self->time_left = 0; |
| 308 | } else { |
| 309 | // set speed over a given time (given in milli seconds) |
| 310 | self->time_left = mp_obj_get_int(args[2]) / 20; |
| 311 | self->pulse_accum = 0; |
| 312 | } |
| 313 | servo_timer_irq_callback(); |
| 314 | return mp_const_none; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_servo_speed_obj, 1, 3, pyb_servo_speed); |
| 319 | |
Damien George | 9b196cd | 2014-03-26 21:47:19 +0000 | [diff] [blame] | 320 | STATIC const mp_map_elem_t pyb_servo_locals_dict_table[] = { |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 321 | { MP_OBJ_NEW_QSTR(MP_QSTR_pulse_width), (mp_obj_t)&pyb_servo_pulse_width_obj }, |
Damien George | af797f4 | 2014-04-21 18:21:07 +0100 | [diff] [blame] | 322 | { MP_OBJ_NEW_QSTR(MP_QSTR_calibration), (mp_obj_t)&pyb_servo_calibration_obj }, |
Damien George | 9b196cd | 2014-03-26 21:47:19 +0000 | [diff] [blame] | 323 | { MP_OBJ_NEW_QSTR(MP_QSTR_angle), (mp_obj_t)&pyb_servo_angle_obj }, |
Damien George | 2293471 | 2014-04-09 00:45:45 +0100 | [diff] [blame] | 324 | { MP_OBJ_NEW_QSTR(MP_QSTR_speed), (mp_obj_t)&pyb_servo_speed_obj }, |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
Damien George | 9b196cd | 2014-03-26 21:47:19 +0000 | [diff] [blame] | 327 | STATIC MP_DEFINE_CONST_DICT(pyb_servo_locals_dict, pyb_servo_locals_dict_table); |
| 328 | |
Damien George | 0e9d96f | 2014-03-24 11:48:39 +0000 | [diff] [blame] | 329 | const mp_obj_type_t pyb_servo_type = { |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 330 | { &mp_type_type }, |
| 331 | .name = MP_QSTR_Servo, |
Damien George | 0e9d96f | 2014-03-24 11:48:39 +0000 | [diff] [blame] | 332 | .print = pyb_servo_print, |
| 333 | .make_new = pyb_servo_make_new, |
Damien George | 9b196cd | 2014-03-26 21:47:19 +0000 | [diff] [blame] | 334 | .locals_dict = (mp_obj_t)&pyb_servo_locals_dict, |
Damien George | 626f6b8 | 2014-03-22 15:52:33 +0000 | [diff] [blame] | 335 | }; |