
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
IMU/Sensors/Acc/ADXL345.cpp@4:f62337b907e5, 2013-08-29 (annotated)
- Committer:
- maetugr
- Date:
- Thu Aug 29 13:52:30 2013 +0000
- Revision:
- 4:f62337b907e5
- Parent:
- 0:3e7450f1a938
The Altitude Sensor is now implemented, it's really 10DOF now ;); TODO: Autocalibration
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maetugr | 0:3e7450f1a938 | 1 | #include "ADXL345.h" |
maetugr | 0:3e7450f1a938 | 2 | |
maetugr | 0:3e7450f1a938 | 3 | ADXL345::ADXL345(PinName sda, PinName scl) : I2C_Sensor(sda, scl, ADXL345_I2C_ADDRESS) |
maetugr | 0:3e7450f1a938 | 4 | { |
maetugr | 4:f62337b907e5 | 5 | #warning these three offsets are calibration values to make shure the measurement is 0 when no acceleration is present |
maetugr | 0:3e7450f1a938 | 6 | offset[0] = -9.1; // offset calculated by hand... (min + ((max - min) / 2) |
maetugr | 0:3e7450f1a938 | 7 | offset[1] = -7.1; // TODO: make this automatic with saving to filesystem |
maetugr | 0:3e7450f1a938 | 8 | offset[2] = 6; |
maetugr | 0:3e7450f1a938 | 9 | |
maetugr | 0:3e7450f1a938 | 10 | // Set Offset - programmed into the OFSX, OFSY, and OFXZ registers, respectively, as 0xFD, 0x03 and 0xFE. |
maetugr | 0:3e7450f1a938 | 11 | writeRegister(ADXL345_OFSX_REG, 0xFA); // to get these offsets just lie your sensor down on the table always the axis pointing down to earth has 200+ and the others should have more or less 0 |
maetugr | 0:3e7450f1a938 | 12 | writeRegister(ADXL345_OFSY_REG, 0xFE); |
maetugr | 0:3e7450f1a938 | 13 | writeRegister(ADXL345_OFSZ_REG, 0x0A); |
maetugr | 0:3e7450f1a938 | 14 | |
maetugr | 0:3e7450f1a938 | 15 | writeRegister(ADXL345_BW_RATE_REG, 0x0F); // 3200Hz BW-Rate |
maetugr | 0:3e7450f1a938 | 16 | writeRegister(ADXL345_DATA_FORMAT_REG, 0x0B); // set data format to full resolution and +-16g |
maetugr | 0:3e7450f1a938 | 17 | writeRegister(ADXL345_POWER_CTL_REG, 0x08); // set mode |
maetugr | 0:3e7450f1a938 | 18 | } |
maetugr | 0:3e7450f1a938 | 19 | |
maetugr | 0:3e7450f1a938 | 20 | void ADXL345::read(){ |
maetugr | 0:3e7450f1a938 | 21 | readraw(); |
maetugr | 0:3e7450f1a938 | 22 | for (int i = 0; i < 3; i++) |
maetugr | 0:3e7450f1a938 | 23 | data[i] = raw[i] - offset[i]; // TODO: didnt care about units |
maetugr | 0:3e7450f1a938 | 24 | } |
maetugr | 0:3e7450f1a938 | 25 | |
maetugr | 0:3e7450f1a938 | 26 | void ADXL345::readraw(){ |
maetugr | 0:3e7450f1a938 | 27 | char buffer[6]; |
maetugr | 0:3e7450f1a938 | 28 | readMultiRegister(ADXL345_DATAX0_REG, buffer, 6); |
maetugr | 0:3e7450f1a938 | 29 | |
maetugr | 0:3e7450f1a938 | 30 | raw[0] = (short) ((int)buffer[1] << 8 | (int)buffer[0]); |
maetugr | 0:3e7450f1a938 | 31 | raw[1] = (short) ((int)buffer[3] << 8 | (int)buffer[2]); |
maetugr | 0:3e7450f1a938 | 32 | raw[2] = (short) ((int)buffer[5] << 8 | (int)buffer[4]); |
maetugr | 0:3e7450f1a938 | 33 | } |
maetugr | 0:3e7450f1a938 | 34 | |
maetugr | 0:3e7450f1a938 | 35 | void ADXL345::calibrate(int times, float separation_time) |
maetugr | 0:3e7450f1a938 | 36 | { |
maetugr | 0:3e7450f1a938 | 37 | // calibrate sensor with an average of count samples (result of calibration stored in offset[]) |
maetugr | 0:3e7450f1a938 | 38 | float calib[3] = {0,0,0}; // temporary array for the sum of calibration measurement |
maetugr | 0:3e7450f1a938 | 39 | |
maetugr | 0:3e7450f1a938 | 40 | for (int j = 0; j < times; j++) { // read 'times' times the data in a very short time |
maetugr | 0:3e7450f1a938 | 41 | readraw(); |
maetugr | 0:3e7450f1a938 | 42 | for (int i = 0; i < 3; i++) |
maetugr | 0:3e7450f1a938 | 43 | calib[i] += raw[i]; |
maetugr | 0:3e7450f1a938 | 44 | wait(separation_time); |
maetugr | 0:3e7450f1a938 | 45 | } |
maetugr | 0:3e7450f1a938 | 46 | |
maetugr | 0:3e7450f1a938 | 47 | for (int i = 0; i < 3; i++) |
maetugr | 0:3e7450f1a938 | 48 | offset[i] = calib[i]/times; // take the average of the calibration measurements |
maetugr | 0:3e7450f1a938 | 49 | } |