Skip to content

Commit a23ee4b

Browse files
committed
1. Minor code refactoring
2. Updated the TCM to match BlueRobotics' Blue Rov Heavy configuration 3. Uniformed the order for cmd_vel to surge, sway, heave, roll, pitch, yaw in all the functions and libraries. 4. Fixed a bug in the update_setpoints function, I miscalculated the array index which was off by 1 5. Uniformed the reference system to match the one used by the most literature on underwater robotics.
1 parent 43d683a commit a23ee4b

File tree

15 files changed

+45097
-45060
lines changed

15 files changed

+45097
-45060
lines changed

Core/Inc/navigation/ThrusterConfigurationMatrix.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ extern "C" {
2525
#include "arm_math.h"
2626

2727
/**
28-
* The fixed 8x6 mixing matrix: the columns are, respectively: surge, sway, heave, pitch, roll, yaw.
29-
* The rows are, respectively, thrusters from 0 to 7.
28+
* The fixed 8x6 mixing matrix: the columns are, respectively: surge, sway, heave, roll, pitch, yaw.
29+
* The rows are, respectively, thrusters from 1 to 8. Refer to BlueRobotics' Blue ROV Heavy configuration.
3030
*/
3131
__attribute__((aligned(4))) extern float FIXED_MIXING_MATRIX[8][6];
3232

Core/Inc/navigation/navigation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ typedef struct
4444
/**
4545
* @brief Calculates PWM output values from command velocity
4646
*
47-
* @param cmd_vel
48-
* @param pwm_output
47+
* @param cmd_vel: surge, sway, heave, roll, pitch, yaw
48+
* @param pwm_output: thrusters 1 to 8
4949
* @return eventual error code
5050
*/
5151
arm_status calculate_pwm(const float cmd_vel[6], uint32_t pwm_output[8]);

Core/Inc/navigation/stabilize_mode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ void init_pids(float kps[PID_NUMBER], float kis[PID_NUMBER], float kds[PID_NUMBE
3838
/**
3939
* @brief Calculates the PWM output using PID control to adjust orientation and depth based on joystick input.
4040
*
41-
* @param joystick_input Array of 6 cmd velocity values: [surge, sway, heave, pitch, roll, yaw].
42-
* @param pwm_output Array to store the 8 calculated PWM output values.
41+
* @param joystick_input Array of 6 cmd velocity values: [surge, sway, heave, roll, pitch, yaw].
42+
* @param pwm_output Array to store the 8 calculated PWM output values (thrusters 1 to 8, referring to BlueRobotics' Blue Rov Heavy)
4343
* @param orientation_quaternion Pointer to the current orientation as a quaternion.
4444
* @param water_pressure Pointer to the current water pressure (used to estimate depth).
4545
* @return int8_t A status code indicating the success or failure of the PWM calculation.

Core/Src/freertos.cpp

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,10 @@ void StartDefaultTask(void *argument)
269269
break;
270270
}
271271
clamp_pwm_output(pwm_output, 8);
272-
// offset to compensate for the optocoupler offset
273-
for(int8_t i = 0; i < 8; i++) pwm_output[i] += OPTOCOUPLER_INTRODUCED_OFFSET_uS;
274272
set_pwms(pwm_output);
275273
} else set_pwm_idle();
276274

277-
// do not publish the offset by subtracting 50
278-
for(uint8_t i = 0; i < 8; i++) thruster_status_msg.thruster_pwms[i] = pwm_output[i] - OPTOCOUPLER_INTRODUCED_OFFSET_uS;
275+
for(uint8_t i = 0; i < 8; i++) thruster_status_msg.thruster_pwms[i] = pwm_output[i];
279276
rc = rcl_publish(&thruster_status_publisher, &thruster_status_msg, NULL);
280277
if(rc!=RCL_RET_OK) printf("Error publishing (line %d)\n", __LINE__);
281278

@@ -289,29 +286,27 @@ void StartDefaultTask(void *argument)
289286
/* USER CODE BEGIN Application */
290287
void inline set_pwms(uint32_t pwms[8])
291288
{
292-
TIM2 -> CCR1 = pwms[0];
293-
TIM2 -> CCR2 = pwms[1];
294-
TIM2 -> CCR3 = pwms[2];
295-
TIM2 -> CCR4 = pwms[3];
296-
297-
// vertical thrusters
298-
TIM3 -> CCR1 = pwms[4];
299-
TIM3 -> CCR2 = pwms[5];
300-
TIM3 -> CCR3 = pwms[6];
301-
TIM3 -> CCR4 = pwms[7];
289+
// HERE THE PWM Channel - Thruster relation is defined
290+
TIM2 -> CCR1 = pwms[0] + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
291+
TIM2 -> CCR2 = pwms[1] + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
292+
TIM2 -> CCR3 = pwms[2] + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
293+
TIM2 -> CCR4 = pwms[3] + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
294+
// VERTICAL THRUSTERS
295+
TIM3 -> CCR1 = pwms[4] + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
296+
TIM3 -> CCR2 = pwms[5] + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
297+
TIM3 -> CCR3 = pwms[6] + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
298+
TIM3 -> CCR4 = pwms[7] + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
302299
}
303300
void inline set_pwm_idle()
304301
{
305-
TIM2 -> CCR1 = PWM_IDLE;
306-
TIM2 -> CCR2 = PWM_IDLE;
307-
TIM2 -> CCR3 = PWM_IDLE;
308-
TIM2 -> CCR4 = PWM_IDLE;
309-
310-
// vertical thrusters
311-
TIM3 -> CCR1 = PWM_IDLE;
312-
TIM3 -> CCR2 = PWM_IDLE;
313-
TIM3 -> CCR3 = PWM_IDLE;
314-
TIM3 -> CCR4 = PWM_IDLE;
302+
TIM2 -> CCR1 = PWM_IDLE + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
303+
TIM2 -> CCR2 = PWM_IDLE + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
304+
TIM2 -> CCR3 = PWM_IDLE + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
305+
TIM2 -> CCR4 = PWM_IDLE + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
306+
TIM3 -> CCR1 = PWM_IDLE + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
307+
TIM3 -> CCR2 = PWM_IDLE + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
308+
TIM3 -> CCR3 = PWM_IDLE + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
309+
TIM3 -> CCR4 = PWM_IDLE + OPTOCOUPLER_INTRODUCED_OFFSET_uS;
315310
}
316311
void clamp_pwm_output(uint32_t pwms[], int N) {
317312
for(uint16_t i = 0; i < N; i++) {
@@ -331,16 +326,14 @@ void update_pid_constants(arm_pid_instance_f32 *pid, float32_t Kp, float32_t Ki,
331326
pid->A2 = Kd;
332327
}
333328
void imu_subscription_callback(const void * msgin) {
334-
const sensor_msgs__msg__Imu * msg = (const sensor_msgs__msg__Imu *)msgin;
329+
335330
}
336331
void cmd_vel_subscription_callback (const void * msgin) {
337332

338333
}
339334
void arm_disarm_service_callback(const void * request_msg, void * response_msg) {
340335
std_srvs__srv__SetBool_Request * req_in = (std_srvs__srv__SetBool_Request *) request_msg;
341336
std_srvs__srv__SetBool_Response * res_in = (std_srvs__srv__SetBool_Response *) response_msg;
342-
343-
// Handle request message and set the response message values
344337
rov_arm_mode = req_in->data ? ROV_ARMED : ROV_DISARMED;
345338
printf("%d: arm mode.\n", (int)rov_arm_mode);
346339
res_in->success = true;
@@ -351,9 +344,7 @@ void arm_disarm_service_callback(const void * request_msg, void * response_msg)
351344
void set_nav_mode_service_callback(const void * request_msg, void * response_msg) {
352345
nereo_interfaces__srv__SetNavigationMode_Request * req_in = (nereo_interfaces__srv__SetNavigationMode_Request *) request_msg;
353346
nereo_interfaces__srv__SetNavigationMode_Response * res_in = (nereo_interfaces__srv__SetNavigationMode_Response *) response_msg;
354-
355347
navigation_mode = (NavigationModes)req_in->navigation_mode;
356-
357348
res_in->mode_after_set = navigation_mode;
358349
res_in->success = true;
359350
}

Core/Src/navigation/ThrusterConfigurationMatrix.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
#include <navigation/ThrusterConfigurationMatrix.h>
99

1010
__attribute__((aligned(4))) float FIXED_MIXING_MATRIX[8][6] = {
11+
{0.7071067812, -0.7071067812, 0, 0, 0, -1},
1112
{0.7071067812, 0.7071067812, 0, 0, 0, 1},
12-
{-0.7071067812, 0.7071067812, 0, 0, 0, -1},
13-
{-0.7071067812, 0.7071067812, 0, 0, 0, -1},
14-
{0.7071067812, 0.7071067812, 0, 0, 0, 1},
15-
{0, 0, 1, 1, -1, 0},
16-
{0, 0, 1, 1, 1, 0},
13+
{0.7071067812, 0.7071067812, 0, 0, 0, -1},
14+
{0.7071067812, -0.7071067812, 0, 0, 0, 1},
15+
{0, 0, -1, -1, 1, 0},
1716
{0, 0, 1, -1, -1, 0},
18-
{0, 0, 1, -1, 1, 0}
17+
{0, 0, 1, 1, 1, 0},
18+
{0, 0, -1, 1, -1, 0}
1919
};
2020

Core/Src/navigation/navigation.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,10 @@
99
#ifdef __cplusplus
1010
extern "C" {
1111
#endif
12-
/**
13-
* @brief Calculates the PWM output values based on the joystick input.
14-
*
15-
* This function takes the cmd_vel values, normalizes them, and then
16-
* multiplies them with a fixed mixing matrix to generate the PWM output values.
17-
* The PWM output values are then normalized and mapped to the range of PWM_MIN
18-
* to PWM_MAX.
19-
*
20-
* @param joystick_input An array of 6 float values representing the joystick input.
21-
* @param pwm_output An array of 8 uint16_t values to store the calculated PWM output.
22-
* @return OK if the calculation was successful, MAT_MULT_ERROR if the matrix
23-
* multiplication failed.
24-
*/
12+
13+
static uint8_t tcm_initialized = 0;
14+
static arm_matrix_instance_f32 fixed_mixing_matrix_instance;
15+
2516
arm_status calculate_pwm(const float in_joystick_input[6], uint32_t pwm_output[8])
2617
{
2718
float joystick_input[6];
@@ -34,13 +25,15 @@ arm_status calculate_pwm(const float in_joystick_input[6], uint32_t pwm_output[8
3425

3526
float f_pwm_output[8];
3627

37-
__attribute__((aligned(4))) float pwm_output_8_1[8][1] = {0};
28+
__attribute__((aligned(4))) float pwm_output_8_1[8] = {0};
3829

39-
arm_matrix_instance_f32 fixed_mixing_matrix_instance;
4030
arm_matrix_instance_f32 joystick_input_instance;
4131
arm_matrix_instance_f32 pwm_output_instance;
4232

43-
arm_mat_init_f32(&fixed_mixing_matrix_instance, 8, 6, (float *)FIXED_MIXING_MATRIX);
33+
if(tcm_initialized == 0) {
34+
arm_mat_init_f32(&fixed_mixing_matrix_instance, 8, 6, (float *)FIXED_MIXING_MATRIX);
35+
tcm_initialized = 1;
36+
}
4437
arm_mat_init_f32(&joystick_input_instance, 6, 1, (float *)joystick_input);
4538
arm_mat_init_f32(&pwm_output_instance, 8, 1, (float *)pwm_output_8_1);
4639
arm_status code = arm_mat_mult_f32(&fixed_mixing_matrix_instance, &joystick_input_instance, &pwm_output_instance);

0 commit comments

Comments
 (0)