Skip to content

Commit 497b9ad

Browse files
committed
Add ability for asynchronous IMU calibration
Update calibration example with demo code
1 parent 8c1750c commit 497b9ad

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

examples/Example3_Calibration/Example3_Calibration.ino

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,23 @@ void setup()
4949
// it will take 255 samples and wait until done; each sample takes about
5050
// 2.4ms, so about 612ms total
5151
myOtos.calibrateImu();
52-
// myOtos.calibrateImu(255, true); // Or specify samples and whether to wait
52+
53+
// Alternatively, you can specify the number of samples and whether to wait
54+
// until it's done. If you don't want to wait, you can asynchronously check
55+
// how many samples remain with the code below. Once zero samples remain,
56+
// the calibration is done!
57+
// myOtos.calibrateImu(255, false);
58+
// bool done = false;
59+
// while(done == false)
60+
// {
61+
// // Check how many samples remain
62+
// uint8_t samplesRemaining;
63+
// myOtos.getImuCalibrationProgress(samplesRemaining);
64+
65+
// // If 0 samples remain, the calibration is done
66+
// if(samplesRemaining == 0)
67+
// done = true;
68+
// }
5369

5470
// Here we can set the linear and angular scalars, which can compensate for
5571
// scaling issues with the sensor measurements. Note that as of firmware

src/sfeQwiicOtos.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ sfeTkError_t sfeQwiicOtos::calibrateImu(uint8_t numSamples, bool waitUntilDone)
110110
if(err != kSTkErrOk)
111111
return err;
112112

113+
// Wait 1 sample period (2.4ms) to ensure the register updates
114+
delayMs(3);
115+
113116
// Do we need to wait until the calibration finishes?
114117
if(!waitUntilDone)
115118
return kSTkErrOk;
@@ -119,11 +122,6 @@ sfeTkError_t sfeQwiicOtos::calibrateImu(uint8_t numSamples, bool waitUntilDone)
119122
// of read attempts
120123
for(uint8_t numAttempts = numSamples; numAttempts > 0; numAttempts--)
121124
{
122-
// Give a short delay between reads. As of firmware v1.0, samples take
123-
// 2.4ms each, so 3ms should guarantee the next sample is done. This
124-
// also ensures the max attempts is not exceeded in normal operation
125-
delayMs(3);
126-
127125
// Read the gryo calibration register value
128126
uint8_t calibrationValue;
129127
err = _commBus->readRegisterByte(kRegImuCalib, calibrationValue);
@@ -133,12 +131,23 @@ sfeTkError_t sfeQwiicOtos::calibrateImu(uint8_t numSamples, bool waitUntilDone)
133131
// Check if calibration is done
134132
if(calibrationValue == 0)
135133
return kSTkErrOk;
134+
135+
// Give a short delay between reads. As of firmware v1.0, samples take
136+
// 2.4ms each, so 3ms should guarantee the next sample is done. This
137+
// also ensures the max attempts is not exceeded in normal operation
138+
delayMs(3);
136139
}
137140

138141
// Max number of attempts reached, calibration failed
139142
return kSTkErrFail;
140143
}
141144

145+
sfeTkError_t sfeQwiicOtos::getImuCalibrationProgress(uint8_t &numSamples)
146+
{
147+
// Read the IMU calibration register
148+
return _commBus->readRegisterByte(kRegImuCalib, numSamples);
149+
}
150+
142151
sfe_otos_linear_unit_t sfeQwiicOtos::getLinearUnit()
143152
{
144153
return _linearUnit;

src/sfeQwiicOtos.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ class sfeQwiicOtos
102102

103103
sfeTkError_t calibrateImu(uint8_t numSamples = 255, bool waitUntilDone = true);
104104

105+
sfeTkError_t getImuCalibrationProgress(uint8_t &numSamples);
106+
105107
sfe_otos_linear_unit_t getLinearUnit();
106108

107109
void setLinearUnit(sfe_otos_linear_unit_t unit);

0 commit comments

Comments
 (0)