Version modifié Polytech Paris Sud correction erreur de calcul sur la lib initiale

Dependents:   MS5607modele

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MS5607Base.h Source File

MS5607Base.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 #ifndef MS5607_BASE_H
00024 #define MS5607_BASE_H
00025 
00026 class MS5607Base {
00027 public:
00028     void init(float itemp=0,float ipress=0) {
00029         writeCommand(RESET, 3);
00030         c1 = readPROM(1);
00031         c2 = readPROM(2);
00032         c3 = readPROM(3);
00033         c4 = readPROM(4);
00034         c5 = readPROM(5);
00035         c6 = readPROM(6);
00036         inittemp = itemp;
00037         initpress= ipress;
00038     }
00039     
00040     void printCoefficients() {
00041         printf("%d, %d, %d, %d, %d, %d\n", c1, c2, c3, c4, c5, c6);
00042     }
00043 
00044     int getRawTemperature() {
00045         return readADC(ADC_D2 | OSR_4096);
00046     }
00047 
00048     int getRawPressure() {
00049         return readADC(ADC_D1 | OSR_4096);
00050     }
00051 
00052     float getTemperature() {
00053         int dT = getRawTemperature() - ((0x0FFFF&c5) << 8);
00054         int temp = 2000 + (((int64_t)dT * (0x0FFFF&c6)) >> 23);
00055         
00056         // 2nd order temperature compensation
00057         if (temp < 2000) {
00058             int t2 = ((int64_t) dT * dT )>> 31;
00059             temp -= t2;
00060         }
00061 
00062         return float(temp) / 100;
00063     }
00064 
00065     float getPressure() {
00066         int dT = getRawTemperature() - (c5 << 8);
00067         int temp = 2000 + ((dT * c6) >> 23);
00068         int64_t off = ((int64_t) c2 << 17) + ((int64_t) dT * c4 >> 6);
00069         int64_t sens = ((int64_t) c1 << 16) + ((int64_t) dT * c3 >> 7);
00070 
00071         // 2nd order temperature compensation
00072         if (temp < 2000) {
00073             int64_t off2 = (int64_t) 61 * (temp - 2000) * (temp - 2000) >> 4;
00074             int64_t sens2 = (int64_t) 2 * (temp - 2000) * (temp - 2000);
00075             if (temp < -1500) {
00076                 off2 += (int64_t) 15 * (temp + 1500) * (temp + 1500);
00077                 sens2 += (int64_t) 8 * (temp + 1500) * (temp + 1500);
00078             }
00079             off -= off2;
00080             sens -= sens2;
00081         }
00082 
00083         return float((((int64_t) getRawPressure() * sens >> 21) - off) >> 15);
00084     }
00085 
00086     float getAltitude(int presssure = 0) {
00087         return toAltitude(presssure ? presssure : (int) getPressure());
00088     }
00089 
00090 protected:
00091     int32_t c1, c2, c3, c4, c5, c6;
00092     float inittemp, initpress;
00093     enum {
00094         RESET     = 0x1E,
00095         ADC_READ  = 0x00,
00096         ADC_CONV  = 0x40,
00097         ADC_D1    = 0x00,
00098         ADC_D2    = 0x10,
00099         OSR_256   = 0x00,
00100         OSR_512   = 0x02,
00101         OSR_1024  = 0x04,
00102         OSR_2048  = 0x06,
00103         OSR_4096  = 0x08,
00104         PROM_READ = 0xA0
00105     };
00106 
00107     virtual void writeCommand(int command, int ms = 0) = 0;
00108     virtual int readPROM(int address) = 0;
00109     virtual int readADC(int command) = 0;
00110 
00111 
00112 
00113     float toAltitude(int pressure) {
00114         // Ref. 29124-AltimeterAppNote1.pdf
00115         const float R = 287.052; // specific gas constant R*/M0
00116         const float g = 9.80665; // standard gravity 
00117         const float t_grad = 0.0065; // gradient of temperature
00118         if(inittemp==0) inittemp=15;
00119         if(initpress==0) initpress=101325;
00120         float t0 = 273.15 + inittemp; // temperature at 0 altitude
00121         float p0 = initpress;// 101700; // pressure at 0 altitude
00122 
00123         return t0 / t_grad * (1 - exp((t_grad * R / g) * log(pressure / p0)));
00124     }
00125 };
00126 
00127 #endif