Make imu configuration parameters work

This commit is contained in:
Oleg Kalachev
2026-08-08 01:24:43 +03:00
parent 6b36488245
commit 0a53170097
2 changed files with 27 additions and 23 deletions
+26 -22
View File
@@ -10,8 +10,9 @@
#include "filter.h" #include "filter.h"
#include "util.h" #include "util.h"
IMU imu; IMU *imu;
int imuModel = 1, imuBus = 0; int imuModel = 1;
int imuBus = 0; // 0 - SPI, 1 - I2C
int imuSckPin = SCK, imuMisoPin = MISO, imuMosiPin = MOSI, imuCsPin = -1, imuIntPin = -1; int imuSckPin = SCK, imuMisoPin = MISO, imuMosiPin = MOSI, imuCsPin = -1, imuIntPin = -1;
int imuSdaPin = SDA, imuSclPin = SCL; int imuSdaPin = SDA, imuSclPin = SCL;
Vector imuRotation(0, 0, PI / 2); // imu orientation as Euler angles Vector imuRotation(0, 0, PI / 2); // imu orientation as Euler angles
@@ -27,30 +28,33 @@ LowPassFilter<Vector> gyroBiasFilter(0.001);
void setupIMU() { void setupIMU() {
print("Setup IMU\n"); print("Setup IMU\n");
free(imu);
if (imuBus == 0 && imuCsPin > 0) { if (imuBus == 0 && imuCsPin > 0) {
// SPI connection // SPI connection
SPI.begin(imuSckPin, imuMisoPin, imuMosiPin); SPI.begin(imuSckPin, imuMisoPin, imuMosiPin);
imu.begin((IMU::Model)imuModel, SPI, imuCsPin, imuIntPin); imu = IMU::create(imuModel, SPI, imuCsPin, imuIntPin);
imu->begin();
} else if (imuBus == 1) { } else if (imuBus == 1) {
// I2C connection // I2C connection
Wire.setPins(imuSdaPin, imuSclPin); Wire.setPins(imuSdaPin, imuSclPin);
imu.begin((IMU::Model)imuModel, Wire, imuIntPin); imu = IMU::create(imuModel, Wire, imuIntPin);
imu->begin();
} }
configureIMU(); configureIMU();
} }
void configureIMU() { void configureIMU() {
imu.setAccelRange(imu.ACCEL_RANGE_4G); imu->setAccelRange(IMU::ACCEL_RANGE_4G);
imu.setGyroRange(imu.GYRO_RANGE_2000DPS); imu->setGyroRange(IMU::GYRO_RANGE_2000DPS);
imu.setDLPF(imu.DLPF_MAX); imu->setDLPF(IMU::DLPF_MAX);
imu.setRate(imu.RATE_1KHZ_APPROX); imu->setRate(IMU::RATE_1KHZ_APPROX);
imu.setupInterrupt(); imu->setupInterrupt();
} }
void readIMU() { void readIMU() {
imu.waitForData(); imu->waitForData();
imu.getGyro(gyro.x, gyro.y, gyro.z); imu->getGyro(gyro.x, gyro.y, gyro.z);
imu.getAccel(acc.x, acc.y, acc.z); imu->getAccel(acc.x, acc.y, acc.z);
calibrateGyroOnce(); calibrateGyroOnce();
// Apply scale and bias // Apply scale and bias
@@ -72,7 +76,7 @@ void calibrateGyroOnce() {
void calibrateAccel() { void calibrateAccel() {
print("Calibrating accelerometer\n"); print("Calibrating accelerometer\n");
imu.setAccelRange(imu.ACCEL_RANGE_2G); // the most sensitive mode imu->setAccelRange(IMU::ACCEL_RANGE_2G); // the most sensitive mode
print("1/6 Place level [8 sec]\n"); print("1/6 Place level [8 sec]\n");
pause(8); pause(8);
@@ -106,9 +110,9 @@ void calibrateAccelOnce() {
// Compute the average of the accelerometer readings // Compute the average of the accelerometer readings
acc = Vector(0, 0, 0); acc = Vector(0, 0, 0);
for (int i = 0; i < samples; i++) { for (int i = 0; i < samples; i++) {
imu.waitForData(); imu->waitForData();
Vector sample; Vector sample;
imu.getAccel(sample.x, sample.y, sample.z); imu->getAccel(sample.x, sample.y, sample.z);
acc = acc + sample; acc = acc + sample;
} }
acc = acc / samples; acc = acc / samples;
@@ -133,18 +137,18 @@ void printIMUCalibration() {
} }
void printIMUInfo() { void printIMUInfo() {
imu.status() ? print("status: ERROR %d\n", imu.status()) : print("status: OK\n"); imu->status() ? print("status: ERROR %d\n", imu->status()) : print("status: OK\n");
print("model: %s\n", imu.getModel()); print("model: %s\n", imu->getModel());
print("who am I: 0x%02X\n", imu.whoAmI()); print("who am I: 0x%02X\n", imu->whoAmI());
print("rate: %.0f\n", loopRate); print("rate: %.0f\n", loopRate);
print("interrupt mode: %s\n", imuIntPin != -1 ? "pin" : "timer"); print("interrupt mode: %s\n", imuIntPin != -1 ? "pin" : "timer");
print("temperature: %.1f °C\n", imu.getTemp()); print("temperature: %.1f °C\n", imu->getTemp());
print("gyro: %f %f %f\n", gyro.x, gyro.y, gyro.z); print("gyro: %f %f %f\n", gyro.x, gyro.y, gyro.z);
print("acc: %f %f %f\n", acc.x, acc.y, acc.z); print("acc: %f %f %f\n", acc.x, acc.y, acc.z);
imu.waitForData(); imu->waitForData();
Vector rawGyro, rawAcc; Vector rawGyro, rawAcc;
imu.getGyro(rawGyro.x, rawGyro.y, rawGyro.z); imu->getGyro(rawGyro.x, rawGyro.y, rawGyro.z);
imu.getAccel(rawAcc.x, rawAcc.y, rawAcc.z); imu->getAccel(rawAcc.x, rawAcc.y, rawAcc.z);
print("raw gyro: %f %f %f\n", rawGyro.x, rawGyro.y, rawGyro.z); print("raw gyro: %f %f %f\n", rawGyro.x, rawGyro.y, rawGyro.z);
print("raw acc: %f %f %f\n", rawAcc.x, rawAcc.y, rawAcc.z); print("raw acc: %f %f %f\n", rawAcc.x, rawAcc.y, rawAcc.z);
} }
+1 -1
View File
@@ -100,7 +100,7 @@ LogTopic logTopics[] = {
LogTopic(5, LogTopic(5,
{"voltage", &voltage}, {"voltage", &voltage},
{"temp", &temperatureRead}, {"temp", &temperatureRead},
{"imuTemp", []() { return imu.getTemp(); }}), {"imuTemp", []() { return imu->getTemp(); }}),
}; };
void *logBuffer; // buffer for log data void *logBuffer; // buffer for log data