How to Achieve Multi-axis Linear Interpolation & Electronic Cam By PLC For EtherCAT Motion Controller?
Tags: programming, tutorial, plc
Published: 2022-11-16 11:51:02

## 01 Preparations
Firstly, download the latest programming software "ZDevelop".

Then, prepare one XPLC series EtherCAT bus motion controller, and do wiring between EtherCAT motion controller and ZDevelop.

[ Note: if there is no controller, ZBasic development also can be done through simulation -- download the program into simulator. The simulator has been built in ZDevelop]

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jaolydhz30lwwa9mbbq4.png)




## 02 Download PLC into Controller
(1) New build project item, and download program files into controller for motion. Please refer to below process showing.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qgs409y5jemetak1wcn.png)

(2) Open ".zpj" file -- connect to controller -- download the program -- program can run.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3x4z9q3mnc1w9dlx372p.png)

It is recommended to set only one main file for PLC automatic operation, so that the PLC has only one main loop, and other modules are called in the main loop.

PLC commands are not case-sensitive. There is one uniform API function, for PLC commands' details, Please refer to the "ZMotion PLC Programming Manual" 




## 03 PLC Ladder of Diagram Program
The ladder diagram programming method is to draw the sequential control ladder diagram on the programming interface by using sequence symbols and soft element numbers. Since the sequential control loop is represented by contact symbols and coil symbols, which means the display is more intuitive and the program content is easier to understand.

What's more, it is more convenient to monitor and debug the program in the ladder diagram display state. The ladder diagram display example is shown below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cnsoptaarlp5yql0mbpk.png)

Please attention, END program end command must be included in program ending. Otherwise, it will report errors, and it can't be downloaded into controller for execution.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ho4bo3zlyknvioi04vu.png)

(1) For PLC instructions, there are several types according to instruction usages.

A. commonly used instructions: fetching contacts, output coils, timers, counters, etc.

B. contact comparison instructions: compare the values of two registers, and if the condition is met, the contact will be conducted.

C. transfer and comparison instructions: compare and transfer data between registers according to the rules.

D. loop and jump instructions: including conditional loop instruction and jump to subroutine execution instructions.

E. operation instructions: including four arithmetic operations and logical operation instructions.

F. shift instruction: move the data of the source operand bit by bit.

G. data processing instructions: perform other operations, such as encoding, decoding, etc.

H. floating-point arithmetic instructions: operations on 32-bit floating-point numbers.

I. other instructions: parameters related to axis motion.


(2) There are 16-bit PLC instructions and 32-bit PLC instructions according to operands bit.

16-bit data and 32-bit data are processed using different instructions. Except for the data length, the two are the same in other respects, and the processing data types both are with signed numbers.

→ 16-bit instruction -- the value range transmitted: -32768 -+32767.

→ 32-bit instruction -- the value range transmitted: -2147483648 - +2147483647.

(32-bit instructions generally occupy two consecutive 16-bit spaces)


(3) There are consecutive execution type and pulse execution type according to execution methods.

→ consecutive execution: each scan period executes once when conditions are met.

→ pulse execution: only execute once when conditions are met.

(Note: consecutive execution instruction + symbol P = pulse execution instruction)


(4) PLC soft-element form

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fyo8dge4w80sywcxl1go.png)

The data type of counter C and timer T is related to the instruction used when accessing. When accessing through 16-bit instruction, the lower 16 bits are automatically used, and when accessing through 32-bit instruction, 32 bits are used.


(5) Corresponding relation between PLC and related register of Basic.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s8nx6ko4nuyl1zsaoaxo.png)





## 04 PLC Calls Basic Instructions
PLC can call Basic standard instructions through EXE instructions or EXEP instructions.

EXEP instructions are pulse format of EXE instructions, which can be used to call Basic standard instruction only when drive output is from OFF to ON.

Grammar Format:  "EXE   @BASIC instruction" is equal to "BASIC instruction".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32xsvw1jnh8obmes3tpn.png)

Note: When using the EXE instruction to call the register, refer to the syntax of Basic after @, and "EXE @DT0=10" cannot appear, the correct writing should be "EXE @TABLE(0)=10".

