<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.12">
  <POU Name="PS_UnitModeManager" Id="{07b03d93-d3ad-4644-9925-bed4dffdc9a3}" SpecialFunc="None">
    <Declaration><![CDATA[FUNCTION_BLOCK PS_UnitModeManager
VAR_INPUT
	Execute 		: BOOL;
	eModeCommand 	: E_PMLUnitMode;
	ePMLState		: E_PMLState;
END_VAR
VAR_OUTPUT
	eModeStatus 	: E_PMLUnitMode;
	Done 			: BOOL;
	Error 			: BOOL;
	ErrorID 		: UDINT;
END_VAR
VAR
	fbTrigger		: R_TRIG;
END_VAR]]></Declaration>
    <Implementation>
      <ST><![CDATA[fbTrigger(CLK := Execute);

IF fbTrigger.Q THEN (* rising edge Execute *)
	Done 		:= FALSE;
	Error 		:= FALSE;
	ErrorID 	:= 0;

     CASE eModeStatus OF
		E_PMLUnitMode.UNDEFINED:
					eModeStatus := eModeCommand;
					Done := TRUE;

		E_PMLUnitMode.AUTOMATIC:
					IF (ePMLState = E_PMLState.STOPPED) OR (ePMLState = E_PMLState.ABORTED) OR (ePMLState = E_PMLState.IDLE) THEN
						eModeStatus := eModeCommand;
						Done := TRUE;
					ELSIF ((ePMLState = E_PMLState.SUSPENDED) OR (ePMLState = E_PMLState.HELD) OR (ePMLState = E_PMLState.COMPLETE))
						AND (eModeCommand = E_PMLUnitMode.SEMIAUTOMATIC) THEN
		
						eModeStatus := eModeCommand;
						Done := TRUE;
					ELSIF (ePMLState = E_PMLState.HELD) AND (eModeCommand = E_PMLUnitMode.MAINTENANCE) THEN
						eModeStatus := eModeCommand;
						Done := TRUE;
					ELSE
						Error := TRUE;
						ErrorID := 1;
					END_IF

		E_PMLUnitMode.MAINTENANCE:
					IF (ePMLState = E_PMLState.STOPPED) OR (ePMLState = E_PMLState.ABORTED) OR (ePMLState = E_PMLState.IDLE) THEN
						eModeStatus := eModeCommand;
						Done := TRUE;
					ELSIF (ePMLState = E_PMLState.HELD) AND ((eModeCommand = E_PMLUnitMode.AUTOMATIC) OR (eModeCommand = E_PMLUnitMode.SEMIAUTOMATIC)) THEN
						eModeStatus := eModeCommand;
						Done := TRUE;
					ELSE
						Error := TRUE;
						ErrorID := 1;
					END_IF

		E_PMLUnitMode.MANUAL:
					IF (ePMLState = E_PMLState.STOPPED) OR (ePMLState = E_PMLState.ABORTED) OR (ePMLState = E_PMLState.IDLE) THEN
						eModeStatus := eModeCommand;
						Done := TRUE;
					ELSE
						Error := TRUE;
						ErrorID := 1;
					END_IF

		E_PMLUnitMode.SEMIAUTOMATIC:
					IF (ePMLState = E_PMLState.STOPPED) OR (ePMLState = E_PMLState.ABORTED) OR (ePMLState = E_PMLState.IDLE) THEN
						eModeStatus := eModeCommand;
						Done := TRUE;
					ELSIF ((ePMLState = E_PMLState.SUSPENDED) OR (ePMLState = E_PMLState.HELD) OR (ePMLState = E_PMLState.COMPLETE))
						AND (eModeCommand = E_PMLUnitMode.AUTOMATIC) THEN
		
						eModeStatus := eModeCommand;
						Done := TRUE;
					ELSIF (ePMLState = E_PMLState.HELD) AND (eModeCommand = E_PMLUnitMode.MAINTENANCE) THEN
						eModeStatus := eModeCommand;
						Done := TRUE;
					ELSE
						Error := TRUE;
						ErrorID := 1;
					END_IF

		E_PMLUnitMode.DRYRUN:
					eModeStatus := eModeCommand;
					Done 		:= TRUE;

		E_PMLUnitMode.USERMODE1:
					eModeStatus := eModeCommand;
					Done 		:= TRUE;

		E_PMLUnitMode.USERMODE2:
					eModeStatus := eModeCommand;
					Done 		:= TRUE;

		E_PMLUnitMode.IDLE:
					IF (ePMLState = E_PMLState.STOPPED) OR (ePMLState = E_PMLState.ABORTED) OR (ePMLState = E_PMLState.IDLE) THEN
						eModeStatus := eModeCommand;
						Done 		:= TRUE;
					ELSE
						Error 	:= TRUE;
						ErrorID := 1;
					END_IF

		E_PMLUnitMode.ESTOP:
					IF (ePMLState = E_PMLState.STOPPED) OR (ePMLState = E_PMLState.ABORTED) OR (ePMLState = E_PMLState.IDLE) THEN
						 eModeStatus := eModeCommand;
						 Done 		:= TRUE;
					ELSE
						 Error 	:= TRUE;
						 ErrorID := 1;
					END_IF
     END_CASE
END_IF

 (* reset with bExecute = FALSE *)
 IF NOT Execute THEN
     Done 		:= FALSE;
     Error 		:= FALSE;
     ErrorID 	:= 0;
 END_IF]]></ST>
    </Implementation>
    <LineIds Name="PS_UnitModeManager">
      <LineId Id="3" Count="104" />
    </LineIds>
  </POU>
</TcPlcObject>