Tyler Weaver / ITG3200

Dependents:   9Dof_unit_testing

Fork of ITG3200 by James Watanabe

Revision:
11:9a354f34d8e3
Parent:
9:05396b551a9a
Child:
12:d624e9c6dae7
--- a/ITG3200.cpp	Tue Oct 30 17:19:00 2012 +0000
+++ b/ITG3200.cpp	Wed Oct 31 15:42:01 2012 +0000
@@ -1,6 +1,6 @@
 /**
  * @file ITG3200.cpp
- * @author Aaron Berk
+ * @author Tyler Weaver
  *
  * @section LICENSE
  *
@@ -26,6 +26,7 @@
  *
  * @section DESCRIPTION
  *
+ * Modified version of Aaron Berk's library.
  * ITG-3200 triple axis, digital interface, gyroscope.
  *
  * Datasheet:
@@ -36,87 +37,94 @@
 #include "ITG3200.h"
 #include <new>
 
-ITG3200::ITG3200(PinName sda, PinName scl, bool fastmode) : calibSamples(0), i2c_(*reinterpret_cast<I2C*>(i2cRaw)){
+ITG3200::ITG3200(PinName sda, PinName scl, bool fastmode) : calibSamples(0), i2c_(*reinterpret_cast<I2C*>(i2cRaw))
+{
     // Placement new to avoid additional heap memory allocation.
     new(i2cRaw) I2C(sda, scl);
 
     offset[0] = offset[1] = offset[2] = 0;
 
-    if(fastmode){
+    if(fastmode) {
         //400kHz, fast mode.
         i2c_.frequency(400000);
-    }
-    else
+    } else
         i2c_.frequency(100000);
-}    
-    
-ITG3200::~ITG3200(){
+}
+
+ITG3200::~ITG3200()
+{
     // If the I2C object is initialized in the buffer in this object, call destructor of it.
     if(&i2c_ == reinterpret_cast<I2C*>(&i2cRaw))
         reinterpret_cast<I2C*>(&i2cRaw)->~I2C();
 }
 
-void ITG3200::init(){
+void ITG3200::init()
+{
     //Set FS_SEL to 0x03 for proper operation.
     //See datasheet for details.
     char tx[2];
     tx[0] = DLPF_FS_REG;
     //FS_SEL bits sit in bits 4 and 3 of DLPF_FS register.
     tx[1] = 0x03 << 3;
-    
+
     i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
 
 }
 
-void ITG3200::setCalibrationCurve(const float offset[3], const float slope[3]){
-    if(offset){
+void ITG3200::setCalibrationCurve(const float offset[3], const float slope[3])
+{
+    if(offset) {
         for(int i = 0; i < 3; i++)
             this->foffset[i] = offset[i];
     }
-    if(slope){
+    if(slope) {
         for(int i = 0; i < 3; i++)
             this->slope[i] = slope[i];
     }
 }
 
-char ITG3200::getWhoAmI(void){
+char ITG3200::getWhoAmI(void)
+{
 
     //WhoAmI Register address.
     char tx = WHO_AM_I_REG;
     char rx;
-    
-    i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
-    
-    i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
-    
-    return rx;
-
-}
-
-void ITG3200::setWhoAmI(char address){
 
-    char tx[2];
-    tx[0] = WHO_AM_I_REG;
-    tx[1] = address;
-    
-    i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
-
-}
+    i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
 
-char ITG3200::getSampleRateDivider(void){
-
-    char tx = SMPLRT_DIV_REG;
-    char rx;
-    
-    i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
-    
     i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
 
     return rx;
 
 }
 
-void ITG3200::setSampleRateDivider(char divider){
+void ITG3200::setWhoAmI(char address)
+{
+
+    char tx[2];
+    tx[0] = WHO_AM_I_REG;
+    tx[1] = address;
+
+    i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
+
+}
+
+char ITG3200::getSampleRateDivider(void)
+{
+
+    char tx = SMPLRT_DIV_REG;
+    char rx;
+
+    i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
+
+    i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
+
+    return rx;
+
+}
+
+void ITG3200::setSampleRateDivider(char divider)
+{
 
     char tx[2];
     tx[0] = SMPLRT_DIV_REG;
@@ -126,142 +134,149 @@
 
 }
 
-int ITG3200::getInternalSampleRate(void){
+int ITG3200::getInternalSampleRate(void)
+{
 
     char tx = DLPF_FS_REG;
     char rx;
-    
+
     i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
-    
+
     i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
-    
+
     //DLPF_CFG == 0 -> sample rate = 8kHz.
-    if(rx == 0){
+    if(rx == 0) {
         return 8;
-    } 
+    }
     //DLPF_CFG = 1..7 -> sample rate = 1kHz.
-    else if(rx >= 1 && rx <= 7){
+    else if(rx >= 1 && rx <= 7) {
         return 1;
     }
     //DLPF_CFG = anything else -> something's wrong!
-    else{
+    else {
         return -1;
     }
-    
+
 }
 
-void ITG3200::setLpBandwidth(char bandwidth){
+void ITG3200::setLpBandwidth(char bandwidth)
+{
 
     char tx[2];
     tx[0] = DLPF_FS_REG;
     //Bits 4,3 are required to be 0x03 for proper operation.
     tx[1] = bandwidth | (0x03 << 3);
-    
+
     i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
 
 }
 
-char ITG3200::getInterruptConfiguration(void){
+char ITG3200::getInterruptConfiguration(void)
+{
 
     char tx = INT_CFG_REG;
     char rx;
-    
+
     i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
-    
+
     i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
-    
+
     return rx;
 
 }
 
-void ITG3200::setInterruptConfiguration(char config){
+void ITG3200::setInterruptConfiguration(char config)
+{
 
     char tx[2];
     tx[0] = INT_CFG_REG;
     tx[1] = config;
-    
+
     i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
 
 }
 
-bool ITG3200::isPllReady(void){
+bool ITG3200::isPllReady(void)
+{
 
     char tx = INT_STATUS;
     char rx;
-    
+
     i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
-    
+
     i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
-    
+
     //ITG_RDY bit is bit 4 of INT_STATUS register.
-    if(rx & 0x04){
+    if(rx & 0x04) {
         return true;
-    }
-    else{
+    } else {
         return false;
     }
-    
+
 }
 
-bool ITG3200::isRawDataReady(void){
+bool ITG3200::isRawDataReady(void)
+{
 
     char tx = INT_STATUS;
     char rx;
-    
+
     i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
-    
+
     i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
-    
+
     //RAW_DATA_RDY bit is bit 1 of INT_STATUS register.
-    if(rx & 0x01){
+    if(rx & 0x01) {
         return true;
-    }
-    else{
+    } else {
         return false;
     }
-    
+
 }
 
-int ITG3200::getWord(int regi){
+int ITG3200::getWord(int regi)
+{
 
     char tx = regi;
     char rx[2];
-    
+
     i2c_.write(I2C_ADDRESS, &tx, 1);
-    
+
     i2c_.read(I2C_ADDRESS, rx, 2);
-    
+
     return swapExtend(rx);
 }
 
-float ITG3200::getTemperature(){
+float ITG3200::getTemperature()
+{
     //Offset = -35 degrees, 13200 counts. 280 counts/degrees C.
     return 35.0 + ((getRawTemperature() + 13200)/280.0);
-    
+
 }
 
-void ITG3200::getRawGyroXYZ(int readings[3]){
+void ITG3200::getRawGyroXYZ(int readings[3])
+{
 
     char tx = GYRO_XOUT_H_REG;
     char rx[2];
-    
+
     i2c_.write(I2C_ADDRESS, &tx, 1);
-    
+
     i2c_.read(I2C_ADDRESS, rx, 6);
-    
+
     for(int i = 0; i < 3; i++)
         readings[i] = swapExtend(&rx[i * 2]);
 }
 
-void ITG3200::getGyroXYZ(int readings[3], Correction corr){
+void ITG3200::getGyroXYZ(int readings[3], Correction corr)
+{
     getRawGyroXYZ(readings);
-    switch(corr){
-    case OffsetCorrection:
-        for(int i = 0; i < 3; i++)
-            readings[i] -= offset[i];
-        break;
-    case Calibration:
-        {
+    switch(corr) {
+        case OffsetCorrection:
+            for(int i = 0; i < 3; i++)
+                readings[i] -= offset[i];
+            break;
+        case Calibration: {
             float temp = getTemperature();
             for(int i = 0; i < 3; i++)
                 readings[i] -= slope[i] * temp + foffset[i];
@@ -271,20 +286,22 @@
 }
 
 
-char ITG3200::getPowerManagement(void){
+char ITG3200::getPowerManagement(void)
+{
 
     char tx = PWR_MGM_REG;
     char rx;
-    
+
     i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
-    
+
     i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
-    
+
     return rx;
 
 }
 
-void ITG3200::setPowerManagement(char config){
+void ITG3200::setPowerManagement(char config)
+{
 
     char tx[2];
     tx[0] = PWR_MGM_REG;
@@ -294,12 +311,13 @@
 
 }
 
-void ITG3200::calibrate(double time){
+void ITG3200::calibrate(double time)
+{
     long sum[3] = {0};
     int sumCount = 0;
     Timer t;
     t.start();
-    while(t.read() < time){
+    while(t.read() < time) {
         int gyro[3];
         getRawGyroXYZ(gyro);
         for(int i = 0; i < 3; i++)
@@ -309,7 +327,7 @@
     t.stop();
 
     // Avoid zero division
-    if(0 < sumCount){
+    if(0 < sumCount) {
         for(int i = 0; i < 3; i++)
             offset[i] = sum[i] / sumCount;
         // Update member variable only if successful