Successful acro and level mode now! Relying on MPU9250 as base sensor. I'm working continuously on tuning and features :) NEWEST VERSION ON: https://github.com/MaEtUgR/FlyBed (CODE 100% compatible/copyable)

Dependencies:   mbed

Committer:
maetugr
Date:
Thu Nov 19 18:47:27 2015 +0000
Revision:
8:609a2ad4c30e
made I2C-Sensors working in parallel, added rolling buffer for PID derivative, played with the PID and frequency parameters in main

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maetugr 8:609a2ad4c30e 1 #include "I2C_Sensor.h"
maetugr 8:609a2ad4c30e 2
maetugr 8:609a2ad4c30e 3 // calculate the 8-Bit write/read I2C-Address from the 7-Bit adress of the device
maetugr 8:609a2ad4c30e 4 #define GET_I2C_WRITE_ADDRESS(ADR) (ADR << 1&0xFE) // ADR & 1111 1110
maetugr 8:609a2ad4c30e 5 #define GET_I2C_READ_ADDRESS(ADR) (ADR << 1|0x01) // ADR | 0000 0001
maetugr 8:609a2ad4c30e 6
maetugr 8:609a2ad4c30e 7 I2C_Sensor::I2C_Sensor(PinName sda, PinName scl, char i2c_address) : i2c(sda, scl), local("local")
maetugr 8:609a2ad4c30e 8 {
maetugr 8:609a2ad4c30e 9 I2C_Sensor::i2c_address = i2c_address;
maetugr 8:609a2ad4c30e 10 i2c.frequency(400000); // standard speed
maetugr 8:609a2ad4c30e 11 //i2c.frequency(1000000); // ultrafast!
maetugr 8:609a2ad4c30e 12 }
maetugr 8:609a2ad4c30e 13
maetugr 8:609a2ad4c30e 14 void I2C_Sensor::saveCalibrationValues(float values[], int size, char * filename)
maetugr 8:609a2ad4c30e 15 {
maetugr 8:609a2ad4c30e 16 FILE *fp = fopen(strcat("/local/", filename), "w");
maetugr 8:609a2ad4c30e 17 for(int i = 0; i < size; i++)
maetugr 8:609a2ad4c30e 18 fprintf(fp, "%f\r\n", values[i]);
maetugr 8:609a2ad4c30e 19 fclose(fp);
maetugr 8:609a2ad4c30e 20 }
maetugr 8:609a2ad4c30e 21
maetugr 8:609a2ad4c30e 22 void I2C_Sensor::loadCalibrationValues(float values[], int size, char * filename)
maetugr 8:609a2ad4c30e 23 {
maetugr 8:609a2ad4c30e 24 FILE *fp = fopen(strcat("/local/", filename), "r");
maetugr 8:609a2ad4c30e 25 for(int i = 0; i < size; i++)
maetugr 8:609a2ad4c30e 26 fscanf(fp, "%f", &values[i]);
maetugr 8:609a2ad4c30e 27 fclose(fp);
maetugr 8:609a2ad4c30e 28 }
maetugr 8:609a2ad4c30e 29
maetugr 8:609a2ad4c30e 30 // I2C functions --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
maetugr 8:609a2ad4c30e 31
maetugr 8:609a2ad4c30e 32
maetugr 8:609a2ad4c30e 33 char I2C_Sensor::readRegister(char reg)
maetugr 8:609a2ad4c30e 34 {
maetugr 8:609a2ad4c30e 35 char value = 0;
maetugr 8:609a2ad4c30e 36
maetugr 8:609a2ad4c30e 37 i2c.write(i2c_address, &reg, 1);
maetugr 8:609a2ad4c30e 38 i2c.read(i2c_address, &value, 1);
maetugr 8:609a2ad4c30e 39
maetugr 8:609a2ad4c30e 40 return value;
maetugr 8:609a2ad4c30e 41 }
maetugr 8:609a2ad4c30e 42
maetugr 8:609a2ad4c30e 43 void I2C_Sensor::writeRegister(char reg, char data)
maetugr 8:609a2ad4c30e 44 {
maetugr 8:609a2ad4c30e 45 char buffer[2] = {reg, data};
maetugr 8:609a2ad4c30e 46 i2c.write(i2c_address, buffer, 2);
maetugr 8:609a2ad4c30e 47 }
maetugr 8:609a2ad4c30e 48
maetugr 8:609a2ad4c30e 49 int I2C_Sensor::readMultiRegister(char reg, char* output, int size)
maetugr 8:609a2ad4c30e 50 {
maetugr 8:609a2ad4c30e 51 if (0 != i2c.write (i2c_address, &reg, 1)) return 1; // tell register address of the MSB get the sensor to do slave-transmit subaddress updating.
maetugr 8:609a2ad4c30e 52 if (0 != i2c.read (i2c_address, output, size)) return 1; // tell it where to store the data read
maetugr 8:609a2ad4c30e 53 return 0;
maetugr 8:609a2ad4c30e 54 }