ESP32-CAN/main/main.cpp

74 lines
2.1 KiB
C++
Raw Normal View History

2026-03-16 14:25:49 +00:00
#include "BNO08x.hpp"
#include "BNO08xGlobalTypes.hpp"
2026-03-16 00:49:02 +00:00
#include "drone_controller.h"
2026-03-16 14:25:49 +00:00
#include "esp_timer.h"
#include <cmath>
2026-03-16 14:25:49 +00:00
#include <cstdint>
#include <stdio.h>
2026-03-16 00:49:02 +00:00
2026-03-16 14:25:49 +00:00
static const constexpr char *TAG = "Main";
2026-03-16 00:49:02 +00:00
2026-03-16 14:25:49 +00:00
static struct Vec3C accel = {0, 0, 0};
static struct Vec3C last_accel = {0, 0, 0};
2026-03-16 14:25:49 +00:00
static struct QuatC rot = {0, 0, 0, 0};
static struct Vec3C pos = {0, 0, 0};
static struct Vec3C vel = {0, 0, 0};
static int64_t last_time = -1;
2026-03-16 00:49:02 +00:00
static struct Vec3C euler_ang = {0, 0, 0};
2026-03-16 14:25:49 +00:00
extern "C" void app_main(void) {
BNO08x *imu = new BNO08x();
2026-03-16 00:49:02 +00:00
2026-03-16 14:25:49 +00:00
if (!imu->initialize()) {
ESP_LOGE(TAG, "Init failure");
return;
}
2026-03-16 00:49:02 +00:00
2026-03-16 14:25:49 +00:00
imu->dynamic_calibration_autosave_enable();
imu->dynamic_calibration_enable(BNO08xCalSel::all);
// Linear accel at 100Hz (10000UL us)
imu->rpt.rv_game.enable(2500UL);
imu->rpt.linear_accelerometer.enable(2500UL);
imu->rpt.rv_game.tare();
2026-03-16 14:25:49 +00:00
imu->register_cb([imu]() {
if (imu->rpt.linear_accelerometer.has_new_data()) {
auto sens_accel = imu->rpt.linear_accelerometer.get();
accel = {sens_accel.x, sens_accel.y, sens_accel.z};
int64_t time = esp_timer_get_time();
if (last_time != -1) {
float dt = (time - last_time) / 1000000.0f;
2026-03-16 14:25:49 +00:00
Vec3C delta = apply_rot(&accel, &rot);
delta.x += last_accel.x;
delta.y += last_accel.y;
delta.z += last_accel.z;
2026-03-16 14:25:49 +00:00
multiply_vec_by(&delta, dt * 0.5f);
2026-03-16 14:25:49 +00:00
add_to_vec(&vel, &delta);
last_accel = accel;
2026-03-16 14:25:49 +00:00
}
last_time = time;
}
2026-03-16 14:25:49 +00:00
if (imu->rpt.rv_game.has_new_data()) {
auto sens_rot = imu->rpt.rv_game.get_quat();
auto euler = imu->rpt.rv_game.get_euler();
euler_ang = {euler.x, euler.y, euler.z};
2026-03-16 14:25:49 +00:00
rot = {sens_rot.i, sens_rot.j, sens_rot.k, sens_rot.real};
}
});
while (1) {
vTaskDelay(pdMS_TO_TICKS(100));
// Print filtered data to see the difference
printf("accel_filt - %f, %f, %f", accel.x, accel.y, accel.z);
printf("; vel - %f, %f, %f", vel.x, vel.y, vel.z);
printf("; euler - %f, %f, %f\n", euler_ang.x, euler_ang.y, euler_ang.z);
// multiply_vec_by(&vel, 0.99);
2026-03-16 00:49:02 +00:00
}
}