From 6b54a4dea91a2e3d405996efc8323db7ff689d32 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 13 May 2022 10:10:02 +0200 Subject: [PATCH] cleaning up, better fault status handling --- linux/devices/Max31865RtdLowlevelHandler.cpp | 6 -- mission/devices/Max31865PT1000Handler.cpp | 75 +++++++++---------- mission/devices/Max31865PT1000Handler.h | 2 + .../devicedefinitions/Max31865Definitions.h | 6 +- 4 files changed, 42 insertions(+), 47 deletions(-) diff --git a/linux/devices/Max31865RtdLowlevelHandler.cpp b/linux/devices/Max31865RtdLowlevelHandler.cpp index 9a1c1539..9eef346e 100644 --- a/linux/devices/Max31865RtdLowlevelHandler.cpp +++ b/linux/devices/Max31865RtdLowlevelHandler.cpp @@ -55,7 +55,6 @@ bool Max31865RtdReader::periodicInitHandling() { continue; } if ((rtd->on or rtd->active) and not rtd->configured) { - sif::debug << "crap1" << std::endl; if (rtd->cd.hasTimedOut()) { uint8_t cfg = (Bias::OFF << CfgBitPos::BIAS_SEL) | (Wires::FOUR_WIRE << CfgBitPos::WIRE_SEL) | @@ -118,9 +117,7 @@ void Max31865RtdReader::periodicReadReqHandling() { if (rtd == nullptr) { continue; } - sif::debug << "crap2" << std::endl; if (rtdIsActive(rtd->idx)) { - sif::debug << "crap3" << std::endl; uint8_t currentCfg = 0; auto result = readCfgReg(rtd->spiCookie, currentCfg); if (result != RETURN_OK) { @@ -154,9 +151,6 @@ void Max31865RtdReader::periodicReadHandling() { handleSpiError(rtd, result, "readRtdVal"); continue; } - if(rtd->idx == 0) { - sif::debug << "Read RAW rtd val 0: " << rtdVal << std::endl; - } if (faultBitSet) { rtd->db.faultBitSet = faultBitSet; } diff --git a/mission/devices/Max31865PT1000Handler.cpp b/mission/devices/Max31865PT1000Handler.cpp index 7c8dbfec..78d437fc 100644 --- a/mission/devices/Max31865PT1000Handler.cpp +++ b/mission/devices/Max31865PT1000Handler.cpp @@ -441,56 +441,51 @@ ReturnValue_t Max31865PT1000Handler::interpretDeviceReply(DeviceCommandId_t id, } case (MAX31865::REQUEST_FAULT_BYTE): { faultByte = packet[1]; -//#if OBSW_VERBOSE_LEVEL >= 1 -//#if FSFW_CPP_OSTREAM_ENABLED == 1 -// sif::warning << "Max31865PT1000Handler::interpretDeviceReply: Object ID: " << std::hex -// << this->getObjectId() -// << ": Fault byte" -// " is: 0b" -// << std::bitset<8>(faultByte) << std::endl; -//#else -// sif::printWarning( -// "Max31865PT1000Handler::interpretDeviceReply: Fault byte" -// " is: 0b" BYTE_TO_BINARY_PATTERN "\n", -// BYTE_TO_BINARY(faultByte)); -//#endif -//#endif - ReturnValue_t result = sensorDataset.read(); + bool faultStatusChanged = (faultByte != lastFaultStatus); + // Spam protection + if (faultStatusChanged or ((faultByte == lastFaultStatus) and (sameFaultStatusCounter < 3))) { + // TODO: Think about triggering an event here +#if OBSW_VERBOSE_LEVEL >= 1 +#if FSFW_CPP_OSTREAM_ENABLED == 1 + sif::warning << "Max31865PT1000Handler::interpretDeviceReply: Object ID: " << std::hex + << this->getObjectId() << ": Fault byte is: 0b" << std::bitset<8>(faultByte) + << std::endl; +#else + sif::printWarning( + "Max31865PT1000Handler::interpretDeviceReply: Fault byte" + " is: 0b" BYTE_TO_BINARY_PATTERN "\n", + BYTE_TO_BINARY(faultByte)); +#endif +#endif + if (faultStatusChanged) { + sameFaultStatusCounter = 0; + } else { + sameFaultStatusCounter++; + } + } + lastFaultStatus = faultByte; + PoolReadGuard pg(&sensorDataset); + auto result = pg.getReadResult(); if (result != HasReturnvaluesIF::RETURN_OK) { // Configuration error #if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::debug << "Max31865PT1000Handler::interpretDeviceReply: Object ID: " << std::hex - << this->getObjectId() - << ":" - "Error reading dataset!" - << std::endl; + sif::warning << "Max31865PT1000Handler::interpretDeviceReply: Object ID: " << std::hex + << this->getObjectId() << ": Error reading dataset" << std::endl; #else - sif::printDebug( - "Max31865PT1000Handler::interpretDeviceReply: " - "Error reading dataset!\n"); + sif::printWarning("Max31865PT1000Handler::interpretDeviceReply: Error reading dataset\n"); #endif return result; } + if(faultStatusChanged) { + sensorDataset.lastErrorByte.setValid(true); + sensorDataset.lastErrorByte = faultByte; + } sensorDataset.errorByte.setValid(true); sensorDataset.errorByte = faultByte; + if (faultByte != 0) { sensorDataset.temperatureCelcius.setValid(false); } - - result = sensorDataset.commit(); - if (result != HasReturnvaluesIF::RETURN_OK) { - // Configuration error -#if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::debug << "Max31865PT1000Handler::interpretDeviceReply: Object ID: " << std::hex - << this->getObjectId() << ": Error commiting dataset!" << std::endl; -#else - sif::printDebug( - "Max31865PT1000Handler::interpretDeviceReply: " - "Error commiting dataset!\n"); -#endif - return result; - } - break; } default: @@ -516,7 +511,9 @@ ReturnValue_t Max31865PT1000Handler::initializeLocalDataPool(localpool::DataPool using namespace MAX31865; localDataPoolMap.emplace(static_cast(PoolIds::RTD_VALUE), new PoolEntry({0})); localDataPoolMap.emplace(static_cast(PoolIds::TEMPERATURE_C), - new PoolEntry({0}, 1, true)); + new PoolEntry({0})); + localDataPoolMap.emplace(static_cast(PoolIds::LAST_FAULT_BYTE), + new PoolEntry({0})); localDataPoolMap.emplace(static_cast(PoolIds::FAULT_BYTE), new PoolEntry({0})); poolManager.subscribeForPeriodicPacket(sensorDataset.getSid(), false, 30.0, false); return HasReturnvaluesIF::RETURN_OK; diff --git a/mission/devices/Max31865PT1000Handler.h b/mission/devices/Max31865PT1000Handler.h index f6f42778..eb7f3372 100644 --- a/mission/devices/Max31865PT1000Handler.h +++ b/mission/devices/Max31865PT1000Handler.h @@ -110,6 +110,8 @@ class Max31865PT1000Handler : public DeviceHandlerBase { bool resetFaultBit = false; dur_millis_t startTime = 0; uint8_t currentCfg = 0; + uint8_t lastFaultStatus = 0; + uint16_t sameFaultStatusCounter = 0; std::string locString; uint8_t faultByte = 0; uint8_t deviceIdx = 0; diff --git a/mission/devices/devicedefinitions/Max31865Definitions.h b/mission/devices/devicedefinitions/Max31865Definitions.h index 1a3cca7a..9f54a322 100644 --- a/mission/devices/devicedefinitions/Max31865Definitions.h +++ b/mission/devices/devicedefinitions/Max31865Definitions.h @@ -9,7 +9,7 @@ namespace MAX31865 { -enum class PoolIds : lp_id_t { RTD_VALUE, TEMPERATURE_C, FAULT_BYTE }; +enum class PoolIds : lp_id_t { RTD_VALUE, TEMPERATURE_C, LAST_FAULT_BYTE, FAULT_BYTE }; enum Wires : unsigned int { TWO_WIRE = 0, THREE_WIRE = 1, FOUR_WIRE = 0 }; enum ConvMode : unsigned int { NORM_OFF = 0, AUTO = 1 }; @@ -56,7 +56,7 @@ static constexpr uint8_t CLEAR_FAULT_BIT_VAL = 0b0000'0010; static constexpr size_t MAX_REPLY_SIZE = 5; -class Max31865Set : public StaticLocalDataSet<3> { +class Max31865Set : public StaticLocalDataSet<4> { public: /** * Constructor used by owner and data creators like device handlers. @@ -75,6 +75,8 @@ class Max31865Set : public StaticLocalDataSet<3> { lp_var_t(sid.objectId, static_cast(PoolIds::RTD_VALUE), this); lp_var_t temperatureCelcius = lp_var_t(sid.objectId, static_cast(PoolIds::TEMPERATURE_C), this); + lp_var_t lastErrorByte = + lp_var_t(sid.objectId, static_cast(PoolIds::LAST_FAULT_BYTE), this); lp_var_t errorByte = lp_var_t(sid.objectId, static_cast(PoolIds::FAULT_BYTE), this); };