
(*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX				INIT			XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*)

IF NOT bInit THEN
	bWarnMsgCmd:=bpersWarnMsgCmd;
	bErrMsgStopReq:=bpersErrMsgStopReq;
END_IF


(*Get Trigger Signals*)
rtrigHW_Input(CLK:=bHW_Input);
rtrigManStartForLub(CLK:=bManStartForLub);
rtrigManStartFillFnc(CLK:=bManStartFillFnc);
rtrigCntAutoCycle(CLK:=bCntAutoCycle);
rtrigStartNomalCycle(CLK:=		rtrigManStartForLub.Q 
							OR  bLubRequestFromAxes
							OR  bLubRequestFromAutoCycle
							OR 	bLubRequestFromTimer
					);


(*Pre Set Volume*)
rtrigPreSetVolume(CLK:=bPreSetVolume);
IF rtrigPreSetVolume.Q THEN
	lrpersInternalVolume:=(clrMaxPreSetVolume * UDINT_TO_LREAL(udiPreSetVolume)) / 100.0;
END_IF


(*Count Up when reasing trigger on HW Input*)
IF rtrigHW_Input.Q THEN
	udiInput_rtrig_Cnt:=(udiInput_rtrig_Cnt + 1);
END_IF


(*Get Time of equeal Counter Value and Feedback On*)
tTonET_EqualInput_rtrig_Cnt:=tonEqualInput_rtrig_Cnt.ET;
tonEqualInput_rtrig_Cnt(IN:=(udiInput_rtrig_Cnt = udiInput_rtrig_CntOld), PT:=T#5S);


(*Check for Empty Pulse*)
IF (udiInput_rtrig_Cnt <> udiInput_rtrig_CntOld) THEN
	IF (tTonET_EqualInput_rtrig_Cnt > T#1900MS) AND (tTonET_EqualInput_rtrig_Cnt < T#2100MS) THEN
		bEmtpyFreqActive:=TRUE;
	ELSE
		bEmtpyFreqActive:=FALSE;
	END_IF
END_IF
IF tonEqualInput_rtrig_Cnt.Q THEN
	bEmtpyFreqActive:=FALSE;
END_IF


(*Ton Feedback Signal On*)
tonFeedbackOn(IN:=bHW_Input, PT:=T#5S);


(*Error on Pump*)
tonErrPump(IN:= NOT bHW_Input, PT:=T#40S);


(*Cmd Error when not ready to start*)
bPumpReady:=tonFeedbackOn.Q AND NOT bWarnMsgEmpty AND NOT bWarnMsgPump AND NOT bLubPorcStarted ;

(*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*)




(*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX				INDICATOR HANDLING 			  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*)

(*Indicator Handling for Axes*)
IF (udiDistAxis_1 < udipersDistToLastLubCycleAxis_1) THEN
	udipersDistToLastLubCycleAxis_1:=udiDistAxis_1;
END_IF
IF (udiDistAxis_2 < udipersDistToLastLubCycleAxis_2) THEN
	udipersDistToLastLubCycleAxis_2:=udiDistAxis_2;
END_IF
IF (udiDistAxis_3 < udipersDistToLastLubCycleAxis_3) THEN
	udipersDistToLastLubCycleAxis_3:=udiDistAxis_3;
END_IF

udiCurDistAxis_1:=(udiDistAxis_1 - udipersDistToLastLubCycleAxis_1);(*Set FB Output for Visualization*)
udiCurDistAxis_2:=(udiDistAxis_2 - udipersDistToLastLubCycleAxis_2);(*Set FB Output for Visualization*)
udiCurDistAxis_3:=(udiDistAxis_3 - udipersDistToLastLubCycleAxis_3);(*Set FB Output for Visualization*)

bLubRequestFromAxes := 		((udiCurDistAxis_1 >=  udiAxisDistForLub) AND (udiAxisDistForLub > 0))
						OR 	((udiCurDistAxis_2 >=  udiAxisDistForLub) AND (udiAxisDistForLub > 0))
						OR 	((udiCurDistAxis_3 >=  udiAxisDistForLub) AND (udiAxisDistForLub > 0));


(*Indicator with auto cycle Counter*)	
IF rtrigCntAutoCycle.Q THEN
	udipersNumOfAutoCycle:=(udipersNumOfAutoCycle + 1);
END_IF
udiCurAutoCyc:=udipersNumOfAutoCycle;(*Set FB Output for Visualization*)

bLubRequestFromAutoCycle:=(udipersNumOfAutoCycle >= udiAutoCycForLub) AND (udiAutoCycForLub > 0);


(*Indicator with timer*)
IF (NOT bAxesInMotion OR tonTimerForLub.Q) THEN
	tpersTemporaryTime:=(tpersTemporaryTime + tonTimerForLub.ET);
	tonTimerForLub(IN:=FALSE);
END_IF
tonTimerForLub(IN:=bAxesInMotion, PT:=T#1M);

IF (tpersTemporaryTime >= T#1H) THEN
	udipersTimeForLubCounter:=(udipersTimeForLubCounter + 1);
	tpersTemporaryTime:=T#0MS;
END_IF
udiCurTime:=udipersTimeForLubCounter;(*Set FB Output for Visualization*)					

bLubRequestFromTimer:=(udipersTimeForLubCounter >= udiTimeForLub) AND (udiTimeForLub >  0);						


(*Reset Indicators*)
IF (rtrigStartNomalCycle.Q OR rtrigManStartFillFnc.Q) THEN
	udipersDistToLastLubCycleAxis_1:=udiDistAxis_1;
	udipersDistToLastLubCycleAxis_2:=udiDistAxis_2;
	udipersDistToLastLubCycleAxis_3:=udiDistAxis_3;
	
	udipersNumOfAutoCycle:=0;
	
	tpersTemporaryTime:=T#0MS;
	udipersTimeForLubCounter:=0;
END_IF

(*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*)




(*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX				LUBRICATION FUNCTION 			  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*)

CASE iState OF

0:(*Init*)
	IF (rtrigStartNomalCycle.Q AND bPumpReady) THEN
		uiLubCycleCnt:=0;
		bLubPorcStarted:=TRUE;
		iState:=10;

	ELSIF (rtrigManStartFillFnc.Q AND bPumpReady) THEN
		bLubPorcStarted:=TRUE;
		iState:=510;
	END_IF


10:(*First pulse of 8s*)
	tonLubCycle(IN:=TRUE, PT:=t#8s);
	bHW_Output:=TRUE;

	IF tonLubCycle.Q THEN
		udiInput_rtrig_Cnt_AtStart:=udiInput_rtrig_Cnt;
		bHW_Output:=FALSE;
		tonLubCycle(IN:=FALSE);
		iState:=20;
	END_IF


20:(*Wait for next Pulse max 53s*)
	tonLubCycle(IN:=TRUE, PT:=T#53S);

	(*Check for Lubrication Output On PK1 and PK2*)
	IF (udiInput_rtrig_Cnt = (udiInput_rtrig_Cnt_AtStart + 2)) AND tonFeedbackOn.Q THEN
		lrpersInternalVolume:=lrpersInternalVolume - (2*clrOuputVolPerOutput);
		uiLubCycleCnt:=(uiLubCycleCnt + 1);
		tonLubCycle(IN:=FALSE);
		iState:=30;

	ELSIF (tonLubCycle.Q OR bWarnMsgPump) THEN
		tonLubCycle(IN:=FALSE);
		bLubPorcStarted:=FALSE;
		IF (NOT bWarnMsgEmpty AND NOT bWarnMsgPump) THEN
			bWarnMsgCmd:=TRUE;
		END_IF
		iState:=0;
	END_IF


30:(*Check next Order*)
	IF (udiNumOfCycle >= 1) THEN
		IF (uiLubCycleCnt < (udiNumOfCycle*2)) THEN
			iState:=10;
		ELSE
			bLubPorcStarted:=FALSE;
			bWarnMsgCmd:=FALSE;
			bErrMsgStopReq:=FALSE;
			iState:=0;
		END_IF
	ELSE
		IF (uiLubCycleCnt < 2) THEN
			iState:=10;
		ELSE
			bLubPorcStarted:=FALSE;
			bWarnMsgCmd:=FALSE;
			bErrMsgStopReq:=FALSE;
			iState:=0;
		END_IF
	END_IF


510:(*Fill Function 12s*)
	tonLubCycle(IN:=TRUE, PT:=t#12s);
	bHW_Output:=TRUE;

	IF tonLubCycle.Q THEN
		udiInput_rtrig_Cnt_AtStart:=udiInput_rtrig_Cnt;
		bHW_Output:=FALSE;
		tonLubCycle(IN:=FALSE);
		iState:=520;
	END_IF


520:(*Wait to by ready for the next Function, max 1660s *)
	tonLubCycle(IN:=TRUE, PT:=t#1660s);

	(*Check for Lubrication Output On PK1 and PK2*)
	IF (udiInput_rtrig_Cnt = (udiInput_rtrig_Cnt_AtStart + 40)) AND tonFeedbackOn.Q THEN
		lrpersInternalVolume:=lrpersInternalVolume - (40*clrOuputVolPerOutput);
		bLubPorcStarted:=FALSE;
		bWarnMsgCmd:=FALSE;
		bErrMsgStopReq:=FALSE;
		tonLubCycle(IN:=FALSE);
		iState:=0;
		
	ELSIF (udiInput_rtrig_Cnt = (udiInput_rtrig_Cnt_AtStart + 80)) AND tonFeedbackOn.Q THEN
		lrpersInternalVolume:=lrpersInternalVolume - (80*clrOuputVolPerOutput);
		bLubPorcStarted:=FALSE;
		bWarnMsgCmd:=FALSE;
		bErrMsgStopReq:=FALSE;
		tonLubCycle(IN:=FALSE);
		iState:=0;

	ELSIF (tonLubCycle.Q OR bWarnMsgPump) THEN
		tonLubCycle(IN:=FALSE);
		bLubPorcStarted:=FALSE;
		IF (NOT bWarnMsgEmpty AND NOT bWarnMsgPump) THEN
			bWarnMsgCmd:=TRUE;
		END_IF
		iState:=0;
	END_IF

END_CASE

(*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*)




(*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX				REACTION / WARNING / ERROR 			     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*)

IF ((rtrigStartNomalCycle.Q OR rtrigManStartFillFnc.Q) AND NOT bPumpReady) THEN
	bWarnMsgCmd:=TRUE;
END_IF


(*Output -> Empty*)
bWarnMsgEmpty:=bEmtpyFreqActive;


(*Output -> Problem on the Pump*)
bWarnMsgPump:=tonErrPump.Q;


(*Stop Request*)
IF ((bLubRequestFromAxes OR  bLubRequestFromAutoCycle OR bLubRequestFromTimer) AND NOT bPumpReady) THEN 
	bErrMsgStopReq:=TRUE; (*Set to false in the sequence after a successfull cycle*)
END_IF


(*Output -> Warning Volume*)
bWarnMsgVolume:=(((lrpersInternalVolume/clrMaxPreSetVolume)*100.0) < UDINT_TO_LREAL(udiLevWarnVolume)) AND (udiLevWarnVolume > 0);


(*Output -> Current Volume*)
IF (lrpersInternalVolume < 0) THEN
	lrpersInternalVolume:=0;
END_IF
udiCurVolume:=LREAL_TO_UDINT((lrpersInternalVolume/clrMaxPreSetVolume)*100.0);


(*Set Presistent Data*)
bpersWarnMsgCmd:=bWarnMsgCmd;
bpersErrMsgStopReq:=bErrMsgStopReq;


(*Set Internal Flags*)
udiInput_rtrig_CntOld:=udiInput_rtrig_Cnt;
bInit:=TRUE;

(*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*)