Added safety program pre comissioning and started implementing different modes

This commit is contained in:
Matthias Heisig
2024-03-20 18:08:57 +01:00
parent 57987cb19f
commit ef26159a4f
55 changed files with 10419 additions and 1663 deletions

View File

@@ -9,7 +9,10 @@ VAR
_xConfirmAlarms : BOOL;
_xEnableString : BOOL;
_xEnableInverter : BOOL;
{attribute 'OPC.UA.DA' := '0'}
_fbString : FB_String('String 1');
_fbInverter : FB_PowerSupplySunspec;
_stInverterData : ST_SUNSPEC_CURRENT_VALUES;
@@ -18,9 +21,7 @@ VAR
// Variable to detect battery status change
_eLastBatteryStatus : E_BATTERY_STATUS;
// Variable to detect any battery status change
_xBatteryStatusChange : BOOL;
// State machine state
_iState : INT;
// Internal inverter power
@@ -33,7 +34,7 @@ VAR
_tonStartupDelay : TON := (PT := T#10S);
// Small delay for inverter shutdown
_tonBeginShutdown : TON := (PT := T#10S);
_tonBeginShutdown : TON := (PT := T#30S);
// Not all strings in automatic mode
_fbNoAutomaticModeAlarm : Fb_TcAlarm;
@@ -47,11 +48,24 @@ VAR
// Timer for ADS read
_timADSReadTimer : TON;
// MQTT client
_fbMQTTClient : FB_IotMqttClient;
// Release manual mode
_xReleaseManualMode : BOOL;
// connect to mqtt broker
_xConnectToMQTTBrocker : BOOL;
// Current internal set inverter power value
_diInternalPowerSetpoint : DINT;
_diSetpointActivePower : DINT;
// Current BMS control mode (Auto local, Auto remote, etc...)
// On restart star in manual mode (so the ems can not directly start the bms)
_eBMSControlMode : E_BMS_CONTROL_MODE := E_BMS_CONTROL_MODE.MANUAL;
// UPS
_fbUPS : FB_S_UPS_BAPI;
// Safety
xSafetyRun AT %Q* : BOOL;
xSafetyErrAck AT %Q* : BOOL;
// DEBUG
_xRestart : BOOL;
@@ -73,24 +87,69 @@ END_IF
// DEBUG
// ===============================
// ===============================
// Handle Manual mode release
// ===============================
IF _iState = 0 THEN
_xReleaseManualMode := TRUE;
ELSE
_xReleaseManualMode := FALSE;
END_IF
// ===============================
// Handle UPS events
// ===============================
_fbUPS(
sNetID:= '',
iPLCPort:= 851,
tTimeout:= DEFAULT_ADS_TIMEOUT,
eUpsMode:= eSUPS_WrPersistData_Shutdown,
ePersistentMode:= SPDM_2PASS,
tRecoverTime:= T#10S,
bPowerFailDetect=> ,
eState=> );
IF _xFirstCycle THEN
_xFirstCycle := FALSE;
_fbString.Name := 'String 1';
_xConnectToMQTTBrocker := TRUE;
END_IF
// Dely release of errors during PLC startup phase
_tonStartupDelay(IN := TRUE);
// Ack alarms from HMI
_xConfirmAlarms := GVL_SCADA.stAckAlarmsButton.xRequest;
IF GVL_SCADA.stAckAlarmsButton.xRequest THEN
GVL_SCADA.stAckAlarmsButton.xRequest := FALSE;
END_IF
// Call string 1
_fbString(
xEnable := _xEnableString,
stHMIInterface:= ,
stHMIInterface:= GVL_SCADA.stHMIInterface,
xEmergencyStopOk:= _xEmergencyStopOk,
xReleaseErrors:= _xReleaseErrors AND _tonStartupDelay.Q,
xReleaseLimitErrors:= _xReleaseLimitsErrors AND _tonStartupDelay.Q,
xReleaseLimitErrors:= _xReleaseLimitsErrors AND _tonStartupDelay.Q,
xReleaseManualMode := _xReleaseManualMode,
xConfirmAlarms:= _xConfirmAlarms);
// HMI Feedback
GVL_SCADA.stHMIInterface.rVoltage := _fbString.rCurrentVoltage;
IF _fbString.eStatus = E_COMPONENT_STATUS.ON THEN
IF _iState = 30 AND _rPowerInverter > 0 THEN
GVL_SCADA.stHMIInterface.eStatus := E_COMPONENT_STATUS.DISCHARGING;
ELSIF _iState = 30 AND _rPowerInverter < 0 THEN
GVL_SCADA.stHMIInterface.eStatus := E_COMPONENT_STATUS.CHARGING;
ELSE
GVL_SCADA.stHMIInterface.eStatus :=_fbString.eStatus;
END_IF
ELSE
GVL_SCADA.stHMIInterface.eStatus :=_fbString.eStatus;
END_IF
// DEACTIVATED FOR DEBUG REASONS !!!
// Call inverter
//_fbInverter(
@@ -118,18 +177,46 @@ _fbADSReader(
BUSY=> ,
ERR=> ,
ERRID=> );
// ===============================
// MQTT cummunication
// ===============================
// Handle connection to broker
_fbMQTTClient.Execute(_xConnectToMQTTBrocker);
// ===============================
// State machine
// ===============================
CASE _iState OF
CASE _eBMSControlMode OF
E_BMS_CONTROL_MODE.AUTO_REMOTE:
SM_AUTO_REMOTE();
E_BMS_CONTROL_MODE.AUTO_LOCAL:
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus := E_BATTERY_STATUS.TESTING;
SM_AUTO_LOCAL();
E_BMS_CONTROL_MODE.MANUAL:
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus := E_BATTERY_STATUS.MAINTENANCE;
SM_MANUAL();
E_BMS_CONTROL_MODE.SAFETY_CHECK:
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus := E_BATTERY_STATUS.MAINTENANCE;
SM_SAFETY_CHECK();
E_BMS_CONTROL_MODE.CAPACITY_TEST:
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus := E_BATTERY_STATUS.TESTING;
SM_CAPACITY_TEST();
END_CASE
// Reset alarm confirmation
IF _xConfirmAlarms THEN
_xConfirmAlarms := FALSE;
END_IF]]></ST>
</Implementation>
<Action Name="SM_AUTO_LOCAL" Id="{def23375-eebc-4fdc-90a2-71266ffc4850}">
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Action>
<Action Name="SM_AUTO_REMOTE" Id="{b5166e16-4fea-442b-9560-02c156f9a9ad}">
<Implementation>
<ST><![CDATA[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
@@ -174,7 +261,7 @@ CASE _iState OF
30: // String and inverter enabled
// Set inverter power to modbus requested power
_rPowerInverter := DINT_TO_REAL(GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower);
_rPowerInverter := DINT_TO_REAL(GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower);//DINT_TO_REAL(GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower);
// Check if the battery should still be active
IF (GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower = 0) THEN
@@ -205,6 +292,7 @@ CASE _iState OF
// Start string shutdown
_xEnableString := FALSE;
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus := E_CHARGE_STATUS.UNDEFINED;
GVL_MODBUS.stModbusEMSComm.stModbusReg10.uiActiveParallelMembers := 0;
_iState := 35;
END_IF
@@ -236,7 +324,7 @@ CASE _iState OF
_xEnableString := FALSE;
// Change battery status
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus := E_CHARGE_STATUS.FULL;
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus := E_CHARGE_STATUS.EMPTY;
GVL_MODBUS.stModbusEMSComm.stModbusReg10.uiActiveParallelMembers := 0;
_iState := 35;
END_IF
@@ -307,71 +395,36 @@ CASE _iState OF
// 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>
END_CASE]]></ST>
</Implementation>
</Method>
<Action Name="SetMQTTParameter" Id="{f5ef2661-7fa1-4c9b-aa13-fba0d7bff32a}">
</Action>
<Action Name="SM_CAPACITY_TEST" Id="{705978cf-2798-4a38-8f24-148e2ec1d46e}">
<Implementation>
<ST><![CDATA[//_fbMQTTClient.sClientId:= '';
_fbMQTTClient.sHostName:= GVL_MQTT.sMQTTBrokerAddr;
_fbMQTTClient.nHostPort:= 1883;
//_fbMQTTClient.sTopicPrefix := '';
_fbMQTTClient.nKeepAlive:= GVL_MQTT.uiMQTTKeepAlive;
// _fbMQTTClient.ipMessageQueue := ;]]></ST>
<ST><![CDATA[]]></ST>
</Implementation>
</Action>
<Action Name="SM_MANUAL" Id="{ddef276e-9f4f-4258-b863-d254dd94b701}">
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Action>
<Action Name="SM_SAFETY_CHECK" Id="{6d8e5993-cf32-4980-9ea3-c1fbfa4b8601}">
<Implementation>
<ST><![CDATA[// start pumps
// open valves
// wait for equilibrium and voltage in range
// engange dc breakers
// wait for stop or error
// on stop: open dc circuit breakers and close valves
// wait for voltage to drop below xx volts
// stop pumps]]></ST>
</Implementation>
</Action>
<LineIds Name="MAIN">
@@ -388,144 +441,98 @@ _fbMQTTClient.nKeepAlive:= GVL_MQTT.uiMQTTKeepAlive;
<LineId Id="534" Count="0" />
<LineId Id="398" Count="0" />
<LineId Id="404" Count="0" />
<LineId Id="1187" Count="0" />
<LineId Id="1185" Count="0" />
<LineId Id="1179" Count="0" />
<LineId Id="1178" Count="0" />
<LineId Id="1186" Count="0" />
<LineId Id="1180" Count="1" />
<LineId Id="1183" Count="1" />
<LineId Id="1182" Count="0" />
<LineId Id="1188" Count="0" />
<LineId Id="1029" Count="0" />
<LineId Id="1028" Count="0" />
<LineId Id="1030" Count="1" />
<LineId Id="1037" Count="2" />
<LineId Id="1041" Count="4" />
<LineId Id="1033" Count="0" />
<LineId Id="1032" 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="126" Count="1" />
<LineId Id="971" Count="1" />
<LineId Id="970" Count="0" />
<LineId Id="974" Count="0" />
<LineId Id="973" Count="0" />
<LineId Id="975" Count="0" />
<LineId Id="128" Count="7" />
<LineId Id="1189" Count="0" />
<LineId Id="136" Count="0" />
<LineId Id="1101" Count="2" />
<LineId Id="1105" Count="0" />
<LineId Id="1109" Count="2" />
<LineId Id="1114" Count="0" />
<LineId Id="1118" Count="1" />
<LineId Id="1115" Count="2" />
<LineId Id="1108" Count="0" />
<LineId Id="137" Count="8" />
<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="958" Count="0" />
<LineId Id="152" Count="3" />
<LineId Id="1257" Count="1" />
<LineId Id="1262" Count="2" />
<LineId Id="1341" Count="0" />
<LineId Id="1265" Count="0" />
<LineId Id="1337" Count="1" />
<LineId Id="1342" Count="0" />
<LineId Id="1339" Count="0" />
<LineId Id="1266" Count="1" />
<LineId Id="1343" Count="0" />
<LineId Id="1268" Count="2" />
<LineId Id="1344" Count="0" />
<LineId Id="1271" Count="0" />
<LineId Id="1259" Count="0" />
<LineId Id="663" Count="0" />
<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" />
<LineIds Name="MAIN.SM_AUTO_LOCAL">
<LineId Id="1" Count="0" />
</LineIds>
<LineIds Name="MAIN.SM_AUTO_REMOTE">
<LineId Id="2" Count="178" />
<LineId Id="1" Count="0" />
</LineIds>
<LineIds Name="MAIN.SM_CAPACITY_TEST">
<LineId Id="1" Count="0" />
</LineIds>
<LineIds Name="MAIN.SM_MANUAL">
<LineId Id="1" Count="0" />
</LineIds>
<LineIds Name="MAIN.SM_SAFETY_CHECK">
<LineId Id="1" Count="0" />
<LineId Id="3" Count="0" />
<LineId Id="2" Count="0" />
<LineId Id="5" Count="0" />
<LineId Id="4" Count="0" />
<LineId Id="7" Count="0" />
<LineId Id="6" Count="0" />
<LineId Id="9" Count="0" />
<LineId Id="8" Count="0" />
<LineId Id="15" Count="1" />
<LineId Id="13" Count="0" />
<LineId Id="12" Count="0" />
<LineId Id="18" Count="0" />
<LineId Id="17" Count="0" />
</LineIds>
</POU>
</TcPlcObject>