-
Notifications
You must be signed in to change notification settings - Fork 4
/
Motors.c
56 lines (45 loc) · 1.29 KB
/
Motors.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
ChibiCopter - https://github.com/grantmd/ChibiCopter
A quadcopter platform running under ChibiOS/RT.
Manages motor speeds and sends them via PWM
*/
#include "ch.h"
#include "hal.h"
#include "Motors.h"
static PWMConfig pwmcfg = {
1000000, /* 1MHz PWM clock frequency. */
2500, /* Initial PWM period 20ms. */ // 50hz (20ms) for standard servo/ESC, 400hz for fast servo/ESC (2.5ms)
NULL,
{
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_ACTIVE_HIGH, NULL}
},
0,
#if STM32_PWM_USE_ADVANCED
0
#endif
};
pwmcnt_t motor_speeds[NUM_MOTORS];
void MotorsInit(void){
pwmStart(&PWMD8, &pwmcfg);
palSetPadMode(GPIOC, 6, PAL_MODE_ALTERNATE(3));
palSetPadMode(GPIOC, 7, PAL_MODE_ALTERNATE(3));
palSetPadMode(GPIOC, 8, PAL_MODE_ALTERNATE(3));
palSetPadMode(GPIOC, 9, PAL_MODE_ALTERNATE(3));
unsigned i;
for (i=0; i<NUM_MOTORS; i++){
MotorsSetSpeed(i, 0);
}
}
void MotorsSetSpeed(unsigned motor, pwmcnt_t speed){
if (motor >= NUM_MOTORS) return;
if (speed > MAX_MOTOR_SPEED) speed = MAX_MOTOR_SPEED;
motor_speeds[motor] = speed;
pwmEnableChannel(&PWMD8, motor, PWM_PERCENTAGE_TO_WIDTH(&PWMD8, speed));
}
pwmcnt_t MotorsGetSpeed(unsigned motor){
if (motor >= NUM_MOTORS) return 0;
return motor_speeds[motor];
}