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
I2C_Sensor.cpp
00001 #include "I2C_Sensor.h" 00002 00003 // calculate the 8-Bit write/read I2C-Address from the 7-Bit adress of the device 00004 #define GET_I2C_WRITE_ADDRESS(ADR) (ADR << 1&0xFE) // ADR & 1111 1110 00005 #define GET_I2C_READ_ADDRESS(ADR) (ADR << 1|0x01) // ADR | 0000 0001 00006 00007 I2C_Sensor::I2C_Sensor(PinName sda, PinName scl, char i2c_address) : i2c(sda, scl), local("local") 00008 { 00009 I2C_Sensor::i2c_address = i2c_address; 00010 //i2c.frequency(400000); // standard speed 00011 i2c.frequency(1000000); // ultrafast! 00012 } 00013 00014 void I2C_Sensor::saveCalibrationValues(float values[], int size, char * filename) 00015 { 00016 FILE *fp = fopen(strcat("/local/", filename), "w"); 00017 for(int i = 0; i < size; i++) 00018 fprintf(fp, "%f\r\n", values[i]); 00019 fclose(fp); 00020 } 00021 00022 void I2C_Sensor::loadCalibrationValues(float values[], int size, char * filename) 00023 { 00024 FILE *fp = fopen(strcat("/local/", filename), "r"); 00025 for(int i = 0; i < size; i++) 00026 fscanf(fp, "%f", &values[i]); 00027 fclose(fp); 00028 } 00029 00030 // TODO TODO TODO-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 00031 // 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) 00032 // IT DIDN'T WORK STABLE IN OTHER COMBINATIONS (if someone has an idea why please let me know) 00033 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 00034 00035 char I2C_Sensor::readRegister(char reg) 00036 { 00037 char value = 0; 00038 00039 i2c.write(i2c_address, ®, 1); 00040 i2c.read(i2c_address, &value, 1); 00041 00042 return value; 00043 } 00044 00045 void I2C_Sensor::writeRegister(char reg, char data) 00046 { 00047 char buffer[2] = {reg, data}; 00048 i2c.write(i2c_address, buffer, 2); 00049 } 00050 00051 void I2C_Sensor::readMultiRegister(char reg, char* output, int size) 00052 { 00053 i2c.write (i2c_address, ®, 1); // tell register address of the MSB get the sensor to do slave-transmit subaddress updating. 00054 i2c.read (i2c_address, output, size); // tell it where to store the data read 00055 }
Generated on Wed Jul 13 2022 22:03:48 by 1.7.2