eive-obsw/mission/payload/RadiationSensorHandler.cpp

261 lines
9.8 KiB
C++
Raw Normal View History

2022-01-17 15:58:27 +01:00
#include <OBSWConfig.h>
2022-02-24 11:47:32 +01:00
#include <devices/gpioIds.h>
2021-05-02 13:48:39 +02:00
#include <fsfw/datapool/PoolReadGuard.h>
#include <fsfw/tasks/TaskFactory.h>
2023-03-26 16:42:00 +02:00
#include <mission/payload/RadiationSensorHandler.h>
2023-03-26 16:13:54 +02:00
#include <mission/power/gsDefs.h>
2023-03-26 16:42:00 +02:00
#include <mission/tcs/max1227.h>
2021-05-02 13:48:39 +02:00
RadiationSensorHandler::RadiationSensorHandler(object_id_t objectId, object_id_t comIF,
2022-12-21 14:11:31 +01:00
CookieIF *comCookie, GpioIF *gpioIF,
2022-12-22 11:00:04 +01:00
Stack5VHandler &stackHandler)
: DeviceHandlerBase(objectId, comIF, comCookie),
dataset(this),
gpioIF(gpioIF),
stackHandler(stackHandler) {
2022-02-24 11:47:32 +01:00
if (comCookie == nullptr) {
2022-01-17 15:58:27 +01:00
sif::error << "RadiationSensorHandler: Invalid com cookie" << std::endl;
}
2023-07-18 18:26:57 +02:00
// Time out immediately so we get an immediate measurement at device startup.
measurementCd.timeOut();
2021-05-02 13:48:39 +02:00
}
2022-01-17 15:58:27 +01:00
RadiationSensorHandler::~RadiationSensorHandler() {}
2021-05-02 13:48:39 +02:00
2022-01-17 15:58:27 +01:00
void RadiationSensorHandler::doStartUp() {
2022-12-22 11:00:04 +01:00
if (internalState == InternalState::OFF) {
2023-01-10 11:13:34 +01:00
ReturnValue_t retval = stackHandler.deviceToOn(StackCommander::RAD_SENSOR, true);
2022-12-22 11:00:04 +01:00
if (retval == BUSY) {
return;
}
internalState = InternalState::POWER_SWITCHING;
}
if (internalState == InternalState::POWER_SWITCHING) {
if (stackHandler.isSwitchOn()) {
2022-12-22 16:18:30 +01:00
internalState = InternalState::SETUP;
2022-12-22 11:00:04 +01:00
}
}
2022-01-17 15:58:27 +01:00
if (internalState == InternalState::CONFIGURED) {
2022-02-22 20:20:14 +01:00
if (goToNormalMode) {
setMode(MODE_NORMAL);
2022-12-22 11:00:04 +01:00
} else {
2022-02-22 20:20:14 +01:00
setMode(_MODE_TO_ON);
}
2022-01-17 15:58:27 +01:00
}
2021-05-02 13:48:39 +02:00
}
void RadiationSensorHandler::doShutDown() {
2023-01-10 11:13:34 +01:00
ReturnValue_t retval = stackHandler.deviceToOff(StackCommander::RAD_SENSOR, true);
if (retval == BUSY) {
return;
}
internalState = InternalState::OFF;
setMode(_MODE_POWER_DOWN);
}
2021-05-03 12:49:14 +02:00
2022-01-17 15:58:27 +01:00
ReturnValue_t RadiationSensorHandler::buildNormalDeviceCommand(DeviceCommandId_t *id) {
2023-07-18 11:01:53 +02:00
if (measurementCd.isBusy()) {
return NOTHING_TO_SEND;
}
2022-01-17 15:58:27 +01:00
switch (communicationStep) {
2021-05-03 12:49:14 +02:00
case CommunicationStep::START_CONVERSION: {
2023-07-18 09:44:44 +02:00
*id = radSens::START_CONVERSION;
2022-01-17 15:58:27 +01:00
communicationStep = CommunicationStep::READ_CONVERSIONS;
break;
2021-05-03 12:05:19 +02:00
}
case CommunicationStep::READ_CONVERSIONS: {
2023-07-18 09:44:44 +02:00
*id = radSens::READ_CONVERSIONS;
2022-01-17 15:58:27 +01:00
communicationStep = CommunicationStep::START_CONVERSION;
break;
2021-05-03 12:05:19 +02:00
}
default: {
2022-01-17 15:58:27 +01:00
sif::debug << "RadiationSensorHandler::buildNormalDeviceCommand: Unknown communication "
<< "step" << std::endl;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-05-03 12:05:19 +02:00
}
2022-01-17 15:58:27 +01:00
}
return buildCommandFromCommand(*id, nullptr, 0);
2021-05-02 13:48:39 +02:00
}
2022-01-17 15:58:27 +01:00
ReturnValue_t RadiationSensorHandler::buildTransitionDeviceCommand(DeviceCommandId_t *id) {
if (internalState == InternalState::SETUP) {
2023-07-18 09:44:44 +02:00
*id = radSens::WRITE_SETUP;
2022-01-17 15:58:27 +01:00
} else {
return NOTHING_TO_SEND;
}
return buildCommandFromCommand(*id, nullptr, 0);
2021-05-02 13:48:39 +02:00
}
2022-01-17 15:58:27 +01:00
ReturnValue_t RadiationSensorHandler::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
const uint8_t *commandData,
size_t commandDataLen) {
switch (deviceCommand) {
2023-07-18 09:44:44 +02:00
case (radSens::WRITE_SETUP): {
cmdBuffer[0] = radSens::SETUP_DEFINITION;
2022-01-17 15:58:27 +01:00
rawPacket = cmdBuffer;
rawPacketLen = 1;
internalState = InternalState::CONFIGURED;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-01-17 15:58:27 +01:00
}
2023-07-18 09:44:44 +02:00
case (radSens::START_CONVERSION): {
2022-02-24 11:55:06 +01:00
ReturnValue_t result = gpioIF->pullHigh(gpioIds::ENABLE_RADFET);
// Test a small delay between pulling the RADFET high and reading the sensor. As long as this
// delay remains small enough, this should not cause scheduling issues. Do not make this
// delay large, this device might be scheduled inside the ACS PST!
TaskFactory::delayTask(5);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-02-24 11:55:06 +01:00
#if OBSW_VERBOSE_LEVEL >= 1
2022-12-21 14:11:31 +01:00
sif::warning
<< "RadiationSensorHandler::buildCommandFromCommand: Pulling RADFET Enable pin "
"high failed"
<< std::endl;
2022-02-24 11:55:06 +01:00
#endif
}
2022-01-17 15:58:27 +01:00
/* First the fifo will be reset here */
2023-07-18 09:44:44 +02:00
cmdBuffer[0] = radSens::RESET_DEFINITION;
cmdBuffer[1] = radSens::CONVERSION_DEFINITION;
2022-01-17 15:58:27 +01:00
rawPacket = cmdBuffer;
rawPacketLen = 2;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-01-17 15:58:27 +01:00
}
2023-07-18 09:44:44 +02:00
case (radSens::READ_CONVERSIONS): {
cmdBuffer[0] = radSens::DUMMY_BYTE;
std::memset(cmdBuffer, radSens::DUMMY_BYTE, radSens::READ_SIZE);
2022-01-17 15:58:27 +01:00
rawPacket = cmdBuffer;
2023-07-18 09:44:44 +02:00
rawPacketLen = radSens::READ_SIZE;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-01-17 15:58:27 +01:00
}
2023-07-18 09:44:44 +02:00
case radSens::ENABLE_DEBUG_OUTPUT: {
2022-04-04 13:40:45 +02:00
printPeriodicData = true;
rawPacketLen = 0;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-04-04 13:40:45 +02:00
}
2023-07-18 09:44:44 +02:00
case radSens::DISABLE_DEBUG_OUTPUT: {
2022-04-04 13:40:45 +02:00
rawPacketLen = 0;
printPeriodicData = false;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-04-04 13:40:45 +02:00
}
2021-05-03 12:49:14 +02:00
default:
2022-01-17 15:58:27 +01:00
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
}
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2021-05-02 13:48:39 +02:00
}
void RadiationSensorHandler::fillCommandAndReplyMap() {
2023-07-18 09:44:44 +02:00
this->insertInCommandMap(radSens::WRITE_SETUP);
this->insertInCommandMap(radSens::START_CONVERSION);
this->insertInCommandMap(radSens::ENABLE_DEBUG_OUTPUT);
this->insertInCommandMap(radSens::DISABLE_DEBUG_OUTPUT);
this->insertInCommandAndReplyMap(radSens::READ_CONVERSIONS, 1, &dataset, radSens::READ_SIZE);
2021-05-02 13:48:39 +02:00
}
2022-01-17 15:58:27 +01:00
ReturnValue_t RadiationSensorHandler::scanForReply(const uint8_t *start, size_t remainingSize,
DeviceCommandId_t *foundId, size_t *foundLen) {
*foundId = this->getPendingCommand();
2021-07-01 17:27:51 +02:00
2022-01-17 15:58:27 +01:00
switch (*foundId) {
2023-07-18 09:44:44 +02:00
case radSens::START_CONVERSION:
case radSens::WRITE_SETUP:
2022-04-04 13:40:45 +02:00
*foundLen = remainingSize;
2022-01-17 15:58:27 +01:00
return IGNORE_REPLY_DATA;
2023-07-18 09:44:44 +02:00
case radSens::READ_CONVERSIONS: {
2022-02-24 11:55:06 +01:00
ReturnValue_t result = gpioIF->pullLow(gpioIds::ENABLE_RADFET);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-02-24 11:55:06 +01:00
#if OBSW_VERBOSE_LEVEL >= 1
2022-03-30 18:11:21 +02:00
sif::warning << "RadiationSensorHandler::scanForReply; Pulling RADFET Enale pin "
2022-02-24 11:55:06 +01:00
"low failed"
<< std::endl;
#endif
}
2022-02-24 11:47:32 +01:00
break;
}
2023-07-18 09:44:44 +02:00
case radSens::ENABLE_DEBUG_OUTPUT:
case radSens::DISABLE_DEBUG_OUTPUT:
2022-04-04 13:40:45 +02:00
sif::info << "RadiationSensorHandler::scanForReply: " << remainingSize << std::endl;
break;
2021-07-01 17:27:51 +02:00
default:
2022-01-17 15:58:27 +01:00
break;
}
2021-07-01 17:27:51 +02:00
2022-01-17 15:58:27 +01:00
*foundLen = remainingSize;
2021-07-01 17:27:51 +02:00
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-05-02 13:48:39 +02:00
}
ReturnValue_t RadiationSensorHandler::interpretDeviceReply(DeviceCommandId_t id,
2022-01-17 15:58:27 +01:00
const uint8_t *packet) {
switch (id) {
2023-07-18 09:44:44 +02:00
case radSens::READ_CONVERSIONS: {
2022-01-17 15:58:27 +01:00
uint8_t offset = 0;
2023-07-19 15:36:06 +02:00
measurementCd.resetTimer();
2023-07-18 11:01:53 +02:00
{
PoolReadGuard readSet(&dataset);
uint16_t tempRaw = ((packet[offset] & 0x0f) << 8) | packet[offset + 1];
dataset.temperatureCelcius = max1227::getTemperature(tempRaw);
offset += 2;
dataset.ain0 = (*(packet + offset) << 8) | *(packet + offset + 1);
offset += 2;
dataset.ain1 = (*(packet + offset) << 8) | *(packet + offset + 1);
offset += 6;
dataset.ain4 = (*(packet + offset) << 8) | *(packet + offset + 1);
offset += 2;
dataset.ain5 = (*(packet + offset) << 8) | *(packet + offset + 1);
offset += 2;
dataset.ain6 = (*(packet + offset) << 8) | *(packet + offset + 1);
offset += 2;
dataset.ain7 = (*(packet + offset) << 8) | *(packet + offset + 1);
dataset.setValidity(true, true);
}
2022-03-17 10:48:35 +01:00
if (printPeriodicData) {
sif::info << "Radiation sensor temperature: " << dataset.temperatureCelcius << " °C"
<< std::dec << std::endl;
sif::info << "Radiation sensor ADC value channel 0: " << dataset.ain0 << std::endl;
sif::info << "Radiation sensor ADC value channel 1: " << dataset.ain1 << std::endl;
sif::info << "Radiation sensor ADC value channel 4: " << dataset.ain4 << std::endl;
sif::info << "Radiation sensor ADC value channel 5: " << dataset.ain5 << std::endl;
sif::info << "Radiation sensor ADC value channel 6: " << dataset.ain6 << std::endl;
sif::info << "Radiation sensor ADC value channel 7: " << dataset.ain7 << std::endl;
}
2023-07-18 11:01:53 +02:00
ReturnValue_t result =
getHkManagerHandle()->generateHousekeepingPacket(dataset.getSid(), &dataset, true);
if (result != returnvalue::OK) {
// TODO: Maybe add event?
sif::error << "Generating HK set for radiation sensor failed" << std::endl;
}
2022-01-17 15:58:27 +01:00
break;
2021-05-03 12:49:14 +02:00
}
default: {
2022-01-17 15:58:27 +01:00
sif::debug << "RadiationSensorHandler::interpretDeviceReply: Unknown reply id" << std::endl;
return DeviceHandlerIF::UNKNOWN_DEVICE_REPLY;
2021-05-03 12:49:14 +02:00
}
2022-01-17 15:58:27 +01:00
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-05-02 13:48:39 +02:00
}
2022-01-17 15:58:27 +01:00
uint32_t RadiationSensorHandler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) {
return 5000;
2021-05-02 13:48:39 +02:00
}
2022-01-17 15:58:27 +01:00
ReturnValue_t RadiationSensorHandler::initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
LocalDataPoolManager &poolManager) {
2023-07-18 09:44:44 +02:00
localDataPoolMap.emplace(radSens::TEMPERATURE_C, new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(radSens::AIN0, new PoolEntry<uint16_t>({0}));
localDataPoolMap.emplace(radSens::AIN1, new PoolEntry<uint16_t>({0}));
localDataPoolMap.emplace(radSens::AIN4, new PoolEntry<uint16_t>({0}));
localDataPoolMap.emplace(radSens::AIN5, new PoolEntry<uint16_t>({0}));
localDataPoolMap.emplace(radSens::AIN6, new PoolEntry<uint16_t>({0}));
localDataPoolMap.emplace(radSens::AIN7, new PoolEntry<uint16_t>({0}));
2023-07-18 11:01:53 +02:00
// It should normally not be necessary to enable this set, as a sample TM will be generated
// after a measurement. If this is still enabled, sample with double the measurement frequency
// to ensure we get all measurements.
2022-08-15 11:57:57 +02:00
poolManager.subscribeForRegularPeriodicPacket(
2023-07-18 11:01:53 +02:00
subdp::RegularHkPeriodicParams(dataset.getSid(), false, DEFAULT_MEASUREMENT_CD_MS / 2));
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-05-02 13:48:39 +02:00
}
2022-02-22 20:20:14 +01:00
void RadiationSensorHandler::setToGoToNormalModeImmediately() { this->goToNormalMode = true; }
2022-03-17 10:48:35 +01:00
void RadiationSensorHandler::enablePeriodicDataPrint(bool enable) {
this->printPeriodicData = enable;
}