;********************************  PositionServo User Indexing Program  ********************************
;********************************************** Header **************************************************
;Title			:	Sample Homing Program From Training Exercises
;Author		:	AC Technology International Ltd
;Description		:	Program Performs Simple Homing Routine using the pre-defined homing
;			;	funtionality provided in the PositionServo drive.
;			:	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.2
;Date 			:	24/01/08
;
;************************************************  I/O List  ************************************************
;	Input A1	-	not used
;	Input A2	-	not used
;	Input A3	-	Enable Input / Safety stop
;	Input A4	-	Request Homing Input
;	Input B1	-	Homing Sensor
;	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

;Variables Used for Homing Functionality:
;****************************************************************
;* Set Fast homing speed		:  	Variable #242	*
;* Set Slow homing speed		: 	Variable #243	*
;* Set Homing accel/decel		: 	Variable #239	*
;* Set home offset in User Units	:	variable #240	*
;* Set home offset in encoder pulses	:	Variable #241	*
;* Set Home Switch input		:	Variable #246	*
;* Set Home Method			:	Variable #244	*
;****************************************************************

;Homing specific set up..
VAR_HOME_FAST_VEL = 10	     	; rps
VAR_HOME_SLOW_VEL = 1	      	; rps
VAR_HOME_ACCEL = 100	  	; rps/sec^2
VAR_HOME_OFFSET= 1		; 1 Rev (User Unit) offset from sensor
VAR_HOME_SWITCH_INPUT = 4	; Set input A1  (0-A1, 1-A2…3-A4, 4-B1,…11-C4)
			; NOTE: Switches A1 & A2 are reserved for Hard limit Switches and shouldn't be set as homing switch
VAR_HOME_METHOD = 19		; Set required homing function, see table in Programming manual

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"


UNITS = 1				; Units in RPS
Accel = 1000				; Accelerate 1000 RPS/S
Decel = 1000				; Decelerate 1000 RPS/s
MaxV = 20				; Set Normal Operating Speed 20 RPS

Define System_Enable In_A3	; Define Variable for Enable Input / Safety stop
Define Homing_Request In_A4	; Define Variable for Homing Request Input
Define Homing_Complete Out1	; Define Variable for Homing Completed Output


;************************************************ 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:
Event Homing_Call off		; Turn off Event to check for homing Reuest Input		
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
Event Homing_Call off		; Turn off Event to check for homing Reuest Input		
Homing_Complete = 0		; Turn off Homing Completed Variable
VAR_START_HOMING = 1		; starts homing sequence
Homing_Complete = 1		; Turn on Homing Completed Variable
Event Homing_Call on		; Turn on Event to check for homing Reuest Input		
;Drive homed

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



