Chris LU / LSM9DS0_self_riding_bike

Fork of LSM9DS0 by Chris LU

Files at this revision

API Documentation at this revision

Comitter:
YCTung
Date:
Sat Feb 27 09:03:06 2016 +0000
Parent:
0:0dbf7ee73651
Child:
2:48eb33afd0fa
Commit message:
Added some functions which can return measured data directly

Changed in this revision

LSM9DS0.cpp Show annotated file Show diff for this revision Revisions of this file
LSM9DS0.h Show annotated file Show diff for this revision Revisions of this file
--- a/LSM9DS0.cpp	Fri Feb 26 12:56:04 2016 +0000
+++ b/LSM9DS0.cpp	Sat Feb 27 09:03:06 2016 +0000
@@ -93,6 +93,10 @@
     setMagODR(mODR); // Set the magnetometer output data rate.
     setMagScale(mScale); // Set the magnetometer's range.
     
+    setGyroOffset(0,0,0);
+    setAccelOffset(0,0,0);
+    setMagOffset(0,0,0);
+    
     // Once everything is initialized, return the WHO_AM_I registers we read:
     return (xmTest << 8) | gTest;
 }
@@ -328,6 +332,70 @@
   xmWriteByte(FIFO_CTRL_REG, 0x00);       // Enable accelerometer bypass mode
 }
 
+//**********************
+//  Gyro section
+//**********************
+void LSM9DS0::readGyro()
+{
+    uint8_t temp[6]; // We'll read six bytes from the gyro into temp
+    gReadBytes(OUT_X_L_G, temp, 6); // Read 6 bytes, beginning at OUT_X_L_G
+    gx = (temp[1] << 8) | temp[0]; // Store x-axis values into gx
+    gy = (temp[3] << 8) | temp[2]; // Store y-axis values into gy
+    gz = (temp[5] << 8) | temp[4]; // Store z-axis values into gz
+}
+
+void LSM9DS0::setGyroOffset(int16_t _gx, int16_t _gy, int16_t _gz)
+{
+    gyroOffset[0] = _gx;
+    gyroOffset[1] = _gy;
+    gyroOffset[2] = _gz;
+}
+
+int16_t LSM9DS0::readRawGyroX( void )
+{
+    uint8_t temp[2];
+    gReadBytes(OUT_X_L_G, temp, 2);
+    gx = (temp[1] << 8) | temp[0];
+    return gx;
+}
+
+int16_t LSM9DS0::readRawGyroY( void )
+{
+    uint8_t temp[2];
+    gReadBytes(OUT_Y_L_G, temp, 2);
+    gy = (temp[1] << 8) | temp[0];
+    return gy;
+}
+
+int16_t LSM9DS0::readRawGyroZ( void )
+{
+    uint8_t temp[2];
+    gReadBytes(OUT_Z_L_G, temp, 2);
+    gz = (temp[1] << 8) | temp[0];
+    return gz;
+}
+
+float LSM9DS0::readFloatGyroX( void )
+{
+    float output = calcGyro(readRawGyroX() - gyroOffset[0]);
+    return output;
+}
+
+float LSM9DS0::readFloatGyroY( void )
+{
+    float output = calcGyro(readRawGyroY() - gyroOffset[1]);
+    return output;
+}
+
+float LSM9DS0::readFloatGyroZ( void )
+{
+    float output = calcGyro(readRawGyroZ() - gyroOffset[2]);
+    return output;
+}
+
+//**********************
+//  Accel section
+//**********************
 void LSM9DS0::readAccel()
 {
     uint8_t temp[6]; // We'll read six bytes from the accelerometer into temp   
@@ -337,6 +405,58 @@
     az = (temp[5] << 8) | temp[4]; // Store z-axis values into az
 }
 
+void LSM9DS0::setAccelOffset(int16_t _ax, int16_t _ay, int16_t _az)
+{
+    accelOffset[0] = _ax;
+    accelOffset[1] = _ay;
+    accelOffset[2] = _az;
+}
+
+int16_t LSM9DS0::readRawAccelX( void )
+{
+    uint8_t temp[2];
+    xmReadBytes(OUT_X_L_A, temp, 2);
+    ax = (temp[1] << 8) | temp[0];
+    return ax;
+}
+
+int16_t LSM9DS0::readRawAccelY( void )
+{
+    uint8_t temp[2];
+    xmReadBytes(OUT_Y_L_A, temp, 2);
+    ay = (temp[1] << 8) | temp[0];
+    return ay;
+}
+
+int16_t LSM9DS0::readRawAccelZ( void )
+{
+    uint8_t temp[2];
+    xmReadBytes(OUT_Z_L_A, temp, 2);
+    az = (temp[1] << 8) | temp[0];
+    return az;
+}
+
+float LSM9DS0::readFloatAccelX( void )
+{
+    float output = calcAccel(readRawAccelX() - accelOffset[0]);
+    return output;
+}
+
+float LSM9DS0::readFloatAccelY( void )
+{
+    float output = calcAccel(readRawAccelY() - accelOffset[1]);
+    return output;
+}
+
+float LSM9DS0::readFloatAccelZ( void )
+{
+    float output = calcAccel(readRawAccelZ() - accelOffset[2]);
+    return output;
+}
+
+//**********************
+//  Mag section
+//**********************
 void LSM9DS0::readMag()
 {
     uint8_t temp[6]; // We'll read six bytes from the mag into temp 
@@ -346,6 +466,58 @@
     mz = (temp[5] << 8) | temp[4]; // Store z-axis values into mz
 }
 
