(*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*)
(*
 Description:			Function for TriboServ Flex Pump Lubrication System Type 404DLS

 Version:       		2.01    (16.03.2018)

 Autor:         		Martin Thoeni 15.02.2018

 Used libraries:		Standard.lib

 Version Changes:       
 Version:	Date:       Description:									Autor:
 2.00		15.02.2018	Tested and released Basic Version				Martin Thoeni		 
 2.01		16.03.2018	Bug-fix in volume calculation in case of 80
						cycles during fill function
						Bug-fix for empty cartridge detection

Copyright 2018 by Guedel AG
*)
(*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*)
FUNCTION_BLOCK GFB_TriboServFlexxPump404DLS
VAR_INPUT
	bHW_Input						:BOOL;  (*Hardware Input Signal of the Lubricaton Pump*)
	udiDistAxis_1					:UDINT; (*Accumulated Axis Distance Axis 1 [km]*)
	udiDistAxis_2					:UDINT; (*Accumulated Axis Distance Axis 2 [km]*)
	udiDistAxis_3					:UDINT; (*Accumulated Axis Distance Axis 3 [km]*)
	udiAxisDistForLub				:UDINT; (*Start Lubrication cycle if one of the Axis drove this distance*) 
	bCntAutoCycle					:BOOL;	(*Count up cycle counter with a reasing Trigger*)
	udiAutoCycForLub				:UDINT; (*Start Lubrication cycle if the cycle counter reacht this value*) 
	bAxesInMotion					:BOOL;  (*Count up the Timer when this value is true*)
	udiTimeForLub					:UDINT; (*Start Lubrication cycle if the timer reach this value [h]*) 
	bManStartForLub					:BOOL;  (*A rising trigger will start the lubrication cycle*)
	udiNumOfCycle					:UDINT; (*Number of lubrication cycles*)
	bManStartFillFnc				:BOOL;  (*Start Fill Function*)
	udiPreSetVolume					:UDINT; (*Preset Volume in [%] *)
	bPreSetVolume					:BOOL;  (*Preset Volume after Fill up the Lubricant. Internal Triggered*)
	udiLevWarnVolume				:UDINT; (*Volume Level for activate Warning [%]*)
END_VAR
VAR_OUTPUT
	bHW_Output						:BOOL;  (*Hardware Output Signal to the Lubrication Pump*)
	udiCurDistAxis_1				:UDINT; (*Current Axis Distance to last lubrication cycle Axis 1 [km]*)
	udiCurDistAxis_2				:UDINT; (*Current Axis Distance to last lubrication cycle Axis 2 [km]*)
	udiCurDistAxis_3				:UDINT; (*Current Axis Distance to last lubrication cycle Axis 3 [km]*)
	udiCurAutoCyc					:UDINT; (*Current Auto Cycle*) 	
	udiCurTime						:UDINT; (*Current Run Time of the Axis*)
	bLubPorcStarted					:BOOL;  (*State lubrication process is in progress. No other commands can be started*)
	bWarnMsgCmd						:BOOL;  (*Not ready for Stard Lubrication cycle or cycle was not successful*)
	bWarnMsgEmpty					:BOOL;  (*Warning message when the lubricant is empty*)
	bWarnMsgPump					:BOOL;  (*Warning message when the pump state is not ok*)
	bErrMsgStopReq					:BOOL;  (*Error message for a stop request for the system. No more motion of the axles is allowed *)
	udiCurVolume					:UDINT; (*Current cartridge volume in [%] (Optional) *)
	bWarnMsgVolume					:BOOL;  (*Level of the Lubricant is low*)
END_VAR
VAR
	bInit							:BOOL;
	rtrigHW_Input					:R_TRIG;
	rtrigManStartForLub				:R_TRIG;
	rtrigManStartFillFnc			:R_TRIG;
	rtrigCntAutoCycle				:R_TRIG;	
	rtrigStartNomalCycle			:R_TRIG;
	rtrigPreSetVolume				:R_TRIG;

	tTonET_EqualInput_rtrig_Cnt		:TIME;
	tonEqualInput_rtrig_Cnt			:TON;
	udiInput_rtrig_Cnt				:UDINT;
	udiInput_rtrig_CntOld			:UDINT;
		
	bEmtpyFreqActive				:BOOL;	
	tonFeedbackOn					:TON;
	tonErrPump						:TON;

	bLubRequestFromAxes				:BOOL;
	bLubRequestFromAutoCycle		:BOOL;
	tonTimerForLub					:TON;
	bLubRequestFromTimer			:BOOL;
	
	
	iState							:INT;
	uiLubCycleCnt					:UINT;
	udiInput_rtrig_Cnt_AtStart		:UDINT;
	tonLubCycle						:TON;

	bPumpReady						:BOOL;
END_VAR
VAR CONSTANT
	clrMaxPreSetVolume				:LREAL:=400; (* Preset Volume in [cm^3] *)
	clrOuputVolPerOutput			:LREAL:=0.15;(* Output Volume per Output[cm^3] *)
END_VAR
VAR PERSISTENT
	udipersDistToLastLubCycleAxis_1	:UDINT;
	udipersDistToLastLubCycleAxis_2	:UDINT;
	udipersDistToLastLubCycleAxis_3	:UDINT;
	
	udipersNumOfAutoCycle			:UDINT;

	tpersTemporaryTime				:TIME;
	udipersTimeForLubCounter		:UDINT;
	
	bpersWarnMsgCmd					:BOOL; 
	bpersErrMsgStopReq				:BOOL; 
	
	lrpersInternalVolume			:LREAL;
END_VAR