Skip to content

Commit

Permalink
add compensation + test ROM in reset (#28)
Browse files Browse the repository at this point in the history
* add compensation + test ROM in reset
  • Loading branch information
RobTillaart authored Jan 25, 2022
1 parent 46d800c commit 23f03ca
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 40 deletions.
63 changes: 38 additions & 25 deletions MS5611.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,6 +76,7 @@ MS5611::MS5611(uint8_t deviceAddress)
_deviceID = 0;
_pressureOffset = 0;
_temperatureOffset = 0;
_compensation = true;
}


Expand All @@ -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

Expand All @@ -104,8 +106,7 @@ bool MS5611::begin(TwoWire * wire)
_wire->begin();
if (! isConnected()) return false;

reset();
return true;
return reset();
}


Expand All @@ -120,7 +121,7 @@ bool MS5611::isConnected()
}


void MS5611::reset()
bool MS5611::reset()
{
command(MS5611_CMD_RESET);
uint32_t start = micros();
Expand All @@ -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.
Expand All @@ -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;
}


Expand All @@ -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];
Expand All @@ -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;

Expand Down
11 changes: 8 additions & 3 deletions MS5611.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() !
Expand All @@ -121,6 +125,7 @@ class MS5611
float C[7];
uint32_t _lastRead;
uint32_t _deviceID;
bool _compensation;

TwoWire * _wire;
};
Expand Down
37 changes: 30 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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

Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -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?

2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=MS5611
version=0.3.7
version=0.3.8
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for MS5611 temperature and pressure sensor
Expand Down
6 changes: 3 additions & 3 deletions test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -85,7 +85,7 @@ unittest(test_read_sensor)
{
MS5611 sensor(0x77);

assertTrue(sensor.begin());
assertFalse(sensor.begin());

assureEqual(MS5611_READ_OK, sensor.read());

Expand All @@ -101,7 +101,7 @@ unittest(test_overSampling)
{
MS5611 sensor(0x77);

assertTrue(sensor.begin());
assertFalse(sensor.begin());

// default
assureEqual(OSR_ULTRA_LOW, sensor.getOversampling());
Expand Down

0 comments on commit 23f03ca

Please sign in to comment.