Skip to content

Commit

Permalink
Split Huawei setValue in private/public implementation. Prevent setti…
Browse files Browse the repository at this point in the history
…ng values when internal power control mode is enabled
  • Loading branch information
MalteSchm committed Oct 28, 2023
1 parent 49a1030 commit f64e316
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions include/Huawei_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class HuaweiCanClass {

private:
void processReceivedParameters();
void _setValue(float in, uint8_t parameterType);

TaskHandle_t _HuaweiCanCommunicationTaskHdl = NULL;
bool _initialized = false;
Expand Down
21 changes: 14 additions & 7 deletions src/Huawei_can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ void HuaweiCanClass::loop()
// Set voltage limit in periodic intervals
if ( _nextAutoModePeriodicIntMillis < millis()) {
MessageOutput.printf("[HuaweiCanClass::loop] Periodically setting voltage limit: %f \r\n", config.Huawei_Auto_Power_Voltage_Limit);
setValue(config.Huawei_Auto_Power_Voltage_Limit, HUAWEI_ONLINE_VOLTAGE);
_setValue(config.Huawei_Auto_Power_Voltage_Limit, HUAWEI_ONLINE_VOLTAGE);
_nextAutoModePeriodicIntMillis = millis() + 60000;
}

Expand All @@ -319,7 +319,7 @@ void HuaweiCanClass::loop()

if (inverter != nullptr) {
if(inverter->isProducing()) {
setValue(0.0, HUAWEI_ONLINE_CURRENT);
_setValue(0.0, HUAWEI_ONLINE_CURRENT);
// Don't run auto mode for a second now. Otherwise we may send too much over the CAN bus
_autoModeBlockedTillMillis = millis() + 1000;
MessageOutput.printf("[HuaweiCanClass::loop] Inverter is active, disable\r\n");
Expand Down Expand Up @@ -349,7 +349,7 @@ void HuaweiCanClass::loop()
_autoPowerEnabledCounter--;
if (_autoPowerEnabledCounter == 0) {
_autoPowerEnabled = false;
setValue(0, HUAWEI_ONLINE_CURRENT);
_setValue(0, HUAWEI_ONLINE_CURRENT);
return;
}
} else {
Expand All @@ -366,20 +366,27 @@ void HuaweiCanClass::loop()
float outputCurrent = efficiency * (newPowerLimit / _rp.output_voltage);
MessageOutput.printf("[HuaweiCanClass::loop] Output current %f \r\n", outputCurrent);
_autoPowerEnabled = true;
setValue(outputCurrent, HUAWEI_ONLINE_CURRENT);
_setValue(outputCurrent, HUAWEI_ONLINE_CURRENT);

// Don't run auto mode some time to allow for output stabilization after issuing a new value
_autoModeBlockedTillMillis = millis() + 2 * HUAWEI_DATA_REQUEST_INTERVAL_MS;
} else {
// requested PL is below minium. Set current to 0
_autoPowerEnabled = false;
setValue(0.0, HUAWEI_ONLINE_CURRENT);
_setValue(0.0, HUAWEI_ONLINE_CURRENT);
}
}
}
}

void HuaweiCanClass::setValue(float in, uint8_t parameterType)
{
if (_mode != HUAWEI_MODE_AUTO_INT) {
_setValue(in, parameterType);
}
}

void HuaweiCanClass::_setValue(float in, uint8_t parameterType)
{

const CONFIG_T& config = Configuration.get();
Expand All @@ -391,7 +398,7 @@ void HuaweiCanClass::setValue(float in, uint8_t parameterType)
uint16_t value;

if (in < 0) {
MessageOutput.printf("[HuaweiCanClass::setValue] Error: Tried to set voltage/current to negative value %f \r\n", in);
MessageOutput.printf("[HuaweiCanClass::_setValue] Error: Tried to set voltage/current to negative value %f \r\n", in);
}

// Start PSU if needed
Expand Down Expand Up @@ -435,7 +442,7 @@ void HuaweiCanClass::setMode(uint8_t mode) {

if (_mode == HUAWEI_MODE_AUTO_INT && mode != HUAWEI_MODE_AUTO_INT) {
_autoPowerEnabled = false;
setValue(0, HUAWEI_ONLINE_CURRENT);
_setValue(0, HUAWEI_ONLINE_CURRENT);
}

if(mode == HUAWEI_MODE_AUTO_EXT || mode == HUAWEI_MODE_AUTO_INT) {
Expand Down

0 comments on commit f64e316

Please sign in to comment.