<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.12">
  <POU Name="FB_String" Id="{46501225-f446-4674-bfed-3be64273e576}" SpecialFunc="None">
    <Declaration><![CDATA[FUNCTION_BLOCK FB_String
VAR_INPUT
	// Enable
	xEnable : BOOL;
	
	// Start in balancing mode
	xStartBalancing : BOOL;
	
	// String in safety check mode
	xInSafetyCheckMode : BOOL;
	
	// Requested inverter power
	rPowerInverter : REAL;
	
	// Components shortage workaround
	stStringModuleVoltageConfig : ST_STRING_VOLT_CONFIG;

	// Module 1 HMI interface
	stHMIInterface : REFERENCE TO ST_STRING_HMI_INTERFACE;

	// Emergency stop ok
	xEmergencyStopOk : BOOL;
	
	// Reset Safety
	xResetSafety : BOOL;
	
	// All safetyinterlocks from safety plc are ok
	xSafetyIntlksOk AT %I* : BOOL;
	
	// Release alarms
	xReleaseErrors : BOOL;
	
	// Release analog io limit errors
	xReleaseLimitErrors : BOOL;
	
	// Release manual mode
	xReleaseManualMode : BOOL;
	
	// Input to confirm all errors
	xConfirmAlarms : BOOL;
	
	// Repair switch closed
	xRepairSwitchOk AT %I* : BOOL;
	
	// String inverter ip
	sInverterIP : STRING;
	
END_VAR
VAR_OUTPUT
	// Current string voltage
	rCurrentVoltage : REAL;
	
	// Module in shutdown segment discharge mode
	xInShutdownDischargeMode : BOOL;
	
	// Module can be discharged during shutdown sequence
	xShutdownDischargeAllowed : BOOL;

	// String ready
	xReady : BOOL;
	
	// String completely off
	xOff : BOOL;
	
	// Signal to close dc circuit breaker
	xCloseDCCB AT %Q* : BOOL;
	
	// Signal that dc circuit breakers are closed
	xDCCBOpen AT %I* : BOOL;
	
	// Reset signal for safety dc circuit breaker
	xResetSafetyDCCB AT %Q* : BOOL;
	
	// All modules in automatic mode
	xAllModulesInAutoMode : BOOL;
	
	xError : BOOL;
	xWarning : BOOL;
	
	eStatus : E_COMPONENT_STATUS;
	
	// Inverter status data
	stInverterData : ST_SUNSPEC_CURRENT_VALUES;
	
	// Smallest segment voltage
	rSmallestSegmentVoltage : REAL;
	
	// Highest segment voltage
	rHighestSegmentVoltage : REAL;
	
	// Balancing done
	xBalancingDone : BOOL;
END_VAR
VAR
	_fbModule1 : FB_Module(CONCAT(Name,' - Module 1'));
	_fbModule2 : FB_Module(CONCAT(Name,' - Module 2'));
	_fbModule3 : FB_Module(CONCAT(Name,' - Module 3'));
	
	// All modules are ready
	_xAllModulesReady : BOOL;
	
	// All modules in shutdown discharge mode
	_xAllModulesInShutdownDischargeMode : BOOL;
	
	// Flag for module balance checking
	_xBalanceOk : BOOL;
	
	// Modules out of balance alarm message
	_fbModulesOutOfBalanceAlarm : Fb_TcAlarm;
	
	// Safetyinterlocks pending alarm
	_fbSafetyInterlocksNotOkAlarm : FB_TcAlarm;
	
	// Inverter startup error
	_fbInverterStartupTimeoutAlarm : FB_TcAlarm;
	
	// DC Main switch not closed
	_fbDCMainSwitchNotClosed : FB_TcAlarm;
	
	// Shutdown discharge stopped messages
	_fbSDDCLevel : FB_TcMessage;
	_fbSDUnitThreshold : FB_TcMessage;
	
	// State for start and stop
	_iState : INT := 0;
	
	// Timer for result pulse to safety
	_tonResetPulseLength : TON := (PT := T#250MS);
	
	// Error timer for not closing dc relais
	_tonErrorDCCBNotClosed : TON := (PT := T#5S);
	
	// Delayed balance check signal
	_fbBalanceNotOkSignal : FB_ReleaseSignal;
	
	// String name
	_sName : STRING;
	
	// String inverter
	_fbInverter : FB_PowerSupplySunspec(Name);
	
	// Internal inverter power command
	_rPowerInverterInternal : REAL;
	
	// Enable inverter flag
	_xEnableInverter : BOOL;
	
	// Fault timer for inverter startup
	_tonInverterStartupTimeout : TON := (PT := T#2M);
	
	// Deabug delay timer for inverter shutdown
	_tonInverterShutdownDelay : TON := (PT := T#10S);
	
	// Timer for Safety ok timeout
	_tonSafetyOkTimeout : TON := (PT := T#2M);
	
	// Analog input for string current measurement
	_fbStringCurrent : FB_AnalogInput(CONCAT(Name,' - Current'));
	
	xErrorInverter : BOOL;
	
	_xReleaseLimitErrorsInternal : BOOL;
	
	// Balancing done
	_xBalancingDone : BOOL;
	
	// Enable modules internal signal
	_xEnable : BOOL;
	
	// Start balancing internal signal
	_xStartBalancing : BOOL;
	
END_VAR

VAR PERSISTENT
	rCapacityWH : REAL;
	rCapacityAH : REAL;
END_VAR
]]></Declaration>
    <Implementation>
      <ST><![CDATA[// Reset error flag
xError := FALSE;

// Reset all modules in automatic mode
xAllModulesInAutoMode := TRUE;

// Reset safety interlocks flag
//xSafetyIntlksOk := TRUE;

// Reset Safety
xResetSafetyDCCB := xResetSafety;

// ===============================
// DC Main switch error handling
// ===============================
IF (NOT xRepairSwitchOk) AND (NOT _fbDCMainSwitchNotClosed.bRaised) THEN
	_fbDCMainSwitchNotClosed.Raise(0);
END_IF

IF xRepairSwitchOk AND _fbDCMainSwitchNotClosed.bRaised THEN
	_fbDCMainSwitchNotClosed.Clear(0, FALSE);
END_IF


// ===============================
// DC current measurement
// ===============================
_fbStringCurrent(
	stScalingConfig:= GVL_CONFIG.stConfigSCSCurrent, 
	stEWConfig:= GVL_CONFIG.stEWLSCSCurrent, 
	stEWDelayConfig:= GVL_CONFIG.stEWDSCSCurrent, 
	xReleaseErrors:= xReleaseErrors, 
	xReleaseLimitErrors:= FALSE, 
	xReleaseHardwareErrors:= xReleaseErrors, 
	xConfirmAlarms:= xConfirmAlarms, 
	xError=> , 
	xWarning=> , 
	rScaledValue=> , 
	xErrorLow=> , 
	xWarningLow=> , 
	xWarningHigh=> , 
	xErrorHigh=> , 
	stHMIInterface=> stHMIInterface.stStringCurrent);



// ===============================
// Module 1
// ===============================
_fbModule1(
	xEnable := _xEnable,
	xStartBalancing := _xStartBalancing,
	xInverterEnabled := _fbInverter.xActive,
	stModuleVoltageConfig := stStringModuleVoltageConfig.stModule1VoltConfig,
	xInSafetyCheckMode := xInSafetyCheckMode,
	xEmergencyStopOk:= xEmergencyStopOk,
	stHMIInterface:= stHMIInterface.stHMIInterfaceModule1, 
	xReleaseErrors:= xReleaseErrors, 
	xReleaseLimitErrors:= xReleaseLimitErrors AND _xReleaseLimitErrorsInternal,
	xReleaseManualMode := xReleaseManualMode,
	xConfirmAlarms:= xConfirmAlarms,
	rBalancingTargetVoltage := rSmallestSegmentVoltage);
	
IF _fbModule1.xWarning THEN
	xWarning := TRUE;
END_IF	

IF _fbModule1.xError THEN
	xError := TRUE;
END_IF

IF NOT _fbModule1.xAllUnitsInAutomatic THEN
	xAllModulesInAutoMode := FALSE;
END_IF


// ===============================
// Module 2
// ===============================

_fbModule2(
	xEnable := _xEnable,
	xStartBalancing := _xStartBalancing,
	xInverterEnabled := _fbInverter.xActive,
	stModuleVoltageConfig := stStringModuleVoltageConfig.stModule2VoltConfig,
	xInSafetyCheckMode := xInSafetyCheckMode,
	xEmergencyStopOk:= xEmergencyStopOk,
	stHMIInterface:= stHMIInterface.stHMIInterfaceModule2, 
	xReleaseErrors:= xReleaseErrors, 
	xReleaseLimitErrors:= xReleaseLimitErrors AND _xReleaseLimitErrorsInternal,
	xReleaseManualMode := xReleaseManualMode,
	xConfirmAlarms:= xConfirmAlarms,
	rBalancingTargetVoltage := rSmallestSegmentVoltage);
	
IF _fbModule2.xWarning THEN
	xWarning := TRUE;
END_IF	

IF _fbModule2.xError THEN
	xError := TRUE;
END_IF

IF NOT _fbModule2.xAllUnitsInAutomatic THEN
	xAllModulesInAutoMode := FALSE;
END_IF


// ===============================
// Module 3
// ===============================
_fbModule3(
	xEnable := _xEnable,
	xStartBalancing := _xStartBalancing,
	xInverterEnabled := _fbInverter.xActive,
	stModuleVoltageConfig := stStringModuleVoltageConfig.stModule3VoltConfig,
	xInSafetyCheckMode := xInSafetyCheckMode,
	xEmergencyStopOk:= xEmergencyStopOk,
	stHMIInterface:= stHMIInterface.stHMIInterfaceModule3, 
	xReleaseErrors:= xReleaseErrors, 
	xReleaseLimitErrors:= xReleaseLimitErrors AND _xReleaseLimitErrorsInternal,
	xReleaseManualMode := xReleaseManualMode,
	xConfirmAlarms:= xConfirmAlarms,
	rBalancingTargetVoltage := rSmallestSegmentVoltage);
	
IF _fbModule3.xWarning THEN
	xWarning := TRUE;
END_IF	

IF _fbModule3.xError THEN
	xError := TRUE;
END_IF

IF NOT _fbModule3.xAllUnitsInAutomatic THEN
	xAllModulesInAutoMode := FALSE;
END_IF


// ===============================
// Handle shutdown discharge mode
// ===============================
_xAllModulesInShutdownDischargeMode := _fbModule1.xInShutdownDischargeMode AND _fbModule2.xInShutdownDischargeMode AND _fbModule3.xInShutdownDischargeMode;


// ===============================
// Handle safety interlock alarm
// ===============================
IF NOT xSafetyIntlksOk AND NOT _fbSafetyInterlocksNotOkAlarm.bRaised THEN
	_fbSafetyInterlocksNotOkAlarm.Raise(0);
END_IF

IF xSafetyIntlksOk AND _fbSafetyInterlocksNotOkAlarm.bRaised THEN
	_fbSafetyInterlocksNotOkAlarm.Clear(0, TRUE);
END_IF


// ===============================
// Modules ready check
// ===============================
_xAllModulesReady := _fbModule1.xReady AND _fbModule2.xReady AND _fbModule3.xReady;


// ===============================
// Balancing done check
// ===============================
_xBalancingDone := _fbModule1.xBalancingDone AND _fbModule2.xBalancingDone AND _fbModule3.xBalancingDone;


// ===============================
// Modules in shutdown discharge mode
// ===============================
xInShutdownDischargeMode := _fbModule1.xInShutdownDischargeMode AND _fbModule2.xInShutdownDischargeMode AND _fbModule3.xInShutdownDischargeMode;


// ===============================
// Units shutdown discharge allowed
// ===============================
xShutdownDischargeAllowed := _fbModule1.xShutdownDischargeAllowed AND _fbModule2.xShutdownDischargeAllowed AND _fbModule3.xShutdownDischargeAllowed;

// ===============================
// All modules off
// ===============================
xOff := _fbModule1.xOff AND _fbModule2.xOff AND _fbModule3.xOff;

// ===============================
// Calculate string voltage
// ===============================
rCurrentVoltage := _fbModule1.rCurrentVoltage + _fbModule2.rCurrentVoltage + _fbModule3.rCurrentVoltage;
stHMIInterface.rVoltage := rCurrentVoltage;

// ===============================
// String balance check
// ===============================
// Reset balance ok flag
_xBalanceOk := TRUE;

// Test module 1 with module 2
IF ABS(_fbModule1.rCurrentVoltage - _fbModule2.rCurrentVoltage) > GVL_CONFIG.rMaxAbsDiffVoltageModulesInString THEN
	_xBalanceOk := FALSE;
END_IF

// Test module 1 with module 3
IF ABS(_fbModule1.rCurrentVoltage - _fbModule3.rCurrentVoltage) > GVL_CONFIG.rMaxAbsDiffVoltageModulesInString THEN
	_xBalanceOk := FALSE;
END_IF

// Test module 2 with module 3
IF ABS(_fbModule2.rCurrentVoltage - _fbModule3.rCurrentVoltage) > GVL_CONFIG.rMaxAbsDiffVoltageModulesInString THEN
	_xBalanceOk := FALSE;
END_IF

// Release signal for balance not ok
_fbBalanceNotOkSignal(
	xSignal:= NOT _xBalanceOk, 
	xRelease:= xEnable AND _xAllModulesReady, 
	timOnDelay:= T#10S, 
	timOffDelay:= T#10S, 
	xReleaseSignal=> );

// Signal an error if all units are ready and module is out of balance
IF _fbBalanceNotOkSignal.xReleaseSignal THEN
	xError := TRUE;
END_IF

// Raise error
IF _fbBalanceNotOkSignal.xReleaseSignal AND (NOT _fbModulesOutOfBalanceAlarm.bRaised) THEN
	_fbModulesOutOfBalanceAlarm.Raise(0);
END_IF

// Clear error
IF (NOT _fbBalanceNotOkSignal.xReleaseSignal) AND _fbModulesOutOfBalanceAlarm.bRaised AND xConfirmAlarms THEN
	_fbModulesOutOfBalanceAlarm.Clear(0, FALSE);
END_IF

// Confirm error
IF _fbModulesOutOfBalanceAlarm.eConfirmationState = TcEventConfirmationState.WaitForConfirmation AND xConfirmAlarms THEN
	_fbModulesOutOfBalanceAlarm.Confirm(0);
END_IF

// ===============================
// String ready validation check
// ===============================
_tonResetPulseLength();
_tonErrorDCCBNotClosed();
_tonSafetyOkTimeout();


// ===============================
// Get smalles segment voltage
// of all units
// ===============================
rSmallestSegmentVoltage := MIN(_fbModule1.rSmallestSegmentVoltage, _fbModule2.rSmallestSegmentVoltage, _fbModule3.rSmallestSegmentVoltage);
rHighestSegmentVoltage := MAX(_fbModule1.rSmallestSegmentVoltage, _fbModule2.rSmallestSegmentVoltage, _fbModule3.rSmallestSegmentVoltage);


// Call inverter
_fbInverter(
	sInverterIPAddr:= sInverterIP, 
	xEnable:= _xEnableInverter AND xEmergencyStopOk,
	rPower:= _rPowerInverterInternal, 
	xReset:= xConfirmAlarms, 
	rMaxBattPower:= DINT_TO_REAL(GVL_CONFIG.diMaxStringDischargePower), 
	stCurrentValues => stInverterData);
	
IF (_iState >= 30) AND (_iState < 40) THEN
	rCapacityAH := rCapacityAH + ((stInverterData.rActDCCurrent * 0.01) / 3600);
	rCapacityWH := rCapacityWH + ((stInverterData.rActACPower * 0.01) / 3600);
END_IF

CASE _iState OF
	0: // Idle
		// Start in normal mode
		IF xEnable AND (NOT xStartBalancing) AND xAllModulesInAutoMode AND xRepairSwitchOk THEN
			_xEnable := TRUE;
			_iState := 5;
		END_IF
		
		// Start in balancing mode
		IF (NOT xEnable) AND xStartBalancing AND xAllModulesInAutoMode THEN
			_xStartBalancing := TRUE;
			_xReleaseLimitErrorsInternal := FALSE;
			_iState := 7;
		END_IF
		
	5: // Wait for all modules to be ready in normal mode
		IF _xAllModulesReady AND _xBalanceOk THEN
			xResetSafetyDCCB := TRUE;
			IF (NOT xInSafetyCheckMode) THEN
				_xReleaseLimitErrorsInternal := TRUE;
			END_IF
			_tonResetPulseLength.IN := TRUE;
			_iState := 10;
		END_IF
		
		IF (NOT xEnable) THEN
			_xEnable := FALSE;
			_iState := 0;
		END_IF
		
		IF xError THEN
			_xEnable := FALSE;
			_iState := 1000;
		END_IF
		
	7: // Wait for all modules to be ready in balancing mode
		IF _xAllModulesReady THEN
			_iState := 50;
		END_IF
		
		IF (NOT xStartBalancing) THEN
			_xStartBalancing := FALSE;
			_iState := 0;
		END_IF
		
		IF xError THEN
			_xEnable := FALSE;
			_iState := 1000;
		END_IF
		
	
	10: // Reset safety from sensors
		IF _tonResetPulseLength.Q THEN
			_tonResetPulseLength.IN := FALSE;
			xResetSafetyDCCB := FALSE;
			_tonSafetyOkTimeout.IN := TRUE;
			_iState := 15;
		END_IF
		
	15: // Wait for Safety to be ok
		IF xSafetyIntlksOk THEN
			_tonSafetyOkTimeout.IN := FALSE;
			xCloseDCCB := TRUE;
			_tonErrorDCCBNotClosed.IN := TRUE;
			_iState := 20;
		END_IF
		
		IF NOT xEnable THEN
			_tonSafetyOkTimeout.IN := FALSE;
			_xEnable := FALSE;
			_iState := 40;
		END_IF
		
		IF _tonSafetyOkTimeout.Q THEN
			_tonSafetyOkTimeout.IN := FALSE;
			xCloseDCCB := TRUE;
			xError := TRUE;
			xReady := FALSE;
			_iState := 1000;
		END_IF
	
	20: // Check if DC relais closed and safety is ok
		IF NOT xDCCBOpen THEN
			_xEnableInverter := TRUE;
			_rPowerInverterInternal := rPowerInverter;
			//_rPowerInverterInternal := 0.0;
			_iState := 21;
		END_IF
		IF _tonErrorDCCBNotClosed.Q THEN
			_tonErrorDCCBNotClosed.IN := FALSE;
			xError := TRUE;
			xReady := FALSE;
			_iState := 1000;
		END_IF
		
		IF NOT xEnable THEN
			_tonSafetyOkTimeout.IN := FALSE;
			_xEnable := FALSE;
			_iState := 40;
		END_IF
		
	21: // Wait for inverter to be ready
		_tonInverterStartupTimeout(IN := TRUE);
		IF _fbInverter.xActive AND (NOT _fbInverter.xError) THEN
			_iState := 30;
			xReady := TRUE;
			_tonInverterStartupTimeout(IN := FALSE);
		END_IF
		
		IF (NOT xEnable) OR (NOT _xAllModulesReady) THEN
			_xEnableInverter := FALSE;
			_rPowerInverterInternal := 0.0;
			_xEnable := FALSE;
			_iState := 31;
		END_IF
		
		// Inverter error or ttimeout for startup
		IF _fbInverter.xError OR (NOT xRepairSwitchOk) THEN // _tonInverterStartupTimeout.Q
			IF _tonInverterStartupTimeout.Q AND (NOT _fbInverterStartupTimeoutAlarm.bRaised) THEN
				_fbInverterStartupTimeoutAlarm.Raise(0);
			END_IF
			_iState := 1000;
			_xEnableInverter := FALSE;
			xError := TRUE;
			xErrorInverter := TRUE;
			_tonInverterStartupTimeout(IN := FALSE);
		END_IF
	
	30: // All modules ready
		// !!! ATTENTION !!!
		// BMS HAS TO SHUT DOWN THE INVERTER BEFORE DISSABLING THE STRING
		// OTHERWISE THE DC CIRCUIT BREAKERS WILL OPEN WHILE THE INVERTER IS STILL ACTIVE
		// THIS CAN DAMAGE THE INVERTER
		IF rPowerInverter > DINT_TO_REAL(GVL_CONFIG.diMaxStringDischargePower) THEN
			// Limit discharge power (> because discharging is a positive number)
			_rPowerInverterInternal := DINT_TO_REAL(GVL_CONFIG.diMaxStringDischargePower);
		ELSIF rPowerInverter < DINT_TO_REAL(GVL_CONFIG.diMaxStringChargingPower) THEN
			// Limit charging power (< because charging is a negative number)
			_rPowerInverterInternal := DINT_TO_REAL(GVL_CONFIG.diMaxStringChargingPower);
		ELSE
			// Power command within range
			_rPowerInverterInternal := rPowerInverter;
		END_IF
		
		IF NOT xEnable THEN
			// _rPowerInverterInternal := 0.0;
			// _xEnableInverter := FALSE;
			_xEnable := FALSE;
			_xReleaseLimitErrorsInternal := FALSE;
			IF GVL_CONFIG.xShutdownDischargeWithInverter THEN
				_rPowerInverterInternal := GVL_CONFIG.rAbsShutdownDischargePower;
				_iState := 31;
			ELSE
				_rPowerInverterInternal := 0.0;
				_xEnableInverter := FALSE;
				_iState := 40;
			END_IF
			
		ELSIF (NOT _xAllModulesReady) OR (NOT _xBalanceOk) OR (NOT xSafetyIntlksOk) OR (NOT xRepairSwitchOk) THEN
			xError := TRUE;
			_xReleaseLimitErrorsInternal := FALSE;
			_xEnable := FALSE;
			_rPowerInverterInternal := 0.0;
			_xEnableInverter := FALSE;
			_iState := 1000;
		END_IF
		
	31: // Wait for String to be in in shutdown discharge mode
		IF _xAllModulesInShutdownDischargeMode THEN
			_iState := 32;
		END_IF
		
	32: // Shutdown discharge mode
		IF xShutdownDischargeAllowed AND GVL_CONFIG.xShutdownDischargeWithInverter AND xSafetyIntlksOk AND (stInverterData.rActDCVoltage > 620.0) THEN
			_rPowerInverterInternal := GVL_CONFIG.rAbsShutdownDischargePower;
		ELSE
			// Send shutdown message
			IF NOT xShutdownDischargeAllowed THEN
				_fbSDUnitThreshold.Send(0);
			END_IF
			
			IF (stInverterData.rActDCVoltage < 620.0) THEN
				_fbSDDCLevel.Send(0);
			END_IF
			
			_rPowerInverterInternal := 0.0;
			_xEnableInverter := FALSE;
			_iState := 40;
		END_IF
		
		// Restart on Enable or StartBalancing
		IF xEnable OR xStartBalancing THEN
			_rPowerInverterInternal := 0.0;
			_xEnableInverter := FALSE;
			_iState := 40;
		END_IF
		
	40: // Wait for inverter to shut down
		IF NOT _fbInverter.xActive THEN
			_iState := 41;
		END_IF
		
	41: // Debug delay time for inverter shutdown
		_tonInverterShutdownDelay(IN := TRUE);
		
		IF _tonInverterShutdownDelay.Q THEN
			_tonInverterShutdownDelay(IN := FALSE);
			xCloseDCCB := FALSE;
			_iState := 0;
		END_IF
		
	50: // Wait for balancing of all units to be done
		IF _xBalancingDone THEN
			_xStartBalancing := FALSE;
			_iState := 51;
		END_IF
		
	51: // Check if start balancing has been releases to avoid a restart
		IF (NOT xStartBalancing) THEN
			_iState := 0;
		END_IF
		
	1000: // Error state
		// Leave error state only if modules are deactivated
		IF NOT xEnable THEN
			_tonResetPulseLength.IN := FALSE;
			_tonErrorDCCBNotClosed.IN := FALSE;
			_iState := 0;
		END_IF
END_CASE

// Copy inverter data to SCADA interface
stHMIInterface.stInverterData := stInverterData;

IF _xAllModulesReady AND _xBalanceOk AND (_iState = 30) THEN
	xReady := TRUE;
ELSE
	xReady := FALSE;
END_IF

// Reset inverter startup timeout alarm
IF _fbInverterStartupTimeoutAlarm.bRaised AND xConfirmAlarms THEN
	_fbInverterStartupTimeoutAlarm.Clear(0, TRUE);
END_IF

// ===============================
// String status sum
// ===============================
IF xOff THEN
	eStatus := E_COMPONENT_STATUS.OFF;
END_IF

IF xReady THEN
	eStatus := E_COMPONENT_STATUS.ON;
END_IF

IF xError THEN
	eStatus := E_COMPONENT_STATUS.ERROR;
END_IF]]></ST>
    </Implementation>
    <Method Name="FB_init" Id="{9e8494eb-1b40-4be9-91c8-810ecbdf7f0c}">
      <Declaration><![CDATA[METHOD FB_init : BOOL
VAR_INPUT
	bInitRetains : BOOL; // if TRUE, the retain variables are initialized (warm start / cold start)
	bInCopyCode : BOOL;  // if TRUE, the instance afterwards gets moved into the copy code (online change)
	
	sName : STRING;
END_VAR
]]></Declaration>
      <Implementation>
        <ST><![CDATA[_sName := sName;

// Set names for modules
_fbModule1.Name := Concat(_sName, ' - Module 1');
_fbModule2.Name := Concat(_sName, ' - Module 2');
_fbModule3.Name := Concat(_sName, ' - Module 3');

// Create out of balance alarm
_fbModulesOutOfBalanceAlarm.CreateEx(stEventEntry := TC_EVENTS.BMSEvents.StringImbalance, bWithConfirmation := TRUE, 0);
_fbModulesOutOfBalanceAlarm.ipArguments.Clear().AddString(_sName);

// Create safetyinterlocks active alarm
_fbSafetyInterlocksNotOkAlarm.CreateEx(stEventEntry := TC_EVENTS.BMSEvents.SafetyIntlksActive, bWithConfirmation := FALSE, 0);
_fbSafetyInterlocksNotOkAlarm.ipArguments.Clear().AddString(_sName);

// Create inverter startup timeout alarm
_fbInverterStartupTimeoutAlarm.CreateEx(stEventEntry := TC_EVENTS.BMSEvents.InverterStartupTimeout, bWithConfirmation := TRUE, 0);
_fbInverterStartupTimeoutAlarm.ipArguments.Clear().AddString(_sName);

// Create DC Main Switch not closed alarm
_fbDCMainSwitchNotClosed.CreateEx(stEventEntry := TC_EVENTS.General.DCMainSwitchNotClosed, bWithConfirmation := FALSE, 0);
_fbDCMainSwitchNotClosed.ipArguments.Clear().AddString(_sName);

// Shutdown discharge messages
_fbSDDCLevel.CreateEx(TC_EVENTS.BMSEvents.SDDCVoltage, 0);
_fbSDDCLevel.ipArguments.Clear().AddString(_sName);

_fbSDUnitThreshold.CreateEx(TC_EVENTS.BMSEvents.SDUnitThreshhold, 0);
_fbSDUnitThreshold.ipArguments.Clear().AddString(_sName);]]></ST>
      </Implementation>
    </Method>
    <Property Name="Name" Id="{19fcb6d4-fd4b-49f9-9791-1e4c931b9e69}">
      <Declaration><![CDATA[PROPERTY Name : String]]></Declaration>
      <Get Name="Get" Id="{a4b6ba34-8ad9-46b1-939c-45cef957fd9a}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[Name := _sName;]]></ST>
        </Implementation>
      </Get>
      <Set Name="Set" Id="{c32997c5-bac4-4ef0-bc87-edcbbb2e542f}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[_sName := Name;

// Set modules names
_fbModule1.Name := CONCAT(_sName, ' - Module 1');
_fbModule2.Name := CONCAT(_sName, ' - Module 2');
_fbModule3.Name := CONCAT(_sName, ' - Module 3');

// Create alarm messages
_fbModulesOutOfBalanceAlarm.ipArguments.Clear().AddString(_sName);
_fbSafetyInterlocksNotOkAlarm.ipArguments.Clear().AddString(_sName);]]></ST>
        </Implementation>
      </Set>
    </Property>
    <LineIds Name="FB_String">
      <LineId Id="4873" Count="525" />
      <LineId Id="527" Count="0" />
    </LineIds>
    <LineIds Name="FB_String.FB_init">
      <LineId Id="7" Count="0" />
      <LineId Id="33" Count="0" />
      <LineId Id="32" Count="0" />
      <LineId Id="34" Count="2" />
      <LineId Id="16" Count="0" />
      <LineId Id="15" Count="0" />
      <LineId Id="17" Count="0" />
      <LineId Id="20" Count="0" />
      <LineId Id="31" Count="0" />
      <LineId Id="29" Count="1" />
      <LineId Id="28" Count="0" />
      <LineId Id="45" Count="0" />
      <LineId Id="44" Count="0" />
      <LineId Id="46" Count="1" />
      <LineId Id="56" Count="0" />
      <LineId Id="55" Count="0" />
      <LineId Id="57" Count="1" />
      <LineId Id="67" Count="0" />
      <LineId Id="66" Count="0" />
      <LineId Id="68" Count="1" />
      <LineId Id="71" Count="0" />
      <LineId Id="70" Count="0" />
      <LineId Id="72" Count="0" />
    </LineIds>
    <LineIds Name="FB_String.Name.Get">
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_String.Name.Set">
      <LineId Id="2" Count="0" />
      <LineId Id="9" Count="0" />
      <LineId Id="6" Count="2" />
      <LineId Id="5" Count="0" />
      <LineId Id="13" Count="0" />
      <LineId Id="11" Count="0" />
      <LineId Id="10" Count="0" />
      <LineId Id="12" Count="0" />
    </LineIds>
  </POU>
</TcPlcObject>