Skip to content

Commit

Permalink
DifferentialPressureSensorBase: Complete combination of common code
Browse files Browse the repository at this point in the history
All common code of differential pressure sensors is now collected in
class DifferentialPressureSensorBase.

The specialized drivers MS4515Driver and AMS5915Driver are now very
thin.

Signed-off-by: Kai Horstmann <horstmannkai@hotmail.com>
  • Loading branch information
hor63 committed Feb 15, 2024
1 parent 8dbb7db commit 9c49607
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 336 deletions.
114 changes: 6 additions & 108 deletions src/drivers/AMS5915/AMS5915Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,37 +59,13 @@ AMS5915Driver::AMS5915Driver(
#if defined HAVE_LOG4CXX_H
initLogger();
#endif /* HAVE_LOG4CXX_H */
}


AMS5915Driver::~AMS5915Driver() {}

void AMS5915Driver::fillCalibrationDataParameters () {

if (UnInitVal == pressureBias) {
writeConfigValue(*calibrationDataParameters, pressureBiasCalibrationName, 0.0);
} else {
writeConfigValue(*calibrationDataParameters, pressureBiasCalibrationName, pressureBias);
}

// write the 0-bias only once after the status initialization.
if (statusInitDone) {
useCalibrationDataUpdateFile = false;
}
i2cAddress = AMS5915I2CAddr;

LOG4CXX_DEBUG (logger,__PRETTY_FUNCTION__ << ": Device " << instanceName
<< " Write pressure bias to calibration data = " << pressureBias);
}

void AMS5915Driver::applyCalibrationData(){

double pressureBiasD = pressureBias;
readOrCreateConfigValue(*calibrationDataParameters,pressureBiasCalibrationName,pressureBiasD);
pressureBias = FloatType(pressureBiasD);
LOG4CXX_DEBUG (logger,__PRETTY_FUNCTION__ << ": Device " << instanceName
<< " Read pressure bias from calibration data = " << pressureBias);
}

AMS5915Driver::~AMS5915Driver() {}

