Library to work with the MS5611 pressure sensors. Forked from the MS5607 library

Dependents:   MS5611Example Atlas HC-06_MPU9250_F303K_Stream

Fork of MS5607 by Hiroshi Yamaguchi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MS5611Base.h Source File

MS5611Base.h

00001 /*
00002 Copyright (c) 2012, Senio Networks, Inc.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021 */
00022 
00023 /*  MS5611 Modifications
00024 Aerodyne Labs
00025 Matthew Nelson - 2014
00026 This library was originally the MS5607 library.  It has been modified to 
00027 to be used the MS5611 Pressure sensor
00028 */
00029 
00030 #ifndef MS5611_BASE_H
00031 #define MS5611_BASE_H
00032 
00033 class MS5611Base {
00034 public:
00035     void printCoefficients() {
00036         printf("%d, %d, %d, %d, %d, %d \r\n", c1, c2, c3, c4, c5, c6);
00037     }
00038 
00039     int getRawTemperature() {
00040         return readADC(ADC_D2 | OSR_4096);
00041     }
00042 
00043     int getRawPressure() {
00044         return readADC(ADC_D1 | OSR_4096);
00045     }
00046 
00047     float getTemperature() {
00048         int dT = getRawTemperature() - (c5 << 8);
00049         int temp = 2000 + ((dT * c6) >> 23);
00050         
00051         // 2nd order temperature compensation
00052         if (temp < 2000) {
00053             int t2 = (int64_t) dT * dT >> 31;
00054             temp -= t2;
00055         }
00056 
00057         return float(temp) / 100;
00058     }
00059 
00060     float getPressure() {
00061         int dT = getRawTemperature() - (c5 << 8);
00062         int temp = 2000 + ((dT * c6) >> 23);
00063         int64_t off = ((int64_t) c2 << 16) + ((int64_t) dT * c4 >> 7);
00064         int64_t sens = ((int64_t) c1 << 15) + ((int64_t) dT * c3 >> 8);
00065 
00066         // 2nd order temperature compensation
00067         if (temp < 2000) {
00068             int64_t off2 = (int64_t) 5 * (temp - 2000) * (temp - 2000) >> 1;
00069             int64_t sens2 = (int64_t) 5 * (temp - 2000) * (temp - 2000) >> 2;
00070             if (temp < -1500) {
00071                 off2 += (int64_t) 7 * (temp + 1500) * (temp + 1500);
00072                 sens2 += (int64_t) 11 * (temp + 1500) * (temp + 1500) >> 1;
00073             }
00074             off -= off2;
00075             sens -= sens2;
00076         }
00077 
00078         return float((((int64_t) getRawPressure() * sens >> 21) - off) >> 15);
00079     }
00080 
00081     float getAltitude(int presssure = 0) {
00082         return toAltitude(presssure ? presssure : (int) getPressure());
00083     }
00084 
00085 protected:
00086     int32_t c1, c2, c3, c4, c5, c6;
00087 
00088     enum {
00089         RESET     = 0x1E,
00090         ADC_READ  = 0x00,
00091         ADC_CONV  = 0x40,
00092         ADC_D1    = 0x00,
00093         ADC_D2    = 0x10,
00094         OSR_256   = 0x00,
00095         OSR_512   = 0x02,
00096         OSR_1024  = 0x04,
00097         OSR_2048  = 0x06,
00098         OSR_4096  = 0x08,
00099         PROM_READ = 0xA0
00100     };
00101 
00102     virtual void writeCommand(int command, int ms = 0) = 0;
00103     virtual int readPROM(int address) = 0;
00104     virtual int readADC(int command) = 0;
00105 
00106     void init() {
00107         writeCommand(RESET, 3);
00108         c1 = readPROM(1);
00109         c2 = readPROM(2);
00110         c3 = readPROM(3);
00111         c4 = readPROM(4);
00112         c5 = readPROM(5);
00113         c6 = readPROM(6);
00114     }
00115 
00116     float toAltitude(int pressure) {
00117         // Ref. 29124-AltimeterAppNote1.pdf
00118         const float R = 287.052; // specific gas constant R*/M0
00119         const float g = 9.80665; // standard gravity 
00120         const float t_grad = 0.0065; // gradient of temperature
00121         const float t0 = 273.15 + 15; // temperature at 0 altitude
00122         const float p0 = 101325; // pressure at 0 altitude
00123 
00124         return t0 / t_grad * (1 - exp((t_grad * R / g) * log(pressure / p0)));
00125     }
00126 };
00127 
00128 #endif