diff --git a/MS5611.cpp b/MS5611.cpp index 2d95a4d..f23f5f0 100644 --- a/MS5611.cpp +++ b/MS5611.cpp @@ -2,11 +2,13 @@ // FILE: MS5611.cpp // AUTHOR: Rob Tillaart // Erni - testing/fixes -// VERSION: 0.3.7 +// VERSION: 0.3.8 // PURPOSE: MS5611 Temperature & Humidity library for Arduino // URL: https://github.com/RobTillaart/MS5611 // // HISTORY: +// 0.3.8 2022-01-24 reset() returns bool +// get/setCompensation() // 0.3.7 2022-01-22 fix #26 added getPromHash() // fix #24 default all examples address 0x77 // 0.3.6 2022-01-15 add setOffset functions; minor refactor; @@ -74,6 +76,7 @@ MS5611::MS5611(uint8_t deviceAddress) _deviceID = 0; _pressureOffset = 0; _temperatureOffset = 0; + _compensation = true; } @@ -91,8 +94,7 @@ bool MS5611::begin(uint8_t dataPin, uint8_t clockPin, TwoWire * wire) } if (! isConnected()) return false; - reset(); - return true; + return reset(); } #endif @@ -104,8 +106,7 @@ bool MS5611::begin(TwoWire * wire) _wire->begin(); if (! isConnected()) return false; - reset(); - return true; + return reset(); } @@ -120,7 +121,7 @@ bool MS5611::isConnected() } -void MS5611::reset() +bool MS5611::reset() { command(MS5611_CMD_RESET); uint32_t start = micros(); @@ -140,6 +141,7 @@ void MS5611::reset() C[5] = 256; // Tref = C[5] * 2^8 C[6] = 1.1920928955E-7; // TEMPSENS = C[6] / 2^23 // read factory calibrations from EEPROM. + bool ROM_OK = true; for (uint8_t reg = 0; reg < 7; reg++) { // used indices match datasheet. @@ -151,7 +153,12 @@ void MS5611::reset() _deviceID <<= 4; _deviceID ^= tmp; // Serial.println(readProm(reg)); + if (reg > 0) + { + ROM_OK = ROM_OK && (tmp != 0); + } } + return ROM_OK; } @@ -171,9 +178,12 @@ int MS5611::read(uint8_t bits) uint32_t _D2 = readADC(); if (_result) return _result; + // Serial.println(_D1); + // Serial.println(_D2); + // TEST VALUES - comment lines above - // uint32_t D1 = 9085466; - // uint32_t D2 = 8569150; + // uint32_t _D1 = 9085466; + // uint32_t _D2 = 8569150; // TEMP & PRESS MATH - PAGE 7/20 float dT = _D2 - C[5]; @@ -182,27 +192,30 @@ int MS5611::read(uint8_t bits) float offset = C[2] + dT * C[4]; float sens = C[1] + dT * C[3]; - // SECOND ORDER COMPENSATION - PAGE 8/20 - // COMMENT OUT < 2000 CORRECTION IF NOT NEEDED - // NOTE TEMPERATURE IS IN 0.01 C - if (_temperature < 2000) + if (_compensation) { - float T2 = dT * dT * 4.6566128731E-10; - float t = (_temperature - 2000) * (_temperature - 2000); - float offset2 = 2.5 * t; - float sens2 = 1.25 * t; - // COMMENT OUT < -1500 CORRECTION IF NOT NEEDED - if (_temperature < -1500) + // SECOND ORDER COMPENSATION - PAGE 8/20 + // COMMENT OUT < 2000 CORRECTION IF NOT NEEDED + // NOTE TEMPERATURE IS IN 0.01 C + if (_temperature < 2000) { - t = (_temperature + 1500) * (_temperature + 1500); - offset2 += 7 * t; - sens2 += 5.5 * t; + float T2 = dT * dT * 4.6566128731E-10; + float t = (_temperature - 2000) * (_temperature - 2000); + float offset2 = 2.5 * t; + float sens2 = 1.25 * t; + // COMMENT OUT < -1500 CORRECTION IF NOT NEEDED + if (_temperature < -1500) + { + t = (_temperature + 1500) * (_temperature + 1500); + offset2 += 7 * t; + sens2 += 5.5 * t; + } + _temperature -= T2; + offset -= offset2; + sens -= sens2; } - _temperature -= T2; - offset -= offset2; - sens -= sens2; + // END SECOND ORDER COMPENSATION } - // END SECOND ORDER COMPENSATION _pressure = (_D1 * sens * 4.76837158205E-7 - offset) * 3.051757813E-5; diff --git a/MS5611.h b/MS5611.h index e165657..07e7dd1 100644 --- a/MS5611.h +++ b/MS5611.h @@ -3,7 +3,7 @@ // FILE: MS5611.h // AUTHOR: Rob Tillaart // Erni - testing/fixes -// VERSION: 0.3.7 +// VERSION: 0.3.8 // PURPOSE: Arduino library for MS5611 temperature and pressure sensor // URL: https://github.com/RobTillaart/MS5611 @@ -30,7 +30,7 @@ // CS to GND ==> 0x77 -#define MS5611_LIB_VERSION (F("0.3.7")) +#define MS5611_LIB_VERSION (F("0.3.8")) #ifndef MS5611_DEFAULT_ADDRESS #define MS5611_DEFAULT_ADDRESS 0x77 @@ -63,7 +63,8 @@ class MS5611 bool isConnected(); // reset command + get constants - void reset(); + // returns false if ROM constants == 0; + bool reset(); // the actual reading of the sensor; // returns MS5611_READ_OK upon success @@ -97,6 +98,9 @@ class MS5611 uint32_t getDeviceID() const { return _deviceID; }; + void setCompensation(bool flag = true) { _compensation = flag; }; + bool getCompensation() { return _compensation; }; + // develop functions. /* void setAddress(uint8_t address) { _address = address; }; // RANGE CHECK + isConnected() ! @@ -121,6 +125,7 @@ class MS5611 float C[7]; uint32_t _lastRead; uint32_t _deviceID; + bool _compensation; TwoWire * _wire; }; diff --git a/README.md b/README.md index ed666b8..6ee1d28 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ An experimental SPI version of the library can be found here - https://github.com/RobTillaart/MS5611_SPI -#### breakout +#### Breakout GY-63 ```cpp // @@ -48,7 +48,7 @@ An experimental SPI version of the library can be found here // ``` -#### related libraries +#### Related libraries For pressure conversions see - https://github.com/RobTillaart/pressure @@ -88,17 +88,28 @@ MS5611_DEFAULT_ADDRESS upon uniqueness of the factory calibration values. +#### 0.3.8 + +- reset() returns bool indicating succesful ROM read +- get/setCompensation() to enable/disable compensation. + + ## Interface #### Base - **MS5611(uint8_t deviceAddress = MS5611_DEFAULT_ADDRESS)** constructor. Since 0.3.7 a default address 0x77 is added. -- **bool begin(uint8_t sda, uint8_t scl, TwoWire \*wire = &Wire)** for ESP and alike, optionally set Wire interface. initializes internals, -- **bool begin(TwoWire \*wire = &Wire)** for UNO and alike, optionally set Wire interface. Initializes internals. +- **bool begin(uint8_t sda, uint8_t scl, TwoWire \*wire = &Wire)** for ESP and alike, optionally set Wire interface. +Initializes internals by calling reset(). +Return false indicates either isConnected() error or reset() error. +- **bool begin(TwoWire \*wire = &Wire)** for UNO and alike, optionally set Wire interface. +Initializes internals by calling reset(). +Return false indicates either isConnected() error or reset() error. - **bool isConnected()** checks availability of device address on the I2C bus. (see note above NANO 33 BLE). -- **reset()** resets the chip and loads constants from its ROM. +- **bool reset()** resets the chip and loads constants from its ROM. +Returns false if ROM could not be read. - **int read(uint8_t bits)** the actual reading of the sensor. Number of bits determines the oversampling factor. Returns MS5611_READ_OK upon success. - **int read()** wraps the **read()** above, uses the preset oversampling (see below). @@ -149,6 +160,10 @@ Default the offset is set to 0. - **int getLastResult()** checks last I2C communication. Replace with more informative error handling? - **uint32_t lastRead()** last time when **read()** was called in milliseconds since startup. + + +#### DeviceID + - **uint32_t getDeviceID()** returns the hashed values of the calibration PROM. As these calibration are set in the factory and differ (enough) per sensor these can serve as an unique deviceID. @@ -161,6 +176,14 @@ Having a device-ID can be used in many ways: Note: this is not an official ID from the device / datasheet, it is made up from calibration data. +#### 2nd order pressure compensation + +- **setCompensation(bool flag = true)** to enable/desiable the 2nd order compensation. +The default = true. +Disabling the compensation will be slightly faster but you loose precision. +- **getCompensation()** returns flag set above. + + ## Operation See examples @@ -170,7 +193,7 @@ See examples - update documentation - separate release notes? -- proper error handling +- proper error handling. - redo lower level functions? - handle the read + math of temperature first? -- flag to enable / disable the compensation part? + diff --git a/library.json b/library.json index d360a23..75ff4c6 100644 --- a/library.json +++ b/library.json @@ -18,7 +18,7 @@ "type": "git", "url": "https://github.com/RobTillaart/MS5611.git" }, - "version": "0.3.7", + "version": "0.3.8", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/library.properties b/library.properties index 5580225..493f22c 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=MS5611 -version=0.3.7 +version=0.3.8 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for MS5611 temperature and pressure sensor diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp index a4380d2..f2de74b 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -72,7 +72,7 @@ unittest(test_constants) unittest(test_constructor) { MS5611 sensor(0x77); - assertTrue(sensor.begin()); + assertFalse(sensor.begin()); // as there is no sensor, and no ROM values. assertEqualFloat(-9.99, sensor.getTemperature(), 0.01); assertEqualFloat(-9.99, sensor.getPressure(), 0.01); @@ -85,7 +85,7 @@ unittest(test_read_sensor) { MS5611 sensor(0x77); - assertTrue(sensor.begin()); + assertFalse(sensor.begin()); assureEqual(MS5611_READ_OK, sensor.read()); @@ -101,7 +101,7 @@ unittest(test_overSampling) { MS5611 sensor(0x77); - assertTrue(sensor.begin()); + assertFalse(sensor.begin()); // default assureEqual(OSR_ULTRA_LOW, sensor.getOversampling());