@@ -52,51 +52,52 @@ static const uint16_t key_setpoint[ULMOTORS_NUM_MOTORS] = {
5252 MOTOR_RR_SETPOINT
5353};
5454
55- static void ulMotorsController_UpdateFeedback ( ulMotorsController_t * ctrl )
55+ static void Motors_UpdateFromDB_FloatArray ( const uint16_t * keys , float * dst , int count )
5656{
57- for (int i = 0 ; i < ULMOTORS_NUM_MOTORS ; i ++ ) {
58- int32_t rpm_db = 0 ;
59-
60- if (ulDatabase_getInt32 (key_rpm [i ], & rpm_db )) {
61- ctrl -> measured_rpm [i ] = (float )rpm_db ;
57+ for (int i = 0 ; i < count ; i ++ ) {
58+ int32_t val = 0 ;
59+ if (ulDatabase_getInt32 (keys [i ], & val )) {
60+ dst [i ] = (float )val ;
6261 }
6362 }
6463}
6564
65+ static void ulMotorsController_UpdateFeedback (ulMotorsController_t * ctrl )
66+ {
67+ Motors_UpdateFromDB_FloatArray (key_rpm , ctrl -> measured_rpm , ULMOTORS_NUM_MOTORS );
68+ }
69+
70+ static void ulMotorsController_UpdateFromDB (ulMotorsController_t * ctrl )
71+ {
72+ Motors_UpdateFromDB_FloatArray (key_setpoint , ctrl -> setpoint_rpm , ULMOTORS_NUM_MOTORS );
73+ }
74+
6675
6776static void init_motors_hw_mapping (ulMotorsController_t * ctrl )
6877{
6978 // MOTOR_1_FL
70- ctrl -> motors [0 ].IN1_port = GPIOE ;
71- ctrl -> motors [0 ].IN1_pin = GPIO_PIN_2 ;
72- ctrl -> motors [0 ].IN2_port = GPIOE ;
73- ctrl -> motors [0 ].IN2_pin = GPIO_PIN_4 ;
7479 ctrl -> motors [0 ].pwm_src = PWM_SRC_PCA9685 ;
75- ctrl -> motors [0 ].pwm .pca .channel = 0 ;
80+ ctrl -> motors [0 ].pwm .pca .channel = 0 ; // MOTOR_1_PWM
81+ ctrl -> motors [0 ].in1_pca_channel = 1 ; // MOTOR_1_FWD
82+ ctrl -> motors [0 ].in2_pca_channel = 2 ; // MOTOR_1_RVR
7683
7784 // MOTOR_2_FR
78- ctrl -> motors [1 ].IN1_port = GPIOE ;
79- ctrl -> motors [1 ].IN1_pin = GPIO_PIN_5 ;
80- ctrl -> motors [1 ].IN2_port = GPIOE ;
81- ctrl -> motors [1 ].IN2_pin = GPIO_PIN_6 ;
8285 ctrl -> motors [1 ].pwm_src = PWM_SRC_PCA9685 ;
83- ctrl -> motors [1 ].pwm .pca .channel = 1 ;
86+ ctrl -> motors [1 ].pwm .pca .channel = 4 ; // MOTOR_2_PWM
87+ ctrl -> motors [1 ].in1_pca_channel = 5 ; // MOTOR_2_FWD
88+ ctrl -> motors [1 ].in2_pca_channel = 6 ; // MOTOR_2_RVR
8489
8590 // MOTOR_3_RL
86- ctrl -> motors [2 ].IN1_port = GPIOD ;
87- ctrl -> motors [2 ].IN1_pin = GPIO_PIN_0 ;
88- ctrl -> motors [2 ].IN2_port = GPIOD ;
89- ctrl -> motors [2 ].IN2_pin = GPIO_PIN_1 ;
9091 ctrl -> motors [2 ].pwm_src = PWM_SRC_PCA9685 ;
91- ctrl -> motors [2 ].pwm .pca .channel = 2 ;
92+ ctrl -> motors [2 ].pwm .pca .channel = 15 ; // MOTOR_3_PWM
93+ ctrl -> motors [2 ].in1_pca_channel = 14 ; // MOTOR_3_FWD
94+ ctrl -> motors [2 ].in2_pca_channel = 13 ; // MOTOR_3_RVR
9295
9396 // MOTOR_4_RR
94- ctrl -> motors [3 ].IN1_port = GPIOD ;
95- ctrl -> motors [3 ].IN1_pin = GPIO_PIN_2 ;
96- ctrl -> motors [3 ].IN2_port = GPIOD ;
97- ctrl -> motors [3 ].IN2_pin = GPIO_PIN_3 ;
9897 ctrl -> motors [3 ].pwm_src = PWM_SRC_PCA9685 ;
99- ctrl -> motors [3 ].pwm .pca .channel = 3 ;
98+ ctrl -> motors [3 ].pwm .pca .channel = 11 ; // MOTOR_4_PWM
99+ ctrl -> motors [3 ].in1_pca_channel = 10 ; // MOTOR_4_FWD
100+ ctrl -> motors [3 ].in2_pca_channel = 9 ; // MOTOR_4_RVR
100101
101102}
102103
@@ -157,18 +158,6 @@ void ulMotorsController_Init(ulMotorsController_t* ctrl)
157158}
158159
159160
160- static void ulMotorsController_UpdateFromDB (ulMotorsController_t * ctrl )
161- {
162- for (int i = 0 ; i < ULMOTORS_NUM_MOTORS ; i ++ ) {
163- int32_t sp_db = 0 ;
164-
165- if (ulDatabase_getInt32 (key_setpoint [i ], & sp_db )) {
166- ctrl -> setpoint_rpm [i ] = (float )sp_db ;
167- }
168- }
169- }
170-
171-
172161void ulMotorsController_Run (ulMotorsController_t * ctrl )
173162{
174163 static char teleplot_buf [512 ];
@@ -262,6 +251,7 @@ static void MotorsTimerCallback(void *argument)
262251
263252 case MOTORS_STATE_IDLE :
264253 {
254+ ulMotorsController_UpdateFromDB (& g_motors_ctrl );
265255 bool need_run = false;
266256 for (int i = 0 ; i < ULMOTORS_NUM_MOTORS ; i ++ ) {
267257 if (fabsf (g_motors_ctrl .setpoint_rpm [i ]) > 1.0f ) {
@@ -285,6 +275,12 @@ static void MotorsTimerCallback(void *argument)
285275
286276 case MOTORS_STATE_RUN :
287277 {
278+ static uint8_t sp_div = 0 ;
279+
280+ if (++ sp_div >= 4 ) {
281+ sp_div = 0 ;
282+ ulMotorsController_UpdateFromDB (& g_motors_ctrl );
283+ }
288284 ulMotorsController_UpdateFeedback (& g_motors_ctrl );
289285 ulMotorsController_Run (& g_motors_ctrl );
290286
@@ -345,8 +341,6 @@ void ulMotorsController_Task(void* argument)
345341
346342 for (;;) {
347343
348- ulMotorsController_UpdateFromDB (& g_motors_ctrl );
349-
350344 osDelay (20 );
351345 }
352346}
0 commit comments