My fully self designed first stable working Quadrocopter Software.

Dependencies:   mbed

Dependents:   fluy343

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BMP085.cpp Source File

BMP085.cpp

00001 #include "BMP085.h"
00002 
00003 BMP085::BMP085(PinName sda, PinName scl) : I2C_Sensor(sda, scl, BMP085_I2C_ADDRESS) {
00004     // read calibration data from E2PROM, needed for formulas in read function
00005     char buffer[22];                                  // 8-Bit pieces of axis data
00006     readMultiRegister(BMP085_CAL_AC1, buffer, 22);       // read all calibration registers in one time 22 Byte = 176 Bit
00007     
00008     AC1 = (short) (buffer[0] << 8 | buffer[1]);         // join 8-Bit pieces to 16-bit short integers
00009     AC2 = (short) (buffer[2] << 8 | buffer[3]);
00010     AC3 = (short) (buffer[4] << 8 | buffer[5]);
00011     AC4 = (unsigned short) (buffer[6] << 8 | buffer[7]);      // unsigned !!
00012     AC5 = (unsigned short) (buffer[8] << 8 | buffer[9]);
00013     AC6 = (unsigned short) (buffer[10] << 8 | buffer[11]);
00014     B1 = (short) (buffer[12] << 8 | buffer[13]);
00015     B2 = (short) (buffer[14] << 8 | buffer[15]);
00016     MB = (short) (buffer[16] << 8 | buffer[17]);
00017     MC = (short) (buffer[18] << 8 | buffer[19]);
00018     MD = (short) (buffer[20] << 8 | buffer[21]);
00019     
00020     
00021     oss = 0; // set Oversampling of Sensor
00022 }
00023 
00024 void BMP085::readraw() {
00025 }
00026 
00027 void BMP085::read() {
00028     unsigned short Uncompensated_Temperature;
00029     long P, Uncompensated_Pressure, X1, X2, X3, B3, B5, B6;
00030     unsigned long B4, B7;
00031     
00032     // read uncompensated Temperature
00033     writeRegister(BMP085_CONTROL, READ_TEMPERATURE); // say the sensor we want to read the temperature
00034     wait(0.005); // Wait at least 4.5ms (written in data sheet)
00035     char buffer[3];  // TODO: nur 2 wenn unten nicht gebraucht                                           // read 16-Bit Temperature (2 registers)
00036     readMultiRegister(BMP085_CONTROL_OUTPUT, buffer, 2);        
00037     Uncompensated_Temperature =  buffer[0] << 8 | buffer[1];               // join 8-Bit pieces to 16-bit short integer
00038     
00039     // calculate real Temperature
00040     X1 = ((Uncompensated_Temperature - AC6) * AC5) >> 15;
00041     X2 = (MC << 11) / (X1 + MD);
00042     #warning 33 is a calibration value for 3.3 degree shifft of temperature
00043     B5 = X1 + X2 - ((33 << 4) - 8); // TODO: I added this term because i think my sensor is about 3.3 degree off which makes a difference in absolute altitude
00044     Temperature = (float)((B5 + 8) >> 4)/10.0; // we want temperature in degree with digit after comma
00045     
00046     // read uncompensated Pressure
00047     writeRegister(BMP085_CONTROL, READ_PRESSURE + (oss << 6)); // say the sensor we want to read the pressure
00048     wait(0.005); // Wait at least 4.5ms (written in data sheet) TODO: oss fest, times vary and calculation of B3                         
00049     readMultiRegister(BMP085_CONTROL_OUTPUT, buffer, 3);                                                    // read 24-Bit Pressure (3 registers)
00050     Uncompensated_Pressure = (unsigned int) (buffer[0] << 16 | buffer[1] << 8 | buffer[0]) >> (8 - oss);    // join 8-Bit pieces to 24-bit integer
00051     
00052     // calculate real Pressure
00053     B6 = B5 - 4000;     // B5 is updated by real temperature above
00054     X1 = (B2 * ( (B6 * B6) >> 12 )) >> 11;
00055     X2 = (AC2 * B6) >> 11;
00056     X3 = X1 + X2;
00057     B3 = (((AC1 * 4 + X3) << oss) + 2) >> 2;
00058 
00059     X1 = (AC3 * B6) >> 13;
00060     X2 = (B1 * ( (B6 * B6) >> 12) ) >> 16;
00061     X3 = ((X1 + X2) + 2) >> 2;
00062     B4 = AC4 * ((unsigned long)X3 + 32768) >> 15;
00063     
00064     B7 = ((unsigned long)Uncompensated_Pressure - B3) * (50000 >> oss);
00065     
00066     if (B7 < 0x80000000)
00067         P = (B7 * 2) / B4;
00068     else 
00069         P = (B7 / B4) * 2;
00070     
00071     X1 = (P >> 8) * (P >> 8);
00072     X1 = (X1 * 3038) >> 16;
00073     X2 = (-7357 * P) >> 16;    
00074     P = P + ((X1 + X2 + 3791) >> 4);
00075     Pressure = (float)P / 100.0;
00076     
00077     // calculate height out of the pressure
00078     Altitude = 44330 * (1.0 - pow((Pressure / 1013.25), 1/5.25588));
00079 }