This commit is contained in:
Robin Müller 2022-02-22 13:27:21 +01:00
parent eb1b4f1b05
commit cad967ec56
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
3 changed files with 566 additions and 0 deletions

View File

@ -0,0 +1,416 @@
#include "PayloadPcduHandler.h"
#include <fsfw/src/fsfw/datapool/PoolReadGuard.h>
#include "devices/gpioIds.h"
PayloadPcduHandler::PayloadPcduHandler(object_id_t objectId, object_id_t comIF, CookieIF* cookie,
GpioIF* gpioIF, bool periodicPrintout)
: DeviceHandlerBase(objectId, comIF, cookie),
adcSet(this),
periodicPrintout(periodicPrintout),
gpioIF(gpioIF) {}
void PayloadPcduHandler::doStartUp() {
if ((state != States::PCDU_OFF) and (state != States::ON_TRANS_SSR)) {
// Config error
sif::error << "PayloadPcduHandler::doStartUp: Invalid state" << std::endl;
}
if (state == States::PCDU_OFF) {
// Switch on relays here
gpioIF->pullHigh(gpioIds::PLPCDU_ENB_VBAT0);
gpioIF->pullHigh(gpioIds::PLPCDU_ENB_VBAT1);
state = States::ON_TRANS_SSR;
transitionOk = true;
}
if (state == States::ON_TRANS_SSR) {
// If necessary, check whether a certain amount of time has elapsed
if (transitionOk) {
transitionOk = false;
// We are now in ON mode
startTransition(MODE_NORMAL, 0);
adcState = AdcStates::BOOT_DELAY;
// The ADC can now be read. If the values are not close to zero, we should not allow
// transition
monMode = MonitoringMode::CLOSE_TO_ZERO;
}
}
}
void PayloadPcduHandler::doTransition(Mode_t modeFrom, Submode_t subModeFrom) {
if (mode == _MODE_TO_NORMAL) {
stateMachineToNormal();
}
}
void PayloadPcduHandler::doShutDown() {}
ReturnValue_t PayloadPcduHandler::buildNormalDeviceCommand(DeviceCommandId_t* id) {
switch (adcState) {
case (AdcStates::SEND_SETUP): {
*id = plpcdu::SETUP_CMD;
buildCommandFromCommand(*id, nullptr, 0);
break;
}
case (AdcStates::NORMAL): {
*id = plpcdu::READ_WITH_TEMP;
buildCommandFromCommand(*id, nullptr, 0);
break;
}
default: {
break;
}
}
return NOTHING_TO_SEND;
}
ReturnValue_t PayloadPcduHandler::buildTransitionDeviceCommand(DeviceCommandId_t* id) {
if (adcState == AdcStates::SEND_SETUP) {
*id = plpcdu::SETUP_CMD;
buildCommandFromCommand(*id, nullptr, 0);
}
return NOTHING_TO_SEND;
}
void PayloadPcduHandler::fillCommandAndReplyMap() {
insertInCommandAndReplyMap(plpcdu::READ_CMD, 2, &adcSet);
insertInCommandAndReplyMap(plpcdu::READ_TEMP, 1, &adcSet);
insertInCommandAndReplyMap(plpcdu::READ_WITH_TEMP, 1, &adcSet);
insertInCommandAndReplyMap(plpcdu::SETUP_CMD, 1);
}
ReturnValue_t PayloadPcduHandler::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
const uint8_t* commandData,
size_t commandDataLen) {
switch (deviceCommand) {
case (plpcdu::SETUP_CMD): {
cmdBuf[0] = plpcdu::SETUP_BYTE;
rawPacket = cmdBuf.data();
rawPacketLen = 1;
break;
}
case (plpcdu::READ_CMD): {
max1227::prepareExternallyClockedRead0ToN(cmdBuf.data(), plpcdu::CHANNEL_N, rawPacketLen);
rawPacket = cmdBuf.data();
break;
}
case (plpcdu::READ_TEMP): {
max1227::prepareExternallyClockedTemperatureRead(cmdBuf.data(), rawPacketLen);
rawPacket = cmdBuf.data();
break;
}
case (plpcdu::READ_WITH_TEMP): {
size_t sz = 0;
max1227::prepareExternallyClockedRead0ToN(cmdBuf.data(), plpcdu::CHANNEL_N, sz);
max1227::prepareExternallyClockedTemperatureRead(cmdBuf.data() + sz, sz);
rawPacketLen = sz;
rawPacket = cmdBuf.data();
}
}
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
}
ReturnValue_t PayloadPcduHandler::scanForReply(const uint8_t* start, size_t remainingSize,
DeviceCommandId_t* foundId, size_t* foundLen) {
// SPI is full duplex
*foundId = getPendingCommand();
*foundLen = remainingSize;
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t PayloadPcduHandler::interpretDeviceReply(DeviceCommandId_t id,
const uint8_t* packet) {
using namespace plpcdu;
switch (id) {
case (SETUP_CMD): {
break;
}
case (READ_TEMP): {
uint8_t tempStartIdx = TEMP_REPLY_SIZE - 2;
adcSet.tempC.value =
max1227::getTemperature(packet[tempStartIdx] << 8 | packet[tempStartIdx + 2]);
break;
}
case (READ_CMD): {
PoolReadGuard pg(&adcSet);
if (pg.getReadResult() != HasReturnvaluesIF::RETURN_OK) {
return pg.getReadResult();
}
handleExtConvRead(packet);
handlePrintout();
break;
}
case (READ_WITH_TEMP): {
PoolReadGuard pg(&adcSet);
if (pg.getReadResult() != HasReturnvaluesIF::RETURN_OK) {
return pg.getReadResult();
}
handleExtConvRead(packet);
uint8_t tempStartIdx = ADC_REPLY_SIZE + TEMP_REPLY_SIZE - 2;
adcSet.tempC.value =
max1227::getTemperature(packet[tempStartIdx] << 8 | packet[tempStartIdx + 2]);
handlePrintout();
break;
}
default: {
break;
}
}
return HasReturnvaluesIF::RETURN_OK;
}
uint32_t PayloadPcduHandler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) {
// 20 minutes transition delay is allowed
return 20 * 60 * 60;
}
ReturnValue_t PayloadPcduHandler::initializeLocalDataPool(localpool::DataPool& localDataPoolMap,
LocalDataPoolManager& poolManager) {
localDataPoolMap.emplace(plpcdu::PlPcduPoolIds::CHANNEL_VEC, &channelValues);
localDataPoolMap.emplace(plpcdu::PlPcduPoolIds::TEMP, &tempC);
return HasReturnvaluesIF::RETURN_OK;
}
void PayloadPcduHandler::setToGoToNormalModeImmediately(bool enable) {
this->goToNormalMode = enable;
}
void PayloadPcduHandler::handleExtConvRead(const uint8_t* bufStart) {
for (uint8_t idx = 0; idx < 12; idx++) {
adcSet.channels[idx] = bufStart[idx * 2 + 1] << 8 | bufStart[idx * 2 + 2];
}
}
void PayloadPcduHandler::handlePrintout() {
if (periodicPrintout) {
if (opDivider.checkAndIncrement()) {
sif::info << "PL PCDU ADC hex [" << std::hex << std::setw(4);
for (uint8_t idx = 0; idx < 12; idx++) {
sif::info << std::setfill('0') << adcSet.channels[idx];
if (idx < 11) {
sif::info << ",";
}
}
sif::info << "] | T[C] " << std::dec << adcSet.tempC.value;
}
}
}
void PayloadPcduHandler::enablePeriodicPrintout(bool enable, uint8_t divider) {
this->periodicPrintout = enable;
opDivider.setDivider(divider);
}
void PayloadPcduHandler::stateMachineToNormal() {
if (adcState == AdcStates::BOOT_DELAY) {
if (adcCountdown.hasTimedOut()) {
adcState = AdcStates::SEND_SETUP;
adcCmdExecuted = false;
}
}
if (adcState == AdcStates::SEND_SETUP) {
if (adcCmdExecuted) {
adcState = AdcStates::NORMAL;
setMode(MODE_NORMAL, plpcdu::NORMAL_ADC_ONLY);
adcCmdExecuted = false;
}
}
if (submode == plpcdu::NORMAL_ALL_ON) {
if (state == States::ON_TRANS_ADC_CLOSE_ZERO) {
if (not commandExecuted) {
countdown.resetTimer();
commandExecuted = true;
}
// ADC values are ok, 5 seconds have elapsed
if (transitionOk and countdown.hasTimedOut()) {
state = States::ON_TRANS_DRO;
// Now start monitoring for negative voltages instead
monMode = MonitoringMode::NEGATIVE;
countdown.resetTimer();
commandExecuted = false;
transitionOk = false;
}
}
if (state == States::ON_TRANS_DRO) {
if (not commandExecuted) {
// Switch on DRO and start monitoring for negative voltagea
gpioIF->pullHigh(gpioIds::PLPCDU_ENB_DRO);
commandExecuted = true;
}
// ADC values are ok, 5 seconds have elapsed
if (transitionOk and countdown.hasTimedOut()) {
state = States::ON_TRANS_X8;
countdown.resetTimer();
commandExecuted = false;
transitionOk = false;
}
}
if (state == States::ON_TRANS_X8) {
if (not commandExecuted) {
// Switch on X8
gpioIF->pullHigh(gpioIds::PLPCDU_ENB_X8);
commandExecuted = true;
}
// ADC values are ok, 5 seconds have elapsed
if (transitionOk and countdown.hasTimedOut()) {
state = States::ON_TRANS_TX;
countdown.resetTimer();
commandExecuted = false;
transitionOk = false;
}
}
if (state == States::ON_TRANS_TX) {
if (not commandExecuted) {
// Switch on TX
gpioIF->pullHigh(gpioIds::PLPCDU_ENB_TX);
commandExecuted = true;
}
// ADC values are ok, 5 seconds have elapsed
if (transitionOk and countdown.hasTimedOut()) {
state = States::ON_TRANS_MPA;
countdown.resetTimer();
commandExecuted = false;
transitionOk = false;
}
}
if (state == States::ON_TRANS_MPA) {
if (not commandExecuted) {
// Switch on MPA
gpioIF->pullHigh(gpioIds::PLPCDU_ENB_MPA);
commandExecuted = true;
}
// ADC values are ok, 5 seconds have elapsed
if (transitionOk and countdown.hasTimedOut()) {
state = States::ON_TRANS_HPA;
countdown.resetTimer();
commandExecuted = false;
transitionOk = false;
}
}
if (state == States::ON_TRANS_HPA) {
if (not commandExecuted) {
// Switch on HPA
gpioIF->pullHigh(gpioIds::PLPCDU_ENB_HPA);
commandExecuted = true;
}
// ADC values are ok, 5 seconds have elapsed
if (transitionOk and countdown.hasTimedOut()) {
state = States::PCDU_ON;
setMode(MODE_NORMAL, plpcdu::NORMAL_ALL_ON);
countdown.resetTimer();
commandExecuted = false;
transitionOk = false;
}
}
}
}
void PayloadPcduHandler::doShutDown() {}
ReturnValue_t PayloadPcduHandler::buildNormalDeviceCommand(DeviceCommandId_t* id) {
switch(adcState) {
case(AdcStates::SEND_SETUP): {
*id = plpcdu::SETUP_CMD;
buildCommandFromCommand(*id, nullptr, 0);
break;
}
case(AdcStates::NORMAL): {
*id = plpcdu::READ_WITH_TEMP;
buildCommandFromCommand(*id, nullptr, 0);
break;
}
default: {
break;
}
}
return NOTHING_TO_SEND;
}
ReturnValue_t PayloadPcduHandler::buildTransitionDeviceCommand(DeviceCommandId_t* id) {
if(adcState == AdcStates::SEND_SETUP) {
*id = plpcdu::SETUP_CMD;
buildCommandFromCommand(*id, nullptr, 0);
}
return HasReturnvaluesIF::RETURN_OK;
}
void PayloadPcduHandler::fillCommandAndReplyMap() {
insertInCommandAndReplyMap(plpcdu::READ_CMD, 2, &adcSet);
insertInCommandAndReplyMap(plpcdu::READ_TEMP, 1, &adcSet);
insertInCommandAndReplyMap(plpcdu::READ_WITH_TEMP, 1, &adcSet);
insertInCommandAndReplyMap(plpcdu::SETUP_CMD, 1);
}
ReturnValue_t PayloadPcduHandler::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
const uint8_t* commandData,
size_t commandDataLen) {
switch(deviceCommand) {
case(plpcdu::SETUP_CMD): {
cmdBuf[0] = plpcdu::SETUP_BYTE;
rawPacket = cmdBuf.data();
rawPacketLen = 1;
break;
}
case(plpcdu::READ_CMD): {
max1227::prepareExternallyClockedRead0ToN(cmdBuf.data(), plpcdu::CHANNEL_N, rawPacketLen);
rawPacket = cmdBuf.data();
break;
}
case(plpcdu::READ_TEMP): {
max1227::prepareExternallyClockedTemperatureRead(cmdBuf.data(), rawPacketLen);
rawPacket = cmdBuf.data();
break;
}
case(plpcdu::READ_WITH_TEMP): {
size_t sz = 0;
max1227::prepareExternallyClockedRead0ToN(cmdBuf.data(), plpcdu::CHANNEL_N, sz);
max1227::prepareExternallyClockedTemperatureRead(cmdBuf.data() + sz, sz);
rawPacketLen = sz;
rawPacket = cmdBuf.data();
}
}
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
}
ReturnValue_t PayloadPcduHandler::scanForReply(const uint8_t* start, size_t remainingSize,
DeviceCommandId_t* foundId, size_t* foundLen) {
// SPI is full duplex
*foundId = getPendingCommand();
*foundLen = remainingSize;
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t PayloadPcduHandler::interpretDeviceReply(DeviceCommandId_t id,
const uint8_t* packet) {
using namespace plpcdu;
switch(id) {
case(SETUP_CMD): {
break;
}
case(READ_WITH_TEMP): {
PoolReadGuard pg(&adcSet);
if(pg.getReadResult() != HasReturnvaluesIF::RETURN_OK) {
return pg.getReadResult();
}
for(uint8_t idx = 0; idx < 12; idx ++) {
adcSet.channels[idx] = packet[idx * 2 + 1] << 8 | packet[idx * 2 + 2];
}
uint8_t tempStartIdx = ADC_REPLY_SIZE + TEMP_REPLY_SIZE - 2;
adcSet.tempC.value = max1227::getTemperature(packet[tempStartIdx] << 8 | packet[tempStartIdx + 1]);
}
}
return HasReturnvaluesIF::RETURN_OK;
}
uint32_t PayloadPcduHandler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) {
// 20 minutes transition delay is allowed
return 20 * 60 * 60;
}
ReturnValue_t PayloadPcduHandler::initializeLocalDataPool(localpool::DataPool& localDataPoolMap,
LocalDataPoolManager& poolManager) {
localDataPoolMap.emplace(plpcdu::PlPcduPoolIds::CHANNEL_VEC, &channelValues);
localDataPoolMap.emplace(plpcdu::PlPcduPoolIds::TEMP, &tempC);
return HasReturnvaluesIF::RETURN_OK;
}

