eive-obsw/mission/system/objects/TcsBoardAssembly.cpp

221 lines
7.3 KiB
C++
Raw Normal View History

2022-03-22 18:48:03 +01:00
#include "TcsBoardAssembly.h"
#include <fsfw/devicehandlers/DeviceHandlerIF.h>
#include <fsfw/ipc/QueueFactory.h>
2022-09-29 19:40:00 +02:00
TcsBoardAssembly::TcsBoardAssembly(object_id_t objectId, PowerSwitchIF* pwrSwitcher,
power::Switch_t theSwitch, TcsBoardHelper helper)
: AssemblyBase(objectId, 24), switcher(pwrSwitcher, theSwitch), helper(helper) {
2022-03-22 18:48:03 +01:00
eventQueue = QueueFactory::instance()->createMessageQueue(24);
2022-03-22 20:35:37 +01:00
ModeListEntry entry;
for (uint8_t idx = 0; idx < NUMBER_RTDS; idx++) {
entry.setObject(helper.rtdInfos[idx].first);
2022-03-22 20:35:37 +01:00
entry.setMode(MODE_OFF);
entry.setSubmode(SUBMODE_NONE);
modeTable.insert(entry);
}
2022-03-22 18:48:03 +01:00
}
void TcsBoardAssembly::performChildOperation() {
auto state = switcher.getState();
if (state != PowerSwitcher::WAIT_OFF and state != PowerSwitcher::WAIT_ON) {
AssemblyBase::performChildOperation();
return;
}
switcher.doStateMachine();
if (state == PowerSwitcher::WAIT_OFF and switcher.getState() == PowerSwitcher::SWITCH_IS_OFF) {
// Indicator that a transition to off is finished
AssemblyBase::handleModeReached();
} else if (state == PowerSwitcher::WAIT_ON and
switcher.getState() == PowerSwitcher::SWITCH_IS_ON) {
// Indicator that mode commanding can be performed now
2022-03-22 19:33:49 +01:00
AssemblyBase::startTransition(targetMode, targetSubmode);
2022-03-22 18:48:03 +01:00
}
}
ReturnValue_t TcsBoardAssembly::commandChildren(Mode_t mode, Submode_t submode) {
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-03-22 18:48:03 +01:00
// Initialize the mode table to ensure all devices are in a defined state
for (uint8_t idx = 0; idx < NUMBER_RTDS; idx++) {
modeTable[idx].setMode(MODE_OFF);
modeTable[idx].setSubmode(SUBMODE_NONE);
}
2023-03-06 15:41:56 +01:00
if (recoveryState == RecoveryState::RECOVERY_IDLE) {
result = checkAndHandleHealthStates(mode, submode);
if (result == NEED_TO_CHANGE_HEALTH) {
return returnvalue::OK;
}
}
2022-03-22 18:48:03 +01:00
if (recoveryState != RecoveryState::RECOVERY_STARTED) {
if (mode == DeviceHandlerIF::MODE_NORMAL or mode == MODE_ON) {
result = handleNormalOrOnModeCmd(mode, submode);
}
}
HybridIterator<ModeListEntry> tableIter(modeTable.begin(), modeTable.end());
executeTable(tableIter);
return result;
}
ReturnValue_t TcsBoardAssembly::checkChildrenStateOn(Mode_t wantedMode, Submode_t wantedSubmode) {
2022-03-22 20:35:37 +01:00
int devsInWrongMode = 0;
try {
for (uint8_t idx = 0; idx < NUMBER_RTDS; idx++) {
if (childrenMap.at(helper.rtdInfos[idx].first).mode != wantedMode) {
2022-03-22 20:35:37 +01:00
devsInWrongMode++;
}
2022-03-22 18:48:03 +01:00
}
2022-03-22 20:35:37 +01:00
} catch (const std::out_of_range& e) {
sif::error << "TcsBoardAssembly: Invalid children map: " << e.what() << std::endl;
}
2023-03-06 17:34:04 +01:00
if (devsInWrongMode == NUMBER_RTDS) {
2022-03-22 20:35:37 +01:00
if (warningSwitch) {
2023-03-06 17:34:04 +01:00
sif::warning << "TcsBoardAssembly::checkChildrenStateOn: All devices in wrong mode"
<< std::endl;
2022-03-22 20:35:37 +01:00
warningSwitch = false;
}
return NOT_ENOUGH_CHILDREN_IN_CORRECT_STATE;
2022-03-22 18:48:03 +01:00
}
// TODO: Can't really do something other than power cycling if devices in wrong mode.
// Might attempt one power-cycle. In any case, trigger an event
2022-03-22 20:35:37 +01:00
if (devsInWrongMode > 0) {
if (warningSwitch) {
sif::warning << "TcsBoardAssembly::checkChildrenStateOn: " << devsInWrongMode << " devices in"
<< " wrong mode" << std::endl;
warningSwitch = false;
}
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-03-22 18:48:03 +01:00
}
ReturnValue_t TcsBoardAssembly::isModeCombinationValid(Mode_t mode, Submode_t submode) {
if (mode == MODE_ON or mode == MODE_OFF or mode == DeviceHandlerIF::MODE_NORMAL) {
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-03-22 18:48:03 +01:00
}
return HasModesIF::INVALID_MODE;
}
2022-09-30 14:30:30 +02:00
ReturnValue_t TcsBoardAssembly::initialize() { return AssemblyBase::initialize(); }
2022-03-22 18:48:03 +01:00
void TcsBoardAssembly::startTransition(Mode_t mode, Submode_t submode) {
if (mode != MODE_OFF) {
2022-03-22 20:35:37 +01:00
switcher.turnOn(true);
2022-03-22 18:48:03 +01:00
switcher.doStateMachine();
if (switcher.getState() == PowerSwitcher::SWITCH_IS_ON) {
AssemblyBase::startTransition(mode, submode);
} else {
// Need to wait with mode commanding until power switcher is done
targetMode = mode;
targetSubmode = submode;
}
} else {
2022-03-22 20:35:37 +01:00
// Perform regular mode commanding first
2022-03-22 18:48:03 +01:00
AssemblyBase::startTransition(mode, submode);
}
}
ReturnValue_t TcsBoardAssembly::handleNormalOrOnModeCmd(Mode_t mode, Submode_t submode) {
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
2022-03-22 18:48:03 +01:00
bool needsSecondStep = false;
Mode_t devMode = 0;
object_id_t objId = 0;
2022-03-22 20:35:37 +01:00
try {
for (uint8_t idx = 0; idx < NUMBER_RTDS; idx++) {
devMode = childrenMap.at(helper.rtdInfos[idx].first).mode;
objId = helper.rtdInfos[idx].first;
2022-03-22 20:35:37 +01:00
if (mode == devMode) {
modeTable[idx].setMode(mode);
} else if (mode == DeviceHandlerIF::MODE_NORMAL) {
if (isUseable(objId, devMode)) {
if (devMode == MODE_ON) {
modeTable[idx].setMode(mode);
modeTable[idx].setSubmode(SUBMODE_NONE);
} else {
modeTable[idx].setMode(MODE_ON);
modeTable[idx].setSubmode(SUBMODE_NONE);
if (internalState != STATE_SECOND_STEP) {
needsSecondStep = true;
}
}
}
} else if (mode == MODE_ON) {
if (isUseable(objId, devMode)) {
2022-03-22 18:48:03 +01:00
modeTable[idx].setMode(MODE_ON);
modeTable[idx].setSubmode(SUBMODE_NONE);
}
}
}
2022-03-22 20:35:37 +01:00
} catch (const std::out_of_range& e) {
sif::error << "TcsBoardAssembly: Invalid children map: " << e.what() << std::endl;
2022-03-22 18:48:03 +01:00
}
if (needsSecondStep) {
result = NEED_SECOND_STEP;
}
return result;
}
bool TcsBoardAssembly::isUseable(object_id_t object, Mode_t mode) {
if (healthHelper.healthTable->isFaulty(object)) {
return false;
}
// Check if device is already in target mode
if (childrenMap[object].mode == mode) {
return true;
}
if (healthHelper.healthTable->isCommandable(object)) {
return true;
}
return false;
}
MessageQueueId_t TcsBoardAssembly::getEventReceptionQueue() { return eventQueue->getId(); }
void TcsBoardAssembly::handleModeReached() {
if (targetMode == MODE_OFF) {
2022-03-22 20:35:37 +01:00
switcher.turnOff(true);
2022-03-22 18:48:03 +01:00
switcher.doStateMachine();
// Need to wait with call to AssemblyBase::handleModeReached until power switcher is done
if (switcher.getState() == PowerSwitcher::SWITCH_IS_OFF) {
AssemblyBase::handleModeReached();
}
} else {
AssemblyBase::handleModeReached();
}
}
2022-03-22 20:35:37 +01:00
void TcsBoardAssembly::handleChildrenLostMode(ReturnValue_t result) {
2022-03-22 20:40:30 +01:00
triggerEvent(CHILDREN_LOST_MODE, result);
2023-03-06 15:41:56 +01:00
startTransition(mode, submode);
}
ReturnValue_t TcsBoardAssembly::checkAndHandleHealthStates(Mode_t deviceMode,
Submode_t deviceSubmode) {
ReturnValue_t status = returnvalue::OK;
2023-03-06 17:34:04 +01:00
for (const auto& dev : helper.rtdInfos) {
HealthState health = healthHelper.healthTable->getHealth(dev.first);
if (health == HealthState::HEALTHY) {
return returnvalue::OK;
}
}
for (const auto& dev : helper.rtdInfos) {
HealthState health = healthHelper.healthTable->getHealth(dev.first);
2023-03-06 15:41:56 +01:00
if (health == FAULTY or health == PERMANENT_FAULTY) {
status = NEED_TO_CHANGE_HEALTH;
} else if (health == EXTERNAL_CONTROL) {
modeHelper.setForced(true);
}
}
2023-03-06 17:34:04 +01:00
return status;
2022-03-22 20:35:37 +01:00
}
void TcsBoardAssembly::handleModeTransitionFailed(ReturnValue_t result) {
if (targetMode == MODE_OFF) {
AssemblyBase::handleModeTransitionFailed(result);
} else {
// To avoid transitioning back to off
triggerEvent(MODE_TRANSITION_FAILED, result);
}
}