Get value from Sparkfun IMU module, 6-degrees-of-freedom (ITG-3200/ADXL345) Gyro sensor and Accel sensor is included.

Dependencies:   ITG3200 mbed

main.cpp

Committer:
ryought
Date:
2012-11-26
Revision:
0:cda6d9f5a43c

File content as of revision 0:cda6d9f5a43c:

#include "ITG3200.h"
#include "ADXL345_I2C.h"

Serial pc(USBTX, USBRX);
ITG3200 gyro(p9, p10);
ADXL345_I2C accel(p9, p10);

int main() {
    gyro.setWhoAmI(0x68);
    pc.printf("Now starting 6-degrees-of-freedom (ITG-3200 ADXL345) test...\n");
    pc.printf("Accelerometer Device ID is: 0x%02x\n", accel.getDeviceID());
    pc.printf("Gyro Devide ID is: 0x02x\n", gyro.getWhoAmI());
    
    int accel_read[3] = {0, 0, 0};
    
    // Accel setup
    // These are here to test whether any of the initialization fails. It will print the failure
    if (accel.setPowerControl(0x00)){
         pc.printf("didn't intitialize power control\n"); 
         return 0;  }
    //Full resolution, +/-16g, 4mg/LSB.
    wait(.001);
     
    if(accel.setDataFormatControl(0x0B)){
        pc.printf("didn't set data format\n");
        return 0;  }
    wait(.001);
    
    //3.2kHz data rate.
    if(accel.setDataRate(ADXL345_3200HZ)){
       pc.printf("didn't set data rate\n");
       return 0;    }
    wait(.001);
    
    //Measurement mode.
    
    if(accel.setPowerControl(MeasurementMode)) {
       pc.printf("didn't set the power control to measurement\n"); 
       return 0;   
    }
     
    // Gyro setup   
    gyro.setLpBandwidth(LPFBW_42HZ);

    while (1) {
        wait(0.1);
        
        accel.getOutput(accel_read);
        
        pc.printf("Gyro[%5i, %5i, %5i]   Accel[%4i, %4i, %4i]\n", 
                            gyro.getGyroX(), gyro.getGyroY(), gyro.getGyroZ(),
                            (int16_t)accel_read[0], (int16_t)accel_read[1], (int16_t)accel_read[2]);
    }
    
}