<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4026.8">
  <POU Name="FB_Module" Id="{87be924f-018d-4c09-997b-f0c0054414cc}" SpecialFunc="None">
    <Declaration><![CDATA[FUNCTION_BLOCK FB_Module
VAR_INPUT
	// Emergency stop ok
	xEmergencyStopOk : BOOL;
	
	// Enable
	xEnable : BOOL;
	
	// Start balancing
	xStartBalancing : BOOL;
	
	// Component shortage workaround
	stModuleVoltageConfig : ST_MODULE_VOLT_CONFIG;
	
	// Module in safety check mode
	xInSafetyCheckMode : BOOL;
	
	// All safetyinterlocks are ok
	xSafetyIntlksOk : BOOL;
	
	// Module completely off
	xOff : BOOL := TRUE;
	
	// HMI Interface
	stHMIInterface : REFERENCE TO ST_MODULE_HMI_INTERFACE;
	
	// Release alarms
	xReleaseErrors : BOOL;
	
	// Release analog io limit errors
	xReleaseLimitErrors : BOOL;
	
	// Release manual mode
	xReleaseManualMode : BOOL;
	
	// Switch all components to manual mode
	xAllToManualMode : BOOL;
	
	// Input to confirm all errors
	xConfirmAlarms : BOOL;
	
	// Balancing target Voltage
	rBalancingTargetVoltage : REAL;
	
	// Inverter enabled status
	xInverterEnabled : BOOL;
	
	// Leakage double segment 1/2
	xLeakageSegments12 AT %I* : BOOL;
	
	// Leakage double segment 3/4
	xLeakageSegments34 AT %I* : BOOL;
	
	// Leakage tank
	xLeakageTank AT %I* : BOOL;
	
	xECState AT %I* : UINT;
END_VAR
VAR_OUTPUT
	// Module voltage
	rCurrentVoltage : REAL;	
	
	// Module ready
	xReady : BOOL;
	
	// Module in shutdown segment discharge mode
	xInShutdownDischargeMode : BOOL;
	
	// Module can be discharged during shutdown sequence
	xShutdownDischargeAllowed : BOOL;

	// One unit has an error
	xError : BOOL;
	
	// One unit has a warning
	xWarning : BOOL;
	
	// All modules in automatic mode
	xAllUnitsInAutomatic : BOOL;
	
	// Smallest segment voltage
	rSmallestSegmentVoltage : REAL;
	
	// Highest segment voltage
	rHighestSegmentVoltage : REAL;
	
	// Balancing done
	xBalancingDone : BOOL;
END_VAR
VAR
	_fbUnit1 : FB_Unit(CONCAT(Name, ' - Unit 1'));
	_fbUnit2 : FB_Unit(CONCAT(Name, ' - Unit 2'));
	_fbUnit3 : FB_Unit(CONCAT(Name, ' - Unit 3'));
	_fbUnit4 : FB_Unit(CONCAT(Name, ' - Unit 4'));
	
	// Flag for unit balance checking
	_xBalanceOk : BOOL;
	
	// All units are ready
	_xAllUnitsReady : BOOL;
	
	// Units out of balance alarm
	_fbUnitsOutOfBalanceAlarm : FB_TcAlarm;
	
	// Connection lost alarm
	_fbConnLostAlarm : FB_TcAlarm;
	
	// Enable balance check
	_xEnableBalanceCheck : BOOL;
	
	// Module name
	_sName : STRING;
	
	// Moving average filter for module voltage
	//_fbMAFVoltage : FB_MovingAverageFilter;
	//_arMAFVoltage : ARRAY[0..9] OF REAL;
	
	_fbBalanceNotOkSignal : FB_ReleaseSignal;
	
	_xECModuleInOP : BOOL;
	
	xDebug1 : BOOL;
	xDebug2 : BOOL;
	xDebug3 : BOOL;
	xDebug4 : BOOL;
END_VAR
]]></Declaration>
    <Implementation>
      <ST><![CDATA[// Reset error and warning flag
xError := FALSE;
xWarning := FALSE;

// Reset automatic mode flag
xAllUnitsInAutomatic := TRUE;

// Reset safety interlocks flag
xSafetyIntlksOk := TRUE;

// ===============================
// Handle module connection lost error
// ===============================
_xECModuleInOP := (xECState AND 16#000F) = 8;
IF (NOT _xECModuleInOP) AND (NOT _fbConnLostAlarm.bRaised) AND xReleaseErrors THEN
	_fbConnLostAlarm.Raise(0);
END_IF

IF (_xECModuleInOP) AND _fbConnLostAlarm.bRaised THEN
	_fbConnLostAlarm.Clear(0, FALSE);
END_IF

IF _fbConnLostAlarm.eConfirmationState = TcEventConfirmationState.WaitForConfirmation AND xConfirmAlarms THEN
	_fbConnLostAlarm.Confirm(0);
END_IF


// ===============================
// Unit 1
// ===============================
_fbUnit1(
	xEnable := xEnable,
	xStartBalancing := xStartBalancing,
	xInverterEnabled := xInverterEnabled,
	xVoltageSensorIs1500V := stModuleVoltageConfig.xUnit1Is1500V,
	xInSafetyCheckMode := xInSafetyCheckMode,
	stUnitConfig:= GVL_CONFIG.stUnitConfig, 
	stHMIInterface:= stHMIInterface.stHMIInterfaceUnit1, 
	xEmergencyStopOk:= xEmergencyStopOk,  
	xReleaseErrors:= xReleaseErrors, 
	xReleaseLimitErrors:= xReleaseLimitErrors, 
	xConfirmAlarms:= xConfirmAlarms,
	xReleaseManualMode := xReleaseManualMode,
	rBalancingTargetVoltage := rBalancingTargetVoltage,
	xAllToManualMode := xAllToManualMode,
	_xWarningConfirmPending=> );

IF _fbUnit1.xWarning THEN
	xWarning := TRUE;
END_IF

IF _fbUnit1.xError THEN
	xError := TRUE;
	xDebug1 := TRUE;
END_IF

IF NOT _fbUnit1.xSafetyIntlksOk THEN
	xSafetyIntlksOk := FALSE;
END_IF

IF NOT _fbUnit1.xAllComponentsInAuto THEN
	xAllUnitsInAutomatic := FALSE;
END_IF


// ===============================
// Unit 2
// ===============================
_fbUnit2(
	xEnable := xEnable,
	xStartBalancing := xStartBalancing,
	xInverterEnabled := xInverterEnabled,
	xVoltageSensorIs1500V := stModuleVoltageConfig.xUnit2Is1500V,
	xInSafetyCheckMode := xInSafetyCheckMode,
	stUnitConfig:= GVL_CONFIG.stUnitConfig, 
	stHMIInterface:= stHMIInterface.stHMIInterfaceUnit2, 
	xEmergencyStopOk:= xEmergencyStopOk,  
	xReleaseErrors:= xReleaseErrors, 
	xReleaseLimitErrors:= xReleaseLimitErrors, 
	xConfirmAlarms:= xConfirmAlarms,
	xReleaseManualMode := xReleaseManualMode,
	rBalancingTargetVoltage := rBalancingTargetVoltage,
	xAllToManualMode := xAllToManualMode,
	_xWarningConfirmPending=> );

IF _fbUnit2.xWarning THEN
	xWarning := TRUE;
END_IF

IF _fbUnit2.xError THEN
	xError := TRUE;
	xDebug2 := TRUE;
END_IF

IF NOT _fbUnit2.xSafetyIntlksOk THEN
	xSafetyIntlksOk := FALSE;
END_IF

IF NOT _fbUnit2.xAllComponentsInAuto THEN
	xAllUnitsInAutomatic := FALSE;
END_IF


// ===============================
// Unit 3
// ===============================
_fbUnit3(
	xEnable := xEnable,
	xStartBalancing := xStartBalancing,
	xInverterEnabled := xInverterEnabled,
	xVoltageSensorIs1500V := stModuleVoltageConfig.xUnit3Is1500V,
	xInSafetyCheckMode := xInSafetyCheckMode,
	stUnitConfig:= GVL_CONFIG.stUnitConfig, 
	stHMIInterface:= stHMIInterface.stHMIInterfaceUnit3, 
	xEmergencyStopOk:= xEmergencyStopOk,  
	xReleaseErrors:= xReleaseErrors, 
	xReleaseLimitErrors:= xReleaseLimitErrors, 
	xConfirmAlarms:= xConfirmAlarms,
	xReleaseManualMode := xReleaseManualMode,
	rBalancingTargetVoltage := rBalancingTargetVoltage,
	xAllToManualMode := xAllToManualMode,
	_xWarningConfirmPending=> );

IF _fbUnit3.xWarning THEN
	xWarning := TRUE;
END_IF

IF _fbUnit3.xError THEN
	xError := TRUE;
	xDebug3 := TRUE;
END_IF

IF NOT _fbUnit3.xSafetyIntlksOk THEN
	xSafetyIntlksOk := FALSE;
END_IF

IF NOT _fbUnit3.xAllComponentsInAuto THEN
	xAllUnitsInAutomatic := FALSE;
END_IF


// ===============================
// Unit 4
// ===============================
_fbUnit4(
	xEnable := xEnable,
	xStartBalancing := xStartBalancing,
	xInverterEnabled := xInverterEnabled,
	xVoltageSensorIs1500V := stModuleVoltageConfig.xUnit4Is1500V,
	xInSafetyCheckMode := xInSafetyCheckMode,
	stUnitConfig:= GVL_CONFIG.stUnitConfig, 
	stHMIInterface:= stHMIInterface.stHMIInterfaceUnit4, 
	xEmergencyStopOk:= xEmergencyStopOk,  
	xReleaseErrors:= xReleaseErrors, 
	xReleaseLimitErrors:= xReleaseLimitErrors, 
	xConfirmAlarms:= xConfirmAlarms,
	xReleaseManualMode := xReleaseManualMode,
	rBalancingTargetVoltage := rBalancingTargetVoltage,
	xAllToManualMode := xAllToManualMode,
	_xWarningConfirmPending=> );

IF _fbUnit4.xWarning THEN
	xWarning := TRUE;
END_IF

IF _fbUnit4.xError THEN
	xError := TRUE;
	xDebug4 := TRUE;
END_IF

IF NOT _fbUnit4.xSafetyIntlksOk THEN
	xSafetyIntlksOk := FALSE;
END_IF

IF NOT _fbUnit4.xAllComponentsInAuto THEN
	xAllUnitsInAutomatic := FALSE;
END_IF


// ===============================
// Units ready check
// ===============================
_xAllUnitsReady := _fbUnit1.xReady AND _fbUnit2.xReady AND _fbUnit3.xReady AND _fbUnit4.xReady;


// ===============================
// Balancing done check
// ===============================
xBalancingDone := _fbUnit1.xBalancingDone AND _fbUnit2.xBalancingDone AND _fbUnit3.xBalancingDone AND _fbUnit4.xBalancingDone;


// ===============================
// Units in shutdown discharge mode
// ===============================
xInShutdownDischargeMode := _fbUnit1.xInShutdownDischargeMode AND _fbUnit2.xInShutdownDischargeMode AND _fbUnit3.xInShutdownDischargeMode AND _fbUnit4.xInShutdownDischargeMode;


// ===============================
// Units shutdown discharge allowed
// ===============================
xShutdownDischargeAllowed := _fbUnit1.xShutdownDischargeAllowed AND _fbUnit2.xShutdownDischargeAllowed AND _fbUnit3.xShutdownDischargeAllowed AND _fbUnit4.xShutdownDischargeAllowed;


// ===============================
// All units off
// ===============================
xOff := _fbUnit1.xOff AND _fbUnit2.xOff AND _fbUnit3.xOff AND _fbUnit4.xOff;

// ===============================
// Calculate module voltage
// ===============================
rCurrentVoltage := _fbUnit1.rCurrentVoltage + _fbUnit2.rCurrentVoltage + _fbUnit3.rCurrentVoltage + _fbUnit4.rCurrentVoltage;
//_fbMAFVoltage(pValues := ADR(_arMAFVoltage), iArraySize := 10, rCurrentValue := rCurrentVoltage, rAverage => stHMIInterface.rVoltage);
//stHMIInterface.rVoltage := rCurrentVoltage;
stHMIInterface.rVoltage := stHMIInterface.rVoltage * 0.9 + rCurrentVoltage * 0.1;


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

// Test unit 1 with unit 2
IF ABS(_fbUnit1.rCurrentVoltage - _fbUnit2.rCurrentVoltage) > GVL_CONFIG.rMaxAbsDiffVoltageUnitsOnModule THEN
	_xBalanceOk := FALSE;
END_IF

// Test unit 1 with unit 3
IF ABS(_fbUnit1.rCurrentVoltage - _fbUnit3.rCurrentVoltage) > GVL_CONFIG.rMaxAbsDiffVoltageUnitsOnModule THEN
	_xBalanceOk := FALSE;
END_IF

// Test unit 1 with unit 4
IF ABS(_fbUnit1.rCurrentVoltage - _fbUnit4.rCurrentVoltage) > GVL_CONFIG.rMaxAbsDiffVoltageUnitsOnModule THEN
	_xBalanceOk := FALSE;
END_IF

// Test unit 2 with unit 3
IF ABS(_fbUnit2.rCurrentVoltage - _fbUnit3.rCurrentVoltage) > GVL_CONFIG.rMaxAbsDiffVoltageUnitsOnModule THEN
	_xBalanceOk := FALSE;
END_IF

// Test unit 2 with unit 4
IF ABS(_fbUnit2.rCurrentVoltage - _fbUnit4.rCurrentVoltage) > GVL_CONFIG.rMaxAbsDiffVoltageUnitsOnModule THEN
	_xBalanceOk := FALSE;
END_IF

// Test unit 3 with unit 4
IF ABS(_fbUnit3.rCurrentVoltage - _fbUnit4.rCurrentVoltage) > GVL_CONFIG.rMaxAbsDiffVoltageUnitsOnModule THEN
	_xBalanceOk := FALSE;
END_IF

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

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

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

// Clear error only with confirmation because of voltage ripple event spam
IF (NOT _fbBalanceNotOkSignal.xReleaseSignal) AND _fbUnitsOutOfBalanceAlarm.bRaised AND xConfirmAlarms THEN
	_fbUnitsOutOfBalanceAlarm.Clear(0, FALSE);
END_IF

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

// ===============================
// Module ready validation check
// ===============================
IF _xAllUnitsReady AND _xBalanceOk THEN
	xReady := TRUE;
ELSE
	xReady := FALSE;
END_IF


// ===============================
// Get the smallest segment Voltage
// for balancing
// ===============================
rSmallestSegmentVoltage := MIN(_fbUnit1.rCurrentVoltage, _fbUnit2.rCurrentVoltage, _fbUnit3.rCurrentVoltage, _fbUnit4.rCurrentVoltage);
rHighestSegmentVoltage := MAX(_fbUnit1.rCurrentVoltage, _fbUnit2.rCurrentVoltage, _fbUnit3.rCurrentVoltage, _fbUnit4.rCurrentVoltage);

// ===============================
// Module status sum
// ===============================
IF xReady AND (NOT xError) THEN
	stHMIInterface.eStatus := E_COMPONENT_STATUS.ON;
END_IF

IF (_fbUnit1.eUnitState = E_COMPONENT_STATUS.STARTING OR _fbUnit1.eUnitState = E_COMPONENT_STATUS.STARTING OR _fbUnit1.eUnitState = E_COMPONENT_STATUS.STARTING OR _fbUnit1.eUnitState = E_COMPONENT_STATUS.STARTING) AND (NOT xError) THEN
	stHMIInterface.eStatus := E_COMPONENT_STATUS.STARTING;
END_IF

IF (_fbUnit1.eUnitState = E_COMPONENT_STATUS.SHUTDOWN OR _fbUnit1.eUnitState = E_COMPONENT_STATUS.SHUTDOWN OR _fbUnit1.eUnitState = E_COMPONENT_STATUS.SHUTDOWN OR _fbUnit1.eUnitState = E_COMPONENT_STATUS.SHUTDOWN) AND (NOT xError) THEN
	stHMIInterface.eStatus := E_COMPONENT_STATUS.SHUTDOWN;
END_IF

IF (_fbUnit1.eUnitState = E_COMPONENT_STATUS.OFF AND _fbUnit1.eUnitState = E_COMPONENT_STATUS.OFF AND _fbUnit1.eUnitState = E_COMPONENT_STATUS.OFF AND _fbUnit1.eUnitState = E_COMPONENT_STATUS.OFF) AND (NOT xError) THEN
	stHMIInterface.eStatus := E_COMPONENT_STATUS.OFF;
END_IF

IF xError THEN
	stHMIInterface.eStatus := E_COMPONENT_STATUS.ERROR;
END_IF
]]></ST>
    </Implementation>
    <Method Name="FB_init" Id="{369c1d27-76e4-45f8-9dbe-03524d3389df}">
      <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 unit names
_fbUnit1.Name := CONCAT(_sName, 'Unit 1');
_fbUnit2.Name := CONCAT(_sName, 'Unit 2');
_fbUnit3.Name := CONCAT(_sName, 'Unit 3');
_fbUnit4.Name := CONCAT(_sName, 'Unit 4');

// Create out of balance alarm
_fbUnitsOutOfBalanceAlarm.CreateEx(stEventEntry := TC_EVENTS.BMSEvents.ModuleImbalance, bWithConfirmation := TRUE, 0);

// Create alarm message
_fbUnitsOutOfBalanceAlarm.ipArguments.Clear().AddString(_sName);

// Create connection lost alarm
_fbConnLostAlarm.CreateEx(stEventEntry := TC_EVENTS.General.CommError, bWithConfirmation := TRUE, 0);
_fbConnLostAlarm.ipArguments.Clear().AddString(_sName);


// Create leakage alarm messages
]]></ST>
      </Implementation>
    </Method>
    <Property Name="Name" Id="{bc8bc990-5071-47c9-a928-a129c60c6f41}">
      <Declaration><![CDATA[PROPERTY Name : String]]></Declaration>
      <Get Name="Get" Id="{c00a6d41-9156-49b5-aa08-e4cc4c913fca}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[Name := _sName;]]></ST>
        </Implementation>
      </Get>
      <Set Name="Set" Id="{ade5f4d9-ff42-4236-b5fc-e43a53c9ca28}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[_sName := Name;

