MAG3110 Virgo robot adaptation

Fork of MAG3110 by JP PANG

Revision:
12:2680e94139bb
Parent:
11:31b140f32906
Child:
14:73a4a09a49af
diff -r 31b140f32906 -r 2680e94139bb MAG3110.cpp
--- a/MAG3110.cpp	Thu Aug 18 07:44:56 2016 +0000
+++ b/MAG3110.cpp	Fri Aug 19 01:14:36 2016 +0000
@@ -8,7 +8,7 @@
     
 }
 
-void MAG3110::begin()
+void MAG3110::begin(int16_t off_x, int16_t off_y, int16_t off_z)
 {
     char cmd[2];
     
@@ -21,12 +21,22 @@
     _i2c.write(MAG_ADDR, cmd, 2);
 
     cmd[0] = MAG_CTRL_REG1;
-    cmd[1] = 0x09;                              //Set ODR=40Hz and OSR=32 and set device to active mode.
+    cmd[1] = 0x01;                              //Set ODR=80Hz and OSR=16 and set device to active mode.
     _i2c.write(MAG_ADDR, cmd, 2);
     
-    // No adjustment initially
-    _avgX = 0;
-    _avgY = 0;
+    char data_bytes[7];
+    data_bytes[0]=MAG_OFF_X_MSB;                //Write offset values to X,Y,Z registers
+    
+    data_bytes[1] = (off_x & 0xFF00) >> 8;
+    data_bytes[2] = (off_x & 0xFF);
+    
+    data_bytes[3] = (off_y & 0xFF00) >> 8;
+    data_bytes[4] = (off_y & 0xFF);
+    
+    data_bytes[5] = (off_z & 0xFF00) >> 8;
+    data_bytes[6] = (off_z & 0xFF);
+   
+    _i2c.write(MAG_ADDR,data_bytes,7);
 }
 
 // Read a single byte form 8 bit register, return as int
@@ -75,16 +85,28 @@
 
 void MAG3110::get_uT(float * uT)
 {
+   
    char data_bytes[7];
    char d[1];
    d[0]=MAG_DR_STATUS;
    
    _i2c.write(MAG_ADDR,d,1,true);  
    _i2c.read(MAG_ADDR,data_bytes,7);// Read the 6 data bytes - LSB and MSB for X, Y and Z Axes.
- 
+    
    uT[0] =  (float)( (int16_t)data_bytes[1] << 8 | (int16_t)data_bytes[2] ) * 0.1;
    uT[1] =  (float)( (int16_t)data_bytes[3] << 8 | (int16_t)data_bytes[4] ) * 0.1;
    uT[2] =  (float)( (int16_t)data_bytes[5] << 8 | (int16_t)data_bytes[6] ) * 0.1;
+   
+   /*
+   int xVal, yVal, zVal;
+   
+   getValues(&xVal,&yVal, &zVal);
+   
+   uT[0] =  (float)( xVal ) * 0.1;
+   uT[1] =  (float)( yVal ) * 0.1;
+   uT[2] =  (float)( zVal ) * 0.1;
+   */
+   
 }