;********************************  PositionServo User Indexing Program  ********************************
;********************************************** Header **************************************************
;Title			:	Sample Homing Program From PositionServo Training Exercises
;Author		:	AC Technology International Ltd
;Description		:	Program Performs Simple Homing Routine followed by Preset Sequence of Moves
;			:	Input A4 used to Re-Initalise Homing Routine during Main Program Execution
;			:	Homing Sensor on Input B1
;			:	Homing Complete Output on Out 1
;Version Number	:	V1.0.1
;Date 			:	22/11/06
;
;************************************************  I/O List  ************************************************
;	Input A1	-	Homing Sensor
;	Input A2	-	not used
;	Input A3	-	Enable Input / Safety stop
;	Input A4	-	Request Homing Input
;	Input B1	-	not used
;	Input B2	-	not used
;	Input B3	-	not used
;	Input B4	-	not used
;	Input C1	-	not used
;	Input C2	-	not used
;	Input C3	-	not used
;	Input C4	-	not used
;
;	Output 1	-	Homing Complete
;	Output 2	-	not used
;	Output 3	-	not used
;	Output 4	-	not used
;
;	Analog In 1	-	not used
;	Analog In 2	-	not used
;	Analog Out	-	not used
;
;	Encoder Out	-	not used
;


;******************************** Initialize and Set Variables ****************************************
; Define Constants and Variables. Assign I/O and Initialize Variable Values

 UNITS = 1				; Units in RPS
Accel = 50				; Accelerate 50 RPS/S
Decel = 50				; Decelerate 50 RPS/s
Define Homing_Sensor In_A1	; Define Variable for Homing Sensor Input
Define System_Enable In_A3	; define Variable for Enable Input
Define Homing_Request In_A4	; Define Variable for Homing Request Input
Define Home_Completed Out1	; Define Variable for Homing complete Indicator

VAR_REFERENCE = 1			;set Reference to Internal
VAR_DRIVEMODE = 2	 		;Set Operating mode to Position mode
VAR_ENABLE_SWITCH_TYPE = 0  	;enable switch function set to "Inhibit"


;************************************************ Events ************************************************
Event homing_call input Homing_Request rise	; Events looks for Homing Request Input and Jumps to Homing Code
	jump homing 
Endevent


;******************************************* Main Program ********************************************

PROGRAM_START:
Wait While System_Enable == 0		; Wait for drive safety enable to be active - Safe Condition
enable					; enable drive

Homing:					; Homing Routine, Performed at Start Up and after Homing Routine Request inout goes high
Home_Completed = 0			; Turn off homing completed indicator
event homing_call off			; Switch off event to check for homing request input
maxv = 10					; set slow velocity for homing
move back until Homing_Sensor		; move back at slow velocity until homing sensor is activate
maxv = 2					; set very slow velocity
move until homing_sensor == 0		; move forward off homing sensor
APOS = 0					; Set actual position register to 0 
maxv = 50					; Set velocity back to normal speed
wait while Homing_Request == 1		; wait for the homing request signal to be removed
event homing_call on			; Switch on event to check for homing request input
Home_Completed = 1			; Turn on homing completed indicator



Move_Profile:				; Basic position loop
Moved 5					; move distance 5
wait time 200					; wait 200 ms
moved 10					; move distance 10
wait time 500					; wait 500 ms
moved -15					; move back distance 15
wait time 1500				; wait 1500 ms
goto move_profile				; restart positioning loop

end

fault_section:
	wait while System_Enable == 1		; wait until enable input is removed
	goto program_start			; restart program


;******************************************* Sub-Routines ********************************************
;	Enter Sub-Routine code here



;************************************** Fault Handler Routine ****************************************
;	Enter Fault Handler code here
ON FAULT
	resume fault_section
ENDFAULT