// Set unit names
_fbUnit1.Name := CONCAT(_sName, ' - Unit 1');
_fbUnit2.Name := CONCAT(_sName, ' - Unit 2');
_fbUnit3.Name := CONCAT(_sName, ' - Unit 3');
_fbUnit4.Name := CONCAT(_sName, ' - Unit 4');

// Create alarm message
_fbUnitsOutOfBalanceAlarm.ipArguments.Clear().AddString(_sName);

// Create connection lost alarm
_fbConnLostAlarm.ipArguments.Clear().AddString(_sName);]]></ST>
        </Implementation>
      </Set>
    </Property>
    <LineIds Name="FB_Module">
      <LineId Id="3" Count="8" />
      <LineId Id="338" Count="0" />
      <LineId Id="340" Count="1" />
      <LineId Id="339" Count="0" />
      <LineId Id="352" Count="0" />
      <LineId Id="337" Count="0" />
      <LineId Id="342" Count="1" />
      <LineId Id="345" Count="2" />
      <LineId Id="344" Count="0" />
      <LineId Id="349" Count="0" />
      <LineId Id="348" Count="0" />
      <LineId Id="350" Count="1" />
      <LineId Id="12" Count="275" />
      <LineId Id="317" Count="2" />
      <LineId Id="326" Count="0" />
      <LineId Id="325" Count="0" />
      <LineId Id="327" Count="1" />
      <LineId Id="330" Count="0" />
      <LineId Id="329" Count="0" />
      <LineId Id="331" Count="1" />
      <LineId Id="334" Count="0" />
      <LineId Id="333" Count="0" />
      <LineId Id="335" Count="1" />
      <LineId Id="322" Count="0" />
      <LineId Id="321" Count="0" />
      <LineId Id="323" Count="1" />
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Module.FB_init">
      <LineId Id="3" Count="13" />
      <LineId Id="18" Count="0" />
      <LineId Id="21" Count="1" />
      <LineId Id="19" Count="1" />
      <LineId Id="17" Count="0" />
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Module.Name.Get">
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Module.Name.Set">
      <LineId Id="3" Count="8" />
      <LineId Id="2" Count="0" />
      <LineId Id="14" Count="0" />
      <LineId Id="13" Count="0" />
      <LineId Id="12" Count="0" />
    </LineIds>
  </POU>
</TcPlcObject>