NOT FINISHED YET!!! My first try to get a self built fully working Quadrocopter based on an mbed, a self built frame and some other more or less cheap parts.

Dependencies:   mbed MODI2C

Committer:
maetugr
Date:
Wed Oct 31 16:53:01 2012 +0000
Revision:
17:e096e85f6c36
Parent:
16:74a6531350b5
Child:
20:e116e596e540
alle Sensoren mit i2c auf I2C_Sensor umgestellt (nicht getestet), noch keine calibration save per I2C_Sensor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maetugr 0:0c4fafa398b4 1 #include "L3G4200D.h"
maetugr 0:0c4fafa398b4 2
maetugr 16:74a6531350b5 3 L3G4200D::L3G4200D(PinName sda, PinName scl) : I2C_Sensor(sda, scl, L3G4200D_I2C_ADDRESS)
maetugr 0:0c4fafa398b4 4 {
maetugr 0:0c4fafa398b4 5 // Turns on the L3G4200D's gyro and places it in normal mode.
maetugr 16:74a6531350b5 6 // Normal power mode, all axes enabled (for detailed info see datasheet)
maetugr 0:0c4fafa398b4 7
maetugr 16:74a6531350b5 8 //writeRegister(L3G4200D_CTRL_REG2, 0x05); // control filter
maetugr 16:74a6531350b5 9 writeRegister(L3G4200D_CTRL_REG2, 0x00); // highpass filter disabled
maetugr 16:74a6531350b5 10 writeRegister(L3G4200D_CTRL_REG3, 0x00);
maetugr 16:74a6531350b5 11 writeRegister(L3G4200D_CTRL_REG4, 0x20); // sets acuracy to 2000 dps (degree per second)
maetugr 16:74a6531350b5 12
maetugr 16:74a6531350b5 13 writeRegister(L3G4200D_REFERENCE, 0x00);
maetugr 16:74a6531350b5 14 //writeRegister(L3G4200D_STATUS_REG, 0x0F);
maetugr 0:0c4fafa398b4 15
maetugr 16:74a6531350b5 16 writeRegister(L3G4200D_INT1_THS_XH, 0x2C); // TODO: WTF??
maetugr 16:74a6531350b5 17 writeRegister(L3G4200D_INT1_THS_XL, 0xA4);
maetugr 16:74a6531350b5 18 writeRegister(L3G4200D_INT1_THS_YH, 0x2C);
maetugr 16:74a6531350b5 19 writeRegister(L3G4200D_INT1_THS_YL, 0xA4);
maetugr 16:74a6531350b5 20 writeRegister(L3G4200D_INT1_THS_ZH, 0x2C);
maetugr 16:74a6531350b5 21 writeRegister(L3G4200D_INT1_THS_ZL, 0xA4);
maetugr 16:74a6531350b5 22 //writeRegister(L3G4200D_INT1_DURATION, 0x00);
maetugr 0:0c4fafa398b4 23
maetugr 16:74a6531350b5 24 writeRegister(L3G4200D_CTRL_REG5, 0x00); // deactivates the filters (only use one of these options)
maetugr 16:74a6531350b5 25 //writeRegister(L3G4200D_CTRL_REG5, 0x12); // activates both high and low pass filters
maetugr 16:74a6531350b5 26 //writeRegister(L3G4200D_CTRL_REG5, 0x01); // activates high pass filter
maetugr 2:93f703d2c4d7 27
maetugr 16:74a6531350b5 28 writeRegister(L3G4200D_CTRL_REG1, 0x0F); // starts Gyro measurement
maetugr 16:74a6531350b5 29
maetugr 16:74a6531350b5 30 // calibrate gyro with an average of count samples (result of calibration stored in offset[])
maetugr 2:93f703d2c4d7 31 for (int j = 0; j < 3; j++)
maetugr 2:93f703d2c4d7 32 offset[j] = 0;
maetugr 2:93f703d2c4d7 33
maetugr 16:74a6531350b5 34 float Gyro_calib[3] = {0,0,0}; // temporary var for the sum of calibration measurement
maetugr 2:93f703d2c4d7 35
maetugr 17:e096e85f6c36 36 const int count = 50;
maetugr 17:e096e85f6c36 37 for (int i = 0; i < count; i++) { // read 50 times the data in a very short time
maetugr 10:953afcbcebfc 38 read();
maetugr 2:93f703d2c4d7 39 for (int j = 0; j < 3; j++)
maetugr 10:953afcbcebfc 40 Gyro_calib[j] += data[j];
maetugr 16:74a6531350b5 41 wait(0.001); // TODO: maybe less or no wait !!
maetugr 2:93f703d2c4d7 42 }
maetugr 2:93f703d2c4d7 43
maetugr 2:93f703d2c4d7 44 for (int j = 0; j < 3; j++)
maetugr 16:74a6531350b5 45 offset[j] = Gyro_calib[j]/count; // take the average of the calibration measurements
maetugr 0:0c4fafa398b4 46 }
maetugr 0:0c4fafa398b4 47
maetugr 10:953afcbcebfc 48 void L3G4200D::read()
maetugr 0:0c4fafa398b4 49 {
maetugr 16:74a6531350b5 50 char buffer[6]; // 8-Bit pieces of axis data
maetugr 16:74a6531350b5 51
maetugr 17:e096e85f6c36 52 //buffer[0] = L3G4200D_OUT_X_L | (1 << 7); // TODO: wiiiiiiso?!
maetugr 14:cf260677ecde 53
maetugr 17:e096e85f6c36 54 readMultiRegister(L3G4200D_OUT_X_L | (1 << 7), buffer, 6); // read axis registers using I2C
maetugr 0:0c4fafa398b4 55
maetugr 17:e096e85f6c36 56 data[0] = (short) (buffer[1] << 8 | buffer[0]); // join 8-Bit pieces to 16-bit short integers
maetugr 11:9bf69bc6df45 57 data[1] = (short) (buffer[3] << 8 | buffer[2]);
maetugr 11:9bf69bc6df45 58 data[2] = (short) (buffer[5] << 8 | buffer[4]);
maetugr 2:93f703d2c4d7 59
maetugr 2:93f703d2c4d7 60 for (int j = 0; j < 3; j++)
maetugr 16:74a6531350b5 61 data[j] -= offset[j]; // add offset from calibration
maetugr 0:0c4fafa398b4 62 }
maetugr 0:0c4fafa398b4 63
maetugr 1:5a64632b1eb9 64 int L3G4200D::readTemp()
maetugr 0:0c4fafa398b4 65 {
maetugr 16:74a6531350b5 66 return (short) readRegister(L3G4200D_OUT_TEMP); // read the sensors register for the temperature
maetugr 0:0c4fafa398b4 67 }