My fully self designed first stable working Quadrocopter Software.

Dependencies:   mbed

Dependents:   fluy343

/media/uploads/maetugr/dsc09031.jpg

Committer:
maetugr
Date:
Mon Aug 31 20:20:50 2015 +0000
Revision:
10:14390c90c3f5
Parent:
2:03e5f7ab473f
before changing to MPU9250

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maetugr 0:12950aa67f2a 1 #include "ADXL345.h"
maetugr 0:12950aa67f2a 2
maetugr 0:12950aa67f2a 3 ADXL345::ADXL345(PinName sda, PinName scl) : I2C_Sensor(sda, scl, ADXL345_I2C_ADDRESS)
maetugr 0:12950aa67f2a 4 {
maetugr 0:12950aa67f2a 5 #warning these three offsets are calibration values to make shure the measurement is 0 when no acceleration is present
maetugr 2:03e5f7ab473f 6 offset[0] = -18; // offset calculated by hand... (min + ((max - min) / 2)
maetugr 2:03e5f7ab473f 7 offset[1] = -17; // TODO: make this automatic with saving to filesystem
maetugr 2:03e5f7ab473f 8 offset[2] = 20;
maetugr 0:12950aa67f2a 9
maetugr 0:12950aa67f2a 10 // Set Offset - programmed into the OFSX, OFSY, and OFXZ registers, respectively, as 0xFD, 0x03 and 0xFE.
maetugr 0:12950aa67f2a 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:12950aa67f2a 12 writeRegister(ADXL345_OFSY_REG, 0xFE);
maetugr 0:12950aa67f2a 13 writeRegister(ADXL345_OFSZ_REG, 0x0A);
maetugr 0:12950aa67f2a 14
maetugr 0:12950aa67f2a 15 writeRegister(ADXL345_BW_RATE_REG, 0x0F); // 3200Hz BW-Rate
maetugr 0:12950aa67f2a 16 writeRegister(ADXL345_DATA_FORMAT_REG, 0x0B); // set data format to full resolution and +-16g
maetugr 0:12950aa67f2a 17 writeRegister(ADXL345_POWER_CTL_REG, 0x08); // set mode
maetugr 0:12950aa67f2a 18 }
maetugr 0:12950aa67f2a 19
maetugr 0:12950aa67f2a 20 void ADXL345::read(){
maetugr 0:12950aa67f2a 21 readraw();
maetugr 0:12950aa67f2a 22 for (int i = 0; i < 3; i++)
maetugr 0:12950aa67f2a 23 data[i] = raw[i] - offset[i]; // TODO: didnt care about units
maetugr 0:12950aa67f2a 24 }
maetugr 0:12950aa67f2a 25
maetugr 0:12950aa67f2a 26 void ADXL345::readraw(){
maetugr 0:12950aa67f2a 27 char buffer[6];
maetugr 0:12950aa67f2a 28 readMultiRegister(ADXL345_DATAX0_REG, buffer, 6);
maetugr 0:12950aa67f2a 29
maetugr 0:12950aa67f2a 30 raw[0] = (short) ((int)buffer[1] << 8 | (int)buffer[0]);
maetugr 0:12950aa67f2a 31 raw[1] = (short) ((int)buffer[3] << 8 | (int)buffer[2]);
maetugr 0:12950aa67f2a 32 raw[2] = (short) ((int)buffer[5] << 8 | (int)buffer[4]);
maetugr 0:12950aa67f2a 33 }
maetugr 0:12950aa67f2a 34
maetugr 0:12950aa67f2a 35 void ADXL345::calibrate(int times, float separation_time)
maetugr 0:12950aa67f2a 36 {
maetugr 0:12950aa67f2a 37 // calibrate sensor with an average of count samples (result of calibration stored in offset[])
maetugr 0:12950aa67f2a 38 float calib[3] = {0,0,0}; // temporary array for the sum of calibration measurement
maetugr 0:12950aa67f2a 39
maetugr 0:12950aa67f2a 40 for (int j = 0; j < times; j++) { // read 'times' times the data in a very short time
maetugr 0:12950aa67f2a 41 readraw();
maetugr 0:12950aa67f2a 42 for (int i = 0; i < 3; i++)
maetugr 0:12950aa67f2a 43 calib[i] += raw[i];
maetugr 0:12950aa67f2a 44 wait(separation_time);
maetugr 0:12950aa67f2a 45 }
maetugr 0:12950aa67f2a 46
maetugr 0:12950aa67f2a 47 for (int i = 0; i < 3; i++)
maetugr 0:12950aa67f2a 48 offset[i] = calib[i]/times; // take the average of the calibration measurements
maetugr 0:12950aa67f2a 49 }