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

146 lines
4.8 KiB
C++
Raw Normal View History

#include "SusAssembly.h"
#include <devices/powerSwitcherList.h>
#include <fsfw/power/PowerSwitchIF.h>
#include <fsfw/serviceinterface.h>
2022-09-29 19:40:00 +02:00
SusAssembly::SusAssembly(object_id_t objectId, PowerSwitchIF* pwrSwitcher, SusAssHelper helper)
: DualLaneAssemblyBase(objectId, pwrSwitcher, SWITCH_NOM, SWITCH_RED,
POWER_STATE_MACHINE_TIMEOUT, SIDE_SWITCH_TRANSITION_NOT_ALLOWED,
TRANSITION_OTHER_SIDE_FAILED),
helper(helper),
pwrSwitcher(pwrSwitcher) {
2022-03-03 20:11:12 +01:00
ModeListEntry entry;
for (uint8_t idx = 0; idx < NUMBER_SUN_SENSORS; idx++) {
initModeTableEntry(helper.susIds[idx], entry, modeTable);
2022-03-03 20:11:12 +01:00
}
}
ReturnValue_t SusAssembly::commandChildren(Mode_t mode, Submode_t submode) {
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
refreshHelperModes();
// Initialize the mode table to ensure all devices are in a defined state
for (uint8_t idx = 0; idx < NUMBER_SUN_SENSORS; idx++) {
modeTable[idx].setMode(MODE_OFF);
modeTable[idx].setSubmode(SUBMODE_NONE);
}
if (recoveryState != RecoveryState::RECOVERY_STARTED) {
if (mode == DeviceHandlerIF::MODE_NORMAL or mode == MODE_ON) {
2022-03-22 11:07:31 +01:00
result = handleNormalOrOnModeCmd(mode, submode);
2022-03-03 20:11:12 +01:00
}
}
HybridIterator<ModeListEntry> tableIter(modeTable.begin(), modeTable.end());
executeTable(tableIter);
return result;
}
ReturnValue_t SusAssembly::handleNormalOrOnModeCmd(Mode_t mode, Submode_t submode) {
using namespace duallane;
2022-08-24 17:27:47 +02:00
ReturnValue_t result = returnvalue::OK;
bool needsSecondStep = false;
auto cmdSeq = [&](object_id_t objectId, Mode_t devMode, uint8_t tableIdx) {
if (mode == devMode) {
modeTable[tableIdx].setMode(mode);
} else if (mode == DeviceHandlerIF::MODE_NORMAL) {
if (isUseable(objectId, devMode)) {
if (devMode == MODE_ON) {
2022-03-03 20:11:12 +01:00
modeTable[tableIdx].setMode(mode);
modeTable[tableIdx].setSubmode(SUBMODE_NONE);
} else {
modeTable[tableIdx].setMode(MODE_ON);
modeTable[tableIdx].setSubmode(SUBMODE_NONE);
if (internalState != STATE_SECOND_STEP) {
needsSecondStep = true;
}
2022-03-03 20:11:12 +01:00
}
}
} else if (mode == MODE_ON) {
if (isUseable(objectId, devMode)) {
2022-03-03 20:11:12 +01:00
modeTable[tableIdx].setMode(MODE_ON);
modeTable[tableIdx].setSubmode(SUBMODE_NONE);
}
}
};
switch (submode) {
case (A_SIDE): {
2022-03-03 20:11:12 +01:00
for (uint8_t idx = 0; idx < NUMBER_SUN_SENSORS_ONE_SIDE; idx++) {
cmdSeq(helper.susIds[idx], helper.susModes[idx], idx);
2022-03-03 20:11:12 +01:00
// Switch off devices on redundant side
modeTable[idx + NUMBER_SUN_SENSORS_ONE_SIDE].setMode(MODE_OFF);
modeTable[idx + NUMBER_SUN_SENSORS_ONE_SIDE].setSubmode(SUBMODE_NONE);
}
break;
2022-03-03 20:11:12 +01:00
}
case (B_SIDE): {
2022-03-03 20:11:12 +01:00
for (uint8_t idx = NUMBER_SUN_SENSORS_ONE_SIDE; idx < NUMBER_SUN_SENSORS; idx++) {
cmdSeq(helper.susIds[idx], helper.susModes[idx], idx);
2022-03-03 20:11:12 +01:00
// Switch devices on nominal side
modeTable[idx - NUMBER_SUN_SENSORS_ONE_SIDE].setMode(MODE_OFF);
modeTable[idx - NUMBER_SUN_SENSORS_ONE_SIDE].setSubmode(SUBMODE_NONE);
}
break;
2022-03-03 20:11:12 +01:00
}
case (DUAL_MODE): {
for (uint8_t idx = 0; idx < NUMBER_SUN_SENSORS; idx++) {
cmdSeq(helper.susIds[idx], helper.susModes[idx], idx);
2022-03-03 20:11:12 +01:00
}
break;
2022-03-03 20:11:12 +01:00
}
}
if (needsSecondStep) {
result = NEED_SECOND_STEP;
}
return result;
}
ReturnValue_t SusAssembly::checkChildrenStateOn(Mode_t wantedMode, Submode_t wantedSubmode) {
using namespace duallane;
2022-03-03 20:11:12 +01:00
refreshHelperModes();
if (wantedSubmode == A_SIDE) {
2022-03-03 20:11:12 +01:00
for (uint8_t idx = 0; idx < NUMBER_SUN_SENSORS_ONE_SIDE; idx++) {
if (helper.susModes[idx] != wantedMode) {
return NOT_ENOUGH_CHILDREN_IN_CORRECT_STATE;
}
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
} else if (wantedSubmode == B_SIDE) {
2022-03-03 20:11:12 +01:00
for (uint8_t idx = NUMBER_SUN_SENSORS_ONE_SIDE; idx < NUMBER_SUN_SENSORS; idx++) {
if (helper.susModes[idx] != wantedMode) {
return NOT_ENOUGH_CHILDREN_IN_CORRECT_STATE;
}
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-03-03 20:11:12 +01:00
} else {
// Trigger event if devices are faulty? This is the last fallback mode, returning
// a failure here would trigger a transition to MODE_OFF unless handleModeTransitionFailed
// is overriden
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-03-03 20:11:12 +01:00
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
}
2022-09-30 14:30:30 +02:00
ReturnValue_t SusAssembly::initialize() { return AssemblyBase::initialize(); }
bool SusAssembly::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;
}
void SusAssembly::refreshHelperModes() {
for (uint8_t idx = 0; idx < helper.susModes.size(); idx++) {
helper.susModes[idx] = childrenMap[helper.susIds[idx]].mode;
}
}