Skip to content

Commit 81be14b

Browse files
authored
Merge pull request #67 from MatzElectronics/master
Improved magnetometer calibration
2 parents a9a4996 + 76ed785 commit 81be14b

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

Learn/Simple Libraries/Sensor/liblsm9ds1/Documentation lsm9ds1 Library.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1.39 KB
Binary file not shown.

Learn/Simple Libraries/Sensor/liblsm9ds1/imu_calibrateMag.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@
2020
int __pinM;
2121
//float __mRes;
2222
int __mBiasRaw[3] = {0,0,0};
23-
23+
unsigned char __settings_mag_scale;
2424

2525
void imu_calibrateMag()
2626
{
27-
int i, j, mx, my, mz;
27+
28+
int i = 0, j, mx, my, mz;
29+
char ck0 = 0, ck1 = 0, ck2 = 0, ck3 = 0, ck4 = 0, ck5 = 0, ck6 = 0, ck7 = 0, ck8 = 0;
2830
int magMin[3] = {0, 0, 0};
2931
int magMax[3] = {0, 0, 0}; // The road warrior
3032

31-
for (i=0; i<128; i++)
33+
float ax, ay, az;
34+
35+
while(i < 128 || ck0 == 0 || ck1 == 0 || ck2 == 0 || ck3 == 0 || ck4 == 0 || ck5 == 0 || ck6 == 0 || ck7 == 0 || ck8 == 0)
3236
{
3337
while (!imu_magAvailable(ALL_AXIS));
3438
imu_readMag(&mx, &my, &mz);
@@ -41,6 +45,20 @@ void imu_calibrateMag()
4145
if (magTemp[j] > magMax[j]) magMax[j] = magTemp[j];
4246
if (magTemp[j] < magMin[j]) magMin[j] = magTemp[j];
4347
}
48+
49+
if(abs(magMax[0] - magMin[0]) > (12000 / ((int) __settings_mag_scale))) ck6 = 1;
50+
if(abs(magMax[1] - magMin[1]) > (12000 / ((int) __settings_mag_scale))) ck7 = 1;
51+
if(abs(magMax[2] - magMin[2]) > (12000 / ((int) __settings_mag_scale))) ck8 = 1;
52+
53+
imu_readAccelCalculated(&ax, &ay, &az);
54+
if(ax > 0.85 && ay < 0.15 && ay > -0.15 && az < 0.15 && az > -0.15) ck0 = 1;
55+
if(ax < -0.85 && ay < 0.15 && ay > -0.15 && az < 0.15 && az > -0.15) ck1 = 1;
56+
if(ay > 0.85 && ax < 0.15 && ax > -0.15 && az < 0.15 && az > -0.15) ck2 = 1;
57+
if(ay < -0.85 && ax < 0.15 && ax > -0.15 && az < 0.15 && az > -0.15) ck3 = 1;
58+
if(az > 0.85 && ay < 0.15 && ay > -0.15 && ax < 0.15 && ax > -0.15) ck4 = 1;
59+
if(az < -0.85 && ay < 0.15 && ay > -0.15 && ax < 0.15 && ax > -0.15) ck5 = 1;
60+
61+
i++;
4462
}
4563

4664
for (j = 0; j < 3; j++)
@@ -55,7 +73,7 @@ void imu_calibrateMag()
5573
lsb = __mBiasRaw[k] & 0x00FF;
5674
imu_SPIwriteByte(__pinM, OFFSET_X_REG_L_M + (2 * k), lsb);
5775
imu_SPIwriteByte(__pinM, OFFSET_X_REG_H_M + (2 * k), msb);
58-
}
76+
}
5977
}
6078

6179

7.9 KB
Binary file not shown.

Learn/Simple Libraries/Sensor/liblsm9ds1/lsm9ds1.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,12 @@ void imu_calibrateAG();
165165
* @brief Calibrates the Magnetometer on the LSM9DS1 IMU module.
166166
*
167167
* @details This is a function accumulates samples from the magnetometer, averages
168-
* them, and then passes the biases back to the LSM9DS1
169-
* for subtraction from all subsequent data. Results in a more accurate measurement in general and can
170-
* remove errors due to imprecise or varying initial placement.
168+
* them, and then passes the biases back to the LSM9DS1 for subtraction from
169+
* all subsequent data. Results in a more accurate measurement in general and can
170+
* remove errors due to imprecise or varying initial placement. The calibration
171+
* function uses the accelerometer to verify that each of the 3 axes has been pointed
172+
* straight up and straight down slowly enough to get good magnetometer readings in
173+
* each dimension.
171174
*
172175
* @note Mag Calibration values ARE stored in the LSM9DS1 and variables that can be retireved and set using other
173176
* functions in this library.

0 commit comments

Comments
 (0)