Skip to content

Commit

Permalink
Set fusion mode independent of registering the callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
corruptbear committed Jun 10, 2024
1 parent 8a542e3 commit 2f77e92
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
3 changes: 2 additions & 1 deletion software/firmware/src/peripherals/include/imu.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,10 @@ typedef struct

void imu_init(void);
void imu_deinit(void);
void imu_register_motion_change_callback(motion_change_callback_t callback, bno055_opmode_t mode);
void imu_register_motion_change_callback(motion_change_callback_t callback);
void imu_register_data_ready_callback(data_ready_callback_t callback);
void imu_set_power_mode(bno055_powermode_t power_mode);
void imu_set_fusion_mode(bno055_opmode_t fusion_mode);
void imu_read_accel_data(bno055_acc_t *acc);
void imu_read_linear_accel_data(bno055_acc_t *acc);
void imu_read_gravity_accel_data(bno055_acc_t *acc);
Expand Down
38 changes: 21 additions & 17 deletions software/firmware/src/peripherals/src/imu.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ static void read_int16_vector(uint8_t reg_number, int16_t *read_buffer, uint32_t

// IMU Chip-Specific API Functions -------------------------------------------------------------------------------------

static void set_mode(bno055_opmode_t mode)
static void imu_set_op_mode(bno055_opmode_t op_mode)
{
// Set the indicated mode and delay to allow it to take effect
i2c_write8(BNO055_OPR_MODE_ADDR, mode);
i2c_write8(BNO055_OPR_MODE_ADDR, op_mode);
am_util_delay_ms(30);
}

static bno055_opmode_t get_mode(void)
static bno055_opmode_t imu_get_op_mode(void)
{
return (bno055_opmode_t)i2c_read8(BNO055_OPR_MODE_ADDR);
}
Expand All @@ -130,7 +130,7 @@ static void set_use_external_crystal(void)
static void enter_suspend_mode(void)
{
// Switch to configuration mode and suspend
set_mode(OPERATION_MODE_CONFIG);
imu_set_op_mode(OPERATION_MODE_CONFIG);
i2c_write8(BNO055_PWR_MODE_ADDR, 0x02);
}

Expand Down Expand Up @@ -231,7 +231,7 @@ void imu_init(void)
while (i2c_read8(BNO055_CHIP_ID_ADDR) != BNO055_ID)
am_util_delay_ms(100);
i2c_write8(BNO055_PAGE_ID_ADDR, 0);
set_mode(OPERATION_MODE_CONFIG);
imu_set_op_mode(OPERATION_MODE_CONFIG);

// Set up an external crystal and the sensor output units
set_use_external_crystal();
Expand Down Expand Up @@ -268,26 +268,30 @@ void imu_deinit(void)
am_hal_iom_uninitialize(i2c_handle);
}

void imu_register_motion_change_callback(motion_change_callback_t callback, bno055_opmode_t mode)
void imu_register_motion_change_callback(motion_change_callback_t callback)
{
// Set up IMU motion-based interrupts
motion_change_callback = callback;
set_mode(OPERATION_MODE_CONFIG);
enable_motion_interrupts();
set_mode(mode);
}

void imu_register_data_ready_callback(data_ready_callback_t callback){
data_ready_callback = callback;
enable_data_ready_interrupts();
}

void imu_set_fusion_mode(bno055_opmode_t fusion_mode)
{
imu_set_op_mode(OPERATION_MODE_CONFIG);
imu_set_op_mode(fusion_mode);
}

void imu_set_power_mode(bno055_powermode_t power_mode)
{
bno055_opmode_t saved_mode = get_mode();
set_mode(OPERATION_MODE_CONFIG);
bno055_opmode_t saved_mode = imu_get_op_mode();
imu_set_op_mode(OPERATION_MODE_CONFIG);
i2c_write8(BNO055_PWR_MODE_ADDR, power_mode);
set_mode(saved_mode);
imu_set_op_mode(saved_mode);
}

void imu_read_accel_data(bno055_acc_t *acc)
Expand Down Expand Up @@ -381,13 +385,13 @@ void imu_read_calibration_status(bno55_calib_status_t *status)
void imu_read_calibration_offsets(bno055_calib_offsets_t *offsets)
{
static int16_t calib_data[11];
bno055_opmode_t saved_mode = get_mode();
bno055_opmode_t saved_mode = imu_get_op_mode();
//calibration values are only availble in config mode
set_mode(OPERATION_MODE_CONFIG);
imu_set_op_mode(OPERATION_MODE_CONFIG);
//read the 11 offset values
read_int16_vector(ACCEL_OFFSET_X_LSB_ADDR, calib_data, sizeof(calib_data));
//revert to the previous mode
set_mode(saved_mode);
imu_set_op_mode(saved_mode);

offsets->accel_offset_x = calib_data[0];
offsets->accel_offset_y = calib_data[1];
Expand Down Expand Up @@ -423,11 +427,11 @@ bool imu_set_axis_remap(bno055_axis_remap_t remap)
uint8_t reg_axis_map_config = remap.x_remap_val | (remap.y_remap_val << 2) | (remap.z_remap_val << 4);
uint8_t reg_axis_map_sign = (remap.x_remap_sign << 2) | (remap.y_remap_sign << 1) | (remap.z_remap_sign);

bno055_opmode_t saved_mode = get_mode();
set_mode(OPERATION_MODE_CONFIG);
bno055_opmode_t saved_mode = imu_get_op_mode();
imu_set_op_mode(OPERATION_MODE_CONFIG);
i2c_write8(BNO055_AXIS_MAP_CONFIG_ADDR, reg_axis_map_config);
i2c_write8(BNO055_AXIS_MAP_SIGN_ADDR, reg_axis_map_sign);
set_mode(saved_mode);
imu_set_op_mode(saved_mode);

//test whether the set is successful
bno055_axis_remap_t remap_verification = {0};
Expand Down
3 changes: 2 additions & 1 deletion software/firmware/src/tasks/app_task_ranging.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ void AppTaskRanging(void *uid)
{
storage_write_motion_status(imu_read_in_motion());
battery_register_event_callback(battery_event_handler);
imu_register_motion_change_callback(motion_change_handler, OPERATION_MODE_ACCONLY);
imu_set_fusion_mode(OPERATION_MODE_ACCONLY);
imu_register_motion_change_callback(motion_change_handler);
}
#endif

Expand Down
3 changes: 2 additions & 1 deletion software/firmware/tests/peripherals/test_imu.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ int main(void)
//bno055_axis_remap_t remap = {.x_remap_val = 1, .y_remap_val = 0, .z_remap_val = 2};
bno055_axis_remap_t remap = {0};

imu_register_motion_change_callback(motion_interrupt, OPERATION_MODE_NDOF);
imu_set_fusion_mode(OPERATION_MODE_NDOF);
imu_register_motion_change_callback(motion_interrupt);
//imu_register_data_ready_callback(read_data);
imu_set_power_mode(POWER_MODE_NORMAL);

Expand Down

0 comments on commit 2f77e92

Please sign in to comment.