Call the Basic linear interpolation syntax in the PLC as shown in the above figure, the linear interpolation PLC syntax "MOVE D0 D2", the operand should be in the format supported by the PLC operand, and the interpolation data is transmitted by the register, which means it cannot be given directly.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ms2ihjf1lqzxeaxado4w.png)





## 05 PLC Linear Interpolation Routine
Control pulse axis 0 and axis 1 do linear interpolation motion, then axis parameters and motion instructions call Basic instruction through EXE, and download edited program into XPLC006E for debugging and running.

(1) PLC control program.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zm3yhd386cx0j054as1.png)


(2) Program description

When the program is powered on and initialized, various parameters of the axis are initialized.

And when the rising edge of X0 is triggered, assign values to the registers D0 and D2 that store the moving distance of the two axes. When triggered by the rising edge of X1, the oscilloscope sampling is started, the MOVE linear interpolation movement is enabled and M0 is self-locking. The moving distance of axis 0 is 300, and motion distance of axis 1 is 400.

M8100 is the IDLE mark of axis 0. When the motion is completed, axis 0 stops, M8100 turns to ON, M1 is set for one cycle, the normally closed contact of M1 is disconnected for one cycle, and M0 self-locking is canceled.

Press X1 again, MOVE executes the linear interpolation motion of axis 0 and axis 1.

X2 is the emergency stop button, if the axis presses X2 during motion, it will stop quickly according to the value set by FASTDEC fast deceleration.


(3) Positions and speed curv of axis 0 and axis 1 captured by oscilloscope.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7u0dqdhrh7257vp1erb.png)


(4) Achieved program of above PLC program in Basic.

```
FOR i=0 TO 10         'MODBUS_BIT register is cleared

   MODBUS_BIT(i)=0

NEXT

BASE(0,1)                 'axis 0 and axis 1 parameters are initialized

UNITS = 100,100

ATYPE =1,1

SPEED = 200,200

ACCEL = 1000,1000

DECEL = 1000,1000

SRAMP = 200,200

DPOS = 0,0

MPOS = 0,0

FASTDEC = 20000,20000

WHILE 1                                 'loop detection input

   IF IN_SCAN(0,2) THEN         'scan electric level changes of IN0-2

   IF IN_EVENT(1)> 0 THEN     'open

       TRIGGER

       MOVE(300,400)

   ELSEIF IN_EVENT(2)> 0 THEN      'stop

       RAPIDSTOP(2)

   ENDIF

 ENDIF

WEND

END
```



## 06 PLC Shear-Cutting (Electronic Cam) Routine.
The PLC completes the shearing process by calling the MOVESLINK automatic cam command of Basic. The slave axis of the MOVESLINK automatic cam automatically plans the speed to follow the main axis movement. For the instruction usage, please refer to ZBasic Program Manual. And the main components of the PLC program are as follows:

(1) initialization part:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3dzhyqbilk8mlskudqnq.png)


(2) select shearing axis No.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pl0wt9ru40to6nce38u7.png)


(3) axis parameters initialization:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i47d0l50tts5lqoax9g1.png)


(4) shearing parameters initialization:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5l78tezw0jkz7ipwye6.png)


(5) shearing motion

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mn3jlxv510gloig21ob8.png)

HMI interface: it can set shear processing parameters and control shearing motion. Also, it can show current axis position information at the same time.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e5orq8ycwj8ehu2e5n9r.png)

The chasing shear waveform is as follows. The main axis is a conveyor belt moving at a constant speed. In the first stage, the slave axis (axis 1) follows the main axis (axis 2) from the initial position to accelerate. Then, the second stage, speeds of master axis and slave axis are the same, which means they achieve synchronization. Then, it is ready to return to the initial position after cutting downwards. In the third stage, the slave axis follows the main axis and decelerates to 0. In the fourth stage, the slave axis returns to the initial position in reverse to prepare for the next round of chasing shear.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjf2jor494z5w4r6vdse.png)


That's all, thank you for your reading -- Economical EtherCAT Motion Controller (3) --  [Multi-axis Linear Interpolation & Electronic Cam Achievement Through PLC.](http://www.zmotionglobal.com/support_info_46.html)


This article is edited by ZMOTION, here, share with you, let's learn together. ZMOTION: DO THE BEST TO USE MOTION CONTROL.
Note: Copyright belongs to ZMotion Technology, if there is reproduction, please indicate article source. Thank you.