EtherCAT Motion Control Card QT Development Tutorial (1)
Tags: ethercat, qt, programming, tutorial
Published: 2023-11-08 09:41:17

Today, Zmotion talks about how to develop EtherCAT motion control card by QT. And there is one complete tutorial series that composed by three aspects.

Here, [the first part](http://www.zmotionglobal.com/support_info_120.html), we will show you how to configure development environment with one simple motion control application example, specifically, the linear interpolation motion control.

Let's begin.


## 1. Hardware Support

Before our development configuration, we need to know which hardware we could use. Here, ECI2828 motion control card is used. 

--Brief Basic Introduction--

[ECI2828](http://www.zmotionglobal.com/pro_info_236.html) belongs to economical motion control card, and it supports rich motion control functions, such as, up to 16 axes linear interpolation, any circular interpolation, space arc, helical interpolation, electronic cam, synchronous follow, virtual axis, robot instructions, etc. And real-time motion control can be achieved through optimized network communication protocol.

--Hardware Configuration--

ECI2828 motion control card is linked with PC through ethernet and RS232, then card can receive the command from PC to run. When there is no enough motion axes, IO or analog resources, expansion modules can be connected through EtherCAT bus and CAN bus.

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

--Programming Method--

There are two programming methods, host computer programming languages or ZBasic, ZPLC, ZHMI that are developed by Zmotion. Here, upper computer programming platform QT is used for ECI2828, and VC, VB, VS, C++, C#, and others all can be used. It only needs to call dynamic library "zmotion.dl" when programming is running. While debugging, we could connect ECI2828 to [ZDevelop](http://www.zmotionglobal.com/pro_list_75.html) that is one free programming software launched by Zmotion itself for debugging and observing.


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




## 2. How to Develop Motion Control by Qt

#### (1) New Build Qt Project

a. build one new Qt project

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

b. select project path

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

c. select Qt compile set (kits)

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

d. select basic type

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



#### (2) Copy Files Related to Function Library to New Created Project

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



#### (3) Add Static Library (zmotion.lib) of Function Library to New Created Project

a. add function library 1

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

b. add function library 2

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

c. add function library 3

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

d. add head file related to function library to project

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

e. state head file and define connection handle

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




## 3. Software Support

We talked about hardware support above, now let's see what we have and what we need to use in the software level.

For host computer development, Zmotion provides one uniform PC function library, there are PC programming manual and examples of each platform and each programming language. 

Generally, ethernet is used to connect controller to IPC, the corresponding connection interface is ZAux_OpenEth(), if the connection is successful, the interface will return one link handle, then it can control the controller through this handle.

Next, let's see some basic operation PC commands.

(1) how to build the connection

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

(2) how to achieve multi-axis interpolation

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

(3) how to get axis position information

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

(4) how to get axis speed information

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





## 4. How to Develop Multi-Axis Interpolation Motion by Qt

#### (1) Multi-Axis Interpolation Motion in Qt Interface

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


#### (2) Development Steps

a. call controller link interface "ZAux_OpenEth()" in construct function to build the connection with controller, when connected successfully, open timer to monitor controller axis information.

```
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    int rint=0;
    ui->setupUi(this);
    //system ON, connect to controller automatically
    rint = ZAux_OpenEth("192.168.0.223", &g_handle);
    //if link successfully, open timer
    if(rint==0)
    {
       startTimer(50);
    }
}
```

b. update each axis' position and speed information through the timer.

```
//timer event
void MainWindow::timerEvent(QTimerEvent *event)
{
    QString StrText;
    float AxisDpos[3]={0};
    float AxisMspeed[3]={0};
    if(NULL != g_handle)
    {
        for(int i=0;i<3;i++)
        {
            //obtain axis 0 position
            ZAux_Direct_GetDpos(g_handle,i,&AxisDpos[i]);
            //obtain axis 0 speed
            ZAux_Direct_GetMspeed(g_handle,i,&AxisMspeed[i]);
        }
        StrText = QString("#axis 0 position:（%1） #axis 1 position:（%2） #axis 2 position:（%3）").arg(AxisDpos[0]).arg(AxisDpos[1]).arg(AxisDpos[2]);
        ui->AxisDpos->setText(StrText);
        StrText = QString("#axis 0 speed:（%1） # axis 1 speed：（%2） # axis 2 speed：（%3）").arg(AxisMspeed[0]).arg(AxisMspeed[1]).arg(AxisMspeed[2]);
        ui->AxisMspeed->setText(StrText);
    }
}
```

c. call multi-axis interpolation command to do multi-axis interpolation motion through open button.

```
//open button slot function: start linear interpolation motion
void MainWindow::on_RunButton_clicked()
{
    QString Text;
    int AxisList[3] = {0,1,2};
    float DisList[3]={0};
    //set axis parameters
    for(int i=0; i<3; i++)
    {
        //set axis type as pulse axis
        ZAux_Direct_SetAtype(g_handle,i,1);
        //set pulse amount, generally it is the number of required pulses for one 1mm
        ZAux_Direct_SetUnits(g_handle,i,1000);
        //set axis motion speed
        ZAux_Direct_SetSpeed(g_handle,i,200);
        //set axis acceleration
        ZAux_Direct_SetAccel(g_handle,i,2000);
        //set axis deceleration
        ZAux_Direct_SetDecel(g_handle,i,2000);
    }
    //update position data of first row
    Text = ui->DisX1->toPlainText();
    DisList[0] = Text.toFloat();
    Text = ui->DisY1->toPlainText();
    DisList[1] = Text.toFloat();
    Text = ui->DisZ1->toPlainText();
    DisList[2] = Text.toFloat();
    ZAux_Direct_MoveAbs(g_handle,3,AxisList,DisList);
    //update position data of second row
    Text = ui->DisX2->toPlainText();
    DisList[0] = Text.toFloat();
    Text = ui->DisY2->toPlainText();
    DisList[1] = Text.toFloat();
    Text = ui->DisZ2->toPlainText();
    DisList[2] = Text.toFloat();
    //update position data of third row
    ZAux_Direct_MoveAbs(g_handle,3,AxisList,DisList);
    Text = ui->DisX3->toPlainText();
    DisList[0] = Text.toFloat();
    Text = ui->DisY3->toPlainText();
    DisList[1] = Text.toFloat();
    Text = ui->DisZ3->toPlainText();
    DisList[2] = Text.toFloat();
    ZAux_Direct_MoveAbs(g_handle,3,AxisList,DisList);
}
```

d. call axis stop command to stop motion through slot function of stop button.

```
//stop axis motion
void MainWindow::on_StopButton_clicked()
{
    ZAux_Direct_Rapidstop(g_handle,2);
}
```







## 5. How to Transplant Routine to Linux Device


#### (1) Copy  Corresponding Linux Library to Project Folder

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


#### (2) Add Function Library's Static Library (libzmotion.so) to New Created Project.

a. add function library 1

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

b. add function library 2

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

c. add function library 3

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



#### (3) Compile Again, Then Run Directly.

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



#### (4) Debug & Monitor

Compile and run the routine, at the same time connect to ZDevelop software to debug. In this way, axis parameters and motion situation all can be watched in real-time.

--multi-axis interpolation X-Y position waveform--


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

--multi-axis interpolation X-Y position waveform (continuous interpolation is OFF)--


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







ABOUT ZMOTION

That's all, thank you for your reading -- E[therCAT Motion Control Card QT Development Tutorial (1) | Development Environment Configuration & Simple Motion Control Example](http://www.zmotionglobal.com/support_info_120.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.