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.
Sensors/Gyro/L3G4200D.cpp@20:e116e596e540, 2012-11-05 (annotated)
- Committer:
- maetugr
- Date:
- Mon Nov 05 09:19:01 2012 +0000
- Revision:
- 20:e116e596e540
- Parent:
- 17:e096e85f6c36
- Child:
- 21:c2a2e7cbabdd
Eine Achse stabil! bereits abgehoben an Schnur! (erst Gyro im I2C_Sensor)(acos nan abgefangen)
Who changed what in which revision?
User | Revision | Line number | New 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 | 20:e116e596e540 | 38 | readraw(); |
maetugr | 2:93f703d2c4d7 | 39 | for (int j = 0; j < 3; j++) |
maetugr | 20:e116e596e540 | 40 | Gyro_calib[j] += raw[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 | 20:e116e596e540 | 50 | readraw(); // read raw measurement data |
maetugr | 0:0c4fafa398b4 | 51 | |
maetugr | 20:e116e596e540 | 52 | for (int i = 0; i < 3; i++) |
maetugr | 20:e116e596e540 | 53 | data[i] = raw[i] - offset[i]; // subtract offset from calibration |
maetugr | 0:0c4fafa398b4 | 54 | } |
maetugr | 0:0c4fafa398b4 | 55 | |
maetugr | 1:5a64632b1eb9 | 56 | int L3G4200D::readTemp() |
maetugr | 0:0c4fafa398b4 | 57 | { |
maetugr | 16:74a6531350b5 | 58 | return (short) readRegister(L3G4200D_OUT_TEMP); // read the sensors register for the temperature |
maetugr | 20:e116e596e540 | 59 | } |
maetugr | 20:e116e596e540 | 60 | |
maetugr | 20:e116e596e540 | 61 | void L3G4200D::readraw() |
maetugr | 20:e116e596e540 | 62 | { |
maetugr | 20:e116e596e540 | 63 | char buffer[6]; // 8-Bit pieces of axis data |
maetugr | 20:e116e596e540 | 64 | |
maetugr | 20:e116e596e540 | 65 | readMultiRegister(L3G4200D_OUT_X_L | (1 << 7), buffer, 6); // read axis registers using I2C // TODO: wiiiiiiso?! | (1 << 7) |
maetugr | 20:e116e596e540 | 66 | |
maetugr | 20:e116e596e540 | 67 | raw[0] = (short) (buffer[1] << 8 | buffer[0]); // join 8-Bit pieces to 16-bit short integers |
maetugr | 20:e116e596e540 | 68 | raw[1] = (short) (buffer[3] << 8 | buffer[2]); |
maetugr | 20:e116e596e540 | 69 | raw[2] = (short) (buffer[5] << 8 | buffer[4]); |
maetugr | 0:0c4fafa398b4 | 70 | } |