eive-obsw/mission/devices/Max31865EiveHandler.cpp

196 lines
7.0 KiB
C++
Raw Normal View History

2022-05-12 09:48:41 +02:00
#include "Max31865EiveHandler.h"
2022-05-31 16:44:57 +02:00
#include <fsfw/datapool/PoolReadGuard.h>
2022-05-12 09:48:41 +02:00
Max31865EiveHandler::Max31865EiveHandler(object_id_t objectId, object_id_t comIF,
CookieIF* comCookie)
: DeviceHandlerBase(objectId, comIF, comCookie, nullptr),
2022-05-14 09:41:28 +02:00
sensorDataset(this, EiveMax31855::RtdCommands::EXCHANGE_SET_ID),
debugDivider(5) {
structLen = exchangeStruct.getSerializedSize();
}
2022-05-12 09:48:41 +02:00
void Max31865EiveHandler::doStartUp() {
2022-05-14 11:34:25 +02:00
updatePeriodicReply(true, EiveMax31855::RtdCommands::EXCHANGE_SET_ID);
2022-05-12 09:48:41 +02:00
if (state == InternalState::NONE or state == InternalState::INACTIVE) {
if (instantNormal) {
state = InternalState::ACTIVE;
} else {
state = InternalState::ON;
}
transitionOk = false;
}
if ((state == InternalState::ON or state == InternalState::ACTIVE) and transitionOk) {
if (instantNormal) {
setMode(MODE_NORMAL);
2022-05-12 09:48:41 +02:00
} else {
setMode(MODE_ON);
}
}
}
void Max31865EiveHandler::doShutDown() {
2022-05-14 11:34:25 +02:00
updatePeriodicReply(false, EiveMax31855::RtdCommands::EXCHANGE_SET_ID);
2022-05-12 09:48:41 +02:00
if (state == InternalState::NONE or state == InternalState::ACTIVE or
state == InternalState::ON) {
state = InternalState::INACTIVE;
transitionOk = false;
} else {
transitionOk = true;
}
if (state == InternalState::INACTIVE and transitionOk) {
setMode(_MODE_POWER_DOWN);
}
}
ReturnValue_t Max31865EiveHandler::buildNormalDeviceCommand(DeviceCommandId_t* id) {
2022-05-13 16:29:57 +02:00
//*id = EiveMax31855::RtdCommands::EXCHANGE_SET_ID;
return NOTHING_TO_SEND;
2022-05-12 09:48:41 +02:00
}
ReturnValue_t Max31865EiveHandler::buildTransitionDeviceCommand(DeviceCommandId_t* id) {
2022-05-13 16:29:57 +02:00
ReturnValue_t result = NOTHING_TO_SEND;
2022-05-12 09:48:41 +02:00
if (state == InternalState::ON) {
*id = EiveMax31855::RtdCommands::ON;
2022-05-13 16:29:57 +02:00
result = buildCommandFromCommand(*id, nullptr, 0);
2022-05-12 09:48:41 +02:00
}
if (state == InternalState::ACTIVE) {
*id = EiveMax31855::RtdCommands::ACTIVE;
2022-05-13 16:29:57 +02:00
result = buildCommandFromCommand(*id, nullptr, 0);
2022-05-12 09:48:41 +02:00
}
if (state == InternalState::INACTIVE) {
*id = EiveMax31855::RtdCommands::OFF;
2022-05-13 16:29:57 +02:00
result = buildCommandFromCommand(*id, nullptr, 0);
2022-05-12 09:48:41 +02:00
}
2022-05-13 16:29:57 +02:00
return result;
2022-05-12 09:48:41 +02:00
}
ReturnValue_t Max31865EiveHandler::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
const uint8_t* commandData,
size_t commandDataLen) {
auto cmdTyped = static_cast<EiveMax31855::RtdCommands>(deviceCommand);
switch (cmdTyped) {
case (EiveMax31855::RtdCommands::ON):
case (EiveMax31855::RtdCommands::ACTIVE):
case (EiveMax31855::RtdCommands::OFF): {
simpleCommand(cmdTyped);
break;
}
case (EiveMax31855::RtdCommands::LOW_THRESHOLD):
case (EiveMax31855::RtdCommands::HIGH_TRESHOLD): {
break;
}
case (EiveMax31855::RtdCommands::CFG): {
break;
}
default:
return NOTHING_TO_SEND;
}
return RETURN_OK;
}
void Max31865EiveHandler::setInstantNormal(bool instantNormal) {
this->instantNormal = instantNormal;
}
2022-05-14 09:41:28 +02:00
void Max31865EiveHandler::setDebugMode(bool enable, uint32_t divider) {
this->debugMode = enable;
debugDivider.setDivider(divider);
}
2022-05-12 09:48:41 +02:00
void Max31865EiveHandler::simpleCommand(EiveMax31855::RtdCommands cmd) {
2022-05-13 15:16:10 +02:00
cmdBuf[0] = static_cast<uint8_t>(cmd);
2022-05-12 09:48:41 +02:00
rawPacket = cmdBuf.data();
rawPacketLen = 1;
}
void Max31865EiveHandler::doTransition(Mode_t modeFrom, Submode_t subModeFrom) {
if (mode == _MODE_TO_NORMAL) {
if (state != InternalState::ACTIVE) {
state = InternalState::ACTIVE;
transitionOk = false;
} else if (transitionOk) {
setMode(MODE_NORMAL);
}
} else {
DeviceHandlerBase::doTransition(modeFrom, subModeFrom);
}
2022-05-12 09:48:41 +02:00
}
void Max31865EiveHandler::fillCommandAndReplyMap() {
2022-05-13 01:17:23 +02:00
insertInCommandMap(EiveMax31855::RtdCommands::ON);
insertInCommandMap(EiveMax31855::RtdCommands::ACTIVE);
insertInCommandMap(EiveMax31855::RtdCommands::OFF);
2022-05-14 11:34:25 +02:00
insertInReplyMap(EiveMax31855::RtdCommands::EXCHANGE_SET_ID, 200, &sensorDataset, 0, true);
2022-05-12 09:48:41 +02:00
}
ReturnValue_t Max31865EiveHandler::scanForReply(const uint8_t* start, size_t remainingSize,
DeviceCommandId_t* foundId, size_t* foundLen) {
2022-05-24 01:14:04 +02:00
if (mode == _MODE_POWER_ON or mode == _MODE_WAIT_ON) {
2022-05-23 17:50:54 +02:00
return IGNORE_FULL_PACKET;
}
2022-05-14 09:41:28 +02:00
if (remainingSize != structLen) {
2022-05-12 09:48:41 +02:00
sif::error << "Invalid reply from RTD reader detected, reply size " << remainingSize
2022-05-14 09:41:28 +02:00
<< " not equal to exchange struct size " << structLen << std::endl;
return DeviceHandlerIF::INVALID_DATA;
2022-05-12 09:48:41 +02:00
}
*foundId = EiveMax31855::RtdCommands::EXCHANGE_SET_ID;
*foundLen = remainingSize;
return RETURN_OK;
}
ReturnValue_t Max31865EiveHandler::interpretDeviceReply(DeviceCommandId_t id,
const uint8_t* packet) {
2022-05-14 09:41:28 +02:00
size_t deserTmp = structLen;
auto result = exchangeStruct.deSerialize(&packet, &deserTmp, SerializeIF::Endianness::MACHINE);
if (result != RETURN_OK) {
return result;
}
if (mode == _MODE_TO_NORMAL and exchangeStruct.active and state == InternalState::ACTIVE) {
transitionOk = true;
}
if (mode == _MODE_START_UP and exchangeStruct.configured and state == InternalState::ON) {
transitionOk = true;
2022-05-14 09:41:28 +02:00
}
// Calculate resistance
float rtdValue = exchangeStruct.adcCode * EiveMax31855::RTD_RREF_PT1000 / INT16_MAX;
// calculate approximation
float approxTemp = exchangeStruct.adcCode / 32.0 - 256.0;
2022-05-31 16:44:57 +02:00
PoolReadGuard pg(&sensorDataset);
if (pg.getReadResult() != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "Max31865EiveHandler: Failed to read sensor dataset" << std::endl;
sensorDataset.temperatureCelcius.setValid(false);
return RETURN_OK;
}
sensorDataset.temperatureCelcius = approxTemp;
sensorDataset.temperatureCelcius.setValid(true);
2022-05-14 09:41:28 +02:00
if (debugMode) {
if (debugDivider.checkAndIncrement()) {
sif::info << "Max31865: " << std::setw(20) << std::left << locString << std::right
<< " | R[Ohm] " << rtdValue << " Ohms | Approx T[C]: " << approxTemp << std::endl;
}
}
2022-05-12 09:48:41 +02:00
return RETURN_OK;
}
uint32_t Max31865EiveHandler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) { return 2000; }
ReturnValue_t Max31865EiveHandler::initializeLocalDataPool(localpool::DataPool& localDataPoolMap,
LocalDataPoolManager& poolManager) {
2022-05-12 11:27:30 +02:00
using namespace MAX31865;
localDataPoolMap.emplace(static_cast<lp_id_t>(PoolIds::RTD_VALUE), new PoolEntry<float>({0}));
localDataPoolMap.emplace(static_cast<lp_id_t>(PoolIds::TEMPERATURE_C), new PoolEntry<float>({0}));
2022-05-30 14:32:50 +02:00
localDataPoolMap.emplace(static_cast<lp_id_t>(PoolIds::LAST_FAULT_BYTE), new PoolEntry<uint8_t>({0}));
2022-05-12 11:27:30 +02:00
localDataPoolMap.emplace(static_cast<lp_id_t>(PoolIds::FAULT_BYTE), new PoolEntry<uint8_t>({0}));
2022-05-12 09:48:41 +02:00
poolManager.subscribeForPeriodicPacket(sensorDataset.getSid(), false, 30.0, false);
return RETURN_OK;
}
2022-05-13 15:16:48 +02:00
void Max31865EiveHandler::setDeviceInfo(uint8_t idx_, std::string location_) {
idx = idx_;
2022-05-13 15:16:48 +02:00
locString = std::move(location_);
}
2022-05-13 17:25:06 +02:00
2022-05-14 11:34:25 +02:00
ReturnValue_t Max31865EiveHandler::initialize() { return DeviceHandlerBase::initialize(); }