@@ -42,6 +42,11 @@ bool imuIsReady() {
42
42
}
43
43
44
44
void imuSetEnabled (bool enabled) {
45
+ // If going from false to true, reset the gyro
46
+ if (!_imuEnabled && enabled) {
47
+ gyroReset ();
48
+ }
49
+
45
50
_imuEnabled = enabled;
46
51
Serial.printf (" [IMU] %s\n " , enabled ? " Enabling" : " Disabling" );
47
52
}
@@ -161,6 +166,9 @@ void imuCalibrate(unsigned long calibrationTimeMs) {
161
166
_gyroOffsetsDPS[1 ] = gyroAvgValues[1 ] / numVals;
162
167
_gyroOffsetsDPS[2 ] = gyroAvgValues[2 ] / numVals;
163
168
169
+ // Remove 1G from the vertical axis (assumed to be Z)
170
+ _accelOffsetsG[2 ] -= 1.0 ;
171
+
164
172
Serial.printf (" [IMU] Gyro Offsets(dps): X(%f) Y(%f) Z(%f), Accel Offsets(g): X(%f) Y(%f) Z(%f)\n " ,
165
173
_gyroOffsetsDPS[0 ],
166
174
_gyroOffsetsDPS[1 ],
@@ -173,6 +181,9 @@ void imuCalibrate(unsigned long calibrationTimeMs) {
173
181
digitalWrite (LED_BUILTIN, LOW);
174
182
}
175
183
184
+ unsigned long _imuLoopTime = 0 ;
185
+ int _imuLoopCount = 0 ;
186
+
176
187
void imuPeriodic () {
177
188
// Initialize the filter if this is the first time we are running through the periodic
178
189
if (!_filterStarted) {
@@ -202,10 +213,20 @@ void imuPeriodic() {
202
213
_accelG[2 ] = _accelToG (accel.acceleration .z ) - _accelOffsetsG[2 ];
203
214
204
215
// 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 ]);
206
217
207
- // Increment the previous time so that we keep proper pace
218
+ // Increment the previous time so ßthat we keep proper pace
208
219
_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
+ }
209
230
}
210
231
}
211
232
@@ -379,6 +400,7 @@ void imuResetYaw() {
379
400
}
380
401
381
402
void gyroReset () {
403
+ Serial.println (" [IMU] Resetting Gyro" );
382
404
imuResetRoll ();
383
405
imuResetPitch ();
384
406
imuResetYaw ();
0 commit comments