An fully working IMU-Filter and Sensor drivers for the 10DOF-Board over I2C. All in one simple class. Include, calibrate sensors, call read, get angles. (3D Visualisation code for Python also included) Sensors: L3G4200D, ADXL345, HMC5883, BMP085

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers L3G4200D.cpp Source File

L3G4200D.cpp

00001 #include "L3G4200D.h"
00002 
00003 L3G4200D::L3G4200D(PinName sda, PinName scl) : I2C_Sensor(sda, scl, L3G4200D_I2C_ADDRESS) {
00004     // Turns on the L3G4200D's gyro and places it in normal mode.
00005     // Normal power mode, all axes enabled (for detailed info see datasheet)
00006     
00007     //writeRegister(L3G4200D_CTRL_REG2, 0x05);               // control filter
00008     writeRegister(L3G4200D_CTRL_REG2, 0x00);            // highpass filter disabled
00009     writeRegister(L3G4200D_CTRL_REG3, 0x00);
00010     writeRegister(L3G4200D_CTRL_REG4, 0x20);            // sets acuracy to 2000 dps (degree per second)
00011     
00012     writeRegister(L3G4200D_REFERENCE, 0x00);
00013     //writeRegister(L3G4200D_STATUS_REG, 0x0F);
00014     
00015     writeRegister(L3G4200D_INT1_THS_XH, 0x2C); // TODO: WTF??
00016     writeRegister(L3G4200D_INT1_THS_XL, 0xA4);
00017     writeRegister(L3G4200D_INT1_THS_YH, 0x2C);
00018     writeRegister(L3G4200D_INT1_THS_YL, 0xA4);
00019     writeRegister(L3G4200D_INT1_THS_ZH, 0x2C);
00020     writeRegister(L3G4200D_INT1_THS_ZL, 0xA4);
00021     //writeRegister(L3G4200D_INT1_DURATION, 0x00);
00022     
00023     writeRegister(L3G4200D_CTRL_REG5, 0x00);            // deactivates the filters (only use one of these options)
00024     //writeRegister(L3G4200D_CTRL_REG5, 0x12);          // activates both high and low pass filters
00025     //writeRegister(L3G4200D_CTRL_REG5, 0x01);          // activates high pass filter
00026     
00027     writeRegister(L3G4200D_CTRL_REG1, 0x0F);            // starts Gyro measurement
00028     
00029     //calibrate(50, 0.01);
00030 }
00031 
00032 void L3G4200D::read() {
00033     readraw();                                          // read raw measurement data
00034     
00035     for (int i = 0; i < 3; i++)
00036             data[i] = (raw[i] - offset[i])*0.07;               // subtract offset from calibration and multiply unit factor (datasheet s.10)
00037 }
00038 
00039 int L3G4200D::readTemp() {
00040     return (char) readRegister(L3G4200D_OUT_TEMP);     // read the sensors register for the temperature
00041 }
00042 
00043 void L3G4200D::readraw() {
00044     char buffer[6];                                     // 8-Bit pieces of axis data
00045     
00046     readMultiRegister(L3G4200D_OUT_X_L | (1 << 7), buffer, 6); // read axis registers using I2C   // TODO: why?!   | (1 << 7)
00047     
00048     raw[0] = (short) (buffer[1] << 8 | buffer[0]);     // join 8-Bit pieces to 16-bit short integers
00049     raw[1] = (short) (buffer[3] << 8 | buffer[2]);
00050     raw[2] = (short) (buffer[5] << 8 | buffer[4]);
00051 }
00052 
00053 void L3G4200D::calibrate(int times, float separation_time) {
00054     // calibrate sensor with an average of count samples (result of calibration stored in offset[])
00055     float calib[3] = {0,0,0};                           // temporary array for the sum of calibration measurement
00056     
00057     for (int i = 0; i < times; i++) {                   // read 'times' times the data in a very short time
00058         readraw();
00059         for (int j = 0; j < 3; j++)
00060             calib[j] += raw[j];
00061         wait(separation_time);
00062     }
00063     
00064     for (int i = 0; i < 3; i++)
00065         offset[i] = calib[i]/times;                     // take the average of the calibration measurements
00066 }