From cd26d337f06af54e1c2b41734eccbc0b1ecc3dc5 Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Tue, 27 May 2025 18:02:56 -0400 Subject: [PATCH 01/10] I think I have added CAN receiving to MSB --- Core/Inc/can_handler.h | 6 ++++ Core/Inc/msb_conf.h | 4 +++ Core/Src/can_handler.c | 81 +++++++++++++++++++++++++++++++++++++++++- Core/Src/main.c | 3 ++ 4 files changed, 93 insertions(+), 1 deletion(-) diff --git a/Core/Inc/can_handler.h b/Core/Inc/can_handler.h index 7d97fe4..d4c1c28 100644 --- a/Core/Inc/can_handler.h +++ b/Core/Inc/can_handler.h @@ -19,10 +19,16 @@ //void can1_callback(CAN_HandleTypeDef *hcan); +void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan); + void vCanDispatch(void *pv_params); extern osThreadId_t can_dispatch_handle; extern const osThreadAttr_t can_dispatch_attributes; +void vCanReceive(void *pv_params); +extern osThreadId_t can_receive_thread; +extern const osThreadAttr_t can_receive_attributes; + int8_t queue_can_msg(can_msg_t msg); void can1_init(); diff --git a/Core/Inc/msb_conf.h b/Core/Inc/msb_conf.h index 00fc0ee..9040752 100644 --- a/Core/Inc/msb_conf.h +++ b/Core/Inc/msb_conf.h @@ -25,6 +25,10 @@ #define CANID_TOF 0x607 #define CANID_WHEEL_TEMP 0x608 #define CANID_IMU_ORIENTATION 0x609 +#define CANID_IMUZERO_FRONTLEFT 0x60A +#define CANID_IMUZERO_FRONTRIGHT 0x60B +#define CANID_IMUZERO_BACKLEFT 0x60C +#define CANID_IMUZERO_BACKRIGHT 0x60D // Sensors to use, comment out to disable diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index af8f191..60ce009 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -12,6 +12,7 @@ #include "can_handler.h" #include "can.h" #include "msb_conf.h" +#include "msb.h" #include "stdio.h" #include @@ -20,10 +21,14 @@ #define CAN_MSG_QUEUE_SIZE 25 /* messages */ static osMessageQueueId_t can_outbound_queue; +static osMessageQueueId_t can_inbound_queue; -static uint16_t id_list[4] = { 0x000, 0x000, 0x000, 0x002 }; +static uint16_t id_list[4] = { + 0x000, 0x000, 0x000, 0x002 +}; // id_list[0] is reserved for the IMU Zero CAN ID, which is added in can1_init(). extern CAN_HandleTypeDef hcan1; +extern device_loc_t device_loc; can_t *can1; @@ -35,11 +40,56 @@ void can1_init() can1->hcan = &hcan1; assert(!can_init(can1)); + + /* Add the correct IMU Zero CAN ID to the filter depending on the location of the MSB. */ + switch (device_loc) { + case DEVICE_FRONT_LEFT: + id_list[0] = CANID_IMUZERO_FRONTLEFT; + case: +DEVICE_FRONT_RIGHT: + id_list[0] = CANID_IMUZERO_FRONTRIGHT; + case DEVICE_BACK_LEFT: + id_list[0] = CANID_IMUZERO_BACKLEFT; + case: +DEVICE_BACK_RIGHT: + id_list[0] = CANID_IMUZERO_BACKRIGHT; + } + assert(!can_add_filter_standard(can1, id_list)); #endif can_outbound_queue = osMessageQueueNew(CAN_MSG_QUEUE_SIZE, sizeof(can_msg_t), NULL); + can_inbound_queue = + osMessageQueueNew(CAN_MSG_QUEUE_SIZE, sizeof(can_msg_t), NULL); +} + +/* Callback to be called when we get a CAN message */ +void can1_callback(CAN_HandleTypeDef *hcan) +{ + CAN_RxHeaderTypeDef rx_header; + can_msg_t new_msg; + + /* Read in CAN message */ + if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rx_header, + new_msg.data) != HAL_OK) { + printf("Failed to recieve CAN message.\n"); + return; + } + + new_msg.len = rx_header.DLC; + + if (rx_header.IDE == CAN_ID_EXT) { + // If the message has an extended CAN ID, save the message accordingly. + new_msg.id = rx_header.ExtId; + new_msg.id_is_extended = true; + } else { + // If the message has a standard CAN ID, save the message accordingly. + new_msg.id = rx_header.StdId; + new_msg.id_is_extended = false; + } + + osMessageQueuePut(can_inbound_queue, &new_msg, 0U, 0U); } osThreadId_t can_dispatch_handle; @@ -86,3 +136,32 @@ int8_t queue_can_msg(can_msg_t msg) osMessageQueuePut(can_outbound_queue, &msg, 0U, 0U); return 0; } + +osThreadId_t can_receive_thread; +const osThreadAttr_t can_receive_attributes = { + .name = "CanProcessing", + .stack_size = 128 * 8, + .priority = (osPriority_t)osPriorityRealtime, +}; + +void vCanReceive(void *pv_params) +{ + can_msg_t msg; + + for (;;) { + while (osOK == + osMessageQueueGet(can_inbound_queue, &msg, 0U, 0U)) { + switch (msg.id) { + case CANID_IMUZERO_BACKLEFT || + CANID_IMUZERO_BACKRIGHT || + CANID_IMUZERO_FRONTLEFT || + CANID_IMUZERO_FRONTRIGHT: + // do thing() + // also don't need to check device_loc here since only the correct CANID should be added to the filter + break; + default: + break; + } + } + } +} diff --git a/Core/Src/main.c b/Core/Src/main.c index d93dac1..a65fe1f 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -233,6 +233,9 @@ int main(void) /* add queues, ... */ can_dispatch_handle = osThreadNew(vCanDispatch, NULL, &can_dispatch_attributes); assert(can_dispatch_handle); + + can_receive_thread = osThreadNew(vCanReceive, NULL, &can_receive_attributes); + assert(can_receive_thread); /* USER CODE END RTOS_QUEUES */ /* Create the thread(s) */ From 94184ebf5fd410152dd38ed701471811ff6a0340 Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Tue, 27 May 2025 18:07:25 -0400 Subject: [PATCH 02/10] clang format --- Core/Inc/msb_conf.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Core/Inc/msb_conf.h b/Core/Inc/msb_conf.h index 9040752..85a2591 100644 --- a/Core/Inc/msb_conf.h +++ b/Core/Inc/msb_conf.h @@ -17,18 +17,18 @@ #define DELAY_CAN_DISPATCH 2 // CAN IDS -#define CANID_TEMP_SENSOR 0x602 -#define CANID_IMU_ACCEL 0x603 -#define CANID_IMU_GYRO 0x604 -#define CANID_STRAIN_SENSE 0x605 -#define CANID_SHOCK_SENSE 0x606 -#define CANID_TOF 0x607 -#define CANID_WHEEL_TEMP 0x608 -#define CANID_IMU_ORIENTATION 0x609 -#define CANID_IMUZERO_FRONTLEFT 0x60A +#define CANID_TEMP_SENSOR 0x602 +#define CANID_IMU_ACCEL 0x603 +#define CANID_IMU_GYRO 0x604 +#define CANID_STRAIN_SENSE 0x605 +#define CANID_SHOCK_SENSE 0x606 +#define CANID_TOF 0x607 +#define CANID_WHEEL_TEMP 0x608 +#define CANID_IMU_ORIENTATION 0x609 +#define CANID_IMUZERO_FRONTLEFT 0x60A #define CANID_IMUZERO_FRONTRIGHT 0x60B -#define CANID_IMUZERO_BACKLEFT 0x60C -#define CANID_IMUZERO_BACKRIGHT 0x60D +#define CANID_IMUZERO_BACKLEFT 0x60C +#define CANID_IMUZERO_BACKRIGHT 0x60D // Sensors to use, comment out to disable @@ -39,7 +39,7 @@ #define SENSOR_TEMP // SHT30 #define SENSOR_SHOCKPOT // ADC1 #define SENSOR_STRAIN // ADC1 -#define SENSOR_TOF // VL6180X +#define SENSOR_TOF // VL6180X #define SENSOR_IMU // LSM6DSO From dd6d7daac5ef9870e4eef71d64f9758e47e8c441 Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Tue, 27 May 2025 18:11:27 -0400 Subject: [PATCH 03/10] Fixed switch statement --- Core/Inc/can_handler.h | 4 +--- Core/Src/can_handler.c | 6 ++---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/Core/Inc/can_handler.h b/Core/Inc/can_handler.h index d4c1c28..c451120 100644 --- a/Core/Inc/can_handler.h +++ b/Core/Inc/can_handler.h @@ -17,9 +17,7 @@ #include "can.h" #include "cmsis_os.h" -//void can1_callback(CAN_HandleTypeDef *hcan); - -void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan); +void can1_callback(CAN_HandleTypeDef *hcan); void vCanDispatch(void *pv_params); extern osThreadId_t can_dispatch_handle; diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 60ce009..302861a 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -45,13 +45,11 @@ void can1_init() switch (device_loc) { case DEVICE_FRONT_LEFT: id_list[0] = CANID_IMUZERO_FRONTLEFT; - case: -DEVICE_FRONT_RIGHT: + case DEVICE_FRONT_RIGHT: id_list[0] = CANID_IMUZERO_FRONTRIGHT; case DEVICE_BACK_LEFT: id_list[0] = CANID_IMUZERO_BACKLEFT; - case: -DEVICE_BACK_RIGHT: + case DEVICE_BACK_RIGHT: id_list[0] = CANID_IMUZERO_BACKRIGHT; } From 7482a91aec48a63d4f353ccb5431151b17bcd85e Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Wed, 28 May 2025 12:55:14 -0400 Subject: [PATCH 04/10] Added imu_zero() --- Core/Inc/msb.h | 1 + Core/Src/can_handler.c | 17 +++++++++++------ Core/Src/monitor.c | 15 ++++++++++++--- Core/Src/msb.c | 17 +++++++++++++++++ 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/Core/Inc/msb.h b/Core/Inc/msb.h index d30f1b3..83f2c4c 100644 --- a/Core/Inc/msb.h +++ b/Core/Inc/msb.h @@ -76,6 +76,7 @@ void strain2_read(uint32_t strain2); void motion_fx_init(void); void process_motion_fx(MFX_input_t *data_in, MFX_output_t *data_out, float delta_time); +void imu_zero(); #endif diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 302861a..219933e 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -45,12 +45,16 @@ void can1_init() switch (device_loc) { case DEVICE_FRONT_LEFT: id_list[0] = CANID_IMUZERO_FRONTLEFT; + break; case DEVICE_FRONT_RIGHT: id_list[0] = CANID_IMUZERO_FRONTRIGHT; + break; case DEVICE_BACK_LEFT: id_list[0] = CANID_IMUZERO_BACKLEFT; + break; case DEVICE_BACK_RIGHT: id_list[0] = CANID_IMUZERO_BACKRIGHT; + break; } assert(!can_add_filter_standard(can1, id_list)); @@ -150,12 +154,13 @@ void vCanReceive(void *pv_params) while (osOK == osMessageQueueGet(can_inbound_queue, &msg, 0U, 0U)) { switch (msg.id) { - case CANID_IMUZERO_BACKLEFT || - CANID_IMUZERO_BACKRIGHT || - CANID_IMUZERO_FRONTLEFT || - CANID_IMUZERO_FRONTRIGHT: - // do thing() - // also don't need to check device_loc here since only the correct CANID should be added to the filter + case CANID_IMUZERO_BACKLEFT: + case CANID_IMUZERO_BACKRIGHT: + case CANID_IMUZERO_FRONTLEFT: + case CANID_IMUZERO_FRONTRIGHT: + if (msg.data[0] > 0) { + imu_zero(); + } break; default: break; diff --git a/Core/Src/monitor.c b/Core/Src/monitor.c index b3c1631..6ba3925 100644 --- a/Core/Src/monitor.c +++ b/Core/Src/monitor.c @@ -13,6 +13,8 @@ #include "monitor.h" extern device_loc_t device_loc; +extern float imu_rotation_data[3]; +extern float imu_zero_reference[3]; uint16_t convert_can(uint16_t original_value, device_loc_t mode) { @@ -184,9 +186,16 @@ void vIMUMonitor(void *pv_params) process_motion_fx(&mFXInput, &mFXOutput, 0.05f); - orientation_data.yaw = (int16_t)mFXOutput.rotation[0]; - orientation_data.pitch = (int16_t)mFXOutput.rotation[1]; - orientation_data.roll = (int16_t)mFXOutput.rotation[2]; + imu_rotation_data[0] = mFXOutput.rotation[0]; // Yaw + imu_rotation_data[1] = mFXOutput.rotation[1]; // Pitch + imu_rotation_data[2] = mFXOutput.rotation[2]; // Roll + + orientation_data.yaw = (int16_t)(mFXOutput.rotation[0] - + imu_zero_reference[0]); + orientation_data.pitch = (int16_t)(mFXOutput.rotation[1] - + imu_zero_reference[1]); + orientation_data.roll = (int16_t)(mFXOutput.rotation[2] - + imu_zero_reference[2]); #ifdef LOG_VERBOSE printf("IMU Accel x: %d y: %d z: %d \r\n", accel_data.accel_x, diff --git a/Core/Src/msb.c b/Core/Src/msb.c index 149b926..451b158 100644 --- a/Core/Src/msb.c +++ b/Core/Src/msb.c @@ -250,4 +250,21 @@ void process_motion_fx(MFX_input_t *data_in, MFX_output_t *data_out, MotionFX_update(mFXState, data_out, data_in, &delta_time, NULL); } + +float imu_rotation_data[3] = { + 0.0f, 0.0f, 0.0f +}; /* Yaw, pitch, and roll. Updated by vIMUMonitor(). */ +float imu_zero_reference[3] = { + 0.0f, 0.0f, 0.0f +}; /* Reference values for zeroing IMU data. Defaults to 0 (i.e. no zeroing by default)*/ +void imu_zero() +{ + /* Stores the current IMU rotation data in imu_zero_reference. These values will be subtracted from future mFXOutput.rotation data in vIMUMonitor(). */ + imu_zero_reference[0] = imu_rotation_data[0]; + imu_zero_reference[1] = imu_rotation_data[1]; + imu_zero_reference[2] = imu_rotation_data[2]; + printf("IMU zeroed to: Yaw: %f, Pitch: %f, Roll: %f\r\n", + imu_zero_reference[0], imu_zero_reference[1], + imu_zero_reference[2]); +} #endif \ No newline at end of file From 2d78fb2968e7d51e34b915b6699ef51a849e05f5 Mon Sep 17 00:00:00 2001 From: Blake Jackson Date: Wed, 28 May 2025 13:03:24 -0400 Subject: [PATCH 05/10] Update Core/Src/can_handler.c Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- Core/Src/can_handler.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 219933e..13f3ad0 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -75,7 +75,7 @@ void can1_callback(CAN_HandleTypeDef *hcan) /* Read in CAN message */ if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rx_header, new_msg.data) != HAL_OK) { - printf("Failed to recieve CAN message.\n"); + printf("Failed to receive CAN message.\n"); return; } From 0c57968cde9bf8137fcf76ed2deaee49417ac81e Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Thu, 29 May 2025 11:43:46 -0400 Subject: [PATCH 06/10] Added wraparound logic for yaw --- Core/Src/monitor.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Core/Src/monitor.c b/Core/Src/monitor.c index 6ba3925..578fa6b 100644 --- a/Core/Src/monitor.c +++ b/Core/Src/monitor.c @@ -190,10 +190,18 @@ void vIMUMonitor(void *pv_params) imu_rotation_data[1] = mFXOutput.rotation[1]; // Pitch imu_rotation_data[2] = mFXOutput.rotation[2]; // Roll - orientation_data.yaw = (int16_t)(mFXOutput.rotation[0] - - imu_zero_reference[0]); + /* Handle yaw zeroing */ + float diff = mFXOutput.rotation[0] - imu_zero_reference[0]; + if (diff < 0.0f) { + diff += 360.0f; // Make sure all yaw data is in the 0 to 360 degree range. + } + orientation_data.yaw = (int16_t)diff; + + /* Handle pitch zeroing */ orientation_data.pitch = (int16_t)(mFXOutput.rotation[1] - imu_zero_reference[1]); + + /* Handle roll zeroing */ orientation_data.roll = (int16_t)(mFXOutput.rotation[2] - imu_zero_reference[2]); From 77a95f2f267eff8ee94be34ca3f4b31113962ff0 Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Thu, 29 May 2025 11:54:09 -0400 Subject: [PATCH 07/10] CAN msg bytes --- Core/Inc/msb.h | 2 +- Core/Src/can_handler.c | 4 +--- Core/Src/msb.c | 22 ++++++++++++++-------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Core/Inc/msb.h b/Core/Inc/msb.h index 83f2c4c..0ca0433 100644 --- a/Core/Inc/msb.h +++ b/Core/Inc/msb.h @@ -76,7 +76,7 @@ void strain2_read(uint32_t strain2); void motion_fx_init(void); void process_motion_fx(MFX_input_t *data_in, MFX_output_t *data_out, float delta_time); -void imu_zero(); +void imu_zero(uint8_t yaw_byte, uint8_t pitch_byte, uint8_t roll_byte); #endif diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 13f3ad0..09f581a 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -158,9 +158,7 @@ void vCanReceive(void *pv_params) case CANID_IMUZERO_BACKRIGHT: case CANID_IMUZERO_FRONTLEFT: case CANID_IMUZERO_FRONTRIGHT: - if (msg.data[0] > 0) { - imu_zero(); - } + imu_zero(msg.data[0], msg.data[1], msg.data[2]); break; default: break; diff --git a/Core/Src/msb.c b/Core/Src/msb.c index 451b158..2a89260 100644 --- a/Core/Src/msb.c +++ b/Core/Src/msb.c @@ -257,14 +257,20 @@ float imu_rotation_data[3] = { float imu_zero_reference[3] = { 0.0f, 0.0f, 0.0f }; /* Reference values for zeroing IMU data. Defaults to 0 (i.e. no zeroing by default)*/ -void imu_zero() +void imu_zero(uint8_t yaw_byte, uint8_t pitch_byte, uint8_t roll_byte) { - /* Stores the current IMU rotation data in imu_zero_reference. These values will be subtracted from future mFXOutput.rotation data in vIMUMonitor(). */ - imu_zero_reference[0] = imu_rotation_data[0]; - imu_zero_reference[1] = imu_rotation_data[1]; - imu_zero_reference[2] = imu_rotation_data[2]; - printf("IMU zeroed to: Yaw: %f, Pitch: %f, Roll: %f\r\n", - imu_zero_reference[0], imu_zero_reference[1], - imu_zero_reference[2]); + /* Stores the current IMU rotation data in imu_zero_reference for selected axes only. */ + if (yaw_byte > 0) { + imu_zero_reference[0] = imu_rotation_data[0]; + printf("Zeroed yaw.\n"); + } + if (pitch_byte > 0) { + imu_zero_reference[1] = imu_rotation_data[1]; + printf("Zeroed pitch.\n"); + } + if (roll_byte > 0) { + imu_zero_reference[2] = imu_rotation_data[2]; + printf("Zeroed roll.\n"); + } } #endif \ No newline at end of file From caa33a5baa9d778bd9ddcecacaf00cd43fa3eff6 Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Thu, 29 May 2025 12:14:49 -0400 Subject: [PATCH 08/10] Changed BACKRIGHT CAN ID --- Core/Inc/msb_conf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/Inc/msb_conf.h b/Core/Inc/msb_conf.h index 85a2591..0abb9c6 100644 --- a/Core/Inc/msb_conf.h +++ b/Core/Inc/msb_conf.h @@ -28,7 +28,7 @@ #define CANID_IMUZERO_FRONTLEFT 0x60A #define CANID_IMUZERO_FRONTRIGHT 0x60B #define CANID_IMUZERO_BACKLEFT 0x60C -#define CANID_IMUZERO_BACKRIGHT 0x60D +#define CANID_IMUZERO_BACKRIGHT 0x60E // Sensors to use, comment out to disable From b8ddc009acd8dfb3aad954a82dea313182e8d1e6 Mon Sep 17 00:00:00 2001 From: Caio Date: Tue, 29 Jul 2025 21:03:41 -0400 Subject: [PATCH 09/10] zero imu data with wheel data --- Core/Inc/monitor.h | 4 ++++ Core/Inc/msb_conf.h | 3 ++- Core/Src/can_handler.c | 6 +++++- Core/Src/monitor.c | 13 +++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Core/Inc/monitor.h b/Core/Inc/monitor.h index 21283ef..9294e1a 100644 --- a/Core/Inc/monitor.h +++ b/Core/Inc/monitor.h @@ -5,6 +5,10 @@ #include "stm32f4xx_hal.h" #include "msb_conf.h" +#ifdef CAN_ENABLE // TODO change to wheel sensor flag +void record_wheel_angle(uint8_t *data); +#endif + #ifdef SENSOR_TEMP /* Defining Temperature Monitor Task */ void vTempMonitor(void *pv_params); diff --git a/Core/Inc/msb_conf.h b/Core/Inc/msb_conf.h index 0abb9c6..f475d06 100644 --- a/Core/Inc/msb_conf.h +++ b/Core/Inc/msb_conf.h @@ -29,11 +29,12 @@ #define CANID_IMUZERO_FRONTRIGHT 0x60B #define CANID_IMUZERO_BACKLEFT 0x60C #define CANID_IMUZERO_BACKRIGHT 0x60E +#define CANID_WHEEL_ANGLE 0x630 // Sensors to use, comment out to disable // internal -// #define CAN_ENABLE +#define CAN_ENABLE // on central #define SENSOR_TEMP // SHT30 diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 09f581a..32cc4b7 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -13,6 +13,7 @@ #include "can.h" #include "msb_conf.h" #include "msb.h" +#include "monitor.h" #include "stdio.h" #include @@ -24,7 +25,7 @@ static osMessageQueueId_t can_outbound_queue; static osMessageQueueId_t can_inbound_queue; static uint16_t id_list[4] = { - 0x000, 0x000, 0x000, 0x002 + 0x000, CANID_WHEEL_ANGLE, 0x000, 0x002 }; // id_list[0] is reserved for the IMU Zero CAN ID, which is added in can1_init(). extern CAN_HandleTypeDef hcan1; @@ -160,7 +161,10 @@ void vCanReceive(void *pv_params) case CANID_IMUZERO_FRONTRIGHT: imu_zero(msg.data[0], msg.data[1], msg.data[2]); break; + case CANID_WHEEL_ANGLE: + break; default: + record_wheel_angle(msg.data); break; } } diff --git a/Core/Src/monitor.c b/Core/Src/monitor.c index 578fa6b..95539a5 100644 --- a/Core/Src/monitor.c +++ b/Core/Src/monitor.c @@ -16,6 +16,8 @@ extern device_loc_t device_loc; extern float imu_rotation_data[3]; extern float imu_zero_reference[3]; +float wheel_angle = 0.0; + uint16_t convert_can(uint16_t original_value, device_loc_t mode) { switch (mode) { @@ -157,6 +159,10 @@ void vIMUMonitor(void *pv_params) printf("Failed to get IMU data \r\n"); } + if (wheel_angle <= 0.1) { + imu_zero(mFXOutput.rotation[0], mFXOutput.rotation[1], mFXOutput.rotation[2]); + } + /* Run values through LPF of sample size */ accel_data.accel_x = imu_data_temp.ui.xl.mg[0]; accel_data.accel_y = imu_data_temp.ui.xl.mg[1]; @@ -186,6 +192,7 @@ void vIMUMonitor(void *pv_params) process_motion_fx(&mFXInput, &mFXOutput, 0.05f); + imu_rotation_data[0] = mFXOutput.rotation[0]; // Yaw imu_rotation_data[1] = mFXOutput.rotation[1]; // Pitch imu_rotation_data[2] = mFXOutput.rotation[2]; // Roll @@ -376,4 +383,10 @@ void vStrainMonitor(void *pv_params) osDelay(DELAY_SHOCKPOT_REFRESH); } } +#endif + +#ifdef CAN_ENABLE +void record_wheel_angle(uint8_t data[8]) { + wheel_angle = data[0] | data[1] << 8 | data[2] << 16 | data[3] << 24; +} #endif \ No newline at end of file From 5ecfafcade96a8f390a496338da9dd8d747fe8f8 Mon Sep 17 00:00:00 2001 From: Caio Date: Thu, 31 Jul 2025 08:10:52 -0400 Subject: [PATCH 10/10] zero working --- Core/Inc/msb.h | 6 +++--- Core/Inc/msb_conf.h | 16 ++++++++-------- Core/Src/can_handler.c | 8 ++++++-- Core/Src/main.c | 35 +++++------------------------------ Core/Src/monitor.c | 27 ++++++++++++++++++++------- Core/Src/msb.c | 23 ++++++++++++----------- Core/Src/stm32f4xx_it.c | 3 ++- MSB-FW.ioc | 14 ++++++++------ Makefile | 4 ++-- 9 files changed, 66 insertions(+), 70 deletions(-) diff --git a/Core/Inc/msb.h b/Core/Inc/msb.h index 0ca0433..90d8b2b 100644 --- a/Core/Inc/msb.h +++ b/Core/Inc/msb.h @@ -38,12 +38,12 @@ int32_t imu_data_get(stmdev_ctx_t *ctx, stmdev_ctx_t *aux_ctx, lsm6dso_md_t *imu_md_temp, lsm6dso_data_t *imu_data_temp); #ifdef SENSOR_SHOCKPOT -void shockpot_read(uint32_t shockpot_sense); +void shockpot_read(uint32_t *shockpot_sense); #endif #ifdef SENSOR_STRAIN -void strain1_read(uint32_t strain1); -void strain2_read(uint32_t strain2); +void strain1_read(uint32_t *strain1); +void strain2_read(uint32_t *strain2); #endif #ifdef SENSOR_IMU diff --git a/Core/Inc/msb_conf.h b/Core/Inc/msb_conf.h index f475d06..3075ad3 100644 --- a/Core/Inc/msb_conf.h +++ b/Core/Inc/msb_conf.h @@ -5,8 +5,8 @@ // MONITOR #define DELAY_TEMP_SENSOR_REFRESH 500 -#define DELAY_IMU_REFRESH 50 -#define DELAY_SHOCKPOT_REFRESH 500 +#define DELAY_IMU_REFRESH 10 +#define DELAY_SHOCKPOT_REFRESH 5 #define DELAY_STRAIN_REFRESH 500 #define DELAY_TOF_REFRESH 500 #define DELAY_WHEEL_TEMP_REFRESH 500 @@ -37,15 +37,15 @@ #define CAN_ENABLE // on central -#define SENSOR_TEMP // SHT30 -#define SENSOR_SHOCKPOT // ADC1 -#define SENSOR_STRAIN // ADC1 -#define SENSOR_TOF // VL6180X +//#define SENSOR_TEMP // SHT30 +//#define SENSOR_SHOCKPOT // ADC1 +//#define SENSOR_STRAIN // ADC1 +//#define SENSOR_TOF // VL6180X #define SENSOR_IMU // LSM6DSO // VERBOSE LOGGING -#define LOG_VERBOSE +//#define LOG_VERBOSE // Note // STACK SIZES @@ -55,5 +55,5 @@ #define TEMP_MONITOR_STACK_SIZE 64 * 16 // temp_monitor_handle #define IMU_MONITOR_STACK_SIZE 128 * 32 // imu_monitor_handle #define TOF_MONITOR_STACK_SIZE 128 * 8 // tof_monitor_handle -#define SHOCKPOT_MONITOR_STACK_SIZE 64 * 8 // shockpot_monitor_handle +#define SHOCKPOT_MONITOR_STACK_SIZE 128 * 8 // shockpot_monitor_handle #define STRAIN_MONITOR_STACK_SIZE 64 * 8 // strain_monitor_handle \ No newline at end of file diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 32cc4b7..205acf7 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -115,6 +115,10 @@ void vCanDispatch(void *pv_params) &msg_from_queue, NULL, osWaitForever)) { #ifdef CAN_ENABLE +while (HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) == 0) { + osDelay(1); + } + msg_status = can_send_msg(can1, &msg_from_queue); if (msg_status != HAL_OK) { printf("Failed to send CAN message"); @@ -153,7 +157,7 @@ void vCanReceive(void *pv_params) for (;;) { while (osOK == - osMessageQueueGet(can_inbound_queue, &msg, 0U, 0U)) { + osMessageQueueGet(can_inbound_queue, &msg, 0U, osWaitForever)) { switch (msg.id) { case CANID_IMUZERO_BACKLEFT: case CANID_IMUZERO_BACKRIGHT: @@ -162,9 +166,9 @@ void vCanReceive(void *pv_params) imu_zero(msg.data[0], msg.data[1], msg.data[2]); break; case CANID_WHEEL_ANGLE: + record_wheel_angle(msg.data); break; default: - record_wheel_angle(msg.data); break; } } diff --git a/Core/Src/main.c b/Core/Src/main.c index a65fe1f..d49cf60 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -121,7 +121,6 @@ int _write(int file, char* ptr, int len) { */ int main(void) { - /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ @@ -153,7 +152,7 @@ int main(void) MX_USART2_UART_Init(); MX_CRC_Init(); /* USER CODE BEGIN 2 */ - printf("INIT MSB... FINDING ID"); + // determine MSB location bool loc1 = HAL_GPIO_ReadPin(Addr0_GPIO_Port, Addr0_Pin); bool loc2 = HAL_GPIO_ReadPin(Addr1_GPIO_Port, Addr1_Pin); @@ -180,30 +179,6 @@ int main(void) device_loc = DEVICE_FRONT_LEFT; } HAL_Delay(500); - uint8_t devices = 0u; - printf("Searching for I2C devices on the bus...\n"); - /* Values outside 0x03 and 0x77 are invalid. */ - for (uint8_t i = 0x03u; i < 0x78u; i++) - { - HAL_IWDG_Refresh(&hiwdg); - uint8_t address = i << 1u ; - /* In case there is a positive feedback, print it out. */ - if (HAL_OK == HAL_I2C_IsDeviceReady(&hi2c3, address, 3u, 10u)) - { - printf("Device found: 0x%02X\n", address); - devices++; - } - } - /* Feedback of the total number of devices. */ - if (0u == devices) - { - printf("No device found.\n"); - } - else - { - printf("Total found devices: %d\n", devices); - } - /* USER CODE END 2 */ @@ -216,7 +191,7 @@ int main(void) // determine the configuration of the device msb_init(); - vcc5_en_write(true); + vcc5_en_write(false); can1_init(); /* USER CODE END RTOS_MUTEX */ @@ -332,7 +307,7 @@ void SystemClock_Config(void) RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { @@ -361,7 +336,7 @@ static void MX_ADC1_Init(void) /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ hadc1.Instance = ADC1; - hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV6; + hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8; hadc1.Init.Resolution = ADC_RESOLUTION_12B; hadc1.Init.ScanConvMode = ENABLE; hadc1.Init.ContinuousConvMode = ENABLE; @@ -371,7 +346,7 @@ static void MX_ADC1_Init(void) hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.NbrOfConversion = 3; hadc1.Init.DMAContinuousRequests = ENABLE; - hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV; if (HAL_ADC_Init(&hadc1) != HAL_OK) { Error_Handler(); diff --git a/Core/Src/monitor.c b/Core/Src/monitor.c index 95539a5..590bf2d 100644 --- a/Core/Src/monitor.c +++ b/Core/Src/monitor.c @@ -159,8 +159,8 @@ void vIMUMonitor(void *pv_params) printf("Failed to get IMU data \r\n"); } - if (wheel_angle <= 0.1) { - imu_zero(mFXOutput.rotation[0], mFXOutput.rotation[1], mFXOutput.rotation[2]); + if (fabs(wheel_angle) <= 1.0) { + imu_zero(1, 1, 1); // zero all axes } /* Run values through LPF of sample size */ @@ -311,21 +311,33 @@ void vShockpotMonitor(void *pv_params) { can_msg_t shockpot_msg = { .id = convert_can(CANID_SHOCK_SENSE, device_loc), - .len = 4, + .len = 6, .data = { 0 } }; uint32_t shock_value = 0; + struct __attribute__((__packed__)) { + float in; + uint16_t raw; + } shockpot_data; + + for (;;) { - shockpot_read(shock_value); + shockpot_read(&shock_value); #ifdef LOG_VERBOSE printf("Shock value:\t%ld\r\n", shock_value); #endif + //printf("Shock value:\t%ld\r\n", shock_value); - endian_swap(&shock_value, sizeof(shock_value)); + // convert to inches, get percent and multiply by 50 mm (stroke length) then convert to inches + float in = (shock_value / 4095.0) * 54.44 * (1/25.4); + + shockpot_data.in = in; + endian_swap(&shockpot_data.in, sizeof(shockpot_data.in)); + shockpot_data.raw = shock_value; - memcpy(shockpot_msg.data, &shock_value, shockpot_msg.len); + memcpy(shockpot_msg.data, &shockpot_data, shockpot_msg.len); /* Send CAN message */ if (queue_can_msg(shockpot_msg)) { printf("Failed to send CAN message\r\n"); @@ -387,6 +399,7 @@ void vStrainMonitor(void *pv_params) #ifdef CAN_ENABLE void record_wheel_angle(uint8_t data[8]) { - wheel_angle = data[0] | data[1] << 8 | data[2] << 16 | data[3] << 24; + endian_swap(data, 4); + wheel_angle = *((float *)data); } #endif \ No newline at end of file diff --git a/Core/Src/msb.c b/Core/Src/msb.c index 2a89260..c6105aa 100644 --- a/Core/Src/msb.c +++ b/Core/Src/msb.c @@ -64,12 +64,13 @@ int8_t msb_init() /* Initialize the IMU */ assert(!LSM6DSO_Init(&imu)); /* This is always connected */ - /* Setup IMU Accelerometer */ + /* Setup IMU Accelerometer - default 104Hz */ LSM6DSO_ACC_Enable(&imu); - /* Setup IMU Gyroscope */ LSM6DSO_GYRO_Enable(&imu); + LSM6DSO_ACC_Set_Filter_Mode(&imu, 0, 4); + LSM6DSO_FIFO_Set_Mode(&imu, 0); LSM6DSO_ACC_Disable_Inactivity_Detection(&imu); @@ -134,21 +135,21 @@ void adc1_read(uint32_t result_buf[3]) #endif #ifdef SENSOR_SHOCKPOT -void shockpot_read(uint32_t shockpot_sense) +void shockpot_read(uint32_t *shockpot_sense) { - memcpy((uint32_t *)shockpot_sense, adc1_buf, sizeof(shockpot_sense)); + memcpy(shockpot_sense, adc1_buf, sizeof(shockpot_sense)); } #endif #ifdef SENSOR_STRAIN -void strain1_read(uint32_t strain1) +void strain1_read(uint32_t *strain1) { - memcpy((uint32_t *)strain1, adc1_buf + 1, sizeof(strain1)); + memcpy(strain1, adc1_buf + 1, sizeof(strain1)); } -void strain2_read(uint32_t strain2) +void strain2_read(uint32_t *strain2) { - memcpy((uint32_t *)strain2, adc1_buf + 2, sizeof(strain2)); + memcpy(strain2, adc1_buf + 2, sizeof(strain2)); } #endif @@ -262,15 +263,15 @@ void imu_zero(uint8_t yaw_byte, uint8_t pitch_byte, uint8_t roll_byte) /* Stores the current IMU rotation data in imu_zero_reference for selected axes only. */ if (yaw_byte > 0) { imu_zero_reference[0] = imu_rotation_data[0]; - printf("Zeroed yaw.\n"); + //printf("Zeroed yaw.\n"); } if (pitch_byte > 0) { imu_zero_reference[1] = imu_rotation_data[1]; - printf("Zeroed pitch.\n"); + //printf("Zeroed pitch.\n"); } if (roll_byte > 0) { imu_zero_reference[2] = imu_rotation_data[2]; - printf("Zeroed roll.\n"); + //printf("Zeroed roll.\n"); } } #endif \ No newline at end of file diff --git a/Core/Src/stm32f4xx_it.c b/Core/Src/stm32f4xx_it.c index cb9f1d6..2ce3922 100644 --- a/Core/Src/stm32f4xx_it.c +++ b/Core/Src/stm32f4xx_it.c @@ -20,6 +20,7 @@ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "stm32f4xx_it.h" +#include "can_handler.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ @@ -180,7 +181,7 @@ void DebugMon_Handler(void) void CAN1_RX0_IRQHandler(void) { /* USER CODE BEGIN CAN1_RX0_IRQn 0 */ - + can1_callback(&hcan1); /* USER CODE END CAN1_RX0_IRQn 0 */ HAL_CAN_IRQHandler(&hcan1); /* USER CODE BEGIN CAN1_RX0_IRQn 1 */ diff --git a/MSB-FW.ioc b/MSB-FW.ioc index 56c3c5a..672b4cf 100644 --- a/MSB-FW.ioc +++ b/MSB-FW.ioc @@ -2,10 +2,11 @@ ADC1.Channel-0\#ChannelRegularConversion=ADC_CHANNEL_0 ADC1.Channel-1\#ChannelRegularConversion=ADC_CHANNEL_5 ADC1.Channel-2\#ChannelRegularConversion=ADC_CHANNEL_6 -ADC1.ClockPrescaler=ADC_CLOCK_SYNC_PCLK_DIV6 +ADC1.ClockPrescaler=ADC_CLOCK_SYNC_PCLK_DIV8 ADC1.ContinuousConvMode=ENABLE ADC1.DMAContinuousRequests=ENABLE -ADC1.IPParameters=Rank-0\#ChannelRegularConversion,master,Channel-0\#ChannelRegularConversion,SamplingTime-0\#ChannelRegularConversion,NbrOfConversionFlag,ClockPrescaler,ScanConvMode,ContinuousConvMode,Rank-1\#ChannelRegularConversion,Channel-1\#ChannelRegularConversion,SamplingTime-1\#ChannelRegularConversion,Rank-2\#ChannelRegularConversion,Channel-2\#ChannelRegularConversion,SamplingTime-2\#ChannelRegularConversion,NbrOfConversion,DMAContinuousRequests +ADC1.EOCSelection=ADC_EOC_SEQ_CONV +ADC1.IPParameters=Rank-0\#ChannelRegularConversion,master,Channel-0\#ChannelRegularConversion,SamplingTime-0\#ChannelRegularConversion,NbrOfConversionFlag,ClockPrescaler,ScanConvMode,ContinuousConvMode,Rank-1\#ChannelRegularConversion,Channel-1\#ChannelRegularConversion,SamplingTime-1\#ChannelRegularConversion,Rank-2\#ChannelRegularConversion,Channel-2\#ChannelRegularConversion,SamplingTime-2\#ChannelRegularConversion,NbrOfConversion,DMAContinuousRequests,EOCSelection ADC1.NbrOfConversion=3 ADC1.NbrOfConversionFlag=1 ADC1.Rank-0\#ChannelRegularConversion=1 @@ -226,7 +227,7 @@ ProjectManager.FreePins=false ProjectManager.HalAssertFull=false ProjectManager.HeapSize=0x200 ProjectManager.KeepUserCode=true -ProjectManager.LastFirmware=true +ProjectManager.LastFirmware=false ProjectManager.LibraryCopy=1 ProjectManager.MainLocation=Core/Src ProjectManager.NoMain=false @@ -248,8 +249,9 @@ RCC.AHBFreq_Value=84000000 RCC.APB1CLKDivider=RCC_HCLK_DIV2 RCC.APB1Freq_Value=42000000 RCC.APB1TimFreq_Value=84000000 -RCC.APB2Freq_Value=84000000 -RCC.APB2TimFreq_Value=84000000 +RCC.APB2CLKDivider=RCC_HCLK_DIV4 +RCC.APB2Freq_Value=21000000 +RCC.APB2TimFreq_Value=42000000 RCC.CortexFreq_Value=84000000 RCC.EthernetFreq_Value=84000000 RCC.FCLKCortexFreq_Value=84000000 @@ -258,7 +260,7 @@ RCC.HCLKFreq_Value=84000000 RCC.HSE_VALUE=8000000 RCC.HSI_VALUE=16000000 RCC.I2SClocksFreq_Value=192000000 -RCC.IPParameters=48MHZClocksFreq_Value,AHBFreq_Value,APB1CLKDivider,APB1Freq_Value,APB1TimFreq_Value,APB2Freq_Value,APB2TimFreq_Value,CortexFreq_Value,EthernetFreq_Value,FCLKCortexFreq_Value,FamilyName,HCLKFreq_Value,HSE_VALUE,HSI_VALUE,I2SClocksFreq_Value,LSE_VALUE,LSI_VALUE,MCO2PinFreq_Value,PLLCLKFreq_Value,PLLM,PLLN,PLLQCLKFreq_Value,PLLSourceVirtual,RTCFreq_Value,RTCHSEDivFreq_Value,SYSCLKFreq_VALUE,SYSCLKSource,VCOI2SOutputFreq_Value,VCOInputFreq_Value,VCOOutputFreq_Value,VcooutputI2S +RCC.IPParameters=48MHZClocksFreq_Value,AHBFreq_Value,APB1CLKDivider,APB1Freq_Value,APB1TimFreq_Value,APB2CLKDivider,APB2Freq_Value,APB2TimFreq_Value,CortexFreq_Value,EthernetFreq_Value,FCLKCortexFreq_Value,FamilyName,HCLKFreq_Value,HSE_VALUE,HSI_VALUE,I2SClocksFreq_Value,LSE_VALUE,LSI_VALUE,MCO2PinFreq_Value,PLLCLKFreq_Value,PLLM,PLLN,PLLQCLKFreq_Value,PLLSourceVirtual,RTCFreq_Value,RTCHSEDivFreq_Value,SYSCLKFreq_VALUE,SYSCLKSource,VCOI2SOutputFreq_Value,VCOInputFreq_Value,VCOOutputFreq_Value,VcooutputI2S RCC.LSE_VALUE=32768 RCC.LSI_VALUE=32000 RCC.MCO2PinFreq_Value=84000000 diff --git a/Makefile b/Makefile index 0d1465d..5abf5fd 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [4.5.0-RC5] date: [Tue May 06 13:07:23 EDT 2025] +# File automatically-generated by tool: [projectgenerator] version: [4.5.0-RC5] date: [Fri May 30 20:32:44 EDT 2025] ########################################################################################################################## # ------------------------------------------------ @@ -189,7 +189,7 @@ LIBS = -lc -lm -lnosys \ -l:MotionFX_CM4F_wc32_ot_hard.a LIBDIR = \ -LMiddlewares/ST/STM32_MotionFX_Library/Lib -LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections -u printf_floats +LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections -u _printf_float # default action: build all all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin