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