fsfw/src/fsfw/thermal/Heater.cpp

337 lines
11 KiB
C++
Raw Normal View History

2021-07-13 20:22:54 +02:00
#include "fsfw/thermal/Heater.h"
2021-07-13 20:22:54 +02:00
#include "fsfw/devicehandlers/DeviceHandlerFailureIsolation.h"
#include "fsfw/ipc/QueueFactory.h"
2022-02-02 10:29:30 +01:00
#include "fsfw/objectmanager/ObjectManager.h"
#include "fsfw/power/Fuse.h"
2022-02-02 10:29:30 +01:00
Heater::Heater(uint32_t objectId, uint8_t switch0, uint8_t switch1)
: HealthDevice(objectId, 0),
internalState(STATE_OFF),
switch0(switch0),
switch1(switch1),
heaterOnCountdown(10800000) /*about two orbits*/,
parameterHelper(this) {
2022-02-19 16:14:02 +01:00
auto mqArgs = MqArgs(objectId, static_cast<void*>(this));
2022-02-22 10:17:56 +01:00
eventQueue = QueueFactory::instance()->createMessageQueue(
3, MessageQueueMessage::MAX_MESSAGE_SIZE, &mqArgs);
}
2022-02-02 10:29:30 +01:00
Heater::~Heater() { QueueFactory::instance()->deleteMessageQueue(eventQueue); }
ReturnValue_t Heater::set() {
2022-02-02 10:29:30 +01:00
passive = false;
// wait for clear before doing anything
if (internalState == STATE_WAIT) {
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2022-02-02 10:29:30 +01:00
}
if (healthHelper.healthTable->isHealthy(getObjectId())) {
doAction(SET);
if ((internalState == STATE_OFF) || (internalState == STATE_PASSIVE)) {
2022-08-16 01:08:26 +02:00
return returnvalue::FAILED;
2021-04-20 16:12:50 +02:00
} else {
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2022-02-02 10:29:30 +01:00
}
} else {
if (healthHelper.healthTable->isFaulty(getObjectId())) {
if (!reactedToBeingFaulty) {
reactedToBeingFaulty = true;
doAction(CLEAR);
}
2021-04-20 16:12:50 +02:00
}
2022-08-16 01:08:26 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
}
void Heater::clear(bool passive) {
2022-02-02 10:29:30 +01:00
this->passive = passive;
// Force switching off
if (internalState == STATE_WAIT) {
internalState = STATE_ON;
}
if (healthHelper.healthTable->isHealthy(getObjectId())) {
doAction(CLEAR);
} else if (healthHelper.healthTable->isFaulty(getObjectId())) {
if (!reactedToBeingFaulty) {
reactedToBeingFaulty = true;
doAction(CLEAR);
2021-04-20 16:12:50 +02:00
}
2022-02-02 10:29:30 +01:00
}
}
void Heater::doAction(Action action) {
2022-02-02 10:29:30 +01:00
// only act if we are not in the right state or in a transition
if (action == SET) {
if ((internalState == STATE_OFF) || (internalState == STATE_PASSIVE) ||
(internalState == STATE_EXTERNAL_CONTROL)) {
switchCountdown.setTimeout(powerSwitcher->getSwitchDelayMs());
internalState = STATE_WAIT_FOR_SWITCHES_ON;
powerSwitcher->sendSwitchCommand(switch0, PowerSwitchIF::SWITCH_ON);
powerSwitcher->sendSwitchCommand(switch1, PowerSwitchIF::SWITCH_ON);
}
} else { // clear
if ((internalState == STATE_ON) || (internalState == STATE_FAULTY) ||
(internalState == STATE_EXTERNAL_CONTROL)) {
internalState = STATE_WAIT_FOR_SWITCHES_OFF;
switchCountdown.setTimeout(powerSwitcher->getSwitchDelayMs());
powerSwitcher->sendSwitchCommand(switch0, PowerSwitchIF::SWITCH_OFF);
powerSwitcher->sendSwitchCommand(switch1, PowerSwitchIF::SWITCH_OFF);
2021-04-20 16:12:50 +02:00
}
2022-02-02 10:29:30 +01:00
}
}
2022-02-02 10:29:30 +01:00
void Heater::setPowerSwitcher(PowerSwitchIF* powerSwitch) { this->powerSwitcher = powerSwitch; }
ReturnValue_t Heater::performOperation(uint8_t opCode) {
2022-02-02 10:29:30 +01:00
handleQueue();
handleEventQueue();
2022-02-02 10:29:30 +01:00
if (!healthHelper.healthTable->isFaulty(getObjectId())) {
reactedToBeingFaulty = false;
}
2022-02-02 10:29:30 +01:00
switch (internalState) {
2021-04-20 16:12:50 +02:00
case STATE_ON:
2022-02-02 10:29:30 +01:00
if ((powerSwitcher->getSwitchState(switch0) == PowerSwitchIF::SWITCH_OFF) ||
(powerSwitcher->getSwitchState(switch1) == PowerSwitchIF::SWITCH_OFF)) {
// switch went off on its own
// trigger event. FDIR can confirm if it is caused by MniOps and decide on the action
// do not trigger FD events when under external control
if (healthHelper.getHealth() != EXTERNAL_CONTROL) {
triggerEvent(PowerSwitchIF::SWITCH_WENT_OFF);
} else {
internalState = STATE_EXTERNAL_CONTROL;
2021-04-20 16:12:50 +02:00
}
2022-02-02 10:29:30 +01:00
}
break;
2021-04-20 16:12:50 +02:00
case STATE_OFF:
2022-02-02 10:29:30 +01:00
// check if heater is on, ie both switches are on
// if so, just command it to off, to resolve the situation or force a switch stayed on event
// But, only do anything if not already faulty (state off is the stable point for being
// faulty)
if ((!healthHelper.healthTable->isFaulty(getObjectId())) &&
(powerSwitcher->getSwitchState(switch0) == PowerSwitchIF::SWITCH_ON) &&
(powerSwitcher->getSwitchState(switch1) == PowerSwitchIF::SWITCH_ON)) {
// do not trigger FD events when under external control
if (healthHelper.getHealth() != EXTERNAL_CONTROL) {
internalState = STATE_WAIT_FOR_SWITCHES_OFF;
switchCountdown.setTimeout(powerSwitcher->getSwitchDelayMs());
powerSwitcher->sendSwitchCommand(switch0, PowerSwitchIF::SWITCH_OFF);
powerSwitcher->sendSwitchCommand(switch1, PowerSwitchIF::SWITCH_OFF);
} else {
internalState = STATE_EXTERNAL_CONTROL;
2021-04-20 16:12:50 +02:00
}
2022-02-02 10:29:30 +01:00
}
break;
2021-04-20 16:12:50 +02:00
case STATE_PASSIVE:
2022-02-02 10:29:30 +01:00
break;
2021-04-20 16:12:50 +02:00
case STATE_WAIT_FOR_SWITCHES_ON:
2022-02-02 10:29:30 +01:00
if (switchCountdown.hasTimedOut()) {
if ((powerSwitcher->getSwitchState(switch0) == PowerSwitchIF::SWITCH_OFF) ||
(powerSwitcher->getSwitchState(switch1) == PowerSwitchIF::SWITCH_OFF)) {
triggerEvent(HEATER_STAYED_OFF);
internalState = STATE_WAIT_FOR_FDIR; // wait before retrying or anything
} else {
triggerEvent(HEATER_ON);
internalState = STATE_ON;
2021-04-20 16:12:50 +02:00
}
2022-02-02 10:29:30 +01:00
}
break;
2021-04-20 16:12:50 +02:00
case STATE_WAIT_FOR_SWITCHES_OFF:
2022-02-02 10:29:30 +01:00
if (switchCountdown.hasTimedOut()) {
// only check for both being on (ie heater still on)
if ((powerSwitcher->getSwitchState(switch0) == PowerSwitchIF::SWITCH_ON) &&
(powerSwitcher->getSwitchState(switch1) == PowerSwitchIF::SWITCH_ON)) {
if (healthHelper.healthTable->isFaulty(getObjectId())) {
if (passive) {
internalState = STATE_PASSIVE;
2021-04-20 16:12:50 +02:00
} else {
2022-02-02 10:29:30 +01:00
internalState = STATE_OFF; // just accept it
2021-04-20 16:12:50 +02:00
}
2022-02-02 10:29:30 +01:00
triggerEvent(HEATER_ON); // but throw an event to make it more visible
break;
}
triggerEvent(HEATER_STAYED_ON);
internalState = STATE_WAIT_FOR_FDIR; // wait before retrying or anything
} else {
triggerEvent(HEATER_OFF);
if (passive) {
internalState = STATE_PASSIVE;
} else {
internalState = STATE_OFF;
}
2021-04-20 16:12:50 +02:00
}
2022-02-02 10:29:30 +01:00
}
break;
2021-04-20 16:12:50 +02:00
default:
2022-02-02 10:29:30 +01:00
break;
}
2022-02-02 10:29:30 +01:00
if ((powerSwitcher->getSwitchState(switch0) == PowerSwitchIF::SWITCH_ON) &&
(powerSwitcher->getSwitchState(switch1) == PowerSwitchIF::SWITCH_ON)) {
if (wasOn) {
if (heaterOnCountdown.hasTimedOut()) {
// SHOULDDO this means if a heater fails in single mode, the timeout will start again
// I am not sure if this is a bug, but atm I have no idea how to fix this and think
// it will be ok. whatcouldpossiblygowrong™
if (!timedOut) {
triggerEvent(HEATER_TIMEOUT);
timedOut = true;
2021-04-20 16:12:50 +02:00
}
2022-02-02 10:29:30 +01:00
}
2021-04-20 16:12:50 +02:00
} else {
2022-02-02 10:29:30 +01:00
wasOn = true;
heaterOnCountdown.resetTimer();
timedOut = false;
2021-04-20 16:12:50 +02:00
}
2022-02-02 10:29:30 +01:00
} else {
wasOn = false;
}
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
}
2022-02-02 10:29:30 +01:00
void Heater::setSwitch(uint8_t number, ReturnValue_t state, uint32_t* uptimeOfSwitching) {
if (powerSwitcher == NULL) {
return;
}
if (powerSwitcher->getSwitchState(number) == state) {
*uptimeOfSwitching = INVALID_UPTIME;
} else {
if ((*uptimeOfSwitching == INVALID_UPTIME)) {
powerSwitcher->sendSwitchCommand(number, state);
Clock::getUptime(uptimeOfSwitching);
2021-04-20 16:12:50 +02:00
} else {
2022-02-02 10:29:30 +01:00
uint32_t currentUptime;
Clock::getUptime(&currentUptime);
if (currentUptime - *uptimeOfSwitching > powerSwitcher->getSwitchDelayMs()) {
*uptimeOfSwitching = INVALID_UPTIME;
if (healthHelper.healthTable->isHealthy(getObjectId())) {
if (state == PowerSwitchIF::SWITCH_ON) {
triggerEvent(HEATER_STAYED_OFF);
} else {
triggerEvent(HEATER_STAYED_ON);
}
2021-04-20 16:12:50 +02:00
}
2022-02-02 10:29:30 +01:00
}
2021-04-20 16:12:50 +02:00
}
2022-02-02 10:29:30 +01:00
}
}
2022-02-02 10:29:30 +01:00
MessageQueueId_t Heater::getCommandQueue() const { return commandQueue->getId(); }
ReturnValue_t Heater::initialize() {
2022-02-02 10:29:30 +01:00
ReturnValue_t result = SystemObject::initialize();
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
EventManagerIF* manager = ObjectManager::instance()->get<EventManagerIF>(objects::EVENT_MANAGER);
if (manager == NULL) {
2022-08-16 01:08:26 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
result = manager->registerListener(eventQueue->getId());
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
ConfirmsFailuresIF* pcdu = ObjectManager::instance()->get<ConfirmsFailuresIF>(
DeviceHandlerFailureIsolation::powerConfirmationId);
if (pcdu == NULL) {
2022-08-16 01:08:26 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
pcduQueueId = pcdu->getEventReceptionQueue();
2022-02-02 10:29:30 +01:00
result = manager->subscribeToAllEventsFrom(eventQueue->getId(), getObjectId());
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
result = parameterHelper.initialize();
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
result = healthHelper.initialize();
2022-08-16 01:08:26 +02:00
if (result != returnvalue::OK) {
2022-02-02 10:29:30 +01:00
return result;
}
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
}
void Heater::handleQueue() {
2022-02-02 10:29:30 +01:00
CommandMessage command;
ReturnValue_t result = commandQueue->receiveMessage(&command);
2022-08-16 01:08:26 +02:00
if (result == returnvalue::OK) {
2022-02-02 10:29:30 +01:00
result = healthHelper.handleHealthCommand(&command);
2022-08-16 01:08:26 +02:00
if (result == returnvalue::OK) {
2022-02-02 10:29:30 +01:00
return;
2021-04-20 16:12:50 +02:00
}
2022-02-02 10:29:30 +01:00
result = parameterHelper.handleParameterMessage(&command);
2022-08-16 01:08:26 +02:00
if (result == returnvalue::OK) {
2022-02-02 10:29:30 +01:00
return;
}
}
}
ReturnValue_t Heater::getParameter(uint8_t domainId, uint8_t uniqueId,
2022-02-02 10:29:30 +01:00
ParameterWrapper* parameterWrapper,
const ParameterWrapper* newValues, uint16_t startAtIndex) {
if (domainId != DOMAIN_ID_BASE) {
return INVALID_DOMAIN_ID;
}
switch (uniqueId) {
2021-04-20 16:12:50 +02:00
case 0:
2023-03-03 14:30:35 +01:00
parameterWrapper->set(heaterOnCountdown.getTimeoutMs());
2022-02-02 10:29:30 +01:00
break;
2021-04-20 16:12:50 +02:00
default:
2022-02-02 10:29:30 +01:00
return INVALID_IDENTIFIER_ID;
}
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
}
void Heater::handleEventQueue() {
2022-02-02 10:29:30 +01:00
EventMessage event;
2022-08-16 01:08:26 +02:00
for (ReturnValue_t result = eventQueue->receiveMessage(&event); result == returnvalue::OK;
result = eventQueue->receiveMessage(&event)) {
2022-02-02 10:29:30 +01:00
switch (event.getMessageId()) {
case EventMessage::EVENT_MESSAGE:
switch (event.getEvent()) {
case Fuse::FUSE_WENT_OFF:
case HEATER_STAYED_OFF:
// HEATER_STAYED_ON is a setting if faulty does not help, but we need to reach a stable
// state and can check for being faulty before throwing this event again.
case HEATER_STAYED_ON:
if (healthHelper.healthTable->isCommandable(getObjectId())) {
healthHelper.setHealth(HasHealthIF::FAULTY);
internalState = STATE_FAULTY;
2021-04-20 16:12:50 +02:00
}
break;
2022-02-02 10:29:30 +01:00
case PowerSwitchIF::SWITCH_WENT_OFF:
internalState = STATE_WAIT;
event.setMessageId(EventMessage::CONFIRMATION_REQUEST);
if (pcduQueueId != 0) {
eventQueue->sendMessage(pcduQueueId, &event);
} else {
healthHelper.setHealth(HasHealthIF::FAULTY);
internalState = STATE_FAULTY;
}
break;
default:
return;
2021-04-20 16:12:50 +02:00
}
2022-02-02 10:29:30 +01:00
break;
case EventMessage::YOUR_FAULT:
healthHelper.setHealth(HasHealthIF::FAULTY);
internalState = STATE_FAULTY;
break;
case EventMessage::MY_FAULT:
// do nothing, we are already in STATE_WAIT and wait for a clear()
break;
default:
return;
2021-04-20 16:12:50 +02:00
}
2022-02-02 10:29:30 +01:00
}
}