View File

@ -0,0 +1,97 @@
#ifndef LINUX_DEVICES_PLPCDUHANDLER_H_
#define LINUX_DEVICES_PLPCDUHANDLER_H_
#include <fsfw/devicehandlers/DeviceHandlerBase.h>
#include <fsfw/globalfunctions/PeriodicOperationDivider.h>
#include <fsfw/timemanager/Countdown.h>
#include "devicedefinitions/payloadPcduDefinitions.h"
#include "fsfw_hal/common/gpio/GpioIF.h"
/**
* @brief Device handler for the EIVE Payload PCDU
* @details
* Documentation:
* https://egit.irs.uni-stuttgart.de/eive/eive_dokumente/src/branch/master/400_Raumsegment/412_PayloaPCDUDocumentation/release/EIVE-D-421-001_PLPCDU_Documentation.pdf
*
* Important components:
* - SSR - Solid State Relay: Decouples voltages from battery
* - DRO - Dielectric Resonsant Oscillator: Generates modulation signal
* - X8: Frequency X8 Multiplicator
* - TX: Transmitter/Sender module. Modulates data onto carrier signal
* - MPA - Medium Power Amplifier
* - HPA - High Power Amplifier
*/
class PayloadPcduHandler : public DeviceHandlerBase {
public:
PayloadPcduHandler(object_id_t objectId, object_id_t comIF, CookieIF* cookie, GpioIF* gpioIF,
bool periodicPrintout);
void setToGoToNormalModeImmediately(bool enable);
void enablePeriodicPrintout(bool enable, uint8_t divider);
private:
enum class States {
PCDU_OFF,
// Solid State Relay, enable battery voltages VBAT0 and VBAT1. This will also switch on
// the ADC
ON_TRANS_SSR,
ON_TRANS_ADC_CLOSE_ZERO,
// Enable Dielectric Resonant Oscillator and start monitoring voltages as
// soon as DRO voltage reaches 6V
ON_TRANS_DRO,
// Switch on X8 compoennt and monitor voltages for 5 seconds
ON_TRANS_X8,
// Switch on TX component and monitor voltages for 5 seconds
ON_TRANS_TX,
// Switch on MPA component and monitor voltages for 5 seconds
ON_TRANS_MPA,
// Switch on HPA component and monitor voltages for 5 seconds
ON_TRANS_HPA,
// All components of the experiment are on
PCDU_ON,
} state = States::PCDU_OFF;
enum class MonitoringMode { NONE, CLOSE_TO_ZERO, NEGATIVE } monMode = MonitoringMode::NONE;
enum class AdcStates { OFF, BOOT_DELAY, SEND_SETUP, NORMAL } adcState = AdcStates::OFF;
bool goToNormalMode = false;
plpcdu::PlPcduAdcSet adcSet;
std::array<uint8_t, plpcdu::MAX_ADC_REPLY_SIZE> cmdBuf = {};
// This variable is tied to DRO +6 V voltage. Voltages, currents are monitored and the experiment
// is shut down immediately if there is a negative voltage.
bool transitionOk = false;
bool commandExecuted = false;
bool adcCmdExecuted = false;
bool periodicPrintout = false;
PeriodicOperationDivider opDivider = PeriodicOperationDivider(5);
uint8_t tempReadDivisor = 1;
Countdown countdown = Countdown(5000);
Countdown adcCountdown = Countdown(50);
GpioIF* gpioIF;
PoolEntry<uint16_t> channelValues = PoolEntry<uint16_t>({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
PoolEntry<float> tempC = PoolEntry<float>({0.0});
void doTransition(Mode_t modeFrom, Submode_t subModeFrom) override;
void doStartUp() override;
void doShutDown() override;
ReturnValue_t buildNormalDeviceCommand(DeviceCommandId_t* id) override;
ReturnValue_t buildTransitionDeviceCommand(DeviceCommandId_t* id) override;
void fillCommandAndReplyMap() override;
ReturnValue_t buildCommandFromCommand(DeviceCommandId_t deviceCommand, const uint8_t* commandData,
size_t commandDataLen) override;
ReturnValue_t scanForReply(const uint8_t* start, size_t remainingSize, DeviceCommandId_t* foundId,
size_t* foundLen) override;
ReturnValue_t interpretDeviceReply(DeviceCommandId_t id, const uint8_t* packet) override;
uint32_t getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) override;
ReturnValue_t initializeLocalDataPool(localpool::DataPool& localDataPoolMap,
LocalDataPoolManager& poolManager) override;
void handleExtConvRead(const uint8_t* bufStart);
void handlePrintout();
void stateMachineToNormal();
};
#endif /* LINUX_DEVICES_PLPCDUHANDLER_H_ */

View File

@ -0,0 +1,53 @@
#ifndef LINUX_DEVICES_DEVICEDEFINITIONS_PAYLOADPCDUDEFINITIONS_H_
#define LINUX_DEVICES_DEVICEDEFINITIONS_PAYLOADPCDUDEFINITIONS_H_
#include <fsfw/datapoollocal/StaticLocalDataSet.h>
#include <fsfw/devicehandlers/DeviceHandlerIF.h>
#include <cstddef>
#include "mission/devices/max1227.h"
namespace plpcdu {
using namespace max1227;
enum PlPcduPoolIds : uint32_t { CHANNEL_VEC = 0, TEMP = 1 };
static constexpr size_t MAX_ADC_REPLY_SIZE = 64;
static constexpr DeviceCommandId_t READ_CMD = 0;
static constexpr DeviceCommandId_t SETUP_CMD = 1;
static constexpr DeviceCommandId_t READ_TEMP = 2;
static constexpr DeviceCommandId_t READ_WITH_TEMP = 3;
static constexpr Submode_t NORMAL_ADC_ONLY = 0;
static constexpr Submode_t NORMAL_ALL_ON = 1;
// 12 ADC values * 2 + trailing zero
static constexpr size_t ADC_REPLY_SIZE = 25;
// Conversion byte + 24 * zero
static constexpr size_t TEMP_REPLY_SIZE = 25;
static constexpr uint8_t SETUP_BYTE =
max1227::buildSetupByte(ClkSel::EXT_CONV_EXT_TIMED, RefSel::INT_REF_NO_WAKEUP, DiffSel::NONE_0);
static constexpr uint32_t ADC_SET_ID = READ_CMD;
static constexpr uint8_t CHANNELS_NUM = 12;
static constexpr uint8_t CHANNEL_N = CHANNELS_NUM - 1;
// Store temperature as well
static constexpr size_t DATASET_ENTRIES = CHANNELS_NUM + 1;
class PlPcduAdcSet : public StaticLocalDataSet<DATASET_ENTRIES> {
public:
PlPcduAdcSet(HasLocalDataPoolIF* owner) : StaticLocalDataSet(owner, ADC_SET_ID) {}
PlPcduAdcSet(object_id_t objectId) : StaticLocalDataSet(sid_t(objectId, ADC_SET_ID)) {}
lp_vec_t<uint16_t, 12> channels = lp_vec_t<uint16_t, 12>(sid.objectId, CHANNEL_VEC, this);
lp_var_t<float> tempC = lp_var_t<float>(sid.objectId, TEMP, this);
};
} // namespace plpcdu
#endif /* LINUX_DEVICES_DEVICEDEFINITIONS_PAYLOADPCDUDEFINITIONS_H_ */