+void LSM9DS0::setMagOffset(int16_t _mx, int16_t _my, int16_t _mz)
+{
+    magOffset[0] = _mx;
+    magOffset[1] = _my;
+    magOffset[2] = _mz;
+}
+
+int16_t LSM9DS0::readRawMagX( void )
+{
+    uint8_t temp[2];
+    xmReadBytes(OUT_X_L_M, temp, 2);
+    mx = (temp[1] << 8) | temp[0];
+    return mx;
+}
+
+int16_t LSM9DS0::readRawMagY( void )
+{
+    uint8_t temp[2];
+    xmReadBytes(OUT_Y_L_M, temp, 2);
+    my = (temp[1] << 8) | temp[0];
+    return my;
+}
+
+int16_t LSM9DS0::readRawMagZ( void )
+{
+    uint8_t temp[2];
+    xmReadBytes(OUT_Z_L_M, temp, 2);
+    mz = (temp[1] << 8) | temp[0];
+    return mz;
+}
+
+float LSM9DS0::readFloatMagX( void )
+{
+    float output = calcMag(readRawMagX() - magOffset[0]);
+    return output;
+}
+
+float LSM9DS0::readFloatMagY( void )
+{
+    float output = calcMag(readRawMagY() - magOffset[1]);
+    return output;
+}
+
+float LSM9DS0::readFloatMagZ( void )
+{
+    float output = calcMag(readRawMagZ() - magOffset[2]);
+    return output;
+}
+
+//**********************
+//  Temp section
+//**********************
 void LSM9DS0::readTemp()
 {
     uint8_t temp[2]; // We'll read two bytes from the temperature sensor into temp  
@@ -353,15 +525,6 @@
     temperature = (((int16_t) temp[1] << 12) | temp[0] << 4 ) >> 4; // Temperature is a 12-bit signed integer
 }
 
-void LSM9DS0::readGyro()
-{
-    uint8_t temp[6]; // We'll read six bytes from the gyro into temp
-    gReadBytes(OUT_X_L_G, temp, 6); // Read 6 bytes, beginning at OUT_X_L_G
-    gx = (temp[1] << 8) | temp[0]; // Store x-axis values into gx
-    gy = (temp[3] << 8) | temp[2]; // Store y-axis values into gy
-    gz = (temp[5] << 8) | temp[4]; // Store z-axis values into gz
-}
-
 float LSM9DS0::calcGyro(int16_t gyro)
 {
     // Return the gyro raw reading times our pre-calculated DPS / (ADC tick):
--- a/LSM9DS0.h	Fri Feb 26 12:56:04 2016 +0000
+++ b/LSM9DS0.h	Sat Feb 27 09:03:06 2016 +0000
@@ -213,6 +213,13 @@
     int16_t temperature;
     float abias[3];
     float gbias[3];
+    
+    int16_t gyroOffset[3];
+    int16_t accelOffset[3];
+    int16_t magOffset[3];
+    void setGyroOffset(int16_t, int16_t, int16_t);
+    void setAccelOffset(int16_t, int16_t, int16_t);
+    void setMagOffset(int16_t, int16_t, int16_t);
 
     // LSM9DS0 -- LSM9DS0 class constructor
     // The constructor will set up a handful of private variables, and set the
@@ -253,18 +260,36 @@
     // The readings are stored in the class' gx, gy, and gz variables. Read
     // those _after_ calling readGyro().
     void readGyro();
+    int16_t readRawGyroX( void );
+    int16_t readRawGyroY( void );
+    int16_t readRawGyroZ( void );
+    float readFloatGyroX( void );
+    float readFloatGyroY( void );
+    float readFloatGyroZ( void );
     
     // readAccel() -- Read the accelerometer output registers.
     // This function will read all six accelerometer output registers.
     // The readings are stored in the class' ax, ay, and az variables. Read
     // those _after_ calling readAccel().
     void readAccel();
+    int16_t readRawAccelX( void );
+    int16_t readRawAccelY( void );
+    int16_t readRawAccelZ( void );
+    float readFloatAccelX( void );
+    float readFloatAccelY( void );
+    float readFloatAccelZ( void );
     
     // readMag() -- Read the magnetometer output registers.
     // This function will read all six magnetometer output registers.
     // The readings are stored in the class' mx, my, and mz variables. Read
     // those _after_ calling readMag().
     void readMag();
+    int16_t readRawMagX( void );
+    int16_t readRawMagY( void );
+    int16_t readRawMagZ( void );
+    float readFloatMagX( void );
+    float readFloatMagY( void );
+    float readFloatMagZ( void );
 
     // readTemp() -- Read the temperature output register.
     // This function will read two temperature output registers.
@@ -329,13 +354,11 @@
     //      Must be a value from the accel_odr enum (check above, there're 11).
     void setAccelODR(accel_odr aRate);  
 
-        // setAccelABW() -- Set the anti-aliasing filter rate of the accelerometer
+    // setAccelABW() -- Set the anti-aliasing filter rate of the accelerometer
     // Input:
     //  - abwRate = The desired anti-aliasing filter rate of the accel.
     //      Must be a value from the accel_abw enum (check above, there're 4).
     void setAccelABW(accel_abw abwRate);
-
-
     
     // setMagODR() -- Set the output data rate of the magnetometer
     // Input: