fsfw/misc/archive/MgmRM3100Handler.cpp

371 lines
13 KiB
C++
Raw Permalink Normal View History

#include "MgmRM3100Handler.h"
#include "fsfw/datapool/PoolReadGuard.h"
#include "fsfw/devicehandlers/DeviceHandlerMessage.h"
2022-02-02 10:29:30 +01:00
#include "fsfw/globalfunctions/bitutility.h"
#include "fsfw/objectmanager/SystemObjectIF.h"
2022-08-16 12:48:22 +02:00
#include "fsfw/returnvalues/returnvalue.h"
2022-02-02 10:29:30 +01:00
MgmRM3100Handler::MgmRM3100Handler(object_id_t objectId, object_id_t deviceCommunication,
CookieIF *comCookie, uint32_t transitionDelay)
: DeviceHandlerBase(objectId, deviceCommunication, comCookie),
primaryDataset(sharedPool),
transitionDelay(transitionDelay) {}
MgmRM3100Handler::~MgmRM3100Handler() {}
void MgmRM3100Handler::doStartUp() {
2022-02-02 10:29:30 +01:00
switch (internalState) {
case (InternalState::NONE): {
internalState = InternalState::CONFIGURE_CMM;
break;
}
case (InternalState::CONFIGURE_CMM): {
internalState = InternalState::READ_CMM;
break;
}
case (InternalState::READ_CMM): {
if (commandExecuted) {
internalState = InternalState::STATE_CONFIGURE_TMRC;
}
break;
}
case (InternalState::STATE_CONFIGURE_TMRC): {
if (commandExecuted) {
internalState = InternalState::STATE_READ_TMRC;
}
break;
}
case (InternalState::STATE_READ_TMRC): {
if (commandExecuted) {
internalState = InternalState::NORMAL;
if (goToNormalModeAtStartup) {
setMode(MODE_NORMAL);
} else {
setMode(_MODE_TO_ON);
}
2022-02-02 10:29:30 +01:00
}
break;
}
default: {
2022-02-02 10:29:30 +01:00
break;
}
2022-02-02 10:29:30 +01:00
}
}
2022-02-02 10:29:30 +01:00
void MgmRM3100Handler::doShutDown() { setMode(_MODE_POWER_DOWN); }
2022-02-02 10:29:30 +01:00
ReturnValue_t MgmRM3100Handler::buildTransitionDeviceCommand(DeviceCommandId_t *id) {
size_t commandLen = 0;
switch (internalState) {
case (InternalState::NONE):
case (InternalState::NORMAL): {
return NOTHING_TO_SEND;
}
2022-02-02 10:29:30 +01:00
case (InternalState::CONFIGURE_CMM): {
2023-02-26 21:26:24 +01:00
*id = mgmRm3100::CONFIGURE_CMM;
2022-02-02 10:29:30 +01:00
break;
}
2022-02-02 10:29:30 +01:00
case (InternalState::READ_CMM): {
2023-02-26 21:26:24 +01:00
*id = mgmRm3100::READ_CMM;
2022-02-02 10:29:30 +01:00
break;
}
2022-02-02 10:29:30 +01:00
case (InternalState::STATE_CONFIGURE_TMRC): {
2023-02-26 21:26:24 +01:00
commandBuffer[0] = mgmRm3100::TMRC_DEFAULT_VALUE;
2022-02-02 10:29:30 +01:00
commandLen = 1;
2023-02-26 21:26:24 +01:00
*id = mgmRm3100::CONFIGURE_TMRC;
2022-02-02 10:29:30 +01:00
break;
}
2022-02-02 10:29:30 +01:00
case (InternalState::STATE_READ_TMRC): {
2023-02-26 21:26:24 +01:00
*id = mgmRm3100::READ_TMRC;
2022-02-02 10:29:30 +01:00
break;
}
default:
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
// Might be a configuration error
sif::warning << "MgmRM3100Handler::buildTransitionDeviceCommand: "
"Unknown internal state"
<< std::endl;
#else
2022-02-02 10:29:30 +01:00
sif::printWarning(
"MgmRM3100Handler::buildTransitionDeviceCommand: "
"Unknown internal state\n");
#endif
#endif
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2022-02-02 10:29:30 +01:00
}
2022-02-02 10:29:30 +01:00
return buildCommandFromCommand(*id, commandBuffer, commandLen);
}
ReturnValue_t MgmRM3100Handler::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
2022-02-02 10:29:30 +01:00
const uint8_t *commandData,
size_t commandDataLen) {
switch (deviceCommand) {
2023-02-26 21:26:24 +01:00
case (mgmRm3100::CONFIGURE_CMM): {
commandBuffer[0] = mgmRm3100::CMM_REGISTER;
commandBuffer[1] = mgmRm3100::CMM_VALUE;
2022-02-02 10:29:30 +01:00
rawPacket = commandBuffer;
rawPacketLen = 2;
break;
}
2023-02-26 21:26:24 +01:00
case (mgmRm3100::READ_CMM): {
commandBuffer[0] = mgmRm3100::CMM_REGISTER | mgmRm3100::READ_MASK;
2022-02-02 10:29:30 +01:00
commandBuffer[1] = 0;
rawPacket = commandBuffer;
rawPacketLen = 2;
break;
}
2023-02-26 21:26:24 +01:00
case (mgmRm3100::CONFIGURE_TMRC): {
2022-02-02 10:29:30 +01:00
return handleTmrcConfigCommand(deviceCommand, commandData, commandDataLen);
}
2023-02-26 21:26:24 +01:00
case (mgmRm3100::READ_TMRC): {
commandBuffer[0] = mgmRm3100::TMRC_REGISTER | mgmRm3100::READ_MASK;
2022-02-02 10:29:30 +01:00
commandBuffer[1] = 0;
rawPacket = commandBuffer;
rawPacketLen = 2;
break;
}
2023-02-26 21:26:24 +01:00
case (mgmRm3100::CONFIGURE_CYCLE_COUNT): {
2022-02-02 10:29:30 +01:00
return handleCycleCountConfigCommand(deviceCommand, commandData, commandDataLen);
}
2023-02-26 21:26:24 +01:00
case (mgmRm3100::READ_CYCLE_COUNT): {
commandBuffer[0] = mgmRm3100::CYCLE_COUNT_START_REGISTER | mgmRm3100::READ_MASK;
2022-02-02 10:29:30 +01:00
std::memset(commandBuffer + 1, 0, 6);
rawPacket = commandBuffer;
rawPacketLen = 7;
break;
}
2023-02-26 21:26:24 +01:00
case (mgmRm3100::READ_DATA): {
commandBuffer[0] = mgmRm3100::MEASUREMENT_REG_START | mgmRm3100::READ_MASK;
2022-02-02 10:29:30 +01:00
std::memset(commandBuffer + 1, 0, 9);
rawPacketLen = 10;
break;
}
default:
2022-02-02 10:29:30 +01:00
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
}
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
}
2022-02-02 10:29:30 +01:00
ReturnValue_t MgmRM3100Handler::buildNormalDeviceCommand(DeviceCommandId_t *id) {
2023-02-26 21:26:24 +01:00
*id = mgmRm3100::READ_DATA;
2022-02-02 10:29:30 +01:00
return buildCommandFromCommand(*id, nullptr, 0);
}
2022-02-02 10:29:30 +01:00
ReturnValue_t MgmRM3100Handler::scanForReply(const uint8_t *start, size_t len,
DeviceCommandId_t *foundId, size_t *foundLen) {
// For SPI, ID will always be the one of the last sent command
*foundId = this->getPendingCommand();
*foundLen = len;
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
}
ReturnValue_t MgmRM3100Handler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) {
2022-08-16 01:08:26 +02:00
ReturnValue_t result = returnvalue::OK;
2022-02-02 10:29:30 +01:00
switch (id) {
2023-02-26 21:26:24 +01:00
case (mgmRm3100::CONFIGURE_CMM):
case (mgmRm3100::CONFIGURE_CYCLE_COUNT):
case (mgmRm3100::CONFIGURE_TMRC): {
2022-02-02 10:29:30 +01:00
// We can only check whether write was successful with read operation
2022-10-26 13:22:16 +02:00
if (getMode() == _MODE_START_UP) {
2022-02-02 10:29:30 +01:00
commandExecuted = true;
}
break;
}
2023-02-26 21:26:24 +01:00
case (mgmRm3100::READ_CMM): {
2022-02-02 10:29:30 +01:00
uint8_t cmmValue = packet[1];
// We clear the seventh bit in any case
// because this one is zero sometimes for some reason
bitutil::clear(&cmmValue, 6);
if (cmmValue == cmmRegValue and internalState == InternalState::READ_CMM) {
commandExecuted = true;
} else {
// Attempt reconfiguration
internalState = InternalState::CONFIGURE_CMM;
return DeviceHandlerIF::DEVICE_REPLY_INVALID;
}
break;
}
2023-02-26 21:26:24 +01:00
case (mgmRm3100::READ_TMRC): {
2022-02-02 10:29:30 +01:00
if (packet[1] == tmrcRegValue) {
commandExecuted = true;
// Reading TMRC was commanded. Trigger event to inform ground
2022-10-26 13:22:16 +02:00
if (getMode() != _MODE_START_UP) {
2022-02-02 10:29:30 +01:00
triggerEvent(tmrcSet, tmrcRegValue, 0);
}
2022-02-02 10:29:30 +01:00
} else {
// Attempt reconfiguration
internalState = InternalState::STATE_CONFIGURE_TMRC;
return DeviceHandlerIF::DEVICE_REPLY_INVALID;
}
break;
}
2023-02-26 21:26:24 +01:00
case (mgmRm3100::READ_CYCLE_COUNT): {
2022-02-02 10:29:30 +01:00
uint16_t cycleCountX = packet[1] << 8 | packet[2];
uint16_t cycleCountY = packet[3] << 8 | packet[4];
uint16_t cycleCountZ = packet[5] << 8 | packet[6];
if (cycleCountX != cycleCountRegValueX or cycleCountY != cycleCountRegValueY or
cycleCountZ != cycleCountRegValueZ) {
return DeviceHandlerIF::DEVICE_REPLY_INVALID;
}
// Reading TMRC was commanded. Trigger event to inform ground
2022-10-26 13:22:16 +02:00
if (getMode() != _MODE_START_UP) {
2022-02-02 10:29:30 +01:00
uint32_t eventParam1 = (cycleCountX << 16) | cycleCountY;
triggerEvent(cycleCountersSet, eventParam1, cycleCountZ);
}
break;
}
2023-02-26 21:26:24 +01:00
case (mgmRm3100::READ_DATA): {
2022-02-02 10:29:30 +01:00
result = handleDataReadout(packet);
break;
}
default:
2022-02-02 10:29:30 +01:00
return DeviceHandlerIF::UNKNOWN_DEVICE_REPLY;
}
2022-02-02 10:29:30 +01:00
return result;
}
ReturnValue_t MgmRM3100Handler::handleCycleCountConfigCommand(DeviceCommandId_t deviceCommand,
2022-02-02 10:29:30 +01:00
const uint8_t *commandData,
size_t commandDataLen) {
if (commandData == nullptr) {
return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
}
// Set cycle count
if (commandDataLen == 2) {
handleCycleCommand(true, commandData, commandDataLen);
} else if (commandDataLen == 6) {
handleCycleCommand(false, commandData, commandDataLen);
} else {
return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
}
2023-02-26 21:26:24 +01:00
commandBuffer[0] = mgmRm3100::CYCLE_COUNT_VALUE;
2022-02-02 10:29:30 +01:00
std::memcpy(commandBuffer + 1, &cycleCountRegValueX, 2);
std::memcpy(commandBuffer + 3, &cycleCountRegValueY, 2);
std::memcpy(commandBuffer + 5, &cycleCountRegValueZ, 2);
rawPacketLen = 7;
rawPacket = commandBuffer;
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
}
2022-02-02 10:29:30 +01:00
ReturnValue_t MgmRM3100Handler::handleCycleCommand(bool oneCycleValue, const uint8_t *commandData,
size_t commandDataLen) {
2023-02-26 21:26:24 +01:00
mgmRm3100::CycleCountCommand command(oneCycleValue);
2022-02-02 10:29:30 +01:00
ReturnValue_t result =
command.deSerialize(&commandData, &commandDataLen, SerializeIF::Endianness::BIG);
2022-08-16 01:08:26 +02:00
if (result != returnvalue::OK) {
2022-02-02 10:29:30 +01:00
return result;
}
2022-02-02 10:29:30 +01:00
// Data sheet p.30 "while noise limits the useful upper range to ~400 cycle counts."
if (command.cycleCountX > 450) {
return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
}
2022-02-02 10:29:30 +01:00
if (not oneCycleValue and (command.cycleCountY > 450 or command.cycleCountZ > 450)) {
return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
}
2022-02-02 10:29:30 +01:00
cycleCountRegValueX = command.cycleCountX;
cycleCountRegValueY = command.cycleCountY;
cycleCountRegValueZ = command.cycleCountZ;
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
}
ReturnValue_t MgmRM3100Handler::handleTmrcConfigCommand(DeviceCommandId_t deviceCommand,
2022-02-02 10:29:30 +01:00
const uint8_t *commandData,
size_t commandDataLen) {
if (commandData == nullptr or commandDataLen != 1) {
return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
}
2023-02-26 21:26:24 +01:00
commandBuffer[0] = mgmRm3100::TMRC_REGISTER;
2022-02-02 10:29:30 +01:00
commandBuffer[1] = commandData[0];
tmrcRegValue = commandData[0];
rawPacketLen = 2;
rawPacket = commandBuffer;
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
}
void MgmRM3100Handler::fillCommandAndReplyMap() {
2023-02-26 21:26:24 +01:00
insertInCommandAndReplyMap(mgmRm3100::CONFIGURE_CMM, 3);
insertInCommandAndReplyMap(mgmRm3100::READ_CMM, 3);
2023-02-26 21:26:24 +01:00
insertInCommandAndReplyMap(mgmRm3100::CONFIGURE_TMRC, 3);
insertInCommandAndReplyMap(mgmRm3100::READ_TMRC, 3);
2023-02-26 21:26:24 +01:00
insertInCommandAndReplyMap(mgmRm3100::CONFIGURE_CYCLE_COUNT, 3);
insertInCommandAndReplyMap(mgmRm3100::READ_CYCLE_COUNT, 3);
2023-02-26 21:26:24 +01:00
insertInCommandAndReplyMap(mgmRm3100::READ_DATA, 3, &primaryDataset);
}
2022-08-15 19:16:31 +02:00
void MgmRM3100Handler::modeChanged() { internalState = InternalState::NONE; }
// TODO: Fix
/*
2022-02-02 10:29:30 +01:00
ReturnValue_t MgmRM3100Handler::initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
PeriodicHkGenerationHelper &poolManager) {
2023-02-26 21:26:24 +01:00
localDataPoolMap.emplace(mgmRm3100::FIELD_STRENGTHS, &mgmXYZ);
poolManager.setPeriodicFrequency(primaryDataset.getSid(), 10'000);
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
}
*/
uint32_t MgmRM3100Handler::getTransitionDelayMs(Mode_t from, Mode_t to) {
2022-02-02 10:29:30 +01:00
return this->transitionDelay;
}
2022-02-02 10:29:30 +01:00
void MgmRM3100Handler::setToGoToNormalMode(bool enable) { goToNormalModeAtStartup = enable; }
ReturnValue_t MgmRM3100Handler::handleDataReadout(const uint8_t *packet) {
2022-02-02 10:29:30 +01:00
// Analyze data here. The sensor generates 24 bit signed values so we need to do some bitshift
// trickery here to calculate the raw values first
int32_t fieldStrengthRawX = ((packet[1] << 24) | (packet[2] << 16) | (packet[3] << 8)) >> 8;
int32_t fieldStrengthRawY = ((packet[4] << 24) | (packet[5] << 16) | (packet[6] << 8)) >> 8;
2023-02-02 18:26:08 +01:00
int32_t fieldStrengthRawZ = ((packet[7] << 24) | (packet[8] << 16) | (packet[9] << 8)) >> 8;
2022-02-02 10:29:30 +01:00
// Now scale to physical value in microtesla
float fieldStrengthX = fieldStrengthRawX * scaleFactorX;
float fieldStrengthY = fieldStrengthRawY * scaleFactorY;
float fieldStrengthZ = fieldStrengthRawZ * scaleFactorZ;
if (periodicPrintout) {
if (debugDivider.checkAndIncrement()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "MgmRM3100Handler: Magnetic field strength in"
" microtesla:"
<< std::endl;
sif::info << "X: " << fieldStrengthX << " uT" << std::endl;
sif::info << "Y: " << fieldStrengthY << " uT" << std::endl;
sif::info << "Z: " << fieldStrengthZ << " uT" << std::endl;
#else
sif::printInfo("MgmRM3100Handler: Magnetic field strength in microtesla:\n");
sif::printInfo("X: %f uT\n", fieldStrengthX);
sif::printInfo("Y: %f uT\n", fieldStrengthY);
sif::printInfo("Z: %f uT\n", fieldStrengthZ);
#endif
}
2022-02-02 10:29:30 +01:00
}
2022-02-02 10:29:30 +01:00
// TODO: Sanity check on values?
PoolReadGuard readGuard(&primaryDataset);
2022-08-16 01:08:26 +02:00
if (readGuard.getReadResult() == returnvalue::OK) {
2022-08-15 10:49:00 +02:00
primaryDataset.fieldStrengths[0] = fieldStrengthX;
primaryDataset.fieldStrengths[1] = fieldStrengthY;
primaryDataset.fieldStrengths[2] = fieldStrengthZ;
primaryDataset.valid = true;
2022-02-02 10:29:30 +01:00
}
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
}
void MgmRM3100Handler::enablePeriodicPrintouts(bool enable, uint8_t divider) {
periodicPrintout = enable;
debugDivider.setDivider(divider);
}