Interface library for the Atmel Inertial One IMU. Contains drivers for the ITG 3200 3 axis gyro, BMA-150 3 axis accelerometer, and AK8975 3 axis compass

Files at this revision

API Documentation at this revision

Comitter:
Ductapemaster
Date:
Thu Feb 16 08:56:11 2012 +0000
Parent:
12:cab3f7305522
Commit message:
Cleaned up and consolidated gyro methods, implemented accelerometer methods, implemented struct for storing 3d data values now getter methods return that struct.

Changed in this revision

IMU.cpp Show annotated file Show diff for this revision Revisions of this file
IMU.h Show annotated file Show diff for this revision Revisions of this file
--- a/IMU.cpp	Thu Feb 02 08:50:58 2012 +0000
+++ b/IMU.cpp	Thu Feb 16 08:56:11 2012 +0000
@@ -25,7 +25,12 @@
 
  #include "IMU.h"
  
- IMU::IMU(PinName sda, PinName scl) : _i2c(sda, scl) {
+ /**************
+ * Constructor *
+ **************/
+ 
+ IMU::IMU(PinName sda, PinName scl) : _i2c(sda, scl)
+ {
  
     _i2c.frequency(400000);     //400kHz bus speed
     
@@ -39,46 +44,12 @@
     
  }
  
- int IMU::gyroX(void) {
- 
-    char poke = GYRO_XOUT_H_REG;
-    char peek[2];
- 
-    _i2c.write(GYRO_ADR, &poke, 1, false);
-    _i2c.read(GYRO_ADR, peek, 2, false);
-    
-    int16_t result = ( peek[0] << 8 | peek[1] );
-    return result;
+ /***************
+ * Gyro Methods *
+ ***************/
     
- }
-   
- int IMU::gyroY(void) {
- 
-    char poke = GYRO_YOUT_H_REG;
-    char peek[2];
- 
-    _i2c.write(GYRO_ADR, &poke, 1, false);
-    _i2c.read(GYRO_ADR, peek, 2, false);
-    
-    int16_t result = ( peek[0] << 8 | peek[1] );
-    return result;
-    
- }
-       
- int IMU::gyroZ(void){
- 
-    char poke = GYRO_ZOUT_H_REG;
-    char peek[2];
- 
-    _i2c.write(GYRO_ADR, &poke, 1, false);
-    _i2c.read(GYRO_ADR, peek, 2, false);
-    
-    int16_t result = ( peek[0] << 8 | peek[1] );
-    return result;
-
- }
-    
- int* IMU::gyroXYZ(void) {
+ IMU::data3d IMU::getGyroData(void) 
+ {
  
     char poke = GYRO_XOUT_H_REG;
     char peek[6];
@@ -86,20 +57,154 @@
     _i2c.write(GYRO_ADR, &poke, 1, false);
     _i2c.read(GYRO_ADR, peek, 6, false);
     
-    int result[] = { 
-        ( peek[0] << 8 | peek[1] ),     // X
-        ( peek[2] << 8 | peek[3] ),     // Y
-        ( peek[4] << 8 | peek[5] )      // Z
-        };
-    return result;
+    int16_t result[3] = 
+    {
+        
+        ( ( peek[0] << 8 ) | peek[1] ),   // X
+        ( ( peek[2] << 8 ) | peek[3] ),   // Y
+        ( ( peek[4] << 8 ) | peek[5] )    // Z
+        
+    };
+    
+    gyro_data.x = int(result[0]);
+    gyro_data.y = int(result[1]);
+    gyro_data.z = int(result[2]);
+       
+    return gyro_data;
     
  }
 
- void IMU::gyroSetLPF(char _BW) {
+ void IMU::setGyroLPF(char bw) 
+ {
  
-    char poke[2] = { GYRO_DLPF_REG, _BW };
+    char poke[2] = { 
+    
+        GYRO_DLPF_REG, 
+        ( bw | ( FS_SEL_INIT << 3 ) )      //Bandwidth value with FS_SEL bits set properly
+        
+        };
     
     _i2c.write(GYRO_ADR, poke, 2, false);
     
  }
+ 
+ /************************
+ * Accelerometer Methods *
+ ************************/
+ 
+ int IMU::accX(void) 
+ {
+ 
+    char poke = ACC_XOUT_L_REG;
+    char peek[2];
+    
+    _i2c.write(ACC_ADR, &poke, 1, false);
+    _i2c.read(ACC_ADR, peek, 2, false);
+    
+    //Turning a 10 bit signed number into a signed 16 bit int
+    return ( ( (0x80 & peek[1]) ? 0xFFFFFE00 : 0x0 ) | ( ( peek[1] & 0x7F ) << 2 ) | ( peek[0] >> 6 ) );
+
+ }
+
+ int IMU::accY(void) 
+ {
+
+    char poke = ACC_YOUT_L_REG;
+    char peek[2];
+    
+    _i2c.write(ACC_ADR, &poke, 1, false);
+    _i2c.read(ACC_ADR, peek, 2, false);
+    
+    //Turning a 10 bit signed number into a signed 16 bit int
+    return ( ( (0x80 & peek[1]) ? 0xFFFFFE00 : 0x0 ) | ( ( peek[1] & 0x7F ) << 2 ) | ( peek[0] >> 6 ) );
+
+ }
+
+ int IMU::accZ(void) 
+ {
+
+    char poke = ACC_ZOUT_L_REG;
+    char peek[2];
+    
+    _i2c.write(ACC_ADR, &poke, 1, false);
+    _i2c.read(ACC_ADR, peek, 2, false);
+    
+    //Turning a 10 bit signed number into a signed 16 bit int
+    return ( ( (0x80 & peek[1]) ? 0xFFFFFE00 : 0x0 ) | ( ( peek[1] & 0x7F ) << 2 ) | ( peek[0] >> 6 ) );
+
+ }
+ 
+  IMU::data3d IMU::getAccData(void) 
+ {
+ 
+    char poke = ACC_XOUT_L_REG;
+    char peek[6];
+    
+    _i2c.write(ACC_ADR, &poke, 1, false);
+    _i2c.read(ACC_ADR, peek, 6, false);
+    
+    //Turning a 10 bit signed number into a signed 16 bit int    
+    
+    acc_data.x = ( ( (0x80 & peek[1]) ? 0xFFFFFE00 : 0x0 ) | ( ( peek[1] & 0x7F ) << 2 ) | ( peek[0] >> 6 ) );
+    acc_data.y = ( ( (0x80 & peek[3]) ? 0xFFFFFE00 : 0x0 ) | ( ( peek[3] & 0x7F ) << 2 ) | ( peek[2] >> 6 ) );
+    acc_data.z = ( ( (0x80 & peek[5]) ? 0xFFFFFE00 : 0x0 ) | ( ( peek[5] & 0x7F ) << 2 ) | ( peek[4] >> 6 ) );
+          
+    return acc_data;
+    
+ }
+
+ void IMU::setAccLPF(char bw) 
+ {
+
+    char poke[2] = { 
+        
+        ACC_OPER_REG,
+        0x00
+        
+        };
+        
+    char peek;
+    
+    _i2c.write(ACC_ADR, poke, 1, false);
+    _i2c.read(ACC_ADR, &peek, 1, false);       //Get current register value
+    
+    poke[1] = ( peek | bw );                  //Insert bandwidth bits at [2:0]
+    _i2c.write(ACC_ADR, poke, 2, false);       //Write register
+
+ }
+
+ void IMU::setAccRange(char range) 
+ {
+
+    char poke[2] = { 
+        
+        ACC_OPER_REG,
+        0x00
+        
+        };
+        
+    char peek;
+    
+    _i2c.write(ACC_ADR, poke, 1, false);
+    _i2c.read(ACC_ADR, &peek, 1, false);       //Get current register value
+    
+    poke[1] = ( peek | range << 3 );          //Insert bandwidth bits at [4:3]
+    _i2c.write(ACC_ADR, poke, 2, false);       //Write register
+
+ }
+
+ void IMU::accUpdateImage(void) 
+ {
+
+ }
+
+ void IMU::accEEWriteEn(bool we) 
+ {
+
+ }
+ 
+ /***********************
+ * Magnetometer Methods *
+ ***********************/
+ 
  
\ No newline at end of file
--- a/IMU.h	Thu Feb 02 08:50:58 2012 +0000
+++ b/IMU.h	Thu Feb 16 08:56:11 2012 +0000
@@ -61,12 +61,37 @@
  
  // Gyro Initial FS_SEL Register Config
  
- #define FS_SEL_INIT   0x03 << 3
+ #define FS_SEL_INIT    0x03 << 3
   
  // BMA150 Accelerometer Registers
  
+ #define ACC_XOUT_L_REG            0x02     //Acceleration data registers
+ #define ACC_XOUT_H_REG            0x03
+ #define ACC_YOUT_L_REG            0x04
+ #define ACC_YOUT_H_REG            0x05
+ #define ACC_ZOUT_L_REG            0x06
+ #define ACC_ZOUT_H_REG            0x07
+ #define ACC_TEMP_REG              0x08     //Temperature register, 0.5 deg. C / LSB
+ #define ACC_CTRL_REG              0x0A     //Control registers
+ #define ACC_OPER_REG              0x14     //Operational registers
+ #define ACC_INT_REG               0x15     //Interrupt registers
  
+ // Accelerometer LPF bandwidths
  
+ #define BW_25HZ        0x00
+ #define BW_50HZ        0x01
+ #define BW_100HZ       0x02
+ #define BW_190HZ       0x03
+ #define BW_375HZ       0x04
+ #define BW_750HZ       0x05
+ #define BW_1500HZ      0x06
+ 
+ // Accelerometer Range values
+ 
+ #define RANGE_2G       0x00
+ #define RANGE_4G       0x01
+ #define RANGE_8G       0x02
+   
  // AK8973 Compass Registers
 
  
@@ -74,7 +99,7 @@
  * 
  * Includes control routines for:
  * - ITG-3200 3-axis, 16 bit gyroscope
- * - BMA-150 3-axis, 16 bit accelerometer
+ * - BMA-150 3-axis, 10 bit accelerometer
  * - AK8975 3-axis, 16 bit magnetometer
  *
  * Datasheets:
@@ -91,6 +116,12 @@
  class IMU {
  
  public:
+    struct data3d
+    {
+        int x, y, z;
+    } gyro_data, acc_data, mag_data;
+ 
+ public:    
  
     /** Creates IMU object and initializes all three chips
     *
@@ -107,39 +138,66 @@
     
     /* Gyro Methods */
     
-    /** Gets current X axis gyro measurement
-    *
-    * @return Raw X axis ADC measurement (signed 16 bits)
-    */
-    int gyroX(void);
-    
-    /** Gets current Y axis gyro measurement
-    * 
-    * @return Raw Y axis ADC measurement (signed 16 bits)
-    */
-    int gyroY(void);
-    
-    /** Gets current Z axis gyro measurement
-    * 
-    * @return Raw Z axis ADC measurement (signed 16 bits)
-    */
-    int gyroZ(void);
-    
     /** Gets current ADC data from all axes of gyro (uses burst read mode)
     * 
-    * @return Array of X, Y, and Z axis gyro measurements (signed 16 bits)
+    * @return data3d struct of X, Y, and Z axis gyro measurements (signed 16 bits)
     */
-    int* gyroXYZ(void);
+    data3d getGyroData(void);
     
-    /** Sets digital LPF bandwidth for all gyro channels
+    /** Sets digital LPF bandwidth for all gyro channels - Not working currently
     * 
-    * @param _BW Filter Bandwidth (use defined bandwidths)
+    * @param bw Filter Bandwidth (use defined bandwidths)
     */
-    void gyroSetLPF(char _BW);
+    void setGyroLPF(char bw);
     
     /* Accelerometer Methods */
     
+    /** Gets current X acceleration
+    *
+    * @return Raw X acceleration, 10 bits signed
+    */
+    int accX(void);
+
+    /** Gets current Y acceleration
+    *
+    * @return Raw Y acceleration, 10 bits signed
+    */
+    int accY(void);
+
+    /** Gets current Z acceleration
+    *
+    * @return Raw Z acceleration, 10 bits signed
+    */
+    int accZ(void);
     
+    /** Gets current acceleration on all axes
+    *
+    * @return data3d struct of acceleration data, signed ints
+    */
+    data3d getAccData(void);
+    
+    /** Sets digital LPF filter bandwidth
+    *
+    * @param bw Digital LPF filter bandwidth
+    */
+    void setAccLPF(char bw);
+
+    /** Sets accelerometer measurement range (+/-2, 4, or 8g's)
+    *
+    * @param range Range of g measurements
+    */    
+    void setAccRange(char range);
+    
+    /** Updates all image registers with data stored in EEPROM
+    *
+    */    
+    void accUpdateImage(void);
+
+    /** Set EEPROM write enable
+    *
+    * @param we Write enable value
+    */    
+    void accEEWriteEn(bool we);
     
     /* Compass Methods */