Merged with fix/ms5607 branch and faster implementation

This commit is contained in:
dario 2024-05-16 22:12:29 +02:00
commit cdb9b4a1d2
2 changed files with 52 additions and 72 deletions

View File

@ -3,6 +3,7 @@
#include <sta/bus/spi/device.hpp> #include <sta/bus/spi/device.hpp>
#include <sta/bus/i2c/device.hpp> #include <sta/bus/i2c/device.hpp>
#include <sta/time.hpp>
#include <sta/endian.hpp> #include <sta/endian.hpp>
@ -115,6 +116,8 @@ namespace sta
*/ */
bool init(); bool init();
void delay();
/** /**
* @brief Set the oversampling rate. * @brief Set the oversampling rate.
* *
@ -128,14 +131,17 @@ namespace sta
* @param type * @param type
* @param blocking * @param blocking
*/ */
void requestData(DataType type, bool blocking = false); void requestData(DataType type);
bool hasData(DataType type); void requestPressure();
void requestTemperature();
/** /**
* @brief Reads the current pressure value from the sensor. Obtains the temperature value from the interal sensor. * @brief Reads the current pressure value from the sensor. Obtains the temperature value from the interal sensor.
* *
* @param unit Specifies the unit for the pressure measurement. Default is hPa. * @param unit Specifies the unit for the pressure measurement. Default is hPa.
* @param cachedTemp Specifies if a cached temperature value should be used.
* @return int32_t The measured value in the specified unit. * @return int32_t The measured value in the specified unit.
*/ */
float getPressure(Unit unit = Unit::hPa); float getPressure(Unit unit = Unit::hPa);
@ -146,7 +152,7 @@ namespace sta
* @return int32_t The measured temperature. * @return int32_t The measured temperature.
* @note There are better sensors for temperature measurements than the MS56xx. * @note There are better sensors for temperature measurements than the MS56xx.
*/ */
int32_t getTemperature(); float getTemperature();
/** /**
* @brief Provide a reference pressure value at a reference altitude in order to estimate the sealevel pressure. * @brief Provide a reference pressure value at a reference altitude in order to estimate the sealevel pressure.
@ -182,7 +188,7 @@ namespace sta
* @note This code was taken from https://github.com/RobTillaart/MS5611 * @note This code was taken from https://github.com/RobTillaart/MS5611
* *
*/ */
void initConstants(bool mathMode = 0); void initConstants();
/** /**
* @brief Convert the pressure value given in Pa to a different unit. * @brief Convert the pressure value given in Pa to a different unit.
@ -233,6 +239,11 @@ namespace sta
OsrLevel osr_; OsrLevel osr_;
Intf intf_; Intf intf_;
float dT_;
float dTInit_;
bool pressReq_ = false;
bool tempReq_ = false;
// Pressure at sealevel. Use the standard atmosphere per default. // Pressure at sealevel. Use the standard atmosphere per default.
float sealevel_ = 1013.25; float sealevel_ = 1013.25;

View File

@ -42,14 +42,9 @@ namespace sta
return true; return true;
} }
void MS56xx::requestData() void MS56xx::delay()
{ {
delay_(osrDelay());
}
bool MS56xx::hasData()
{
} }
void MS56xx::setOsr(OsrLevel osr) void MS56xx::setOsr(OsrLevel osr)
@ -63,67 +58,49 @@ namespace sta
delay_(MS56xx::RESET_DELAY); delay_(MS56xx::RESET_DELAY);
} }
void MS56xx::requestData(DataType type, bool blocking = false) void MS56xx::requestData(DataType type)
{ {
Operations op = type == DataType::PRESSURE ? D1_CONVERSION : D2_CONVERSION; Operations op = type == DataType::PRESSURE ? D1_CONVERSION : D2_CONVERSION;
// Request the ADC to read new data. // Request the ADC to read new data.
busCommand(op + 2*this->osr_); busCommand(op + 2*this->osr_);
}
if (blocking) void MS56xx::requestPressure()
{ {
delay_(osrDelay()); requestData(PRESSURE);
} }
void MS56xx::requestTemperature()
{
requestData(TEMPERATURE);
} }
float MS56xx::getPressure(Unit unit /* = Unit::hPa */) float MS56xx::getPressure(Unit unit /* = Unit::hPa */)
{ {
// Request the ADC to read pressure values.
requestData(PRESSURE, true);
uint8_t buffer[3] = { 0x00, 0x00, 0x00 }; uint8_t buffer[3] = { 0x00, 0x00, 0x00 };
busRead(MS56xx::Operations::ADC_RESULT, buffer, 3); busRead(MS56xx::Operations::ADC_RESULT, buffer, 3);
uint32_t D1 = buffer[0] << 16 | buffer[1] << 8 | buffer[2]; uint32_t D1 = buffer[0] << 16 | buffer[1] << 8 | buffer[2];
// Request the ADC to read temperature values. float offset = C_[2] + dT_ * C_[4];
requestData(PRESSURE, true); float sens = C_[1] + dT_ * C_[3];
busRead(MS56xx::Operations::ADC_RESULT, buffer, 3);
uint32_t D2 = buffer[0] << 16 | buffer[1] << 8 | buffer[2];
float dT = D2 - C_[5];
float temperature = 2000 + dT * C_[6];
float offset = C_[2] + dT * C_[4];
float sens = C_[1] + dT * C_[3];
if (temperature < 2000)
{
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;
}
// The pressure in Pa. // The pressure in Pa.
float pressure = (D1 * sens * 4.76837158205E-7 - offset) * 3.051757813E-5; float pressure = (D1 * sens * 4.76837158205E-7 - offset) * 3.051757813E-5 * 0.01;
// pressure = convertPressure(pressure, unit); // TODO: conversion
return pressure * 0.01; return pressure;
} }
int32_t MS56xx::getTemperature() { float MS56xx::getTemperature()
return 21.0f; {
uint8_t buffer[3] = { 0x00, 0x00, 0x00 };
busRead(MS56xx::Operations::ADC_RESULT, buffer, 3);
uint32_t D2 = buffer[0] << 16 | buffer[1] << 8 | buffer[2];
dT_ = D2 - C_[5];
return 2000 + dT_ * C_[6] * 0.01;
} }
void MS56xx::setPressureReference(float pressRef, float altRef, Unit unit /* = Unit::hPa */) void MS56xx::setPressureReference(float pressRef, float altRef, Unit unit /* = Unit::hPa */)
@ -153,23 +130,15 @@ namespace sta
} }
} }
void MS56xx::initConstants(bool mathMode /* = 0 */) void MS56xx::initConstants()
{ {
C_[0] = 1; C_[0] = 1;
C_[1] = 32768L; // SENSt1 = C[1] * 2^15 | * 2^16 C_[1] = 65536L;
C_[2] = 65536L; // OFFt1 = C[2] * 2^16 | * 2^17 C_[2] = 131072;
C_[3] = 3.90625E-3; // TCS = C[3] / 2^8 | / 2^7 C_[3] = 7.8125E-3;
C_[4] = 7.8125E-3; // TCO = C[4] / 2^7 | / 2^6 C_[4] = 0.015625;
C_[5] = 256; // Tref = C[5] * 2^8 | * 2^8 C_[5] = 256;
C_[6] = 1.1920928955E-7; // TEMPSENS = C[6] / 2^23 | / 2^23 C_[6] = 1.1920928955E-7;
if (mathMode == 1) // Appnote version for pressure.
{
C_[1] = 65536L; // SENSt1
C_[2] = 131072L; // OFFt1
C_[3] = 7.8125E-3; // TCS
C_[4] = 1.5625e-2; // TCO
}
} }
inline float MS56xx::convertPressure(float pressure, Unit unit) inline float MS56xx::convertPressure(float pressure, Unit unit)