Fork to pull magnetic calibration code

Dependencies:   N5110 ST_401_84MHZ mbed

Fork of MPU9150AHRS by Kris Winer

Files at this revision

API Documentation at this revision

Comitter:
whatnick
Date:
Tue Dec 08 02:14:33 2015 +0000
Parent:
0:39935bb3c1a1
Commit message:
Added Magnetometer basic calibration

Changed in this revision

MPU9150.h Show annotated file Show diff for this revision Revisions of this file
N5110.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 39935bb3c1a1 -r 1a4694de3548 MPU9150.h
--- a/MPU9150.h	Sun Jun 29 22:48:08 2014 +0000
+++ b/MPU9150.h	Tue Dec 08 02:14:33 2015 +0000
@@ -541,7 +541,44 @@
    dest2[2] = (float)accel_bias[2]/(float)accelsensitivity;
 }
 
-
+void magcalMPU9150(float * dest1) 
+{
+  uint16_t ii = 0, sample_count = 0;
+  int32_t mag_bias[3] = {0, 0, 0};
+  int16_t mag_max[3] = {0, 0, 0}, mag_min[3] = {0, 0, 0}, mag_temp[3] = {0, 0, 0};
+ 
+  //LOG("Mag Calibration: Wave device in a figure eight until done!");
+  wait(4);
+  
+   sample_count = 64;
+   for(ii = 0; ii < sample_count; ii++) {
+    readMagData(mag_temp);  // Read the mag data   
+    for (int jj = 0; jj < 3; jj++) {
+      if (ii == 0) {
+        mag_max[jj] = mag_temp[jj]; // Offsets may be large enough that mag_temp[i] may not be bipolar! 
+        mag_min[jj] = mag_temp[jj]; // This prevents max or min being pinned to 0 if the values are unipolar...
+      } else {
+        if(mag_temp[jj] > mag_max[jj]) mag_max[jj] = mag_temp[jj];
+        if(mag_temp[jj] < mag_min[jj]) mag_min[jj] = mag_temp[jj];
+      }
+    }
+    wait_ms(135);  // at 8 Hz ODR, new mag data is available every 125 ms
+   }
+ 
+//    Serial.println("mag x min/max:"); Serial.println(mag_max[0]); Serial.println(mag_min[0]);
+//    Serial.println("mag y min/max:"); Serial.println(mag_max[1]); Serial.println(mag_min[1]);
+//    Serial.println("mag z min/max:"); Serial.println(mag_max[2]); Serial.println(mag_min[2]);
+ 
+    mag_bias[0]  = (mag_max[0] + mag_min[0])/2;  // get average x mag bias in counts
+    mag_bias[1]  = (mag_max[1] + mag_min[1])/2;  // get average y mag bias in counts
+    mag_bias[2]  = (mag_max[2] + mag_min[2])/2;  // get average z mag bias in counts
+    
+    dest1[0] = (float) mag_bias[0]*mRes*magCalibration[0];  // save mag biases in G for main program
+    dest1[1] = (float) mag_bias[1]*mRes*magCalibration[1];   
+    dest1[2] = (float) mag_bias[2]*mRes*magCalibration[2];          
+ 
+   //LOG("Mag Calibration done!");
+}
 // Accelerometer and gyroscope self test; check calibration wrt factory settings
 void MPU9150SelfTest(float * destination) // Should return percent deviation from factory trim values, +/- 14 or less deviation is a pass
 {
diff -r 39935bb3c1a1 -r 1a4694de3548 N5110.lib
--- a/N5110.lib	Sun Jun 29 22:48:08 2014 +0000
+++ b/N5110.lib	Tue Dec 08 02:14:33 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/onehorse/code/Adfs/#28c629d0b0d0
+http://mbed.org/users/eencae/code/N5110/#ba8addc061ea
diff -r 39935bb3c1a1 -r 1a4694de3548 main.cpp
--- a/main.cpp	Sun Jun 29 22:48:08 2014 +0000
+++ b/main.cpp	Tue Dec 08 02:14:33 2015 +0000
@@ -104,6 +104,8 @@
     wait(1);
     MPU9150.initMPU9150(); 
     pc.printf("MPU9150 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
+    MPU9150.magcalMPU9150(magbias);
+    pc.printf("Mag cal done....\n\r"); // Initialize device for active mode read of magnetometer
     MPU9150.initAK8975A(magCalibration);
     pc.printf("AK8975 initialized for active data mode....\n\r"); // Initialize device for active mode read of magnetometer
    }
@@ -127,10 +129,11 @@
     mRes = 10.*1229./4096.; // Conversion from 1229 microTesla full scale (4096) to 12.29 Gauss full scale
     // So far, magnetometer bias is calculated and subtracted here manually, should construct an algorithm to do it automatically
     // like the gyro and accelerometer biases
+    /*
     magbias[0] = -5.;   // User environmental x-axis correction in milliGauss
     magbias[1] = -95.;  // User environmental y-axis correction in milliGauss
     magbias[2] = -260.; // User environmental z-axis correction in milliGauss
- 
+    */
 
  while(1) {
   
diff -r 39935bb3c1a1 -r 1a4694de3548 mbed.bld
--- a/mbed.bld	Sun Jun 29 22:48:08 2014 +0000
+++ b/mbed.bld	Tue Dec 08 02:14:33 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/0b3ab51c8877
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/165afa46840b
\ No newline at end of file