servo.c 6.53 KB
Newer Older
Damien George's avatar
Damien George committed
1
2
3
4
#include <stdio.h>

#include "stm32f4xx_hal.h"

5
#include "nlr.h"
Damien George's avatar
Damien George committed
6
7
8
9
10
11
12
13
14
15
16
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "servo.h"

// this servo driver uses hardware PWM to drive servos on PA0, PA1, PA2, PA3 = X1, X2, X3, X4
// TIM2 and TIM5 have CH1, CH2, CH3, CH4 on PA0-PA3 respectively
// they are both 32-bit counters with 16-bit prescaler
// we use TIM2

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#define PYB_SERVO_NUM (4)

typedef struct _pyb_servo_obj_t {
    mp_obj_base_t base;
    uint16_t servo_id;
    uint16_t time_left;
    int16_t pulse_accum;
    uint16_t pulse_cur;
    uint16_t pulse_dest;
} pyb_servo_obj_t;

STATIC const mp_obj_type_t servo_obj_type;

STATIC pyb_servo_obj_t pyb_servo_obj[PYB_SERVO_NUM];

TIM_HandleTypeDef servo_TIM2_Handle;
Damien George's avatar
Damien George committed
33
34
35
36
37

void servo_init(void) {
    // TIM2 clock enable
    __TIM2_CLK_ENABLE();

38
39
40
41
    // set up and enable interrupt
    HAL_NVIC_SetPriority(TIM2_IRQn, 6, 0);
    HAL_NVIC_EnableIRQ(TIM2_IRQn);

Damien George's avatar
Damien George committed
42
    // PWM clock configuration
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
    servo_TIM2_Handle.Instance = TIM2;
    servo_TIM2_Handle.Init.Period = 2000; // timer cycles at 50Hz
    servo_TIM2_Handle.Init.Prescaler = ((SystemCoreClock / 2) / 100000) - 1; // timer runs at 100kHz
    servo_TIM2_Handle.Init.ClockDivision = 0;
    servo_TIM2_Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
    HAL_TIM_PWM_Init(&servo_TIM2_Handle);

    // reset servo objects
    for (int i = 0; i < PYB_SERVO_NUM; i++) {
        pyb_servo_obj[i].base.type = &servo_obj_type;
        pyb_servo_obj[i].servo_id = i + 1;
        pyb_servo_obj[i].time_left = 0;
        pyb_servo_obj[i].pulse_cur = 150; // units of 10us
        pyb_servo_obj[i].pulse_dest = 0;
    }
}

#include "led.h"
void servo_timer_irq_callback(void) {
    led_toggle(1);
    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) {
            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;
            }
            switch (s->servo_id) {
                case 1: TIM2->CCR1 = s->pulse_cur; break;
                case 2: TIM2->CCR2 = s->pulse_cur; break;
                case 3: TIM2->CCR3 = s->pulse_cur; break;
                case 4: TIM2->CCR4 = s->pulse_cur; break;
            }
        }
    }
    if (need_it) {
        __HAL_TIM_ENABLE_IT(&servo_TIM2_Handle, TIM_IT_UPDATE);
    } else {
        __HAL_TIM_DISABLE_IT(&servo_TIM2_Handle, TIM_IT_UPDATE);
    }
Damien George's avatar
Damien George committed
90
91
}

92
STATIC void servo_init_channel(pyb_servo_obj_t *s) {
Damien George's avatar
Damien George committed
93
94
    uint32_t pin;
    uint32_t channel;
95
    switch (s->servo_id) {
Damien George's avatar
Damien George committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
        case 1: pin = GPIO_PIN_0; channel = TIM_CHANNEL_1; break;
        case 2: pin = GPIO_PIN_1; channel = TIM_CHANNEL_2; break;
        case 3: pin = GPIO_PIN_2; channel = TIM_CHANNEL_3; break;
        case 4: pin = GPIO_PIN_3; channel = TIM_CHANNEL_4; break;
        default: return;
    }

    // GPIO configuration
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.Pin = pin;
    GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
    GPIO_InitStructure.Alternate = GPIO_AF1_TIM2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);

    // PWM mode configuration
    TIM_OC_InitTypeDef oc_init;
    oc_init.OCMode = TIM_OCMODE_PWM1;
