Skip to content

Commit 3f5c0e1

Browse files
author
Zhiquan Yeo
committed
Fix updateIMU bug
1 parent 4cd8d55 commit 3f5c0e1

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/imu.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ bool imuIsReady() {
4242
}
4343

4444
void imuSetEnabled(bool enabled) {
45+
// If going from false to true, reset the gyro
46+
if (!_imuEnabled && enabled) {
47+
gyroReset();
48+
}
49+
4550
_imuEnabled = enabled;
4651
Serial.printf("[IMU] %s\n", enabled ? "Enabling" : "Disabling");
4752
}
@@ -161,6 +166,9 @@ void imuCalibrate(unsigned long calibrationTimeMs) {
161166
_gyroOffsetsDPS[1] = gyroAvgValues[1] / numVals;
162167
_gyroOffsetsDPS[2] = gyroAvgValues[2] / numVals;
163168

169+
// Remove 1G from the vertical axis (assumed to be Z)
170+
_accelOffsetsG[2] -= 1.0;
171+
164172
Serial.printf("[IMU] Gyro Offsets(dps): X(%f) Y(%f) Z(%f), Accel Offsets(g): X(%f) Y(%f) Z(%f)\n",
165173
_gyroOffsetsDPS[0],
166174
_gyroOffsetsDPS[1],
@@ -173,6 +181,9 @@ void imuCalibrate(unsigned long calibrationTimeMs) {
173181
digitalWrite(LED_BUILTIN, LOW);
174182
}
175183

184+
unsigned long _imuLoopTime = 0;
185+
int _imuLoopCount = 0;
186+
176187
void imuPeriodic() {
177188
// Initialize the filter if this is the first time we are running through the periodic
178189
if (!_filterStarted) {
@@ -202,10 +213,20 @@ void imuPeriodic() {
202213
_accelG[2] = _accelToG(accel.acceleration.z) - _accelOffsetsG[2];
203214

204215
// Update the filter, which will compute orientation
205-
_ahrsFilter.updateIMU(_accelG[0], _accelG[1], _accelG[2], _gyroRatesDPS[0], _gyroRatesDPS[1], _gyroRatesDPS[2]);
216+
_ahrsFilter.updateIMU(_gyroRatesDPS[0], _gyroRatesDPS[1], _gyroRatesDPS[2], _accelG[0], _accelG[1], _accelG[2]);
206217

207-
// Increment the previous time so that we keep proper pace
218+
// Increment the previous time so ßthat we keep proper pace
208219
_microsPrevious = _microsPrevious + _microsPerReading;
220+
221+
// Stats
222+
_imuLoopTime += micros() - microsNow;
223+
_imuLoopCount++;
224+
225+
if (_imuLoopCount > 100) {
226+
Serial.printf("[IMU] Avg AHRS Update Time: %u us\n", _imuLoopTime / _imuLoopCount);
227+
_imuLoopCount = 0;
228+
_imuLoopTime = 0;
229+
}
209230
}
210231
}
211232

@@ -379,6 +400,7 @@ void imuResetYaw() {
379400
}
380401

381402
void gyroReset() {
403+
Serial.println("[IMU] Resetting Gyro");
382404
imuResetRoll();
383405
imuResetPitch();
384406
imuResetYaw();

0 commit comments

Comments
 (0)