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 14:44:07 2012 +0000
Revision:
16:74a6531350b5
Parent:
15:753c5d6a63b3
Child:
17:e096e85f6c36
nach Kompassumstellung auf I2C_Sensor (nicht getestet)

Who changed what in which revision?

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