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

Dependents:   MS5607modele

Committer:
bouaziz
Date:
Wed Dec 25 15:12:21 2019 +0000
Revision:
1:b7fffbf481e2
Parent:
0:5760862143d1
TP version 1  ENSA

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yamaguch 0:5760862143d1 1 /*
yamaguch 0:5760862143d1 2 Copyright (c) 2012, Senio Networks, Inc.
yamaguch 0:5760862143d1 3
yamaguch 0:5760862143d1 4 Permission is hereby granted, free of charge, to any person obtaining a copy
yamaguch 0:5760862143d1 5 of this software and associated documentation files (the "Software"), to deal
yamaguch 0:5760862143d1 6 in the Software without restriction, including without limitation the rights
yamaguch 0:5760862143d1 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
yamaguch 0:5760862143d1 8 copies of the Software, and to permit persons to whom the Software is
yamaguch 0:5760862143d1 9 furnished to do so, subject to the following conditions:
yamaguch 0:5760862143d1 10
yamaguch 0:5760862143d1 11 The above copyright notice and this permission notice shall be included in
yamaguch 0:5760862143d1 12 all copies or substantial portions of the Software.
yamaguch 0:5760862143d1 13
yamaguch 0:5760862143d1 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yamaguch 0:5760862143d1 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yamaguch 0:5760862143d1 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
yamaguch 0:5760862143d1 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yamaguch 0:5760862143d1 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yamaguch 0:5760862143d1 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
yamaguch 0:5760862143d1 20 THE SOFTWARE.
yamaguch 0:5760862143d1 21 */
yamaguch 0:5760862143d1 22
yamaguch 0:5760862143d1 23 #ifndef MS5607_BASE_H
yamaguch 0:5760862143d1 24 #define MS5607_BASE_H
yamaguch 0:5760862143d1 25
yamaguch 0:5760862143d1 26 class MS5607Base {
yamaguch 0:5760862143d1 27 public:
bouaziz 1:b7fffbf481e2 28 void init(float itemp=0,float ipress=0) {
bouaziz 1:b7fffbf481e2 29 writeCommand(RESET, 3);
bouaziz 1:b7fffbf481e2 30 c1 = readPROM(1);
bouaziz 1:b7fffbf481e2 31 c2 = readPROM(2);
bouaziz 1:b7fffbf481e2 32 c3 = readPROM(3);
bouaziz 1:b7fffbf481e2 33 c4 = readPROM(4);
bouaziz 1:b7fffbf481e2 34 c5 = readPROM(5);
bouaziz 1:b7fffbf481e2 35 c6 = readPROM(6);
bouaziz 1:b7fffbf481e2 36 inittemp = itemp;
bouaziz 1:b7fffbf481e2 37 initpress= ipress;
bouaziz 1:b7fffbf481e2 38 }
bouaziz 1:b7fffbf481e2 39
yamaguch 0:5760862143d1 40 void printCoefficients() {
yamaguch 0:5760862143d1 41 printf("%d, %d, %d, %d, %d, %d\n", c1, c2, c3, c4, c5, c6);
yamaguch 0:5760862143d1 42 }
yamaguch 0:5760862143d1 43
yamaguch 0:5760862143d1 44 int getRawTemperature() {
yamaguch 0:5760862143d1 45 return readADC(ADC_D2 | OSR_4096);
yamaguch 0:5760862143d1 46 }
yamaguch 0:5760862143d1 47
yamaguch 0:5760862143d1 48 int getRawPressure() {
yamaguch 0:5760862143d1 49 return readADC(ADC_D1 | OSR_4096);
yamaguch 0:5760862143d1 50 }
yamaguch 0:5760862143d1 51
yamaguch 0:5760862143d1 52 float getTemperature() {
bouaziz 1:b7fffbf481e2 53 int dT = getRawTemperature() - ((0x0FFFF&c5) << 8);
bouaziz 1:b7fffbf481e2 54 int temp = 2000 + (((int64_t)dT * (0x0FFFF&c6)) >> 23);
yamaguch 0:5760862143d1 55
yamaguch 0:5760862143d1 56 // 2nd order temperature compensation
yamaguch 0:5760862143d1 57 if (temp < 2000) {
bouaziz 1:b7fffbf481e2 58 int t2 = ((int64_t) dT * dT )>> 31;
yamaguch 0:5760862143d1 59 temp -= t2;
yamaguch 0:5760862143d1 60 }
yamaguch 0:5760862143d1 61
yamaguch 0:5760862143d1 62 return float(temp) / 100;
yamaguch 0:5760862143d1 63 }
yamaguch 0:5760862143d1 64
yamaguch 0:5760862143d1 65 float getPressure() {
yamaguch 0:5760862143d1 66 int dT = getRawTemperature() - (c5 << 8);
yamaguch 0:5760862143d1 67 int temp = 2000 + ((dT * c6) >> 23);
yamaguch 0:5760862143d1 68 int64_t off = ((int64_t) c2 << 17) + ((int64_t) dT * c4 >> 6);
yamaguch 0:5760862143d1 69 int64_t sens = ((int64_t) c1 << 16) + ((int64_t) dT * c3 >> 7);
yamaguch 0:5760862143d1 70
yamaguch 0:5760862143d1 71 // 2nd order temperature compensation
yamaguch 0:5760862143d1 72 if (temp < 2000) {
yamaguch 0:5760862143d1 73 int64_t off2 = (int64_t) 61 * (temp - 2000) * (temp - 2000) >> 4;
yamaguch 0:5760862143d1 74 int64_t sens2 = (int64_t) 2 * (temp - 2000) * (temp - 2000);
yamaguch 0:5760862143d1 75 if (temp < -1500) {
yamaguch 0:5760862143d1 76 off2 += (int64_t) 15 * (temp + 1500) * (temp + 1500);
yamaguch 0:5760862143d1 77 sens2 += (int64_t) 8 * (temp + 1500) * (temp + 1500);
yamaguch 0:5760862143d1 78 }
yamaguch 0:5760862143d1 79 off -= off2;
yamaguch 0:5760862143d1 80 sens -= sens2;
yamaguch 0:5760862143d1 81 }
yamaguch 0:5760862143d1 82
yamaguch 0:5760862143d1 83 return float((((int64_t) getRawPressure() * sens >> 21) - off) >> 15);
yamaguch 0:5760862143d1 84 }
yamaguch 0:5760862143d1 85
yamaguch 0:5760862143d1 86 float getAltitude(int presssure = 0) {
yamaguch 0:5760862143d1 87 return toAltitude(presssure ? presssure : (int) getPressure());
yamaguch 0:5760862143d1 88 }
yamaguch 0:5760862143d1 89
yamaguch 0:5760862143d1 90 protected:
yamaguch 0:5760862143d1 91 int32_t c1, c2, c3, c4, c5, c6;
bouaziz 1:b7fffbf481e2 92 float inittemp, initpress;
yamaguch 0:5760862143d1 93 enum {
yamaguch 0:5760862143d1 94 RESET = 0x1E,
yamaguch 0:5760862143d1 95 ADC_READ = 0x00,
yamaguch 0:5760862143d1 96 ADC_CONV = 0x40,
yamaguch 0:5760862143d1 97 ADC_D1 = 0x00,
yamaguch 0:5760862143d1 98 ADC_D2 = 0x10,
yamaguch 0:5760862143d1 99 OSR_256 = 0x00,
yamaguch 0:5760862143d1 100 OSR_512 = 0x02,
yamaguch 0:5760862143d1 101 OSR_1024 = 0x04,
yamaguch 0:5760862143d1 102 OSR_2048 = 0x06,
yamaguch 0:5760862143d1 103 OSR_4096 = 0x08,
yamaguch 0:5760862143d1 104 PROM_READ = 0xA0
yamaguch 0:5760862143d1 105 };
yamaguch 0:5760862143d1 106
yamaguch 0:5760862143d1 107 virtual void writeCommand(int command, int ms = 0) = 0;
yamaguch 0:5760862143d1 108 virtual int readPROM(int address) = 0;
yamaguch 0:5760862143d1 109 virtual int readADC(int command) = 0;
yamaguch 0:5760862143d1 110
bouaziz 1:b7fffbf481e2 111
yamaguch 0:5760862143d1 112
yamaguch 0:5760862143d1 113 float toAltitude(int pressure) {
yamaguch 0:5760862143d1 114 // Ref. 29124-AltimeterAppNote1.pdf
yamaguch 0:5760862143d1 115 const float R = 287.052; // specific gas constant R*/M0
yamaguch 0:5760862143d1 116 const float g = 9.80665; // standard gravity
yamaguch 0:5760862143d1 117 const float t_grad = 0.0065; // gradient of temperature
bouaziz 1:b7fffbf481e2 118 if(inittemp==0) inittemp=15;
bouaziz 1:b7fffbf481e2 119 if(initpress==0) initpress=101325;
bouaziz 1:b7fffbf481e2 120 float t0 = 273.15 + inittemp; // temperature at 0 altitude
bouaziz 1:b7fffbf481e2 121 float p0 = initpress;// 101700; // pressure at 0 altitude
yamaguch 0:5760862143d1 122
yamaguch 0:5760862143d1 123 return t0 / t_grad * (1 - exp((t_grad * R / g) * log(pressure / p0)));
yamaguch 0:5760862143d1 124 }
yamaguch 0:5760862143d1 125 };
yamaguch 0:5760862143d1 126
yamaguch 0:5760862143d1 127 #endif