** ECAM APPLICATION WITH MODULO

** IA[1]: Mark that a Box is captured.
** IA[2]: The value of the captured master location after the homing event

#@AUTOEXEC

##INIT
MO=0;OP=0;IA[1]=0;IA[2]=0;** init general parameters
RM=1;FR=0;EM[1]=0;AG[1]=0;AG[2]=0;**init reference parameters
UM=5;PX=0;PY=0;AC=100000;DC=100000;SP=10000;**init profile parameter


YM=8000000;** SET THE MASTER MODULO TO PREVENT LARGE NUMBERS
XM=32000;**SET ROLLER MODULO, ROLLER HALF CYCLE IS 16000 counts

** Initiate capture and homing sequnces
HY[3]=1;** MASTER CAPTURE EVENT ON AUX FAST INPUT
HY[4]=2;** DO NOTHING AFTER THE EVENT
HY[5]=2;** SET NOTHING TO PY AFTER THE EVENT
MO=1;
**Home the slave axis (in this example home immediate to 0)
HM[2]=0;** Set 0 to the slave homing location
HM[3]=0;**ACTIVATE IMMEDIATE HOMING
HM[1]=1;**LOAD DRIVER
AF,HM[1]=0;**AFTER HOMING WAS DONE
PA=0;BG; ** ADJUST THE ABSOLUTE SOFTWARE COMMAND

** INIT ECAM TABLE
** THE ROLLER TRAJECTORY IS FROM 0 to 14000.
** THE MOTION ACCELERATES TO THE CONVEYER SPEED.

ET[1]=0
ET[2]=1000
ET[3]=2000
ET[4]=6000
ET[5]=10000
ET[6]=14000

EM[5]=1;EM[2]=6; **Limits for ECAM table
EM[4]=5000; **MASTER INTERVALS BETWEEN SLAVE ENTRIES
EM[3]=0;**ECAM ENTRY OFFSET IS CHANGED ACCORDING TO THE CAPTURED POSITION
HY[1]=1;** INITIATING AUXILIARY CAPTURE SEQUENCE

##MAIN
AF,IA[1]=1; ** WAIT UNTIL NEW BOX IS CAPTURED
EM[3]=IA[2];** ADJUST ECAM TABLE OFFSET ACCORDING TO THE MASTER CAPTURED VALUE
IA[1]=0;** BOX IS TREATED - ALLOW THE NEXT BOX CAPTURE.(DONE BY#@AUTO_HY)

##START_ECAM
EM[1]=1;FR=1;** START FOLLOWING
AF,EM[8]>EM[5];** WAIT UNTIL THE FIRST ECAM IS PERFORMED
HY[1]=1; ** ALLOW SWITCH BOUNCE TIME, INITIATE NEW CAPTURE PROCESS
AF,EM[8]=EM[2]; ** WAIT UNTIL THE LAST ECAM IS PERFORMED

##ECAM_ENDS
FR=0;EM[1]=0;** DISENGAGE FOLLOWER AND ECAM
PR=XM/2;BG; ** ADJUST THE SOFTWARE COMMAND TO THE NEXT MATRIX ENTRY (MODULO MOVEMENT)
AF,MS<2;** MAKE SURE THAT THE SOFTWARE POSITION ARRIVED
JP##MAIN;** GO BACK TO MAIN LOOP


** INCASE OF A FAULT, SET OUTPUT 2.
#@AUTO_ER
OP=2
JZ##END

** WHEN A BOX IS ARRIVED
#@AUTO_HY
IA[1]=1; ** MARK THE ARRIVAL
IA[2]=HY[8]; ** SAVE THE CAPTURED SLAVE POSITION FOR THE ECAM OFFSET.
RT

##END
EN