void AMS5915Driver::readConfiguration (Properties4CXX::Properties const &configuration) {

Expand Down Expand Up @@ -160,51 +136,6 @@ void AMS5915Driver::readConfiguration (Properties4CXX::Properties const &configu

}

void AMS5915Driver::driverThreadFunction() {

int numRetries = 0;

if (ioPort == nullptr) {
LOG4CXX_ERROR (logger,"No valid I/O port for driver " << getDriverName()
<< ". The driver is not operable");
} else {
while (!getStopDriverThread() && ( errorMaxNumRetries == 0 || numRetries <= errorMaxNumRetries)) {
try {
ioPort->open();
numRetries = 0;
processingMainLoop ();
ioPort->close();
} catch (std::exception const& e) {
numRetries ++;
LOG4CXX_ERROR(logger,"Error in main loop of driver \"" << getDriverName()
<< "\":" << e.what());
ioPort->close();

std::this_thread::sleep_for(errorTimeout);
}
}
}
}

void AMS5915Driver::processingMainLoop() {
auto nextStartConversion = OEVClock::now();

while (!getStopDriverThread()) {

//std::this_thread::sleep_until(nextStartConversion + 60ms);

readoutAMS5915();

// In case that you miss a cycle advance to the next cycle
auto now = OEVClock::now();
do {
nextStartConversion += updateCyle;
} while (nextStartConversion < now);
std::this_thread::sleep_until(nextStartConversion);
}

}

/// Like in the AVR libraries
#define _BV(x) (1<<(x))

Expand All @@ -221,7 +152,7 @@ FloatType AMS5915Driver::convertRegisterPressureToMBar(
return rc;
}

void AMS5915Driver::readoutAMS5915() {
void AMS5915Driver::readoutSensorData() {

uint8_t sensorValues[4];
uint16_t pressureRawVal;
Expand All @@ -242,44 +173,11 @@ void AMS5915Driver::readoutAMS5915() {
<< ", temperatureRawVal = 0x" << std::hex << temperatureRawVal << std::dec << " = " << temperatureRawVal);

// Formula directly taken from data sheet Rev. 3.1, pg. 11
temperatureVal = FloatType(temperatureRawVal) * (200.0f/2048.0f) - 50.0f;

temperatureVal = static_cast<FloatType>(temperatureRawVal) * (200.0f/2048.0f) - 50.0f;
LOG4CXX_DEBUG(logger, __FUNCTION__ << ": temperatureVal = " << temperatureVal);

FloatType pressureVal = convertRegisterPressureToMBar(pressureRawVal);

if (getIsKalmanUpdateRunning()) {

// When the sensor is saturated skip the measurement
if (pressureVal >= pMin && pressureVal <= pMax) {
GliderVarioMainPriv::LockedCurrentStatus lockedStatus(*varioMain);
FloatType &tempLocalC = lockedStatus.getMeasurementVector()->tempLocalC;

if (useTemperatureSensor) {
tempLocalC = temperatureVal;
}

pressureVal -= pressureBias;

FloatType pressureVariance = fabs(pressureVal) * pressureErrorDynFactor + pressureErrorStatic;
pressureVariance = pressureVariance * pressureVariance;
LOG4CXX_TRACE(logger,__FUNCTION__<< ": pressureVariance = " << pressureVariance);

GliderVarioMeasurementUpdater::dynamicPressureUpd(pressureVal, tempLocalC, pressureVariance,
*lockedStatus.getMeasurementVector(), *lockedStatus.getCurrentStatus());
} // if (pressureVal >= pMin && pressureVal <= pMax)
} else {
// When the sensor is saturated reset collecting initial values
if (pressureVal >= pMin && pressureVal <= pMax) {
if (numValidInitValues < NumInitValues) {
initValues[numValidInitValues] = pressureVal;
numValidInitValues ++;
}
} else {
numValidInitValues = 0;
}
}

pressureVal = convertRegisterPressureToMBar(pressureRawVal);
LOG4CXX_DEBUG(logger, __FUNCTION__ << ": pressureVal = " << pressureVal);

}

Expand Down
62 changes: 3 additions & 59 deletions src/drivers/AMS5915/AMS5915Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,10 @@ class AMS5915Driver : public DifferentialPressureSensorBase {

protected:

/** \brief Fill calibration data parameter list; driver specific
*
* \see DriverBase::fillCalibrationDataParameters()
*/
virtual void fillCalibrationDataParameters () override;

/** \brief Driver specific function to apply calibration data to the driver instance
*
* \see DriverBase::applyCalibrationData()
*/
virtual void applyCalibrationData() override;

/** \brief The main worker thread of this driver
*
* \see GliderVarioDriverBase::driverThreadFunction()
*
/**
* \see DifferentialPressureSensorBase::readoutSensorData()
*/
virtual void driverThreadFunction() override;

/** \brief The inner main loop of the driver after the port was opened
*
* Read data from the sensor, process them, and update the Kalman filter.
*/
virtual void processingMainLoop ();

/** \brief Read out the sensor data
*
* Before reading out the data wait for the conversion to complete.
*/
virtual void readoutAMS5915();
virtual void readoutSensorData() override;

/** \brief Convert pressure sensor reading to pressure in mBar
*
Expand All @@ -119,36 +93,6 @@ class AMS5915Driver : public DifferentialPressureSensorBase {
private:


/** \brief Minimum pressure of the defined range in mBar.
*
*/
FloatType pMin = UnInitVal;

/** \brief Maximum pressure of the defined range in mBar
*
*/
FloatType pMax = UnInitVal;

/** \brief Pressure range of the sensor in mBar.
*
* Range is \ref pMax - \ref pMin.
*/
FloatType pressureRange = UnInitVal;

/** \brief Resolution of the sensor in mBar/bit of register reading
*
* Calculated from (pMax - pMin)/(AMS5915PressureRangeMaxCount - AMS5915PressureRangeMinCount) .
*
*/
FloatType pressureResolution = UnInitVal;

/** \brief Static error component of measurements
*
* This value is calculated in readConfiguration() because it only depends on the range.
* \see pressureErrorDynFactor
*/
FloatType pressureErrorStatic = UnInitVal;

};

} /* namespace openEV */
Expand Down
85 changes: 6 additions & 79 deletions src/drivers/MS4515/MS4515Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,42 +54,17 @@ MS4515Driver::MS4515Driver(
char const *description,
char const *instanceName
)
: DifferentialPressureSensorBase {driverName,description,instanceName,MS4515Lib::theOneAndOnly},
i2cAddress {MS4515DOI2CAddr}
: DifferentialPressureSensorBase {driverName,description,instanceName,MS4515Lib::theOneAndOnly}
{
#if defined HAVE_LOG4CXX_H
initLogger();
#endif /* HAVE_LOG4CXX_H */
}


MS4515Driver::~MS4515Driver() {}

void MS4515Driver::fillCalibrationDataParameters () {

if (UnInitVal == pressureBias) {
writeConfigValue(*calibrationDataParameters, pressureBiasCalibrationName, 0.0);
} else {
writeConfigValue(*calibrationDataParameters, pressureBiasCalibrationName, pressureBias);
}

// write the 0-bias only once after the status initialization.
if (statusInitDone) {
useCalibrationDataUpdateFile = false;
}

LOG4CXX_DEBUG (logger,__PRETTY_FUNCTION__ << ": Device " << instanceName
<< " Write pressure bias to calibration data = " << pressureBias);
i2cAddress = MS4515DOI2CAddr;
}

void MS4515Driver::applyCalibrationData(){

double pressureBiasD = pressureBias;
readOrCreateConfigValue(*calibrationDataParameters,pressureBiasCalibrationName,pressureBiasD);
pressureBias = FloatType(pressureBiasD);
LOG4CXX_DEBUG (logger,__PRETTY_FUNCTION__ << ": Device " << instanceName
<< " Read pressure bias from calibration data = " << pressureBias);
}
MS4515Driver::~MS4515Driver() {}

void MS4515Driver::readConfiguration (Properties4CXX::Properties const &configuration) {

Expand Down Expand Up @@ -281,54 +256,6 @@ void MS4515Driver::readConfiguration (Properties4CXX::Properties const &configur

}

void MS4515Driver::driverThreadFunction() {

int numRetries = 0;

if (ioPort == nullptr) {
LOG4CXX_ERROR (logger,"No valid I/O port for driver " << getDriverName()
<< ". The driver is not operable");
} else {
while (!getStopDriverThread() && ( errorMaxNumRetries == 0 || numRetries <= errorMaxNumRetries)) {
try {
ioPort->open();
numRetries = 0;
processingMainLoop ();
ioPort->close();
} catch (std::exception const& e) {
numRetries ++;
LOG4CXX_ERROR(logger,"Error in main loop of driver \"" << getDriverName()
<< "\":" << e.what());
ioPort->close();

std::this_thread::sleep_for(errorTimeout);
}
}
}
}

void MS4515Driver::processingMainLoop() {

using namespace std::chrono_literals;

auto nextStartConversion = OEVClock::now();

while (!getStopDriverThread()) {

//std::this_thread::sleep_until(nextStartConversion + 60ms);

readoutMS4515();

// In case that you miss a cycle advance to the next cycle
auto now = OEVClock::now();
do {
nextStartConversion += updateCyle;
} while (nextStartConversion < now);
std::this_thread::sleep_until(nextStartConversion);
}

}

/// Like in the AVR libraries
#define _BV(x) (1<<(x))

Expand All @@ -345,7 +272,7 @@ FloatType MS4515Driver::convertRegisterPressureToMBar(
return rc;
}

void MS4515Driver::readoutMS4515() {
void MS4515Driver::readoutSensorData() {

uint8_t sensorValues[4];
uint8_t status = MS4515_STATUS_STALE;
Expand All @@ -371,11 +298,11 @@ void MS4515Driver::readoutMS4515() {
<< ", pressureRaw = 0x" << std::hex << pressureRawVal << " = " << std::dec << pressureRawVal
<< ", temperatureRawVal = 0x" << std::hex << temperatureRawVal << std::dec << " = " << temperatureRawVal);

temperatureVal = FloatType(temperatureRawVal) * (50.0f/511.0f) - 50.0f;
temperatureVal = static_cast<FloatType>(temperatureRawVal) * (50.0f/511.0f) - 50.0f;

LOG4CXX_DEBUG(logger, __FUNCTION__ << ": temperatureVal = " << temperatureVal);

FloatType pressureVal = convertRegisterPressureToMBar(pressureRawVal);
pressureVal = convertRegisterPressureToMBar(pressureRawVal);

if (getIsKalmanUpdateRunning()) {

Expand Down
Loading

0 comments on commit 9c49607

Please sign in to comment.