ACwing FC firmware
STM32WB55: attitude IEKF, altitude Kalman filter, flight core, protocol v2
Loading...
Searching...
No Matches
motors.c
Go to the documentation of this file.
1
4#include "motors.h"
5#if MOTORS_ENABLE
6#include "main.h"
7extern TIM_HandleTypeDef htim1;
8static const uint32_t k_ch[4] = { TIM_CHANNEL_1, TIM_CHANNEL_2, TIM_CHANNEL_3, TIM_CHANNEL_4 };
9#endif
10
11static volatile uint8_t s_armed;
12
13void motors_init(void)
14{
15 s_armed = 0;
16#if MOTORS_ENABLE
17 for (int i = 0; i < 4; ++i) {
18 __HAL_TIM_SET_COMPARE(&htim1, k_ch[i], 0);
19 (void)HAL_TIM_PWM_Start(&htim1, k_ch[i]);
20 }
21#endif
22}
23
24void motors_arm(uint8_t armed)
25{
26 s_armed = armed ? 1u : 0u;
27 if (!s_armed) { const float z[4] = { 0, 0, 0, 0 }; motors_set(z); }
28}
29
30void motors_set(const float duty[4])
31{
32#if MOTORS_ENABLE
33 for (int i = 0; i < 4; ++i) {
34 float d = s_armed ? duty[i] : 0.0f;
35 if (d < 0.0f) d = 0.0f;
36 if (d > 1.0f) d = 1.0f;
37 __HAL_TIM_SET_COMPARE(&htim1, k_ch[i], (uint32_t)(d * (float)MOTORS_PWM_TOP + 0.5f));
38 }
39#else
40 (void)duty;
41#endif
42}
43
44void motors_test(uint8_t motor, float duty)
45{
46#if MOTORS_ENABLE
47 if (s_armed) return; /* never while armed */
48 if (duty < 0.0f) duty = 0.0f;
49 if (duty > 0.2f) duty = 0.2f; /* MOTOR_TEST limit: 20 % */
50 for (int i = 0; i < 4; ++i) {
51 const float d = (motor == (uint8_t)(i + 1)) ? duty : 0.0f;
52 __HAL_TIM_SET_COMPARE(&htim1, k_ch[i], (uint32_t)(d * (float)MOTORS_PWM_TOP + 0.5f));
53 }
54#else
55 (void)motor; (void)duty;
56#endif
57}
void motors_set(const float duty[4])
0..1, saturated
Definition motors.c:30
void motors_arm(uint8_t armed)
disarmed: duty forced to 0
Definition motors.c:24
void motors_init(void)
Definition motors.c:13
static volatile uint8_t s_armed
Definition motors.c:11
void motors_test(uint8_t motor, float duty)
MOTOR_TEST (props off, disarmed): motor 1..4 at duty, the others off; motor 0 = all off.
Definition motors.c:44
PWM for 4 brushed coreless motors (8520, 1S) - PREPARED, unused in phase 1.
#define MOTORS_PWM_TOP
Definition motors.h:17