2026-03-18 21:43:28 +00:00
|
|
|
#include "imu.h"
|
|
|
|
|
#include "esp_log.h"
|
|
|
|
|
#include "esp_timer.h"
|
|
|
|
|
#include "freertos/idf_additions.h"
|
2026-03-30 22:09:23 +01:00
|
|
|
#include "hal/spi_types.h"
|
2026-04-03 17:52:02 +01:00
|
|
|
#include "sens_fus.h"
|
2026-03-18 21:43:28 +00:00
|
|
|
|
2026-03-30 22:09:23 +01:00
|
|
|
#ifdef PS
|
|
|
|
|
#undef PS
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef F
|
|
|
|
|
#undef F
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <Eigen/Dense>
|
2026-03-18 21:43:28 +00:00
|
|
|
|
2026-03-30 22:09:23 +01:00
|
|
|
static const char *TAG = "IMU";
|
2026-03-18 21:43:28 +00:00
|
|
|
|
|
|
|
|
BNO08x *setup_imu(imu_state *state) {
|
2026-03-30 22:09:23 +01:00
|
|
|
BNO08x *imu = new BNO08x(
|
|
|
|
|
bno08x_config_t(SPI2_HOST, GPIO_NUM_26, GPIO_NUM_27, GPIO_NUM_25,
|
|
|
|
|
GPIO_NUM_33, GPIO_NUM_36, GPIO_NUM_32, 2000000, false));
|
2026-03-18 21:43:28 +00:00
|
|
|
|
|
|
|
|
if (!imu->initialize()) {
|
|
|
|
|
ESP_LOGE(TAG, "BNO08x Init failure.");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
imu->dynamic_calibration_autosave_enable();
|
|
|
|
|
imu->dynamic_calibration_enable(BNO08xCalSel::all);
|
|
|
|
|
|
|
|
|
|
imu->rpt.rv_game.enable(2500UL);
|
|
|
|
|
imu->rpt.linear_accelerometer.enable(2500UL);
|
|
|
|
|
|
|
|
|
|
imu->register_cb([imu, state]() {
|
|
|
|
|
if (imu->rpt.rv_game.has_new_data()) {
|
|
|
|
|
auto sens_rot = imu->rpt.rv_game.get_quat();
|
2026-03-30 22:09:23 +01:00
|
|
|
state->rot = {sens_rot.i, sens_rot.j, sens_rot.k, sens_rot.real};
|
2026-03-18 21:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (imu->rpt.linear_accelerometer.has_new_data()) {
|
|
|
|
|
|
2026-03-30 22:09:23 +01:00
|
|
|
auto sens_accel = imu->rpt.linear_accelerometer.get();
|
|
|
|
|
state->accel = {sens_accel.x, sens_accel.y, sens_accel.z};
|
2026-03-18 21:43:28 +00:00
|
|
|
|
2026-03-30 22:09:23 +01:00
|
|
|
int64_t current_time = esp_timer_get_time();
|
2026-03-18 21:43:28 +00:00
|
|
|
|
2026-03-30 22:09:23 +01:00
|
|
|
if (state->last_time != -1) {
|
|
|
|
|
float dt = ((float)(current_time - state->last_time)) / 1000000.0f;
|
|
|
|
|
Vec3C accel_global = apply_rot(&state->accel, &state->rot);
|
2026-03-18 21:43:28 +00:00
|
|
|
|
2026-04-03 17:52:02 +01:00
|
|
|
if (xSemaphoreTake(sens_fus_mutex, (TickType_t)2) == pdTRUE) {
|
|
|
|
|
sens_fus.predict(dt, Eigen::Vector3f(accel_global.x, accel_global.y,
|
|
|
|
|
accel_global.z));
|
|
|
|
|
xSemaphoreGive(sens_fus_mutex);
|
2026-03-30 22:09:23 +01:00
|
|
|
} else {
|
|
|
|
|
ESP_LOGE(TAG, "Failed to get mutex.");
|
2026-03-18 21:43:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-30 22:09:23 +01:00
|
|
|
state->last_time = current_time;
|
2026-03-18 21:43:28 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "IMU Setup Success.");
|
|
|
|
|
return imu;
|
|
|
|
|
}
|