Fix Shelly2 wrong FrequencySet

Fix Shelly2 wrong FrequencySet calculation and add input range checks (#3882)
This commit is contained in:
Theo Arends 2018-09-23 16:41:22 +02:00
parent 01ce1b0c91
commit 683c211241
2 changed files with 32 additions and 14 deletions

View File

@ -6,6 +6,7 @@
* Add force_update to Home Assistant discovery (#3873) * Add force_update to Home Assistant discovery (#3873)
* Fix rule trigger POWER1#STATE execution after restart and SetOption0 is 0 (#3856) * Fix rule trigger POWER1#STATE execution after restart and SetOption0 is 0 (#3856)
* Disable serial logging on Shelly2 as serial is being used by energy monitoring (#3878) * Disable serial logging on Shelly2 as serial is being used by energy monitoring (#3878)
* Fix Shelly2 wrong FrequencySet calculation and add input range checks (#3882)
* *
* 6.2.1.5 20180921 * 6.2.1.5 20180921
* Add authentication to HTTP web pages * Add authentication to HTTP web pages

View File

@ -239,8 +239,12 @@ void McpCalibrationReactivePower()
mcp_calibration_registers.gain_reactive_power = mcp_calibration_registers.gain_reactive_power * mcp_calibration_registers.calibration_reactive_power / mcp_output_registers.reactive_power; mcp_calibration_registers.gain_reactive_power = mcp_calibration_registers.gain_reactive_power * mcp_calibration_registers.calibration_reactive_power / mcp_output_registers.reactive_power;
} }
void McpCalibrationLineFreqency() void McpCalibrationLineFrequency()
{ {
if ((0xFFFF == mcp_output_registers.line_frequency) || (0 == mcp_frequency_registers.gain_line_frequency)) { // Reset values to 50Hz
mcp_output_registers.line_frequency = 50000;
mcp_frequency_registers.gain_line_frequency = 0x8000;
}
mcp_frequency_registers.gain_line_frequency = mcp_frequency_registers.gain_line_frequency * mcp_frequency_registers.line_frequency_ref / mcp_output_registers.line_frequency; mcp_frequency_registers.gain_line_frequency = mcp_frequency_registers.gain_line_frequency * mcp_frequency_registers.line_frequency_ref / mcp_output_registers.line_frequency;
} }
@ -481,7 +485,7 @@ void McpParseFrequency()
if (mcp_calibration_setpoint.line_frequency_ref) { if (mcp_calibration_setpoint.line_frequency_ref) {
mcp_frequency_registers.line_frequency_ref = mcp_calibration_setpoint.line_frequency_ref; mcp_frequency_registers.line_frequency_ref = mcp_calibration_setpoint.line_frequency_ref;
McpCalibrationLineFreqency(); McpCalibrationLineFrequency();
mcp_timeout = 0; mcp_timeout = 0;
McpSetFrequency(); McpSetFrequency();
} }
@ -637,33 +641,46 @@ void McpDrvInit()
boolean McpCommand() boolean McpCommand()
{ {
boolean serviced = true; boolean serviced = true;
unsigned long value = 0;
if (CMND_POWERSET == energy_command_code) { if (CMND_POWERSET == energy_command_code) {
if (XdrvMailbox.data_len && mcp_output_registers.active_power) { if (XdrvMailbox.data_len && mcp_output_registers.active_power) {
Settings.energy_power_calibration = (unsigned long)(CharToDouble(XdrvMailbox.data) * 100); value = (unsigned long)(CharToDouble(XdrvMailbox.data) * 100);
mcp_calibration_setpoint.calibration_active_power = Settings.energy_power_calibration; if ((value > 100) && (value < 200000)) { // Between 1W and 2000W
McpGetCalibration(); Settings.energy_power_calibration = value;
mcp_calibration_setpoint.calibration_active_power = value;
McpGetCalibration();
}
} }
} }
else if (CMND_VOLTAGESET == energy_command_code) { else if (CMND_VOLTAGESET == energy_command_code) {
if (XdrvMailbox.data_len && mcp_output_registers.voltage_rms) { if (XdrvMailbox.data_len && mcp_output_registers.voltage_rms) {
Settings.energy_voltage_calibration = (unsigned long)(CharToDouble(XdrvMailbox.data) * 10); value = (unsigned long)(CharToDouble(XdrvMailbox.data) * 10);
mcp_calibration_setpoint.calibration_voltage = Settings.energy_voltage_calibration; if ((value > 1000) && (value < 2600)) { // Between 100V and 260V
McpGetCalibration(); Settings.energy_voltage_calibration = value;
mcp_calibration_setpoint.calibration_voltage = value;
McpGetCalibration();
}
} }
} }
else if (CMND_CURRENTSET == energy_command_code) { else if (CMND_CURRENTSET == energy_command_code) {
if (XdrvMailbox.data_len && mcp_output_registers.current_rms) { if (XdrvMailbox.data_len && mcp_output_registers.current_rms) {
Settings.energy_current_calibration = (unsigned long)(CharToDouble(XdrvMailbox.data) * 10); value = (unsigned long)(CharToDouble(XdrvMailbox.data) * 10);
mcp_calibration_setpoint.calibration_current = Settings.energy_current_calibration; if ((value > 100) && (value < 80000)) { // Between 10mA and 8A
McpGetCalibration(); Settings.energy_current_calibration = value;
mcp_calibration_setpoint.calibration_current = value;
McpGetCalibration();
}
} }
} }
else if (CMND_FREQUENCYSET == energy_command_code) { else if (CMND_FREQUENCYSET == energy_command_code) {
if (XdrvMailbox.data_len && mcp_output_registers.line_frequency) { if (XdrvMailbox.data_len && mcp_output_registers.line_frequency) {
Settings.energy_frequency_calibration = (unsigned long)(CharToDouble(XdrvMailbox.data) * 10); value = (unsigned long)(CharToDouble(XdrvMailbox.data) * 1000);
mcp_calibration_setpoint.line_frequency_ref = Settings.energy_frequency_calibration; if ((value > 45000) && (value < 65000)) { // Between 45Hz and 65Hz
McpGetFrequency(); Settings.energy_frequency_calibration = value;
mcp_calibration_setpoint.line_frequency_ref = value;
McpGetFrequency();
}
} }
} }
else serviced = false; // Unknown command else serviced = false; // Unknown command