Skip to content

Commit

Permalink
merging functionality from PL refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteSchm committed Apr 13, 2023
1 parent 9999fa2 commit ee37682
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
7 changes: 4 additions & 3 deletions include/PowerLimiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <memory>

typedef enum {
STATE_PL_SHUTDOWN = 0,
STATE_ACTIVE
SHUTDOWN = 0,
ACTIVE
} plStates;

typedef enum {
Expand All @@ -32,8 +32,9 @@ class PowerLimiterClass {
uint32_t _lastLoop = 0;
int32_t _lastRequestedPowerLimit = 0;
uint32_t _lastLimitSetTime = 0;
plStates _plState = STATE_ACTIVE;
plStates _plState = ACTIVE;
bool _disabled = false;
bool _batteryDischargeEnabled = false;

float _powerMeter1Power;
float _powerMeter2Power;
Expand Down
47 changes: 24 additions & 23 deletions src/PowerLimiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ void PowerLimiterClass::loop()

_lastLoop = millis();

// Debug state transistions


// Debug state transistions, TODO: Remove
MessageOutput.printf("****************** PL STATE: %i\r\n", _plState);

std::shared_ptr<InverterAbstract> inverter = Hoymiles.getInverterByPos(config.PowerLimiter_InverterId);
Expand All @@ -45,26 +43,26 @@ void PowerLimiterClass::loop()

// Make sure inverter is turned off if PL is disabled by user
// Make sure inverter is turned off when lower battery threshold is reached
// In this case we willbe in some state and want to reach STATE_PL_SHUTDOWN
if ((!config.PowerLimiter_Enabled && _plState != STATE_PL_SHUTDOWN)
// In this case we are in some state and want to reach STATE_PL_SHUTDOWN
if (((!config.PowerLimiter_Enabled || _disabled) && _plState != SHUTDOWN)
|| isStopThresholdReached(inverter)) {
if (inverter->isProducing()) {
MessageOutput.printf("PL initiated inverter shutdown.\r\n");
inverter->sendPowerControlRequest(Hoymiles.getRadio(), false);
} else {
_plState = STATE_PL_SHUTDOWN;
_plState = SHUTDOWN;
}
return;
}

// PL is disabled
// If power limiter is disabled
if (!config.PowerLimiter_Enabled) {
return;
}

float dcVoltage = inverter->Statistics()->getChannelFieldValue(TYPE_DC, (ChannelNum_t) config.PowerLimiter_InverterChannelId, FLD_UDC);
float acPower = inverter->Statistics()->getChannelFieldValue(TYPE_AC, (ChannelNum_t) config.PowerLimiter_InverterChannelId, FLD_PAC);
float correctedDcVoltage = dcVoltage + (acPower * config.PowerLimiter_VoltageLoadCorrectionFactor);
//float acPower = inverter->Statistics()->getChannelFieldValue(TYPE_AC, (ChannelNum_t) config.PowerLimiter_InverterChannelId, FLD_PAC);
//float correctedDcVoltage = dcVoltage + (acPower * config.PowerLimiter_VoltageLoadCorrectionFactor);

// If the last inverter update is too old, don't do anything.
// If the last inverter update was before the last limit updated, don't do anything.
Expand All @@ -81,24 +79,27 @@ void PowerLimiterClass::loop()
dcVoltage, config.PowerLimiter_VoltageStartThreshold, config.PowerLimiter_VoltageStopThreshold, inverter->isProducing());
}

// Check if we need to move state away from STATE_PL_SHUTDOWN
if (_plState == STATE_PL_SHUTDOWN) {

// Allow discharge when start threshold reached
// This is also the trigger for drain strategy: EMPTY_WHEN_FULL
if (isStartThresholdReached(inverter)) {
_plState = STATE_ACTIVE;
}
// If we're in shutdown move to active operation
if (_plState == SHUTDOWN) {
_plState = ACTIVE;
}

// Allow discharge when drain strategy is EMPTY_AT_NIGHT
if (config.PowerLimiter_BatteryDrainStategy == EMPTY_AT_NIGHT) {
_plState = STATE_ACTIVE;
}
if (isStopThresholdReached(inverter)) {
// Disable battery discharge when empty
_batteryDischargeEnabled = false;
} else if (!canUseDirectSolarPower() ||
config.PowerLimiter_BatteryDrainStategy == EMPTY_AT_NIGHT) {
// Enable battery discharge
_batteryDischargeEnabled = true;
}

return;
// This checks if the battery discharge start conditions are met for the EMPTY_WHEN_FULL case
if (isStartThresholdReached(inverter) && config.PowerLimiter_BatteryDrainStategy == EMPTY_WHEN_FULL) {
_batteryDischargeEnabled = true;
}

int32_t newPowerLimit = calcPowerLimit(inverter, canUseDirectSolarPower());
int32_t newPowerLimit = calcPowerLimit(inverter, !_batteryDischargeEnabled);
// Debug, TODO: Remove
MessageOutput.printf("****************************** Powerlimit: %i\r\n", newPowerLimit);
setNewPowerLimit(inverter, newPowerLimit);
}
Expand Down

0 comments on commit ee37682

Please sign in to comment.