Files
Uniper_PLC/PLC/POUs/MAIN.TcPOU
2024-01-17 11:26:11 +01:00

531 lines
17 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.12">
<POU Name="MAIN" Id="{bbd7302c-91ce-4697-9f4b-743f57ca5819}" SpecialFunc="None">
<Declaration><![CDATA[PROGRAM MAIN
VAR
_xEmergencyStopOk AT %I* : BOOL;
_xReleaseErrors : BOOL := TRUE;
_xReleaseLimitsErrors : BOOL := TRUE;
_xConfirmAlarms : BOOL;
_xEnableString : BOOL;
_xEnableInverter : BOOL;
_fbString : FB_String('String 1');
_fbInverter : FB_PowerSupplySunspec;
_stInverterData : ST_SUNSPEC_CURRENT_VALUES;
// Variable to detect charge status change
_eLastChargeStatus : E_CHARGE_STATUS;
// Variable to detect battery status change
_eLastBatteryStatus : E_BATTERY_STATUS;
// Variable to detect any battery status change
_xBatteryStatusChange : BOOL;
_iState : INT;
// Internal inverter power
_rPowerInverter : REAL;
// Flag for zero power indication
_xNoPowerRequested : BOOL;
// Startup delay for error release during plc startup
_tonStartupDelay : TON := (PT := T#10S);
// Small delay for inverter shutdown
_tonBeginShutdown : TON := (PT := T#10S);
// Not all strings in automatic mode
_fbNoAutomaticModeAlarm : Fb_TcAlarm;
// First cycle tag
_xFirstCycle : BOOL := TRUE;
// ADS reader for modbus server data
_fbADSReader : ADSREAD;
// Timer for ADS read
_timADSReadTimer : TON;
// MQTT client
_fbMQTTClient : FB_IotMqttClient;
// connect to mqtt broker
_xConnectToMQTTBrocker : BOOL;
// DEBUG
_xRestart : BOOL;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[// ===============================
// DEBUG
IF _xRestart AND (_iState = 1010) THEN
GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower := 0;
_xConfirmAlarms := TRUE;
END_IF
IF _xRestart AND (_iState = 0) THEN
_xRestart := FALSE;
GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower := 1000;
END_IF
// DEBUG
// ===============================
IF _xFirstCycle THEN
_xFirstCycle := FALSE;
_fbString.Name := 'String 1';
_xConnectToMQTTBrocker := TRUE;
END_IF
// Dely release of errors during PLC startup phase
_tonStartupDelay(IN := TRUE);
// Call string 1
_fbString(
xEnable := _xEnableString,
stHMIInterface:= ,
xEmergencyStopOk:= _xEmergencyStopOk,
xReleaseErrors:= _xReleaseErrors AND _tonStartupDelay.Q,
xReleaseLimitErrors:= _xReleaseLimitsErrors AND _tonStartupDelay.Q,
xConfirmAlarms:= _xConfirmAlarms);
// DEACTIVATED FOR DEBUG REASONS !!!
// Call inverter
//_fbInverter(
// sInverterIPAddr:= GVL_CONFIG.sInverterIp,
// xEnable:= _xEnableInverter,
// rPower:= _rPowerInverter,
// xReset:= _xConfirmAlarms,
// rMaxBattPower:= DINT_TO_REAL(GVL_CONFIG.diMaxStringDischargePower),
// stCurrentValues => _stInverterData);
// ===============================
// Read modbus request count
// ===============================
_timADSReadTimer(IN := NOT _fbADSReader.BUSY, PT := T#200MS);
_fbADSReader(
NETID:= '',
PORT:= 10500,
IDXGRP:= 16#2000,
IDXOFFS:= 1,
LEN:= 4,
DESTADDR:= ADR(GVL_MODBUS.stModbusEMSComm.stModbusReg11.udiLifeMessage),
READ:= _timADSReadTimer.Q,
TMOUT:= T#1S,
BUSY=> ,
ERR=> ,
ERRID=> );
// ===============================
// MQTT cummunication
// ===============================
// Handle connection to broker
_fbMQTTClient.Execute(_xConnectToMQTTBrocker);
// ===============================
// State machine
// ===============================
CASE _iState OF
0: // Idle
// Wait for power command
IF (ABS(GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower) > GVL_CONFIG.diMinimumAbsPowerForEnable) AND _fbString.xSafetyIntlksOk AND (NOT _fbString.xError) THEN
_iState := 5;
END_IF
5: // Check if power command is within limits
IF GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower < GVL_CONFIG.diMaxStringDischargePower
AND GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower > GVL_CONFIG.diMaxStringChargingPower THEN
_xEnableString := TRUE;
_iState := 10;
ELSE
// Set error bitmap flag
GVL_MODBUS.stModbusEMSComm.stModbusReg11.lwErrorBitmap.0 := 1;
// Goto error state
_iState := 1000;
END_IF
10: // Wait for string to be ready
IF _fbString.xReady AND (NOT _fbString.xError) THEN
_iState := 20;
END_IF
20: // Start main inverter with zero power
_rPowerInverter := 0.0;
_xEnableInverter := TRUE;
_iState := 25;
25: // Wait for inverter to be ready
IF _fbInverter.xActive AND (NOT _fbInverter.xError) THEN
// Set battery status for modbus
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus := E_BATTERY_STATUS.ACTIVE;
GVL_MODBUS.stModbusEMSComm.stModbusReg10.uiActiveParallelMembers := 1;
_iState := 30;
END_IF
// Check for errors
IF _fbString.xError OR _fbInverter.xError THEN
_iState := 1000;
END_IF
30: // String and inverter enabled
// Set inverter power to modbus requested power
_rPowerInverter := DINT_TO_REAL(GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower);
// Check if the battery should still be active
IF (GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower = 0) THEN
_xNoPowerRequested := TRUE;
ELSE
_xNoPowerRequested := FALSE;
END_IF
// Set battery status
IF GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower > 0 THEN
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus := E_CHARGE_STATUS.DISCHARGING;
ELSIF GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower < 0 THEN
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus := E_CHARGE_STATUS.CHARGING;
ELSE
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus := E_CHARGE_STATUS.UNDEFINED;
END_IF
// Add small delay before shutdown by EMS is detected
_tonBeginShutdown(IN := _xNoPowerRequested);
// shutdown triggered from EMS
IF _tonBeginShutdown.Q THEN
_tonBeginShutdown(In := FALSE);
// Set inverter to zero power
_rPowerInverter := 0.0;
// Start string shutdown
_xEnableString := FALSE;
GVL_MODBUS.stModbusEMSComm.stModbusReg10.uiActiveParallelMembers := 0;
_iState := 35;
END_IF
// Shutdown triggered by battery fully charged
IF GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus = E_CHARGE_STATUS.CHARGING AND (_fbString.rCurrentVoltage >= GVL_CONFIG.rStringFullyChargedVoltage) THEN
_tonBeginShutdown(In := FALSE);
// Set inverter to zero power
_rPowerInverter := 0.0;
// Start string shutdown
_xEnableString := FALSE;
// Change battery status
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus := E_CHARGE_STATUS.FULL;
GVL_MODBUS.stModbusEMSComm.stModbusReg10.uiActiveParallelMembers := 0;
_iState := 35;
END_IF
// Shutdown triggered by battery empty
IF GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus = E_CHARGE_STATUS.DISCHARGING AND (_fbString.rCurrentVoltage <= GVL_CONFIG.rStringEmptyVoltage) THEN
_tonBeginShutdown(In := FALSE);
// Set inverter to zero power
_rPowerInverter := 0.0;
// Start string shutdown
_xEnableString := FALSE;
// Change battery status
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus := E_CHARGE_STATUS.FULL;
GVL_MODBUS.stModbusEMSComm.stModbusReg10.uiActiveParallelMembers := 0;
_iState := 35;
END_IF
// Check for errors
IF _fbString.xError OR _fbInverter.xError THEN
_tonBeginShutdown(In := FALSE);
_iState := 1000;
END_IF
35: // Wait for string to be in shutdown discharge mode
IF _fbString.xInShutdownDischargeMode THEN
// Check if we are allowed to discharge during shutdown with inverter
IF GVL_CONFIG.xShutdownDischargeWithInverter THEN
_iState := 40;
ELSE
_rPowerInverter := 0.0;
_xEnableInverter := FALSE;
_iState := 45;
END_IF
END_IF
// Check for errors
IF _fbString.xError OR _fbInverter.xError THEN
_iState := 1000;
END_IF
40: // Wait for inverter discharge done
IF _fbString.xShutdownDischargeAllowed THEN
_rPowerInverter := GVL_CONFIG.rAbsShutdownDischargePower;
ELSE
_rPowerInverter := 0.0;
_xEnableInverter := FALSE;
_iState := 45;
END_IF
// Check for errors
IF _fbString.xError OR _fbInverter.xError THEN
_iState := 1000;
END_IF
45: // Wait for shutdown of string to be done
IF (NOT _fbString.xInShutdownDischargeMode) AND _fbString.xOff THEN
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus := E_BATTERY_STATUS.OFF;
_iState := 0;
END_IF
// Check for errors
IF _fbString.xError OR _fbInverter.xError THEN
_iState := 1000;
END_IF
1000: // Error state
_xEnableString := FALSE;
_xEnableInverter := FALSE;
_rPowerInverter := 0.0;
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus := E_BATTERY_STATUS.ERROR;
_iState := 1010;
1010: // Wait for reset from error state
IF (GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower = 0) AND (NOT _fbString.xError) AND (NOT _fbInverter.xError) THEN
// Reset modbus error register
GVL_MODBUS.stModbusEMSComm.stModbusReg11.lwErrorBitmap := 0;
// Reset modbus error flag
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus := E_BATTERY_STATUS.OFF;
// Goto init state
_iState := 0;
END_IF
END_CASE
// Send MQTT battery status
_xBatteryStatusChange := FALSE;
IF GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus <> _eLastBatteryStatus THEN
_xBatteryStatusChange := TRUE;
END_IF
_eLastBatteryStatus := GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus;
IF GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus <> _eLastChargeStatus THEN
_xBatteryStatusChange := TRUE;
END_IF
_eLastChargeStatus := GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus;
IF _xBatteryStatusChange THEN
SendBatteryStatus();
END_IF
// Reset alarm confirmation
IF _xConfirmAlarms THEN
_xConfirmAlarms := FALSE;
END_IF]]></ST>
</Implementation>
<Method Name="SendBatteryStatus" Id="{d5a8daa9-6942-4419-a57d-7958e56f7e71}">
<Declaration><![CDATA[METHOD SendBatteryStatus
VAR
_sJSONString : T_MaxString;
_fbFormatString : FB_FormatString;
_sBatteryStatus : T_MaxString;
_sChargeStatus : T_MaxString;
_sBMSVersion : T_MaxString;
END_VAR
VAR CONSTANT
_sTemplateString : STRING := '{"batteryStatus": "%s", "chargeStatus": "%s", "bmsVersion": "%s"}';
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[// Temp strings because string format needs write access to these variables
_sBatteryStatus := TO_STRING(GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus);
_sChargeStatus := TO_STRING(GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus);
_sBMSVersion := GVL_MODBUS.stModbusEMSComm.stModbusReg10.sBMSVersion;
// Build JSON string
_fbFormatString(
sFormat:= _sTemplateString,
arg1:= F_STRING(_sBatteryStatus),
arg2:= F_STRING(_sChargeStatus),
arg3:= F_STRINg(_sBMSVersion),
sOut=> _sJSONString);
// Send message if connected and no formatting error occured
IF NOT _fbFormatString.bError AND _fbMQTTClient.bConnected THEN
_fbMQTTClient.Publish('status', ADR(_sJSONString), INT_TO_UDINT(LEN(_sJSONString)), GVL_MQTT.eMQTTQoS, FALSE, FALSE);
END_IF]]></ST>
</Implementation>
</Method>
<Action Name="SetMQTTParameter" Id="{f5ef2661-7fa1-4c9b-aa13-fba0d7bff32a}">
<Implementation>
<ST><![CDATA[//_fbMQTTClient.sClientId:= '';
_fbMQTTClient.sHostName:= GVL_MQTT.sMQTTBrokerAddr;
_fbMQTTClient.nHostPort:= 1883;
//_fbMQTTClient.sTopicPrefix := '';
_fbMQTTClient.nKeepAlive:= GVL_MQTT.uiMQTTKeepAlive;
// _fbMQTTClient.ipMessageQueue := ;]]></ST>
</Implementation>
</Action>
<LineIds Name="MAIN">
<LineId Id="405" Count="0" />
<LineId Id="395" Count="0" />
<LineId Id="400" Count="0" />
<LineId Id="402" Count="1" />
<LineId Id="399" Count="0" />
<LineId Id="407" Count="0" />
<LineId Id="406" Count="0" />
<LineId Id="408" Count="0" />
<LineId Id="410" Count="0" />
<LineId Id="409" Count="0" />
<LineId Id="534" Count="0" />
<LineId Id="398" Count="0" />
<LineId Id="404" Count="0" />
<LineId Id="475" Count="0" />
<LineId Id="396" Count="0" />
<LineId Id="476" Count="0" />
<LineId Id="478" Count="0" />
<LineId Id="624" Count="0" />
<LineId Id="477" Count="0" />
<LineId Id="397" Count="0" />
<LineId Id="126" Count="19" />
<LineId Id="147" Count="0" />
<LineId Id="784" Count="3" />
<LineId Id="598" Count="0" />
<LineId Id="808" Count="0" />
<LineId Id="793" Count="10" />
<LineId Id="789" Count="0" />
<LineId Id="599" Count="0" />
<LineId Id="463" Count="0" />
<LineId Id="601" Count="0" />
<LineId Id="600" Count="0" />
<LineId Id="626" Count="0" />
<LineId Id="617" Count="0" />
<LineId Id="724" Count="0" />
<LineId Id="152" Count="29" />
<LineId Id="218" Count="0" />
<LineId Id="182" Count="0" />
<LineId Id="304" Count="0" />
<LineId Id="207" Count="2" />
<LineId Id="628" Count="0" />
<LineId Id="314" Count="0" />
<LineId Id="782" Count="0" />
<LineId Id="210" Count="2" />
<LineId Id="305" Count="1" />
<LineId Id="308" Count="0" />
<LineId Id="215" Count="0" />
<LineId Id="219" Count="1" />
<LineId Id="222" Count="0" />
<LineId Id="221" Count="0" />
<LineId Id="223" Count="1" />
<LineId Id="234" Count="1" />
<LineId Id="237" Count="1" />
<LineId Id="236" Count="0" />
<LineId Id="309" Count="3" />
<LineId Id="315" Count="1" />
<LineId Id="632" Count="0" />
<LineId Id="360" Count="1" />
<LineId Id="313" Count="0" />
<LineId Id="317" Count="0" />
<LineId Id="241" Count="0" />
<LineId Id="240" Count="0" />
<LineId Id="239" Count="0" />
<LineId Id="327" Count="0" />
<LineId Id="242" Count="1" />
<LineId Id="262" Count="0" />
<LineId Id="264" Count="0" />
<LineId Id="261" Count="0" />
<LineId Id="263" Count="0" />
<LineId Id="265" Count="0" />
<LineId Id="260" Count="0" />
<LineId Id="783" Count="0" />
<LineId Id="245" Count="0" />
<LineId Id="244" Count="0" />
<LineId Id="328" Count="0" />
<LineId Id="330" Count="2" />
<LineId Id="339" Count="0" />
<LineId Id="338" Count="0" />
<LineId Id="333" Count="3" />
<LineId Id="340" Count="3" />
<LineId Id="337" Count="0" />
<LineId Id="329" Count="0" />
<LineId Id="344" Count="1" />
<LineId Id="347" Count="12" />
<LineId Id="346" Count="0" />
<LineId Id="246" Count="2" />
<LineId Id="251" Count="0" />
<LineId Id="249" Count="1" />
<LineId Id="256" Count="1" />
<LineId Id="266" Count="0" />
<LineId Id="296" Count="0" />
<LineId Id="290" Count="0" />
<LineId Id="267" Count="0" />
<LineId Id="291" Count="0" />
<LineId Id="294" Count="1" />
<LineId Id="292" Count="1" />
<LineId Id="268" Count="0" />
<LineId Id="258" Count="0" />
<LineId Id="269" Count="1" />
<LineId Id="272" Count="0" />
<LineId Id="259" Count="0" />
<LineId Id="273" Count="3" />
<LineId Id="278" Count="3" />
<LineId Id="277" Count="0" />
<LineId Id="285" Count="0" />
<LineId Id="287" Count="2" />
<LineId Id="286" Count="0" />
<LineId Id="282" Count="1" />
<LineId Id="300" Count="0" />
<LineId Id="666" Count="0" />
<LineId Id="302" Count="1" />
<LineId Id="301" Count="0" />
<LineId Id="297" Count="2" />
<LineId Id="284" Count="0" />
<LineId Id="183" Count="2" />
<LineId Id="216" Count="1" />
<LineId Id="186" Count="6" />
<LineId Id="252" Count="1" />
<LineId Id="255" Count="0" />
<LineId Id="254" Count="0" />
<LineId Id="193" Count="2" />
<LineId Id="197" Count="0" />
<LineId Id="647" Count="0" />
<LineId Id="646" Count="0" />
<LineId Id="654" Count="0" />
<LineId Id="659" Count="0" />
<LineId Id="648" Count="2" />
<LineId Id="664" Count="0" />
<LineId Id="656" Count="0" />
<LineId Id="655" Count="0" />
<LineId Id="657" Count="1" />
<LineId Id="665" Count="0" />
<LineId Id="661" Count="0" />
<LineId Id="660" Count="0" />
<LineId Id="662" Count="1" />
<LineId Id="199" Count="3" />
<LineId Id="25" Count="0" />
</LineIds>
<LineIds Name="MAIN.SendBatteryStatus">
<LineId Id="27" Count="0" />
<LineId Id="29" Count="1" />
<LineId Id="28" Count="0" />
<LineId Id="32" Count="0" />
<LineId Id="31" Count="0" />
<LineId Id="14" Count="4" />
<LineId Id="5" Count="0" />
<LineId Id="23" Count="0" />
<LineId Id="19" Count="3" />
</LineIds>
<LineIds Name="MAIN.SetMQTTParameter">
<LineId Id="2" Count="4" />
<LineId Id="1" Count="0" />
</LineIds>
</POU>
</TcPlcObject>