From f64e3165fc8b5a40a1bd5e8d6f77b7c3340cc4fa Mon Sep 17 00:00:00 2001 From: MalteSchm Date: Sat, 28 Oct 2023 11:05:57 +0200 Subject: [PATCH] Split Huawei setValue in private/public implementation. Prevent setting values when internal power control mode is enabled --- include/Huawei_can.h | 1 + src/Huawei_can.cpp | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/include/Huawei_can.h b/include/Huawei_can.h index eb69c6c53..32e2a7766 100644 --- a/include/Huawei_can.h +++ b/include/Huawei_can.h @@ -129,6 +129,7 @@ class HuaweiCanClass { private: void processReceivedParameters(); + void _setValue(float in, uint8_t parameterType); TaskHandle_t _HuaweiCanCommunicationTaskHdl = NULL; bool _initialized = false; diff --git a/src/Huawei_can.cpp b/src/Huawei_can.cpp index ffc8929ef..f42c7b018 100644 --- a/src/Huawei_can.cpp +++ b/src/Huawei_can.cpp @@ -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; } @@ -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"); @@ -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 { @@ -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(); @@ -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 @@ -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) {