// PLC for home search procedure. 
// 11.07.14 change flag status (True/False)
//

open PLC HOME clear

IF (@ON(CS_STATUS3_M,CS_HOME_INIT))

// Motor 2 Homing procedure
	IF (@ON(S_HOME_M,MOTOR_2))
	 
// CHECK FOR LIMIT ON
		IF (HWLIM2_MINUS  = TRUE )       			//CHECK PlusEnd Limit to ON
			CMD "#2J^-2000"                			//move
			while (DES_VEL_ZERO_2_M=FALSE)          //Wait for motion complete
			endwhile                 				//End wait
		ENDIF
		IF (HWLIM2_PLUS  = TRUE )        			//CHECK MinusEnd Limit to ON
			CMD "#2J^+2000"               			//move
			while (DES_VEL_ZERO_2_M=FALSE)          //Wait for motion complete
			endwhile                 				//End wait
		ENDIF
	 
	 
//First step - Homing of Limit
//Homing First Step Setup
		i223=-5                   					//Homing speed
		i225=$2c004              					//Limit function OFF
		i226=0                       				//Home ofset
		i907=2                    					//Homing of FLAGn
		i908=2                    					//FLAGn (2=> +Lim)

//Home motion
		JOG_X_HOME
		while (DES_VEL_ZERO_2_M = TRUE)
		endwhile
		while (HOME_COMPLETE_2_M = FALSE)        	//Wait for Home Search START
		endwhile                 					//End wait
		while (DES_VEL_ZERO_2_M = FALSE)         	//Wait for Hoime motion complete
		endwhile                 					//End wait
	 
//Two step -  Homing of Home Label
//Homing Two step Setup
		i223=2                         				//Homing speed 
		i225=$c004               					//Limit function ON
		i226=0                  					//Home ofset
		i907=3                    					//Homing of CHCn 1
		i908=0                    					//CHCn=Index Label (Home flag)0
	  
//Home motion
		JOG_X_HOME
		while (DES_VEL_ZERO_2_M = TRUE)
		endwhile                 					//Home comand
		while (HOME_COMPLETE_2_M = FALSE)        	//Wait for Home Search START
		endwhile                 					//End wait
		while (DES_VEL_ZERO_2_M = FALSE)         	//Wait for Hoime motion complete
		endwhile                 					//End wait
		@SET_OFF(S_HOME_M,HOME_X_MTR)   			// Disable Flags
	ENDIF
	
// Motor 3 Homing procedure

	IF (@ON(S_HOME_M,MOTOR_3))

// CHECK FOR LIMIT ON
		IF (HWLIM3_MINUS  = TRUE )       			//CHECK PlusEnd Limit to ON
			CMD "#3J^-2000"                			//move
			while (DES_VEL_ZERO_3_M=FALSE)          //Wait for motion complete
			endwhile                 				//End wait
		ENDIF
		IF (HWLIM3_PLUS  = TRUE )        			//CHECK MinusEnd Limit to ON
			CMD "#3J^+2000"               			//move
			while (DES_VEL_ZERO_3_M=FALSE)          //Wait for motion complete
			endwhile                 				//End wait
		ENDIF
		
//First step - Homing of Limit
//Homing First Step Setup
		i323=-5            	       					//Homing speed
		i325=$2c008              					//Limit function OFF
		i326=0                      				//Home ofset
		i912=2                    					//Homing of FLAGn
		i913=2                    					//FLAGn (2=> +Lim)
	 
//Home motion
		JOG_Z_HOME
		while (DES_VEL_ZERO_3_M=TRUE)    			//Wait for Hoime motion complete
		endwhile                 					//Home comand
		while (HOME_COMPLETE_3_M=FALSE)          	//Wait for Home Search START
		endwhile                 					//End wait
		while (DES_VEL_ZERO_3_M=FALSE)           	//Wait for Hoime motion complete
		endwhile                 					//End wait
		
//Two step -  Homing of Home Label
//Homing Two step Setup
		i323=2                    					//Homing speed
		i325=$c008               					//Limit function ON
		i326=0                  					//Home ofset
		i912=3                    					//Homing of CHCn 1
		i913=0                    					//CHCn=Index Label (Home flag)0
		
//Home motion
		JOG_Z_HOME               					//Home comand
		while (DES_VEL_ZERO_3_M=TRUE)    			//Wait for Hoime motion complete
		endwhile                 					//Home comand
		while (HOME_COMPLETE_3_M=FALSE)          	//Wait for Home Search START
		endwhile                 					//End wait
		while (DES_VEL_ZERO_3_M=FALSE)           	//Wait for Hoime motion complete
		endwhile                 					//End wait
		@SET_OFF(S_HOME_M,HOME_Z_MTR)   			//Disable Flags
	ENDIF

	@SET_OFF(CS_STATUS3_M,CS_HOME_INIT)       		//Disable Flags

ENDIF
close