From 3feffc3420b41b9fc5873fd7371724f2f6d05fef Mon Sep 17 00:00:00 2001 From: RedshiftVelocities <43499473+RedshiftVelocities@users.noreply.github.com> Date: Sun, 17 Oct 2021 10:53:30 -0600 Subject: [PATCH 1/7] Update MS5837.cpp --- src/MS5837.cpp | 68 +++++++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/src/MS5837.cpp b/src/MS5837.cpp index 3431e81..f7ffeef 100644 --- a/src/MS5837.cpp +++ b/src/MS5837.cpp @@ -96,45 +96,57 @@ void MS5837::setFluidDensity(float density) { } void MS5837::read() { + static uint32_t pressTimer = 0; + static uint32_t tempTimer = 0; + //Check that _i2cPort is not NULL (i.e. has the user forgoten to call .init or .begin?) if (_i2cPort == NULL) { return; } + + if(millis() - tempTimer >= 20) { + //get requested D2 numbers + _i2cPort->beginTransmission(MS5837_ADDR); + _i2cPort->write(MS5837_ADC_READ); + _i2cPort->endTransmission(); - // Request D1 conversion - _i2cPort->beginTransmission(MS5837_ADDR); - _i2cPort->write(MS5837_CONVERT_D1_8192); - _i2cPort->endTransmission(); - - delay(20); // Max conversion time per datasheet - - _i2cPort->beginTransmission(MS5837_ADDR); - _i2cPort->write(MS5837_ADC_READ); - _i2cPort->endTransmission(); + _i2cPort->requestFrom(MS5837_ADDR,3); + D2_temp = 0; + D2_temp = _i2cPort->read(); + D2_temp = (D2_temp << 8) | _i2cPort->read(); + D2_temp = (D2_temp << 8) | _i2cPort->read(); - _i2cPort->requestFrom(MS5837_ADDR,3); - D1_pres = 0; - D1_pres = _i2cPort->read(); - D1_pres = (D1_pres << 8) | _i2cPort->read(); - D1_pres = (D1_pres << 8) | _i2cPort->read(); + // Request D1 conversion + _i2cPort->beginTransmission(MS5837_ADDR); + _i2cPort->write(MS5837_CONVERT_D1_8192); + _i2cPort->endTransmission(); + + pressTimer = millis(); + } - // Request D2 conversion - _i2cPort->beginTransmission(MS5837_ADDR); - _i2cPort->write(MS5837_CONVERT_D2_8192); - _i2cPort->endTransmission(); + //delay(20); // Max conversion time per datasheet + if(millis - pressTimer >= 20) { + //get requested D1 numbers + _i2cPort->beginTransmission(MS5837_ADDR); + _i2cPort->write(MS5837_ADC_READ); + _i2cPort->endTransmission(); - delay(20); // Max conversion time per datasheet + _i2cPort->requestFrom(MS5837_ADDR,3); + D1_pres = 0; + D1_pres = _i2cPort->read(); + D1_pres = (D1_pres << 8) | _i2cPort->read(); + D1_pres = (D1_pres << 8) | _i2cPort->read(); - _i2cPort->beginTransmission(MS5837_ADDR); - _i2cPort->write(MS5837_ADC_READ); - _i2cPort->endTransmission(); + // Request D2 conversion + _i2cPort->beginTransmission(MS5837_ADDR); + _i2cPort->write(MS5837_CONVERT_D2_8192); + _i2cPort->endTransmission(); + + tempTimer = millis(); + } - _i2cPort->requestFrom(MS5837_ADDR,3); - D2_temp = 0; - D2_temp = _i2cPort->read(); - D2_temp = (D2_temp << 8) | _i2cPort->read(); - D2_temp = (D2_temp << 8) | _i2cPort->read(); + //delay(20); // Max conversion time per datasheet calculate(); } From eb0b86b22f08359d8972091ac659c3fea07cc6cc Mon Sep 17 00:00:00 2001 From: RedshiftVelocities <43499473+RedshiftVelocities@users.noreply.github.com> Date: Sun, 17 Oct 2021 11:31:15 -0600 Subject: [PATCH 2/7] Update MS5837.cpp --- src/MS5837.cpp | 60 ++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/src/MS5837.cpp b/src/MS5837.cpp index f7ffeef..ee62f6a 100644 --- a/src/MS5837.cpp +++ b/src/MS5837.cpp @@ -96,8 +96,11 @@ void MS5837::setFluidDensity(float density) { } void MS5837::read() { - static uint32_t pressTimer = 0; - static uint32_t tempTimer = 0; + static uint32_t pressReadStartTime = 0; + static uint32_t tempReadStartTime = 0; + + static bool pressReadNext = true; + bool newData = false; //Check that _i2cPort is not NULL (i.e. has the user forgoten to call .init or .begin?) if (_i2cPort == NULL) @@ -105,50 +108,55 @@ void MS5837::read() { return; } - if(millis() - tempTimer >= 20) { - //get requested D2 numbers + if(millis() - pressReadStartTime > 20 && pressReadNext) { + //get requested D1 numbers _i2cPort->beginTransmission(MS5837_ADDR); _i2cPort->write(MS5837_ADC_READ); _i2cPort->endTransmission(); _i2cPort->requestFrom(MS5837_ADDR,3); - D2_temp = 0; - D2_temp = _i2cPort->read(); - D2_temp = (D2_temp << 8) | _i2cPort->read(); - D2_temp = (D2_temp << 8) | _i2cPort->read(); + D1_pres = 0; + D1_pres = _i2cPort->read(); + D1_pres = (D1_pres << 8) | _i2cPort->read(); + D1_pres = (D1_pres << 8) | _i2cPort->read(); - // Request D1 conversion + // Request D2 conversion _i2cPort->beginTransmission(MS5837_ADDR); - _i2cPort->write(MS5837_CONVERT_D1_8192); + _i2cPort->write(MS5837_CONVERT_D2_8192); _i2cPort->endTransmission(); - pressTimer = millis(); + tempReadStartTime = millis(); + pressReadNext = false; + newData = true; + //Serial.println("read D1, started D2"); } - - //delay(20); // Max conversion time per datasheet - if(millis - pressTimer >= 20) { - //get requested D1 numbers + + if(millis() - tempReadStartTime > 20 && !pressReadNext) { + //get requested D2 numbers _i2cPort->beginTransmission(MS5837_ADDR); _i2cPort->write(MS5837_ADC_READ); _i2cPort->endTransmission(); _i2cPort->requestFrom(MS5837_ADDR,3); - D1_pres = 0; - D1_pres = _i2cPort->read(); - D1_pres = (D1_pres << 8) | _i2cPort->read(); - D1_pres = (D1_pres << 8) | _i2cPort->read(); + D2_temp = 0; + D2_temp = _i2cPort->read(); + D2_temp = (D2_temp << 8) | _i2cPort->read(); + D2_temp = (D2_temp << 8) | _i2cPort->read(); - // Request D2 conversion + // Request D1 conversion _i2cPort->beginTransmission(MS5837_ADDR); - _i2cPort->write(MS5837_CONVERT_D2_8192); + _i2cPort->write(MS5837_CONVERT_D1_8192); _i2cPort->endTransmission(); - tempTimer = millis(); + pressReadStartTime = millis(); + pressReadNext = true; + newData = true; + } + + if(newData) { + calculate(); + newData = false; } - - //delay(20); // Max conversion time per datasheet - - calculate(); } void MS5837::calculate() { From a863ddc9e0afe3756864ca239326a16de7df39ec Mon Sep 17 00:00:00 2001 From: RedshiftVelocities <43499473+RedshiftVelocities@users.noreply.github.com> Date: Sun, 17 Oct 2021 11:49:55 -0600 Subject: [PATCH 3/7] minor tweaks --- examples/MS5837_Example/MS5837_Example.ino | 40 ++++++++++++---------- src/MS5837.cpp | 16 ++++----- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/examples/MS5837_Example/MS5837_Example.ino b/examples/MS5837_Example/MS5837_Example.ino index d0a7ddc..dcd78d8 100644 --- a/examples/MS5837_Example/MS5837_Example.ino +++ b/examples/MS5837_Example/MS5837_Example.ino @@ -69,21 +69,25 @@ void loop() { // Update pressure and temperature readings sensor.read(); - Serial.print("Pressure: "); - Serial.print(sensor.pressure()); - Serial.println(" mbar"); - - Serial.print("Temperature: "); - Serial.print(sensor.temperature()); - Serial.println(" deg C"); - - Serial.print("Depth: "); - Serial.print(sensor.depth()); - Serial.println(" m"); - - Serial.print("Altitude: "); - Serial.print(sensor.altitude()); - Serial.println(" m above mean sea level"); - - delay(1000); -} + static uint32_t lastPrintTime = 0; + + if(millis() - lastPrintTime > 1000) { + Serial.print("Pressure: "); + Serial.print(sensor.pressure()); + Serial.println(" mbar"); + + Serial.print("Temperature: "); + Serial.print(sensor.temperature()); + Serial.println(" deg C"); + + Serial.print("Depth: "); + Serial.print(sensor.depth()); + Serial.println(" m"); + + Serial.print("Altitude: "); + Serial.print(sensor.altitude()); + Serial.println(" m above mean sea level\n"); + + lastPrintTime = millis(); + } +} \ No newline at end of file diff --git a/src/MS5837.cpp b/src/MS5837.cpp index ee62f6a..1dcdbee 100644 --- a/src/MS5837.cpp +++ b/src/MS5837.cpp @@ -100,7 +100,6 @@ void MS5837::read() { static uint32_t tempReadStartTime = 0; static bool pressReadNext = true; - bool newData = false; //Check that _i2cPort is not NULL (i.e. has the user forgoten to call .init or .begin?) if (_i2cPort == NULL) @@ -108,6 +107,7 @@ void MS5837::read() { return; } + //if 20ms have passed since read AND the next reading should be a D1 (pressure) reading, read. if(millis() - pressReadStartTime > 20 && pressReadNext) { //get requested D1 numbers _i2cPort->beginTransmission(MS5837_ADDR); @@ -125,10 +125,10 @@ void MS5837::read() { _i2cPort->write(MS5837_CONVERT_D2_8192); _i2cPort->endTransmission(); - tempReadStartTime = millis(); - pressReadNext = false; - newData = true; - //Serial.println("read D1, started D2"); + tempReadStartTime = millis(); //start a timer for the temperature read + pressReadNext = false; //ensure the next reading is a temperautre reading + + calculate(); //calculate only gets run when necessary } if(millis() - tempReadStartTime > 20 && !pressReadNext) { @@ -150,12 +150,8 @@ void MS5837::read() { pressReadStartTime = millis(); pressReadNext = true; - newData = true; - } - - if(newData) { + calculate(); - newData = false; } } From dac1d517346efa67c7004d08c53138205357959e Mon Sep 17 00:00:00 2001 From: RedshiftVelocities <43499473+RedshiftVelocities@users.noreply.github.com> Date: Sun, 17 Oct 2021 14:34:04 -0600 Subject: [PATCH 4/7] Capitalized long literals for clarity, maneuvered code to better reflect datasheet --- src/MS5837.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/MS5837.cpp b/src/MS5837.cpp index 1dcdbee..f0f1780 100644 --- a/src/MS5837.cpp +++ b/src/MS5837.cpp @@ -168,21 +168,21 @@ void MS5837::calculate() { int64_t OFF2 = 0; int64_t SENS2 = 0; - // Terms called - dT = D2_temp-uint32_t(C[5])*256l; - if ( _model == MS5837_02BA ) { - SENS = int64_t(C[1])*65536l+(int64_t(C[3])*dT)/128l; - OFF = int64_t(C[2])*131072l+(int64_t(C[4])*dT)/64l; - P = (D1_pres*SENS/(2097152l)-OFF)/(32768l); + // Temp calculation + dT = D2_temp-uint32_t(C[5])*256L; + TEMP = 2000L+int64_t(dT)*C[6]/8388608LL; + + //temp-compensated pressure calculation + if ( _model == MS5837_02BA ) { //different calculations for different sensor models + OFF = int64_t(C[2])*131072L+(int64_t(C[4])*dT)/64L; + SENS = int64_t(C[1])*65536L+(int64_t(C[3])*dT)/128L; + P = (D1_pres*SENS/(2097152L)-OFF)/(32768L); } else { - SENS = int64_t(C[1])*32768l+(int64_t(C[3])*dT)/256l; - OFF = int64_t(C[2])*65536l+(int64_t(C[4])*dT)/128l; - P = (D1_pres*SENS/(2097152l)-OFF)/(8192l); + OFF = int64_t(C[2])*65536L+(int64_t(C[4])*dT)/128L; + SENS = int64_t(C[1])*32768L+(int64_t(C[3])*dT)/256L; + P = (D1_pres*SENS/(2097152L)-OFF)/(8192L); } - // Temp conversion - TEMP = 2000l+int64_t(dT)*C[6]/8388608LL; - //Second order compensation if ( _model == MS5837_02BA ) { if((TEMP/100)<20){ //Low temp @@ -196,8 +196,8 @@ void MS5837::calculate() { OFFi = (3*(TEMP-2000)*(TEMP-2000))/2; SENSi = (5*(TEMP-2000)*(TEMP-2000))/8; if((TEMP/100)<-15){ //Very low temp - OFFi = OFFi+7*(TEMP+1500l)*(TEMP+1500l); - SENSi = SENSi+4*(TEMP+1500l)*(TEMP+1500l); + OFFi = OFFi+7*(TEMP+1500L)*(TEMP+1500L); + SENSi = SENSi+4*(TEMP+1500L)*(TEMP+1500L); } } else if((TEMP/100)>=20){ //High temp @@ -213,9 +213,9 @@ void MS5837::calculate() { TEMP = (TEMP-Ti); if ( _model == MS5837_02BA ) { - P = (((D1_pres*SENS2)/2097152l-OFF2)/32768l); + P = (((D1_pres*SENS2)/2097152L-OFF2)/32768L); } else { - P = (((D1_pres*SENS2)/2097152l-OFF2)/8192l); + P = (((D1_pres*SENS2)/2097152L-OFF2)/8192L); } } From f2987402a39bb4aea9448fbc55c2a4f2264a2a08 Mon Sep 17 00:00:00 2001 From: RedshiftVelocities <43499473+RedshiftVelocities@users.noreply.github.com> Date: Sun, 17 Oct 2021 22:07:36 -0600 Subject: [PATCH 5/7] Add timeout code, connection check, and enforce landsharks library for compatibility --- examples/MS5837_Example/MS5837_Example.ino | 2 +- keywords.txt | 2 +- src/MS5837.cpp | 67 ++++++++++++++-------- src/MS5837.h | 8 ++- 4 files changed, 50 insertions(+), 29 deletions(-) diff --git a/examples/MS5837_Example/MS5837_Example.ino b/examples/MS5837_Example/MS5837_Example.ino index dcd78d8..751efaf 100644 --- a/examples/MS5837_Example/MS5837_Example.ino +++ b/examples/MS5837_Example/MS5837_Example.ino @@ -37,7 +37,7 @@ THE SOFTWARE. #include #include "MS5837.h" -MS5837 sensor; +LANDSHARKS_MS5837 sensor; void setup() { diff --git a/keywords.txt b/keywords.txt index e2d3828..b792011 100644 --- a/keywords.txt +++ b/keywords.txt @@ -6,7 +6,7 @@ # Datatypes (KEYWORD1) ####################################### -MS5837 KEYWORD1 +LANDSHARKS_MS5837 KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) diff --git a/src/MS5837.cpp b/src/MS5837.cpp index f0f1780..9503bda 100644 --- a/src/MS5837.cpp +++ b/src/MS5837.cpp @@ -8,29 +8,31 @@ const uint8_t MS5837_PROM_READ = 0xA0; const uint8_t MS5837_CONVERT_D1_8192 = 0x4A; const uint8_t MS5837_CONVERT_D2_8192 = 0x5A; -const float MS5837::Pa = 100.0f; -const float MS5837::bar = 0.001f; -const float MS5837::mbar = 1.0f; +const float LANDSHARKS_MS5837::Pa = 100.0f; +const float LANDSHARKS_MS5837::bar = 0.001f; +const float LANDSHARKS_MS5837::mbar = 1.0f; -const uint8_t MS5837::MS5837_30BA = 0; -const uint8_t MS5837::MS5837_02BA = 1; -const uint8_t MS5837::MS5837_UNRECOGNISED = 255; +const uint8_t LANDSHARKS_MS5837::MS5837_30BA = 0; +const uint8_t LANDSHARKS_MS5837::MS5837_02BA = 1; +const uint8_t LANDSHARKS_MS5837::MS5837_UNRECOGNISED = 255; const uint8_t MS5837_02BA01 = 0x00; // Sensor version: From MS5837_02BA datasheet Version PROM Word 0 const uint8_t MS5837_02BA21 = 0x15; // Sensor version: From MS5837_02BA datasheet Version PROM Word 0 const uint8_t MS5837_30BA26 = 0x1A; // Sensor version: From MS5837_30BA datasheet Version PROM Word 0 -MS5837::MS5837() { +LANDSHARKS_MS5837::MS5837() { fluidDensity = 1029; } -bool MS5837::begin(TwoWire &wirePort) { +bool LANDSHARKS_MS5837::begin(TwoWire &wirePort) { return (init(wirePort)); } -bool MS5837::init(TwoWire &wirePort) { +bool LANDSHARKS_MS5837::init(TwoWire &wirePort) { _i2cPort = &wirePort; //Grab which port the user wants us to use + _i2cPort->setWireTimeout(1000, true); //undocumented feature of Arduino i2c library. Needed to prevent a disconnect from locking up the program. + // Reset the MS5837, per datasheet _i2cPort->beginTransmission(MS5837_ADDR); _i2cPort->write(MS5837_RESET); @@ -83,19 +85,19 @@ bool MS5837::init(TwoWire &wirePort) { return true; } -void MS5837::setModel(uint8_t model) { +void LANDSHARKS_MS5837::setModel(uint8_t model) { _model = model; } -uint8_t MS5837::getModel() { +uint8_t LANDSHARKS_MS5837::getModel() { return (_model); } -void MS5837::setFluidDensity(float density) { +void LANDSHARKS_MS5837::setFluidDensity(float density) { fluidDensity = density; } -void MS5837::read() { +void LANDSHARKS_MS5837::read() { static uint32_t pressReadStartTime = 0; static uint32_t tempReadStartTime = 0; @@ -107,12 +109,16 @@ void MS5837::read() { return; } + connectionGood = true; //if 20ms have passed since read AND the next reading should be a D1 (pressure) reading, read. if(millis() - pressReadStartTime > 20 && pressReadNext) { //get requested D1 numbers _i2cPort->beginTransmission(MS5837_ADDR); _i2cPort->write(MS5837_ADC_READ); - _i2cPort->endTransmission(); + if(_i2cPort->endTransmission() != 0){ + connectionGood = false; + return; + } _i2cPort->requestFrom(MS5837_ADDR,3); D1_pres = 0; @@ -123,7 +129,10 @@ void MS5837::read() { // Request D2 conversion _i2cPort->beginTransmission(MS5837_ADDR); _i2cPort->write(MS5837_CONVERT_D2_8192); - _i2cPort->endTransmission(); + if(_i2cPort->endTransmission() != 0){ + connectionGood = false; + return; + } tempReadStartTime = millis(); //start a timer for the temperature read pressReadNext = false; //ensure the next reading is a temperautre reading @@ -135,7 +144,10 @@ void MS5837::read() { //get requested D2 numbers _i2cPort->beginTransmission(MS5837_ADDR); _i2cPort->write(MS5837_ADC_READ); - _i2cPort->endTransmission(); + if(_i2cPort->endTransmission() != 0){ + connectionGood = false; + return; + } _i2cPort->requestFrom(MS5837_ADDR,3); D2_temp = 0; @@ -146,7 +158,10 @@ void MS5837::read() { // Request D1 conversion _i2cPort->beginTransmission(MS5837_ADDR); _i2cPort->write(MS5837_CONVERT_D1_8192); - _i2cPort->endTransmission(); + if(_i2cPort->endTransmission() != 0){ + connectionGood = false; + return; + } pressReadStartTime = millis(); pressReadNext = true; @@ -155,7 +170,11 @@ void MS5837::read() { } } -void MS5837::calculate() { +bool LANDSHARKS_MS5837::isConnectionGood() { + return connectionGood; +} + +void LANDSHARKS_MS5837::calculate() { // Given C1-C6 and D1, D2, calculated TEMP and P // Do conversion first and then second order temp compensation @@ -219,7 +238,7 @@ void MS5837::calculate() { } } -float MS5837::pressure(float conversion) { +float LANDSHARKS_MS5837::pressure(float conversion) { if ( _model == MS5837_02BA ) { return P*conversion/100.0f; } @@ -228,7 +247,7 @@ float MS5837::pressure(float conversion) { } } -float MS5837::temperature() { +float LANDSHARKS_MS5837::temperature() { return TEMP/100.0f; } @@ -238,16 +257,16 @@ float MS5837::temperature() { // If the atmospheric pressure is not 101300 at the time of reading, the depth reported will be offset // In order to calculate the correct depth, the actual atmospheric pressure should be measured once in air, and // that value should subtracted for subsequent depth calculations. -float MS5837::depth() { - return (pressure(MS5837::Pa)-101300)/(fluidDensity*9.80665); +float LANDSHARKS_MS5837::depth() { + return (pressure(LANDSHARKS_MS5837::Pa)-101300)/(fluidDensity*9.80665); } -float MS5837::altitude() { +float LANDSHARKS_MS5837::altitude() { return (1-pow((pressure()/1013.25),.190284))*145366.45*.3048; } -uint8_t MS5837::crc4(uint16_t n_prom[]) { +uint8_t LANDSHARKS_MS5837::crc4(uint16_t n_prom[]) { uint16_t n_rem = 0; n_prom[0] = ((n_prom[0]) & 0x0FFF); diff --git a/src/MS5837.h b/src/MS5837.h index 77c72f6..994ce7b 100644 --- a/src/MS5837.h +++ b/src/MS5837.h @@ -40,7 +40,7 @@ THE SOFTWARE. #include "Arduino.h" #include -class MS5837 { +class LANDSHARKS_MS5837 { public: static const float Pa; static const float bar; @@ -66,8 +66,8 @@ class MS5837 { */ void setFluidDensity(float density); - /** The read from I2C takes up to 40 ms, so use sparingly is possible. - */ + bool isConnectionGood(); + void read(); /** Pressure returned in mbar or mbar*conversion rate. @@ -97,6 +97,8 @@ class MS5837 { int32_t TEMP; int32_t P; uint8_t _model; + + bool connectionGood; float fluidDensity; From fd4fc4ce8d3d628b5c76a8feb9335e2256ed099e Mon Sep 17 00:00:00 2001 From: RedshiftVelocities <43499473+RedshiftVelocities@users.noreply.github.com> Date: Sun, 17 Oct 2021 22:17:53 -0600 Subject: [PATCH 6/7] Revert "Capitalized long literals for clarity, maneuvered code to better reflect datasheet" This reverts commit dac1d517346efa67c7004d08c53138205357959e. --- src/MS5837.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/MS5837.cpp b/src/MS5837.cpp index 9503bda..1ac449a 100644 --- a/src/MS5837.cpp +++ b/src/MS5837.cpp @@ -187,21 +187,21 @@ void LANDSHARKS_MS5837::calculate() { int64_t OFF2 = 0; int64_t SENS2 = 0; - // Temp calculation - dT = D2_temp-uint32_t(C[5])*256L; - TEMP = 2000L+int64_t(dT)*C[6]/8388608LL; - - //temp-compensated pressure calculation - if ( _model == MS5837_02BA ) { //different calculations for different sensor models - OFF = int64_t(C[2])*131072L+(int64_t(C[4])*dT)/64L; - SENS = int64_t(C[1])*65536L+(int64_t(C[3])*dT)/128L; - P = (D1_pres*SENS/(2097152L)-OFF)/(32768L); + // Terms called + dT = D2_temp-uint32_t(C[5])*256l; + if ( _model == MS5837_02BA ) { + SENS = int64_t(C[1])*65536l+(int64_t(C[3])*dT)/128l; + OFF = int64_t(C[2])*131072l+(int64_t(C[4])*dT)/64l; + P = (D1_pres*SENS/(2097152l)-OFF)/(32768l); } else { - OFF = int64_t(C[2])*65536L+(int64_t(C[4])*dT)/128L; - SENS = int64_t(C[1])*32768L+(int64_t(C[3])*dT)/256L; - P = (D1_pres*SENS/(2097152L)-OFF)/(8192L); + SENS = int64_t(C[1])*32768l+(int64_t(C[3])*dT)/256l; + OFF = int64_t(C[2])*65536l+(int64_t(C[4])*dT)/128l; + P = (D1_pres*SENS/(2097152l)-OFF)/(8192l); } + // Temp conversion + TEMP = 2000l+int64_t(dT)*C[6]/8388608LL; + //Second order compensation if ( _model == MS5837_02BA ) { if((TEMP/100)<20){ //Low temp @@ -215,8 +215,8 @@ void LANDSHARKS_MS5837::calculate() { OFFi = (3*(TEMP-2000)*(TEMP-2000))/2; SENSi = (5*(TEMP-2000)*(TEMP-2000))/8; if((TEMP/100)<-15){ //Very low temp - OFFi = OFFi+7*(TEMP+1500L)*(TEMP+1500L); - SENSi = SENSi+4*(TEMP+1500L)*(TEMP+1500L); + OFFi = OFFi+7*(TEMP+1500l)*(TEMP+1500l); + SENSi = SENSi+4*(TEMP+1500l)*(TEMP+1500l); } } else if((TEMP/100)>=20){ //High temp @@ -232,9 +232,9 @@ void LANDSHARKS_MS5837::calculate() { TEMP = (TEMP-Ti); if ( _model == MS5837_02BA ) { - P = (((D1_pres*SENS2)/2097152L-OFF2)/32768L); + P = (((D1_pres*SENS2)/2097152l-OFF2)/32768l); } else { - P = (((D1_pres*SENS2)/2097152L-OFF2)/8192L); + P = (((D1_pres*SENS2)/2097152l-OFF2)/8192l); } } From b216b6519b7aaa681ac4c3d679e959443992ffee Mon Sep 17 00:00:00 2001 From: RedshiftVelocities <43499473+RedshiftVelocities@users.noreply.github.com> Date: Sun, 17 Oct 2021 22:17:59 -0600 Subject: [PATCH 7/7] Revert "Add timeout code, connection check, and enforce landsharks library for compatibility" This reverts commit f2987402a39bb4aea9448fbc55c2a4f2264a2a08. --- examples/MS5837_Example/MS5837_Example.ino | 2 +- keywords.txt | 2 +- src/MS5837.cpp | 67 ++++++++-------------- src/MS5837.h | 8 +-- 4 files changed, 29 insertions(+), 50 deletions(-) diff --git a/examples/MS5837_Example/MS5837_Example.ino b/examples/MS5837_Example/MS5837_Example.ino index 751efaf..dcd78d8 100644 --- a/examples/MS5837_Example/MS5837_Example.ino +++ b/examples/MS5837_Example/MS5837_Example.ino @@ -37,7 +37,7 @@ THE SOFTWARE. #include #include "MS5837.h" -LANDSHARKS_MS5837 sensor; +MS5837 sensor; void setup() { diff --git a/keywords.txt b/keywords.txt index b792011..e2d3828 100644 --- a/keywords.txt +++ b/keywords.txt @@ -6,7 +6,7 @@ # Datatypes (KEYWORD1) ####################################### -LANDSHARKS_MS5837 KEYWORD1 +MS5837 KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) diff --git a/src/MS5837.cpp b/src/MS5837.cpp index 1ac449a..1dcdbee 100644 --- a/src/MS5837.cpp +++ b/src/MS5837.cpp @@ -8,31 +8,29 @@ const uint8_t MS5837_PROM_READ = 0xA0; const uint8_t MS5837_CONVERT_D1_8192 = 0x4A; const uint8_t MS5837_CONVERT_D2_8192 = 0x5A; -const float LANDSHARKS_MS5837::Pa = 100.0f; -const float LANDSHARKS_MS5837::bar = 0.001f; -const float LANDSHARKS_MS5837::mbar = 1.0f; +const float MS5837::Pa = 100.0f; +const float MS5837::bar = 0.001f; +const float MS5837::mbar = 1.0f; -const uint8_t LANDSHARKS_MS5837::MS5837_30BA = 0; -const uint8_t LANDSHARKS_MS5837::MS5837_02BA = 1; -const uint8_t LANDSHARKS_MS5837::MS5837_UNRECOGNISED = 255; +const uint8_t MS5837::MS5837_30BA = 0; +const uint8_t MS5837::MS5837_02BA = 1; +const uint8_t MS5837::MS5837_UNRECOGNISED = 255; const uint8_t MS5837_02BA01 = 0x00; // Sensor version: From MS5837_02BA datasheet Version PROM Word 0 const uint8_t MS5837_02BA21 = 0x15; // Sensor version: From MS5837_02BA datasheet Version PROM Word 0 const uint8_t MS5837_30BA26 = 0x1A; // Sensor version: From MS5837_30BA datasheet Version PROM Word 0 -LANDSHARKS_MS5837::MS5837() { +MS5837::MS5837() { fluidDensity = 1029; } -bool LANDSHARKS_MS5837::begin(TwoWire &wirePort) { +bool MS5837::begin(TwoWire &wirePort) { return (init(wirePort)); } -bool LANDSHARKS_MS5837::init(TwoWire &wirePort) { +bool MS5837::init(TwoWire &wirePort) { _i2cPort = &wirePort; //Grab which port the user wants us to use - _i2cPort->setWireTimeout(1000, true); //undocumented feature of Arduino i2c library. Needed to prevent a disconnect from locking up the program. - // Reset the MS5837, per datasheet _i2cPort->beginTransmission(MS5837_ADDR); _i2cPort->write(MS5837_RESET); @@ -85,19 +83,19 @@ bool LANDSHARKS_MS5837::init(TwoWire &wirePort) { return true; } -void LANDSHARKS_MS5837::setModel(uint8_t model) { +void MS5837::setModel(uint8_t model) { _model = model; } -uint8_t LANDSHARKS_MS5837::getModel() { +uint8_t MS5837::getModel() { return (_model); } -void LANDSHARKS_MS5837::setFluidDensity(float density) { +void MS5837::setFluidDensity(float density) { fluidDensity = density; } -void LANDSHARKS_MS5837::read() { +void MS5837::read() { static uint32_t pressReadStartTime = 0; static uint32_t tempReadStartTime = 0; @@ -109,16 +107,12 @@ void LANDSHARKS_MS5837::read() { return; } - connectionGood = true; //if 20ms have passed since read AND the next reading should be a D1 (pressure) reading, read. if(millis() - pressReadStartTime > 20 && pressReadNext) { //get requested D1 numbers _i2cPort->beginTransmission(MS5837_ADDR); _i2cPort->write(MS5837_ADC_READ); - if(_i2cPort->endTransmission() != 0){ - connectionGood = false; - return; - } + _i2cPort->endTransmission(); _i2cPort->requestFrom(MS5837_ADDR,3); D1_pres = 0; @@ -129,10 +123,7 @@ void LANDSHARKS_MS5837::read() { // Request D2 conversion _i2cPort->beginTransmission(MS5837_ADDR); _i2cPort->write(MS5837_CONVERT_D2_8192); - if(_i2cPort->endTransmission() != 0){ - connectionGood = false; - return; - } + _i2cPort->endTransmission(); tempReadStartTime = millis(); //start a timer for the temperature read pressReadNext = false; //ensure the next reading is a temperautre reading @@ -144,10 +135,7 @@ void LANDSHARKS_MS5837::read() { //get requested D2 numbers _i2cPort->beginTransmission(MS5837_ADDR); _i2cPort->write(MS5837_ADC_READ); - if(_i2cPort->endTransmission() != 0){ - connectionGood = false; - return; - } + _i2cPort->endTransmission(); _i2cPort->requestFrom(MS5837_ADDR,3); D2_temp = 0; @@ -158,10 +146,7 @@ void LANDSHARKS_MS5837::read() { // Request D1 conversion _i2cPort->beginTransmission(MS5837_ADDR); _i2cPort->write(MS5837_CONVERT_D1_8192); - if(_i2cPort->endTransmission() != 0){ - connectionGood = false; - return; - } + _i2cPort->endTransmission(); pressReadStartTime = millis(); pressReadNext = true; @@ -170,11 +155,7 @@ void LANDSHARKS_MS5837::read() { } } -bool LANDSHARKS_MS5837::isConnectionGood() { - return connectionGood; -} - -void LANDSHARKS_MS5837::calculate() { +void MS5837::calculate() { // Given C1-C6 and D1, D2, calculated TEMP and P // Do conversion first and then second order temp compensation @@ -238,7 +219,7 @@ void LANDSHARKS_MS5837::calculate() { } } -float LANDSHARKS_MS5837::pressure(float conversion) { +float MS5837::pressure(float conversion) { if ( _model == MS5837_02BA ) { return P*conversion/100.0f; } @@ -247,7 +228,7 @@ float LANDSHARKS_MS5837::pressure(float conversion) { } } -float LANDSHARKS_MS5837::temperature() { +float MS5837::temperature() { return TEMP/100.0f; } @@ -257,16 +238,16 @@ float LANDSHARKS_MS5837::temperature() { // If the atmospheric pressure is not 101300 at the time of reading, the depth reported will be offset // In order to calculate the correct depth, the actual atmospheric pressure should be measured once in air, and // that value should subtracted for subsequent depth calculations. -float LANDSHARKS_MS5837::depth() { - return (pressure(LANDSHARKS_MS5837::Pa)-101300)/(fluidDensity*9.80665); +float MS5837::depth() { + return (pressure(MS5837::Pa)-101300)/(fluidDensity*9.80665); } -float LANDSHARKS_MS5837::altitude() { +float MS5837::altitude() { return (1-pow((pressure()/1013.25),.190284))*145366.45*.3048; } -uint8_t LANDSHARKS_MS5837::crc4(uint16_t n_prom[]) { +uint8_t MS5837::crc4(uint16_t n_prom[]) { uint16_t n_rem = 0; n_prom[0] = ((n_prom[0]) & 0x0FFF); diff --git a/src/MS5837.h b/src/MS5837.h index 994ce7b..77c72f6 100644 --- a/src/MS5837.h +++ b/src/MS5837.h @@ -40,7 +40,7 @@ THE SOFTWARE. #include "Arduino.h" #include -class LANDSHARKS_MS5837 { +class MS5837 { public: static const float Pa; static const float bar; @@ -66,8 +66,8 @@ class LANDSHARKS_MS5837 { */ void setFluidDensity(float density); - bool isConnectionGood(); - + /** The read from I2C takes up to 40 ms, so use sparingly is possible. + */ void read(); /** Pressure returned in mbar or mbar*conversion rate. @@ -97,8 +97,6 @@ class LANDSHARKS_MS5837 { int32_t TEMP; int32_t P; uint8_t _model; - - bool connectionGood; float fluidDensity;