############# # PLC multi-position motion sequence # # Motion parameters # var VEL := 2; var ACC := 3; var DEC := 3; var DWELL_TIME := 12; var N_POS := 10; # number of scan positions, count with the 0 position :) var MAX_CYCLES := 5; VAR myAxis : ax${AX_ID=1}; step : static.seqStep; cycles : static.cycleCounter; doMotion : static.doMotion; doMotionOld : static.doMotionOld; posIndex : static.posIndex; direction : static.direction; dwellCnt : static.dwellCnt; END_VAR var errorCode := 0; var pos := 0; # -------------------------------------------------- # Initialization # -------------------------------------------------- if(${SELF}.firstscan){ step := 0; cycles := 0; posIndex := 0; direction := 1; dwellCnt := 0; }; # -------------------------------------------------- # Trigger start # -------------------------------------------------- if(doMotion and not(doMotionOld)){ println('PLC position sequence started'); step := 1; posIndex := 0; direction := 1; }; # -------------------------------------------------- # Position lookup # -------------------------------------------------- pos := 0; if(posIndex==0){pos:=1.0000;} if(posIndex==1){pos:=10.8575;} if(posIndex==2){pos:=20.1050;} if(posIndex==3){pos:=31.8050;} if(posIndex==4){pos:=38.3275;} if(posIndex==5){pos:=51.9900;} if(posIndex==6){pos:=57.6875;} if(posIndex==7){pos:=71.4650;} if(posIndex==8){pos:=81.2450;} if(posIndex==9){pos:=90.0000;} # -------------------------------------------------- # STEP 1 – move to next position # -------------------------------------------------- if(step==1 and doMotion){ errorCode := mc_move_abs(${AX_ID=1},1,pos,VEL,ACC,DEC); if(errorCode){ println('mc_move_abs error:',errorCode); ${SELF}.error := errorCode; }; if(not(mc_get_busy(${AX_ID=1})) and myAxis.mon.attarget){ mc_move_abs(${AX_ID=1},0,pos,VEL,ACC,DEC); dwellCnt := 0; step := 2; println('Move done, index:',posIndex); }; }; # -------------------------------------------------- # STEP 2 – dwell # -------------------------------------------------- if(step==2 and doMotion){ dwellCnt := dwellCnt + 1; if(dwellCnt >= DWELL_TIME){ posIndex := posIndex + direction; # reached end of list if(posIndex >= N_POS){ direction := -1; posIndex := N_POS - 2; }; # reached start of list if(posIndex < 0){ direction := 1; posIndex := 1; cycles := cycles + 1; println('Cycle completed:',cycles); if(cycles >= MAX_CYCLES){ println('Sequence completed'); doMotion := 0; step := 0; }; }; if(step<>0){ step := 1; }; }; }; doMotionOld := doMotion; # -------------------------------------------------- # Safety # -------------------------------------------------- if(not(myAxis.drv.enabled)){ return []; }; if(((not(ax1.mon.lowlim) or not(ax1.mon.highlim))) or errorCode){ mc_power(${AX_ID=1},0); println('Sequence ended by limit switch violation or errorcode:', errorCode); step := 0; ${SELF}.enable := 0; };