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

Dependencies:   mbed

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?

UserRevisionLine numberNew contents of line
maetugr 0:3e7450f1a938 1 #include "I2C_Sensor.h"
maetugr 0:3e7450f1a938 2
maetugr 0:3e7450f1a938 3 // calculate the 8-Bit write/read I2C-Address from the 7-Bit adress of the device
maetugr 0:3e7450f1a938 4 #define GET_I2C_WRITE_ADDRESS(ADR) (ADR << 1&0xFE) // ADR & 1111 1110
maetugr 0:3e7450f1a938 5 #define GET_I2C_READ_ADDRESS(ADR) (ADR << 1|0x01) // ADR | 0000 0001
maetugr 0:3e7450f1a938 6
maetugr 0:3e7450f1a938 7 I2C_Sensor::I2C_Sensor(PinName sda, PinName scl, char i2c_address) : i2c(sda, scl), local("local")
maetugr 0:3e7450f1a938 8 {
maetugr 0:3e7450f1a938 9 I2C_Sensor::i2c_address = i2c_address;
maetugr 0:3e7450f1a938 10 //i2c.frequency(400000); // standard speed
maetugr 0:3e7450f1a938 11 i2c.frequency(1000000); // ultrafast!
maetugr 0:3e7450f1a938 12 }
maetugr 0:3e7450f1a938 13
maetugr 0:3e7450f1a938 14 void I2C_Sensor::saveCalibrationValues(float values[], int size, char * filename)
maetugr 0:3e7450f1a938 15 {
maetugr 0:3e7450f1a938 16 FILE *fp = fopen(strcat("/local/", filename), "w");
maetugr 0:3e7450f1a938 17 for(int i = 0; i < size; i++)
maetugr 0:3e7450f1a938 18 fprintf(fp, "%f\r\n", values[i]);
maetugr 0:3e7450f1a938 19 fclose(fp);
maetugr 0:3e7450f1a938 20 }
maetugr 0:3e7450f1a938 21
maetugr 0:3e7450f1a938 22 void I2C_Sensor::loadCalibrationValues(float values[], int size, char * filename)
maetugr 0:3e7450f1a938 23 {
maetugr 0:3e7450f1a938 24 FILE *fp = fopen(strcat("/local/", filename), "r");
maetugr 0:3e7450f1a938 25 for(int i = 0; i < size; i++)
maetugr 0:3e7450f1a938 26 fscanf(fp, "%f", &values[i]);
maetugr 0:3e7450f1a938 27 fclose(fp);
maetugr 0:3e7450f1a938 28 }
maetugr 0:3e7450f1a938 29
maetugr 4:f62337b907e5 30 // TODO TODO TODO--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
maetugr 0:3e7450f1a938 31 // ATTENTION!!! there was a problem with other interrupts disturbing the i2c communication of the chip... that's why I use I2C to initialise the sonsors and MODI2C to get the data (only made with readMultiRegister)
maetugr 0:3e7450f1a938 32 // IT DIDN'T WORK STABLE IN OTHER COMBINATIONS (if someone has an idea why please let me know)
maetugr 0:3e7450f1a938 33 //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
maetugr 0:3e7450f1a938 34
maetugr 0:3e7450f1a938 35 char I2C_Sensor::readRegister(char reg)
maetugr 0:3e7450f1a938 36 {
maetugr 0:3e7450f1a938 37 char value = 0;
maetugr 0:3e7450f1a938 38
maetugr 0:3e7450f1a938 39 i2c.write(i2c_address, &reg, 1);
maetugr 0:3e7450f1a938 40 i2c.read(i2c_address, &value, 1);
maetugr 0:3e7450f1a938 41
maetugr 0:3e7450f1a938 42 return value;
maetugr 0:3e7450f1a938 43 }
maetugr 0:3e7450f1a938 44
maetugr 0:3e7450f1a938 45 void I2C_Sensor::writeRegister(char reg, char data)
maetugr 0:3e7450f1a938 46 {
maetugr 0:3e7450f1a938 47 char buffer[2] = {reg, data};
maetugr 0:3e7450f1a938 48 i2c.write(i2c_address, buffer, 2);
maetugr 0:3e7450f1a938 49 }
maetugr 0:3e7450f1a938 50
maetugr 0:3e7450f1a938 51 void I2C_Sensor::readMultiRegister(char reg, char* output, int size)
maetugr 0:3e7450f1a938 52 {
maetugr 0:3e7450f1a938 53 i2c.write (i2c_address, &reg, 1); // tell register address of the MSB get the sensor to do slave-transmit subaddress updating.
maetugr 0:3e7450f1a938 54 i2c.read (i2c_address, output, size); // tell it where to store the data read
maetugr 0:3e7450f1a938 55 }