2022-03-03 15:37:36 +01:00
|
|
|
#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,
|
2022-03-17 19:23:39 +01:00
|
|
|
POWER_STATE_MACHINE_TIMEOUT, SIDE_SWITCH_TRANSITION_NOT_ALLOWED,
|
|
|
|
TRANSITION_OTHER_SIDE_FAILED),
|
2022-03-10 11:02:07 +01:00
|
|
|
helper(helper),
|
|
|
|
pwrSwitcher(pwrSwitcher) {
|
2022-03-03 20:11:12 +01:00
|
|
|
ModeListEntry entry;
|
|
|
|
for (uint8_t idx = 0; idx < NUMBER_SUN_SENSORS; idx++) {
|
2022-03-10 11:02:07 +01:00
|
|
|
initModeTableEntry(helper.susIds[idx], entry, modeTable);
|
2022-03-03 20:11:12 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-03 15:37:36 +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;
|
2022-03-03 15:37:36 +01:00
|
|
|
refreshHelperModes();
|
2022-03-17 19:23:39 +01:00
|
|
|
// 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);
|
|
|
|
}
|
2023-03-02 14:59:27 +01:00
|
|
|
if (recoveryState == RecoveryState::RECOVERY_IDLE) {
|
|
|
|
result = checkAndHandleHealthStates(mode, submode);
|
2023-04-05 14:41:34 +02:00
|
|
|
if (result != returnvalue::OK) {
|
|
|
|
return result;
|
2023-03-02 14:59:27 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-17 19:23:39 +01:00
|
|
|
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) {
|
2022-03-10 11:02:07 +01:00
|
|
|
using namespace duallane;
|
2022-08-24 17:27:47 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
2022-03-17 19:23:39 +01:00
|
|
|
bool needsSecondStep = false;
|
2023-03-31 19:14:42 +02:00
|
|
|
handleSideSwitchStates(submode, needsSecondStep);
|
2022-03-17 19:23:39 +01:00
|
|
|
auto cmdSeq = [&](object_id_t objectId, Mode_t devMode, uint8_t tableIdx) {
|
2023-04-05 14:46:45 +02:00
|
|
|
if (isModeCommandable(objectId, devMode)) {
|
2022-03-17 19:23:39 +01:00
|
|
|
modeTable[tableIdx].setMode(mode);
|
2023-04-05 14:41:34 +02:00
|
|
|
modeTable[tableIdx].setSubmode(SUBMODE_NONE);
|
2022-03-03 20:11:12 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
switch (submode) {
|
2022-03-10 11:02:07 +01:00
|
|
|
case (A_SIDE): {
|
2022-03-03 20:11:12 +01:00
|
|
|
for (uint8_t idx = 0; idx < NUMBER_SUN_SENSORS_ONE_SIDE; idx++) {
|
2022-03-17 19:23:39 +01:00
|
|
|
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);
|
|
|
|
}
|
2022-03-17 19:23:39 +01:00
|
|
|
break;
|
2022-03-03 20:11:12 +01:00
|
|
|
}
|
2022-03-10 11:02:07 +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++) {
|
2022-03-17 19:23:39 +01:00
|
|
|
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);
|
|
|
|
}
|
2022-03-17 19:23:39 +01:00
|
|
|
break;
|
2022-03-03 20:11:12 +01:00
|
|
|
}
|
|
|
|
case (DUAL_MODE): {
|
|
|
|
for (uint8_t idx = 0; idx < NUMBER_SUN_SENSORS; idx++) {
|
2022-03-17 19:23:39 +01:00
|
|
|
cmdSeq(helper.susIds[idx], helper.susModes[idx], idx);
|
2022-03-03 20:11:12 +01:00
|
|
|
}
|
2022-03-17 19:23:39 +01:00
|
|
|
break;
|
2022-03-03 20:11:12 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-17 19:23:39 +01:00
|
|
|
if (needsSecondStep) {
|
|
|
|
result = NEED_SECOND_STEP;
|
|
|
|
}
|
|
|
|
return result;
|
2022-03-03 15:37:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t SusAssembly::checkChildrenStateOn(Mode_t wantedMode, Submode_t wantedSubmode) {
|
2022-03-10 11:02:07 +01:00
|
|
|
using namespace duallane;
|
2022-03-03 20:11:12 +01:00
|
|
|
refreshHelperModes();
|
2022-03-10 11:02:07 +01:00
|
|
|
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;
|
2022-03-10 11:02:07 +01:00
|
|
|
} 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-03-03 15:37:36 +01:00
|
|
|
}
|
|
|
|
|
2023-02-26 14:55:33 +01:00
|
|
|
ReturnValue_t SusAssembly::initialize() {
|
|
|
|
for (const auto& child : childrenMap) {
|
|
|
|
updateChildModeByObjId(child.first, MODE_OFF, 0);
|
|
|
|
}
|
|
|
|
return AssemblyBase::initialize();
|
|
|
|
}
|
2022-03-03 15:37:36 +01:00
|
|
|
|
|
|
|
void SusAssembly::refreshHelperModes() {
|
|
|
|
for (uint8_t idx = 0; idx < helper.susModes.size(); idx++) {
|
|
|
|
helper.susModes[idx] = childrenMap[helper.susIds[idx]].mode;
|
|
|
|
}
|
|
|
|
}
|
2023-03-02 14:59:27 +01:00
|
|
|
|
2023-04-05 14:46:45 +02:00
|
|
|
ReturnValue_t SusAssembly::checkAndHandleHealthStates(Mode_t commandedMode,
|
|
|
|
Submode_t commandedSubmode) {
|
2023-03-02 14:59:27 +01:00
|
|
|
using namespace returnvalue;
|
|
|
|
ReturnValue_t status = returnvalue::OK;
|
2023-04-05 10:27:19 +02:00
|
|
|
bool needsHealthOverwritten = false;
|
2023-03-27 14:53:18 +02:00
|
|
|
auto checkSusGroup = [&](object_id_t devNom, object_id_t devRed) {
|
|
|
|
HealthState healthNom = healthHelper.healthTable->getHealth(devNom);
|
|
|
|
HealthState healthRed = healthHelper.healthTable->getHealth(devRed);
|
|
|
|
if ((healthNom == FAULTY or healthNom == PERMANENT_FAULTY) and
|
|
|
|
(healthRed == FAULTY or healthRed == PERMANENT_FAULTY)) {
|
|
|
|
overwriteDeviceHealth(devNom, healthNom);
|
|
|
|
overwriteDeviceHealth(devRed, healthRed);
|
2023-04-05 10:27:19 +02:00
|
|
|
needsHealthOverwritten = true;
|
2023-03-27 14:53:18 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
auto checkHealthForOneDev = [&](object_id_t dev) {
|
2023-03-02 14:59:27 +01:00
|
|
|
HealthState health = healthHelper.healthTable->getHealth(dev);
|
2023-03-27 14:53:18 +02:00
|
|
|
if (health == EXTERNAL_CONTROL) {
|
2023-03-02 14:59:27 +01:00
|
|
|
modeHelper.setForced(true);
|
|
|
|
}
|
|
|
|
};
|
2023-04-05 14:41:34 +02:00
|
|
|
if (commandedSubmode == duallane::DUAL_MODE) {
|
2023-03-27 14:53:18 +02:00
|
|
|
uint8_t idx = 0;
|
|
|
|
for (idx = 0; idx < 6; idx++) {
|
|
|
|
checkSusGroup(helper.susIds[idx], helper.susIds[idx + 6]);
|
|
|
|
checkHealthForOneDev(helper.susIds[idx]);
|
|
|
|
}
|
|
|
|
for (idx = 6; idx < 12; idx++) {
|
|
|
|
checkHealthForOneDev(helper.susIds[idx]);
|
2023-03-02 14:59:27 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-05 14:46:45 +02:00
|
|
|
if (needsHealthOverwritten) {
|
2023-04-05 14:41:34 +02:00
|
|
|
mode = commandedMode;
|
|
|
|
submode = commandedSubmode;
|
|
|
|
// We need second step instead of NEED_TO_CHANGE_HEALTH because we do not want recovery
|
|
|
|
// handling.
|
2023-04-05 10:27:19 +02:00
|
|
|
return NEED_TO_CHANGE_HEALTH;
|
|
|
|
}
|
2023-03-02 14:59:27 +01:00
|
|
|
return status;
|
|
|
|
}
|