meggert
0bfd31bc7e
All checks were successful
EIVE/eive-obsw/pipeline/pr-main This commit looks good
219 lines
6.7 KiB
C++
219 lines
6.7 KiB
C++
|
|
#include <mission/controller/PowerController.h>
|
|
|
|
PowerController::PowerController(object_id_t objectId, bool enableHkSets)
|
|
: ExtendedControllerBase(objectId),
|
|
enableHkSets(enableHkSets),
|
|
parameterHelper(this),
|
|
pwrCtrlCoreHk(this) {}
|
|
|
|
ReturnValue_t PowerController::initialize() {
|
|
ReturnValue_t result = parameterHelper.initialize();
|
|
if (result != returnvalue::OK) {
|
|
return result;
|
|
}
|
|
sif::debug << "Rush B, no stop" << std::endl;
|
|
return ExtendedControllerBase::initialize();
|
|
}
|
|
|
|
ReturnValue_t PowerController::handleCommandMessage(CommandMessage *message) {
|
|
ReturnValue_t result = actionHelper.handleActionMessage(message);
|
|
if (result == returnvalue::OK) {
|
|
return result;
|
|
}
|
|
result = parameterHelper.handleParameterMessage(message);
|
|
if (result == returnvalue::OK) {
|
|
return result;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
MessageQueueId_t PowerController::getCommandQueue() const { return commandQueue->getId(); }
|
|
|
|
ReturnValue_t PowerController::getParameter(uint8_t domainId, uint8_t parameterId,
|
|
ParameterWrapper *parameterWrapper,
|
|
const ParameterWrapper *newValues,
|
|
uint16_t startAtIndex) {
|
|
switch (domainId) {
|
|
case 0x0: // direct members
|
|
switch (parameterId) {
|
|
case 0x0:
|
|
parameterWrapper->set(batteryInternalResistance);
|
|
break;
|
|
case 0x1:
|
|
parameterWrapper->set(batteryMaximumCapacity);
|
|
break;
|
|
default:
|
|
return INVALID_IDENTIFIER_ID;
|
|
}
|
|
break;
|
|
default:
|
|
return INVALID_DOMAIN_ID;
|
|
};
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
void PowerController::performControlOperation() {
|
|
switch (internalState) {
|
|
case InternalState::STARTUP: {
|
|
initialCountdown.resetTimer();
|
|
internalState = InternalState::INITIAL_DELAY;
|
|
return;
|
|
}
|
|
case InternalState::INITIAL_DELAY: {
|
|
if (initialCountdown.hasTimedOut()) {
|
|
internalState = InternalState::READY;
|
|
}
|
|
return;
|
|
}
|
|
case InternalState::READY: {
|
|
// if (mode != MODE_OFF) {
|
|
sif::debug << "oh shit, now i gotta do something" << std::endl;
|
|
calculateStateOfCharge();
|
|
// do something
|
|
//}
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
ReturnValue_t PowerController::initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
|
|
LocalDataPoolManager &poolManager) {
|
|
localDataPoolMap.emplace(pwrctrl::PoolIds::TOTAL_BATTERY_CURRENT, new PoolEntry<int16_t>({0}));
|
|
localDataPoolMap.emplace(pwrctrl::PoolIds::OPEN_CIRCUIT_VOLTAGE_CHARGE,
|
|
new PoolEntry<float>({0.0}));
|
|
localDataPoolMap.emplace(pwrctrl::PoolIds::COULOMB_COUNTER_CHARGE, new PoolEntry<float>({0.0}));
|
|
poolManager.subscribeForRegularPeriodicPacket({pwrCtrlCoreHk.getSid(), enableHkSets, 60.0});
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
LocalPoolDataSetBase *PowerController::getDataSetHandle(sid_t sid) {
|
|
switch (sid.ownerSetId) {
|
|
case pwrctrl::CORE_HK:
|
|
return &pwrCtrlCoreHk;
|
|
default:
|
|
return nullptr;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
ReturnValue_t PowerController::checkModeCommand(Mode_t mode, Submode_t submode,
|
|
uint32_t *msToReachTheMode) {
|
|
if (mode == MODE_OFF) {
|
|
if (submode == SUBMODE_NONE) {
|
|
return returnvalue::OK;
|
|
} else {
|
|
return INVALID_SUBMODE;
|
|
}
|
|
}
|
|
return INVALID_MODE;
|
|
}
|
|
|
|
void PowerController::calculateStateOfCharge() {
|
|
// get time
|
|
Clock::getClock_timeval(&now);
|
|
|
|
// update EPS HK values
|
|
ReturnValue_t result = updateEpsData();
|
|
if (result != returnvalue::OK) {
|
|
triggerEvent(power::DATASET_READ_FAILED);
|
|
sif::info << "Power Controller::Reading of Datasets has failed" << std::endl;
|
|
return;
|
|
}
|
|
|
|
// calculate total battery current
|
|
iBat = p60CoreHk.batteryCurrent.value + bpxBatteryHk.heaterCurrent.value +
|
|
bpxBatteryHk.dischargeCurrent.value;
|
|
|
|
calculateOpenCircuitVoltageCharge();
|
|
calculateCoulombCounterCharge();
|
|
|
|
// commit to dataset
|
|
{
|
|
PoolReadGuard pg(&pwrCtrlCoreHk);
|
|
if (pg.getReadResult() == returnvalue::OK) {
|
|
pwrCtrlCoreHk.totalBatteryCurrent.value = iBat;
|
|
pwrCtrlCoreHk.openCircuitVoltageCharge.value = charge2stateOfCharge(openCircuitVoltageCharge);
|
|
pwrCtrlCoreHk.coulombCounterCharge.value = charge2stateOfCharge(coulombCounterCharge);
|
|
pwrCtrlCoreHk.setValidity(true, true);
|
|
}
|
|
}
|
|
|
|
// store time for next run
|
|
oldTime = now;
|
|
}
|
|
|
|
void PowerController::calculateOpenCircuitVoltageCharge() {
|
|
float vBatCorrected = p60CoreHk.batteryVoltage.value - iBat * batteryInternalResistance;
|
|
if (vBatCorrected >= lookUpTableOcv[1][100]) {
|
|
triggerEvent(power::VOLTAGE_OUT_OF_BOUNDS, 0);
|
|
sif::error << "Power Controller::Voltage is too high" << std::endl;
|
|
return;
|
|
} else if (vBatCorrected <= lookUpTableOcv[1][0]) {
|
|
triggerEvent(power::VOLTAGE_OUT_OF_BOUNDS, 1);
|
|
sif::error << "Power Controller::Voltage is too low" << std::endl;
|
|
return;
|
|
}
|
|
uint8_t lookUpTableIdx = 99;
|
|
while (lookUpTableOcv[1][lookUpTableIdx] > vBatCorrected) {
|
|
lookUpTableIdx--;
|
|
}
|
|
|
|
openCircuitVoltageCharge = linearInterpolation(
|
|
vBatCorrected, lookUpTableOcv[1][lookUpTableIdx], lookUpTableOcv[1][lookUpTableIdx + 1],
|
|
lookUpTableOcv[0][lookUpTableIdx], lookUpTableOcv[0][lookUpTableIdx + 1]);
|
|
}
|
|
|
|
void PowerController::calculateCoulombCounterCharge() {
|
|
if ((pwrCtrlCoreHk.coulombCounterCharge.value == 0) or
|
|
(p60CoreHk.batteryVoltage.value > coulombCounterVoltageUpperThreshold and
|
|
pwrCtrlCoreHk.coulombCounterCharge.value >= coulombCounterChargeUpperThreshold)) {
|
|
coulombCounterCharge = openCircuitVoltageCharge;
|
|
}
|
|
|
|
else {
|
|
double timeDiff = timevalOperations::toDouble(now - oldTime);
|
|
coulombCounterCharge = pwrCtrlCoreHk.coulombCounterCharge.value + iBat * timeDiff;
|
|
}
|
|
}
|
|
|
|
ReturnValue_t PowerController::updateEpsData() {
|
|
std::vector<ReturnValue_t> results;
|
|
{
|
|
PoolReadGuard pgBat(&bpxBatteryHk);
|
|
results.push_back(pgBat.getReadResult());
|
|
}
|
|
{
|
|
PoolReadGuard pgP60(&p60CoreHk);
|
|
results.push_back(pgP60.getReadResult());
|
|
}
|
|
{
|
|
PoolReadGuard pgPdu1(&pdu1CoreHk);
|
|
results.push_back(pgPdu1.getReadResult());
|
|
}
|
|
{
|
|
PoolReadGuard pgPdu2(&pdu2CoreHk);
|
|
results.push_back(pgPdu2.getReadResult());
|
|
}
|
|
{
|
|
PoolReadGuard pgAcu(&acuCoreHk);
|
|
results.push_back(pgAcu.getReadResult());
|
|
}
|
|
for (const auto &result : results) {
|
|
if (result != returnvalue::OK) {
|
|
return result;
|
|
}
|
|
}
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
float PowerController::charge2stateOfCharge(float capacity) {
|
|
return capacity / batteryMaximumCapacity;
|
|
}
|
|
|
|
float PowerController::linearInterpolation(float x, float x0, float x1, float y0, float y1) {
|
|
return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
|
|
}
|