115
    oc_init.Pulse = s->pulse_cur; // units of 10us
Damien George's avatar
Damien George committed
116
117
    oc_init.OCPolarity = TIM_OCPOLARITY_HIGH;
    oc_init.OCFastMode = TIM_OCFAST_DISABLE;
118
    HAL_TIM_PWM_ConfigChannel(&servo_TIM2_Handle, &oc_init, channel);
Damien George's avatar
Damien George committed
119
120

    // start PWM
121
    HAL_TIM_PWM_Start(&servo_TIM2_Handle, channel);
Damien George's avatar
Damien George committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
}

/******************************************************************************/
// Micro Python bindings

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);
    if (v < 50) { v = 50; }
    if (v > 250) { v = 250; }
    switch (p) {
        case 1: TIM2->CCR1 = v; break;
        case 2: TIM2->CCR2 = v; break;
        case 3: TIM2->CCR3 = v; break;
        case 4: TIM2->CCR4 = v; break;
    }
    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);
    TIM2->ARR = pe;
    TIM2->CCR3 = pu;
    return mp_const_none;
}

MP_DEFINE_CONST_FUN_OBJ_2(pyb_pwm_set_obj, pyb_pwm_set);

STATIC void servo_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
    pyb_servo_obj_t *self = self_in;
155
    print(env, "<Servo %lu at %lu>", self->servo_id, self->pulse_cur);
Damien George's avatar
Damien George committed
156
157
}

158
159
160
161
162
163
STATIC mp_obj_t servo_obj_angle(uint n_args, const mp_obj_t *args) {
    pyb_servo_obj_t *self = args[0];
    if (n_args == 1) {
        // get angle
        return mp_obj_new_int((self->pulse_cur - 152) * 90 / 85);
    } else {
Damien George's avatar
Damien George committed
164
#if MICROPY_ENABLE_FLOAT
165
        machine_int_t v = 152 + 85.0 * mp_obj_get_float(args[1]) / 90.0;
Damien George's avatar
Damien George committed
166
#else
167
        machine_int_t v = 152 + 85 * mp_obj_get_int(args[1]) / 90;
Damien George's avatar
Damien George committed
168
#endif
169
170
171
172
173
174
175
176
177
178
179
180
181
        if (v < 65) { v = 65; }
        if (v > 210) { v = 210; }
        self->pulse_dest = v;
        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;
Damien George's avatar
Damien George committed
182
183
184
    }
}

185
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(servo_obj_angle_obj, 1, 3, servo_obj_angle);
Damien George's avatar
Damien George committed
186
187
188
189
190
191
192
193
194
195
196
197
198

STATIC const mp_method_t servo_methods[] = {
    { "angle", &servo_obj_angle_obj },
    { NULL, NULL },
};

STATIC const mp_obj_type_t servo_obj_type = {
    { &mp_type_type },
    .name = MP_QSTR_Servo,
    .print = servo_obj_print,
    .methods = servo_methods,
};

199
200
201
202
203
204
205
206
207
208
209
STATIC mp_obj_t pyb_Servo(mp_obj_t servo_id_o) {
    machine_int_t servo_id = mp_obj_get_int(servo_id_o) - 1;
    if (0 <= servo_id && servo_id < PYB_SERVO_NUM) {
        pyb_servo_obj_t *s = &pyb_servo_obj[servo_id];
        s->pulse_dest = s->pulse_cur;
        s->time_left = 0;
        servo_init_channel(s);
        return s;
    } else {
        nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Servo %d does not exist", servo_id));
    }
Damien George's avatar
Damien George committed
210
211
212
}

MP_DEFINE_CONST_FUN_OBJ_1(pyb_Servo_obj, pyb_Servo);