<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.0.36">
  <POU Name="PRG_PTPFunctions" Id="{09fb3be5-37e5-4ec5-9693-4d1d35653da1}" SpecialFunc="None">
    <Declaration><![CDATA[PROGRAM PRG_PTPFunctions
VAR_IN_OUT
	stMachineMode : ST_MachineMode;
END_VAR

VAR
i: 						INT;
	bStartHoming: 			BOOL;
	fbReleaseAllGroups: 	FB_ReleaseAllGroups;
	fbAxisLogErrorEvent: ARRAY [1..nMaxNumberOfAxis] OF FB_AxisLogErrorEvent;
	fbRTrigManual: 		R_TRIG;
	fbRTrigReference: 	R_TRIG;

	nSetSpeed : INT;
	fbTrigReference: R_TRIG;
	fbRTrigStandby: R_TRIG;
	fbRTrigMDI: R_TRIG;
	fbRTrigAutomatic: R_TRIG;
	fbRTrigErrorRelease: R_TRIG;
	fbRTrigErrorHoming: R_TRIG;
	Blinker1: FB_Blinker;
	cam: BOOL;
	bPushPlcKeyLeft: BOOL;
	fbTON: TON;
END_VAR
]]></Declaration>
    <Implementation>
      <ST><![CDATA[(* ==========================================================*)
(*   PRG: PtpFunctions				        		         *)
(* ----------------------------------------------------------*)
(*   Desc: 													 *)
(*   Auth: MiB / TV                                          *)
(*   Date: 02.09.2005                                        *)
(*   Last: 02.04.2013                                        *)
(*   Rev.: 1.1											     *)
(*			  1.1 JogMode, Tipmode added  10.3.2006          *)
(* ==========================================================*)

(* OpModes:
	Standby			: BOOL;
	Manual			: BOOL;
	MDI				: BOOL;
	Automatic		: BOOL;
	Reference		: BOOL; *)

fbRTrigStandby( Clk := stMachineMode.Standby);
fbRTrigManual( Clk := stMachineMode.Manual );
fbRTrigMDI( Clk := stMachineMode.MDI );
fbRTrigAutomatic( Clk := stMachineMode.Automatic );
fbRTrigReference( Clk := stMachineMode.Homing );

IF (fbRTrigStandby.Q) THEN
	stMachineMode.Manual := FALSE;
	stMachineMode.MDI := FALSE;
	stMachineMode.Automatic := FALSE;
	stMachineMode.Homing := FALSE;
	PLCNciChannelState.Mode[1] :=1;
END_IF

IF (fbRTrigManual.Q) THEN
	stMachineMode.Standby := FALSE;
	stMachineMode.MDI := FALSE;
	stMachineMode.Automatic := FALSE;
	stMachineMode.Homing := FALSE;
	PLCNciChannelState.Mode[1] :=4;
END_IF

IF (fbRTrigMDI.Q) THEN
	stMachineMode.Standby := FALSE;
	stMachineMode.Manual := FALSE;
	stMachineMode.Automatic := FALSE;
	stMachineMode.Homing := FALSE;
	PLCNciChannelState.Mode[1] :=3;
END_IF

IF (fbRTrigAutomatic.Q) THEN
	stMachineMode.Standby := FALSE;
	stMachineMode.Manual := FALSE;
	stMachineMode.MDI := FALSE;
	stMachineMode.Homing := FALSE;
	stMachineMode.Automatic := TRUE;
	PLCNciChannelState.Mode[2] := 2;	
	PLCNciChannelState.Mode[1] :=2;
END_IF

IF (fbRTrigReference.Q) THEN
	stMachineMode.Standby := FALSE;
	stMachineMode.Manual := FALSE;
	stMachineMode.MDI := FALSE;
	stMachineMode.Automatic := FALSE;
	PLCNciChannelState.Mode[1] :=5;
END_IF


// Enable all configured axies 
FOR i:=1 TO nMaxNumberOfAxis DO
	PRG_AxisEnable(nAxisNr:=i , bEnable:= PLCAxisEnable );
END_FOR

IF (NOT PLCAxisEnable) THEN
	stMachineMode.Start:=FALSE;
END_IF


IF PLCDeleteFailure THEN
	PRG_ErrorResetAllAxes(bExecute:= TRUE  , BDone=> );
	IF PRG_ErrorResetAllAxes.bDone THEN
		PRG_ErrorResetAllAxes(bExecute:= FALSE);
		PLCDeleteFailure := FALSE;
		FOR i := FIRST_SYSTEM_MESSAGE TO LAST_SYSTEM_MESSAGE DO
			SysMSGList[i] := FALSE;
		END_FOR
	END_IF
END_IF


(* Reset Channel 1 PTP -> reset all axis *)

IF stMachineMode.Reset THEN
(* 	PLCMachineMode[1].Reference := FALSE;*)
   stMachineMode.Start := FALSE;
   stMachineMode.Stop := FALSE;
  (*  PLCMachineMode[1].Manual := FALSE; *)

(*	bStartHoming := FALSE; *)
	PRG_ResetAllAxesWithError(execute:= TRUE  , Done=> );
	IF PRG_ResetAllAxesWithError.Done THEN
		stMachineMode.Reset := FALSE;
		PLCDeleteFailure := TRUE;
		PRG_ResetAllAxesWithError(execute:= FALSE);
	END_IF
END_IF

IF stMachineMode.Stop THEN
	PRG_StopAllAxes(bExecute := TRUE);
	IF PRG_StopAllAxes.bDone THEN
		stMachineMode.Stop := FALSE;
		PRG_StopAllAxes(bExecute := FALSE);
	END_IF
END_IF



fbTrigReference( CLK := stMachineMode.Homing);
(* Release all axes before homing *)
IF fbTrigReference.Q THEN
	fbReleaseAllGroups.bExec:=TRUE;
	IF (NOT fbReleaseAllGroups.bBusy) THEN
		fbReleaseAllGroups.bExec := FALSE;
	END_IF
END_IF
fbReleaseAllGroups();


fbRTrigErrorRelease( CLK := fbReleaseAllGroups.bErr);
IF fbRTrigErrorRelease.Q THEN
	SysMSGListData[2].nErrorCode := fbReleaseAllGroups.nErrId;
	SysMSGListData[2].sModul := 'FB_ReleaseAllGroups';
	SysMSGListData[2].sAddInfo := 'zusätzlich Info: in Baustein FB_NCIChannel';
	SysMSGList[2] := TRUE;
END_IF



// Homing all Axes
IF stMachineMode.Homing THEN
	PRG_HomingAllAxes(bExec:= stMachineMode.Start);
	stMachineMode.Start := FALSE;
ELSE
	SysMSGList[3] := FALSE;
END_IF

fbRTrigErrorHoming( CLK := PRG_HomingAllAxes.bErr );
IF fbRTrigErrorHoming.Q THEN
	SysMSGListData[3].nErrorCode := PRG_HomingAllAxes.nErrId;
	SysMSGListData[3].sModul := 'PRG_HomingAllAxes';
	SysMSGList[3] := TRUE;
END_IF

Blinker1(Taktzeit:= T#4s , Ausgang=>cam ); (* simulate homing *)

FOR i := 1 TO nMaxNumberOfAxis DO
	arrAxis_Ref[i].PlcToNc.ControlDWord.5 := arrAxisCalibrationCam[i] OR cam;	(* transfer cam to axis interface *)
END_FOR

(* Errorhandler for all axis *)

FOR i:=1 TO nMaxNumberOfAxis DO
	fbAxisLogErrorEvent[i](
	stAxisRef:= arrAxis_REF[i] ,
	bInit:= TRUE,
	bBusy=> ,
	bErr=> ,
	nErrId=> );
END_FOR

PRG_ManualMode(stManualMode:=PLCMachineMode[1].ManualMode);


]]></ST>
    </Implementation>
  </POU>
</TcPlcObject>