2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-22 11:52:33 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-01-01 16:06:20 -05:00
|
|
|
#include "py/runtime.h"
|
2017-07-14 02:38:15 -04:00
|
|
|
#include "py/mphal.h"
|
|
|
|
#include "pin.h"
|
2014-04-02 10:09:36 -04:00
|
|
|
#include "timer.h"
|
2014-03-22 11:52:33 -04:00
|
|
|
#include "servo.h"
|
|
|
|
|
2017-07-14 03:41:43 -04:00
|
|
|
#if MICROPY_HW_ENABLE_SERVO
|
|
|
|
|
2017-07-14 02:38:15 -04:00
|
|
|
// This file implements the pyb.Servo class which controls standard hobby servo
|
|
|
|
// motors that have 3-wires (ground, power, signal).
|
|
|
|
//
|
|
|
|
// The driver uses hardware PWM to drive servos on pins X1, X2, X3, X4 which are
|
|
|
|
// assumed to be on PA0, PA1, PA2, PA3 but not necessarily in that order (the
|
|
|
|
// pins PA0-PA3 are used directly if the X pins are not defined).
|
|
|
|
//
|
|
|
|
// TIM2 and TIM5 have CH1-CH4 on PA0-PA3 respectively. They are both 32-bit
|
|
|
|
// counters with 16-bit prescaler. TIM5 is used by this driver.
|
2014-03-22 11:52:33 -04:00
|
|
|
|
2014-03-22 14:34:16 -04:00
|
|
|
#define PYB_SERVO_NUM (4)
|
|
|
|
|
|
|
|
typedef struct _pyb_servo_obj_t {
|
|
|
|
mp_obj_base_t base;
|
2017-07-14 02:38:15 -04:00
|
|
|
const pin_obj_t *pin;
|
2014-04-08 19:45:45 -04:00
|
|
|
uint8_t pulse_min; // units of 10us
|
|
|
|
uint8_t pulse_max; // units of 10us
|
|
|
|
uint8_t pulse_centre; // units of 10us
|
|
|
|
uint8_t pulse_angle_90; // units of 10us; pulse at 90 degrees, minus pulse_centre
|
|
|
|
uint8_t pulse_speed_100; // units of 10us; pulse at 100% forward speed, minus pulse_centre
|
|
|
|
uint16_t pulse_cur; // units of 10us
|
|
|
|
uint16_t pulse_dest; // units of 10us
|
2014-03-22 14:34:16 -04:00
|
|
|
int16_t pulse_accum;
|
2014-04-08 19:45:45 -04:00
|
|
|
uint16_t time_left;
|
2014-03-22 14:34:16 -04:00
|
|
|
} pyb_servo_obj_t;
|
|
|
|
|
|
|
|
STATIC pyb_servo_obj_t pyb_servo_obj[PYB_SERVO_NUM];
|
|
|
|
|
2014-03-22 11:52:33 -04:00
|
|
|
void servo_init(void) {
|
2014-03-22 14:34:16 -04:00
|
|
|
// reset servo objects
|
|
|
|
for (int i = 0; i < PYB_SERVO_NUM; i++) {
|
2014-04-02 14:55:08 -04:00
|
|
|
pyb_servo_obj[i].base.type = &pyb_servo_type;
|
2014-04-08 19:45:45 -04:00
|
|
|
pyb_servo_obj[i].pulse_min = 64;
|
|
|
|
pyb_servo_obj[i].pulse_max = 242;
|
|
|
|
pyb_servo_obj[i].pulse_centre = 150;
|
|
|
|
pyb_servo_obj[i].pulse_angle_90 = 97;
|
|
|
|
pyb_servo_obj[i].pulse_speed_100 = 70;
|
|
|
|
pyb_servo_obj[i].pulse_cur = 150;
|
2014-03-22 14:34:16 -04:00
|
|
|
pyb_servo_obj[i].pulse_dest = 0;
|
2014-04-08 19:45:45 -04:00
|
|
|
pyb_servo_obj[i].time_left = 0;
|
2014-03-22 14:34:16 -04:00
|
|
|
}
|
2017-07-14 02:38:15 -04:00
|
|
|
|
|
|
|
// assign servo objects to specific pins (must be some permutation of PA0-PA3)
|
|
|
|
#ifdef pyb_pin_X1
|
2018-03-28 01:13:21 -04:00
|
|
|
pyb_servo_obj[0].pin = pyb_pin_X1;
|
|
|
|
pyb_servo_obj[1].pin = pyb_pin_X2;
|
|
|
|
pyb_servo_obj[2].pin = pyb_pin_X3;
|
|
|
|
pyb_servo_obj[3].pin = pyb_pin_X4;
|
2017-07-14 02:38:15 -04:00
|
|
|
#else
|
2018-03-28 01:13:21 -04:00
|
|
|
pyb_servo_obj[0].pin = pin_A0;
|
|
|
|
pyb_servo_obj[1].pin = pin_A1;
|
|
|
|
pyb_servo_obj[2].pin = pin_A2;
|
|
|
|
pyb_servo_obj[3].pin = pin_A3;
|
2017-07-14 02:38:15 -04:00
|
|
|
#endif
|
2014-03-22 14:34:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void servo_timer_irq_callback(void) {
|
|
|
|
bool need_it = false;
|
|
|
|
for (int i = 0; i < PYB_SERVO_NUM; i++) {
|
|
|
|
pyb_servo_obj_t *s = &pyb_servo_obj[i];
|
|
|
|
if (s->pulse_cur != s->pulse_dest) {
|
2014-04-08 19:45:45 -04:00
|
|
|
// clamp pulse to within min/max
|
|
|
|
if (s->pulse_dest < s->pulse_min) {
|
|
|
|
s->pulse_dest = s->pulse_min;
|
|
|
|
} else if (s->pulse_dest > s->pulse_max) {
|
|
|
|
s->pulse_dest = s->pulse_max;
|
|
|
|
}
|
|
|
|
// adjust cur to get closer to dest
|
2014-03-22 14:34:16 -04:00
|
|
|
if (s->time_left <= 1) {
|
|
|
|
s->pulse_cur = s->pulse_dest;
|
|
|
|
s->time_left = 0;
|
|
|
|
} else {
|
|
|
|
s->pulse_accum += s->pulse_dest - s->pulse_cur;
|
|
|
|
s->pulse_cur += s->pulse_accum / s->time_left;
|
|
|
|
s->pulse_accum %= s->time_left;
|
|
|
|
s->time_left--;
|
|
|
|
need_it = true;
|
|
|
|
}
|
2014-04-08 19:45:45 -04:00
|
|
|
// set the pulse width
|
2017-07-14 02:38:15 -04:00
|
|
|
*(&TIM5->CCR1 + s->pin->pin) = s->pulse_cur;
|
2014-03-22 14:34:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (need_it) {
|
2014-04-02 10:09:36 -04:00
|
|
|
__HAL_TIM_ENABLE_IT(&TIM5_Handle, TIM_IT_UPDATE);
|
2014-03-22 14:34:16 -04:00
|
|
|
} else {
|
2014-04-02 10:09:36 -04:00
|
|
|
__HAL_TIM_DISABLE_IT(&TIM5_Handle, TIM_IT_UPDATE);
|
2014-03-22 14:34:16 -04:00
|
|
|
}
|
2014-03-22 11:52:33 -04:00
|
|
|
}
|
|
|
|
|
2014-03-22 14:34:16 -04:00
|
|
|
STATIC void servo_init_channel(pyb_servo_obj_t *s) {
|
2017-07-14 02:38:15 -04:00
|
|
|
static const uint8_t channel_table[4] =
|
2020-02-26 23:36:53 -05:00
|
|
|
{TIM_CHANNEL_1, TIM_CHANNEL_2, TIM_CHANNEL_3, TIM_CHANNEL_4};
|
2017-07-14 02:38:15 -04:00
|
|
|
uint32_t channel = channel_table[s->pin->pin];
|
2014-03-22 11:52:33 -04:00
|
|
|
|
|
|
|
// GPIO configuration
|
2017-07-14 02:38:15 -04:00
|
|
|
mp_hal_pin_config(s->pin, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, GPIO_AF2_TIM5);
|
2014-03-22 11:52:33 -04:00
|
|
|
|
2018-11-27 20:06:24 -05:00
|
|
|
if (__HAL_RCC_TIM5_IS_CLK_DISABLED()) {
|
|
|
|
timer_tim5_init();
|
|
|
|
}
|
|
|
|
|
2014-03-22 11:52:33 -04:00
|
|
|
// PWM mode configuration
|
|
|
|
TIM_OC_InitTypeDef oc_init;
|
|
|
|
oc_init.OCMode = TIM_OCMODE_PWM1;
|
2014-03-22 14:34:16 -04:00
|
|
|
oc_init.Pulse = s->pulse_cur; // units of 10us
|
2014-03-22 11:52:33 -04:00
|
|
|
oc_init.OCPolarity = TIM_OCPOLARITY_HIGH;
|
|
|
|
oc_init.OCFastMode = TIM_OCFAST_DISABLE;
|
2014-04-02 10:09:36 -04:00
|
|
|
HAL_TIM_PWM_ConfigChannel(&TIM5_Handle, &oc_init, channel);
|
2014-03-22 11:52:33 -04:00
|
|
|
|
|
|
|
// start PWM
|
2014-04-02 10:09:36 -04:00
|
|
|
HAL_TIM_PWM_Start(&TIM5_Handle, channel);
|
2014-03-22 11:52:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-06-30 03:22:17 -04:00
|
|
|
// MicroPython bindings
|
2014-03-22 11:52:33 -04:00
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_servo_set(mp_obj_t port, mp_obj_t value) {
|
|
|
|
int p = mp_obj_get_int(port);
|
|
|
|
int v = mp_obj_get_int(value);
|
2020-02-26 23:36:53 -05:00
|
|
|
if (v < 50) {
|
|
|
|
v = 50;
|
|
|
|
}
|
|
|
|
if (v > 250) {
|
|
|
|
v = 250;
|
|
|
|
}
|
2014-03-22 11:52:33 -04:00
|
|
|
switch (p) {
|
2020-02-26 23:36:53 -05:00
|
|
|
case 1:
|
|
|
|
TIM5->CCR1 = v;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
TIM5->CCR2 = v;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
TIM5->CCR3 = v;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
TIM5->CCR4 = v;
|
|
|
|
break;
|
2014-03-22 11:52:33 -04:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(pyb_servo_set_obj, pyb_servo_set);
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_pwm_set(mp_obj_t period, mp_obj_t pulse) {
|
|
|
|
int pe = mp_obj_get_int(period);
|
|
|
|
int pu = mp_obj_get_int(pulse);
|
2014-04-02 10:09:36 -04:00
|
|
|
TIM5->ARR = pe;
|
|
|
|
TIM5->CCR3 = pu;
|
2014-03-22 11:52:33 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(pyb_pwm_set_obj, pyb_pwm_set);
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void pyb_servo_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_servo_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2018-05-04 01:52:03 -04:00
|
|
|
mp_printf(print, "<Servo %u at %uus>", self - &pyb_servo_obj[0] + 1, 10 * self->pulse_cur);
|
2014-03-22 11:52:33 -04:00
|
|
|
}
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \classmethod \constructor(id)
|
|
|
|
/// Create a servo object. `id` is 1-4.
|
2017-01-04 08:10:42 -05:00
|
|
|
STATIC mp_obj_t pyb_servo_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2014-03-24 07:48:39 -04:00
|
|
|
// check arguments
|
2014-04-19 19:16:30 -04:00
|
|
|
mp_arg_check_num(n_args, n_kw, 1, 1, false);
|
2014-03-24 07:48:39 -04:00
|
|
|
|
|
|
|
// get servo number
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t servo_id = mp_obj_get_int(args[0]) - 1;
|
2014-03-24 07:48:39 -04:00
|
|
|
|
|
|
|
// check servo number
|
|
|
|
if (!(0 <= servo_id && servo_id < PYB_SERVO_NUM)) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Servo(%d) doesn't exist"), servo_id + 1);
|
2014-03-24 07:48:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get and init servo object
|
|
|
|
pyb_servo_obj_t *s = &pyb_servo_obj[servo_id];
|
|
|
|
s->pulse_dest = s->pulse_cur;
|
|
|
|
s->time_left = 0;
|
|
|
|
servo_init_channel(s);
|
|
|
|
|
2018-07-08 09:25:11 -04:00
|
|
|
return MP_OBJ_FROM_PTR(s);
|
2014-03-24 07:48:39 -04:00
|
|
|
}
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \method pulse_width([value])
|
|
|
|
/// Get or set the pulse width in milliseconds.
|
2017-08-29 20:59:58 -04:00
|
|
|
STATIC mp_obj_t pyb_servo_pulse_width(size_t n_args, const mp_obj_t *args) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_servo_obj_t *self = MP_OBJ_TO_PTR(args[0]);
|
2014-04-08 19:45:45 -04:00
|
|
|
if (n_args == 1) {
|
|
|
|
// get pulse width, in us
|
|
|
|
return mp_obj_new_int(10 * self->pulse_cur);
|
|
|
|
} else {
|
|
|
|
// set pulse width, in us
|
|
|
|
self->pulse_dest = mp_obj_get_int(args[1]) / 10;
|
|
|
|
self->time_left = 0;
|
|
|
|
servo_timer_irq_callback();
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_servo_pulse_width_obj, 1, 2, pyb_servo_pulse_width);
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \method calibration([pulse_min, pulse_max, pulse_centre, [pulse_angle_90, pulse_speed_100]])
|
|
|
|
/// Get or set the calibration of the servo timing.
|
2014-05-03 11:42:27 -04:00
|
|
|
// TODO should accept 1 arg, a 5-tuple of values to set
|
2017-08-29 20:59:58 -04:00
|
|
|
STATIC mp_obj_t pyb_servo_calibration(size_t n_args, const mp_obj_t *args) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_servo_obj_t *self = MP_OBJ_TO_PTR(args[0]);
|
2014-04-08 19:45:45 -04:00
|
|
|
if (n_args == 1) {
|
|
|
|
// get calibration values
|
|
|
|
mp_obj_t tuple[5];
|
|
|
|
tuple[0] = mp_obj_new_int(10 * self->pulse_min);
|
|
|
|
tuple[1] = mp_obj_new_int(10 * self->pulse_max);
|
|
|
|
tuple[2] = mp_obj_new_int(10 * self->pulse_centre);
|
|
|
|
tuple[3] = mp_obj_new_int(10 * (self->pulse_angle_90 + self->pulse_centre));
|
|
|
|
tuple[4] = mp_obj_new_int(10 * (self->pulse_speed_100 + self->pulse_centre));
|
|
|
|
return mp_obj_new_tuple(5, tuple);
|
|
|
|
} else if (n_args >= 4) {
|
|
|
|
// set min, max, centre
|
|
|
|
self->pulse_min = mp_obj_get_int(args[1]) / 10;
|
|
|
|
self->pulse_max = mp_obj_get_int(args[2]) / 10;
|
|
|
|
self->pulse_centre = mp_obj_get_int(args[3]) / 10;
|
|
|
|
if (n_args == 4) {
|
|
|
|
return mp_const_none;
|
|
|
|
} else if (n_args == 6) {
|
|
|
|
self->pulse_angle_90 = mp_obj_get_int(args[4]) / 10 - self->pulse_centre;
|
|
|
|
self->pulse_speed_100 = mp_obj_get_int(args[5]) / 10 - self->pulse_centre;
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// bad number of arguments
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("calibration expecting 1, 4 or 6 arguments, got %d"), n_args);
|
2014-04-08 19:45:45 -04:00
|
|
|
}
|
2014-04-21 13:21:07 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_servo_calibration_obj, 1, 6, pyb_servo_calibration);
|
2014-04-08 19:45:45 -04:00
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \method angle([angle, time=0])
|
|
|
|
/// Get or set the angle of the servo.
|
|
|
|
///
|
|
|
|
/// - `angle` is the angle to move to in degrees.
|
|
|
|
/// - `time` is the number of milliseconds to take to get to the specified angle.
|
2017-08-29 20:59:58 -04:00
|
|
|
STATIC mp_obj_t pyb_servo_angle(size_t n_args, const mp_obj_t *args) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_servo_obj_t *self = MP_OBJ_TO_PTR(args[0]);
|
2014-03-22 14:34:16 -04:00
|
|
|
if (n_args == 1) {
|
|
|
|
// get angle
|
2014-04-08 19:45:45 -04:00
|
|
|
return mp_obj_new_int((self->pulse_cur - self->pulse_centre) * 90 / self->pulse_angle_90);
|
2014-03-22 14:34:16 -04:00
|
|
|
} else {
|
2020-02-26 23:36:53 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2020-09-11 23:47:59 -04:00
|
|
|
self->pulse_dest = self->pulse_centre + (int16_t)((mp_float_t)self->pulse_angle_90 * mp_obj_get_float(args[1]) / MICROPY_FLOAT_CONST(90.0));
|
2020-02-26 23:36:53 -05:00
|
|
|
#else
|
2014-04-08 19:45:45 -04:00
|
|
|
self->pulse_dest = self->pulse_centre + self->pulse_angle_90 * mp_obj_get_int(args[1]) / 90;
|
2020-02-26 23:36:53 -05:00
|
|
|
#endif
|
2014-03-22 14:34:16 -04:00
|
|
|
if (n_args == 2) {
|
|
|
|
// set angle immediately
|
|
|
|
self->time_left = 0;
|
|
|
|
} else {
|
|
|
|
// set angle over a given time (given in milli seconds)
|
|
|
|
self->time_left = mp_obj_get_int(args[2]) / 20;
|
|
|
|
self->pulse_accum = 0;
|
|
|
|
}
|
|
|
|
servo_timer_irq_callback();
|
|
|
|
return mp_const_none;
|
2014-03-22 11:52:33 -04:00
|
|
|
}
|
|
|
|
}
|
2014-03-24 07:48:39 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_servo_angle_obj, 1, 3, pyb_servo_angle);
|
2014-03-22 11:52:33 -04:00
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \method speed([speed, time=0])
|
|
|
|
/// Get or set the speed of a continuous rotation servo.
|
|
|
|
///
|
|
|
|
/// - `speed` is the speed to move to change to, between -100 and 100.
|
|
|
|
/// - `time` is the number of milliseconds to take to get to the specified speed.
|
2017-08-29 20:59:58 -04:00
|
|
|
STATIC mp_obj_t pyb_servo_speed(size_t n_args, const mp_obj_t *args) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_servo_obj_t *self = MP_OBJ_TO_PTR(args[0]);
|
2014-04-08 19:45:45 -04:00
|
|
|
if (n_args == 1) {
|
|
|
|
// get speed
|
|
|
|
return mp_obj_new_int((self->pulse_cur - self->pulse_centre) * 100 / self->pulse_speed_100);
|
|
|
|
} else {
|
2020-02-26 23:36:53 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2020-09-11 23:47:59 -04:00
|
|
|
self->pulse_dest = self->pulse_centre + (int16_t)((mp_float_t)self->pulse_speed_100 * mp_obj_get_float(args[1]) / MICROPY_FLOAT_CONST(100.0));
|
2020-02-26 23:36:53 -05:00
|
|
|
#else
|
2014-04-08 19:45:45 -04:00
|
|
|
self->pulse_dest = self->pulse_centre + self->pulse_speed_100 * mp_obj_get_int(args[1]) / 100;
|
2020-02-26 23:36:53 -05:00
|
|
|
#endif
|
2014-04-08 19:45:45 -04:00
|
|
|
if (n_args == 2) {
|
|
|
|
// set speed immediately
|
|
|
|
self->time_left = 0;
|
|
|
|
} else {
|
|
|
|
// set speed over a given time (given in milli seconds)
|
|
|
|
self->time_left = mp_obj_get_int(args[2]) / 20;
|
|
|
|
self->pulse_accum = 0;
|
|
|
|
}
|
|
|
|
servo_timer_irq_callback();
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_servo_speed_obj, 1, 3, pyb_servo_speed);
|
|
|
|
|
2017-05-06 03:03:40 -04:00
|
|
|
STATIC const mp_rom_map_elem_t pyb_servo_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pulse_width), MP_ROM_PTR(&pyb_servo_pulse_width_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_calibration), MP_ROM_PTR(&pyb_servo_calibration_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_angle), MP_ROM_PTR(&pyb_servo_angle_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_speed), MP_ROM_PTR(&pyb_servo_speed_obj) },
|
2014-03-22 11:52:33 -04:00
|
|
|
};
|
|
|
|
|
2014-03-26 17:47:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(pyb_servo_locals_dict, pyb_servo_locals_dict_table);
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
pyb_servo_type,
|
|
|
|
MP_QSTR_Servo,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-09-16 10:31:23 -04:00
|
|
|
make_new, pyb_servo_make_new,
|
2021-07-14 00:38:38 -04:00
|
|
|
print, pyb_servo_print,
|
2022-06-24 02:27:46 -04:00
|
|
|
locals_dict, &pyb_servo_locals_dict
|
2021-07-14 00:38:38 -04:00
|
|
|
);
|
2017-07-14 03:41:43 -04:00
|
|
|
|
|
|
|
#endif // MICROPY_HW_ENABLE_SERVO
|