Today, Zheng Sports Assistant will share a tutorial on motion control card application development using MATLAB.
ECI2828 Hardware Introduction
The ECI2828 series motion control card supports up to 16-axis linear interpolation, arbitrary circular interpolation, spatial circular interpolation, helical interpolation, electronic cams, electronic gears, synchronous following, virtual axes, and robot commands; it uses an optimized network communication protocol to achieve real-time motion control.
The ECI2828 series motion control card supports Ethernet and a RS-232 communication interface to connect to a computer, receive commands from the computer, and can connect to various expansion modules via EtherCAT and CAN buses to expand the number of input/output points or motion axes.
Applications for the ECI2828 series motion control card can be developed using software such as VC, VB, VS, C++, and C#. The program requires the dynamic library zmotion.dll to run. During debugging, the ZDevelop software can be connected to the controller simultaneously for easier debugging and observation.
Typical connection configuration diagram of ECI2828 series
MATLAB for Motion Control Development
Ⅰ Create a new project and add functions
1. In MATLAB R2019a, go to "Home" → "New" → "Project" → "Blank Project" to start the project creation wizard.
2. Create a new GUI. (Type "guide" in the command line to create a new GUI)
3. Locate the MATLAB function library in the CD-ROM provided by the manufacturer. The path is as follows (64-bit library as an example).
A. Locate the "8.PC Functions" folder in the CD-ROM provided by the manufacturer and click to enter.
B. Select the "Function Library 2.1" folder.
C. Select the "Windows Platform" folder.
D. Select the appropriate function library as needed; here, we choose the 64-bit library.
E. Unzip the MATLAB compressed package, which contains the corresponding function library.
4. Copy the library files and related files provided by the vendor to the MATLAB working directory.
A. Copy the four files “zauxdll.dll”, “zmotion.dll”, “zauxdll2.h”, and “zmotion.h” to the MATLAB working directory.
For example: C:\...\....\R2019a\bin
B. Copy the two files "zauxdll2.h" and "zmotion.h" to the include directory.
For example: C:\...\....\R2019a\extern\include
C. Copy the "zauxdll.dll" file to the newly created project directory.
5. Configure the MATLAB development environment.
A. In the MATLAB command line, enter "mbuild -setup" and "mex -setup" to select the C language compiler to install.
B. Load the function library using loadlibrary('zauxdll.dll','zauxdll2.h');
C.libfunctions zauxdll -full; or libfunctionsview zauxdll; displays the signatures of shared library functions.
6. Use "calllib" to call functions in the zauxdll function library. For details, please refer to the MATLAB help.
●For information on parameter passing, see calllib help → Passing Parameters.
●The function library has now been added, and you can start MATLAB project development.
II. Refer to the PC function manual
1. The PC function manual is also included in the CD-ROM materials. The specific path is as follows: "CD-ROM materials\8.PC functions\Function library 2.1\ZMotion function library programming manual V2.1.pdf".
2. PC programming typically involves connecting the controller and industrial PC via the network port. The network port connection function interface is ZAux_OpenEth(); if the connection is successful, this interface will return a connection handle. Controlling the controller can be achieved by manipulating this connection handle.
ZAux_OpenEth() Interface Description:
Project application screenshots:
Command line application screenshot:
3. Use the single-axis motion-related instructions to operate the link handle "g_handle" to perform single-axis motion control on the controller. The single-axis motion-related instructions are as follows.
Single-axis relative motion commands:
Single-axis absolute motion commands:
Single-axis continuous motion:
Single-axis stop motion:
III. MATLAB for Single-Axis Motion Development
1. The human-machine interface for single-axis motion control is as follows.
2. Simplified flowchart of the routine.
3. Create new controls in the MATLAB GUI interface to write the program.
A. Create a new "Normal Button" and editable text in the toolbar, and double-click to modify its properties.
B. Right-click the control, view the callback, and select "CallBack" to create a callback response function. A .m format file will be automatically created at this time, where you can write the interface response program.
C. Add the automatically created files to the project.
D. Load the function library and define global variables in the "XXX_OpeningFcn" function.
% --- Executes just before untitled is made visible. function untitled_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to untitled (see VARARGIN) % Choose default command line output for untitledhandles.output = hObject; % Update handles structure guidata(hObject, handles); if not(libisloaded('zauxdll')) % Load library loadlibrary('zauxdll.dll','zauxdll2.h'); end disp("Load library"); % Command line printing global g_handleptr; % Define connection handle global g_nAxis; % Define axis number global g_Dir; % Define motion direction global g_Moveway; % Defines the motion method g_handleptr = libpointer('voidPtrPtr'); g_Dir = 1; g_Moveway = 0; g_nAxis = 0;
4. Write a program for the "Connect Controller" button, which calls the interface "ZAux_OpenEth()" to connect to the controller when the button is clicked.
% --- Executes on button press in pushbutton_openZmc.function pushbutton_openZmc_Callback(hObject, eventdata, handles)% hObject handle to pushbutton_openZmc (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA) handles.timer = timer('Period',0.05,'ExecutionMode','FixedRate',... 'TimerFcn',{@UpdateSliderData, handles});%Timer global g_handleptr;%Define connection handle ip =get(handles.edit_Ip,'String'); disp("Connection controller:"+ip); zmc_ip = char(ip); [res,~] = calllib('zauxdll','ZAux_OpenEth',zmc_ip,g_handleptr); commandCheckHandler("ZAux_OpenEth",res); if res==0 fprintf('Connection to controller successful\n'); set(gcf,'NumberTitle', 'off', 'Name', 'Connection successful'); %msgbox('Connection successful'); start( handles.timer );%Start timer else fprintf('Connection to controller failed, error code %d\n',res); set(gcf,'NumberTitle', 'off', 'Name', 'Connection failed'); msgbox('Connection failure, Please check the IP!');%Connection to controller failed, please check the IP address return; end
5. Right-click on the main interface to add a destruction function, and call "ZAux_Close()" to close the connection.
% --- Executes during object deletion, before destroying properties. Close connection function figure1_DeleteFcn(hObject, eventdata, handles) global g_handleptr; % Define connection handle calllib('zauxdll','ZAux_Close',g_handleptr); disp("Closed connection with controller"); % Print
6. Update the controller axis status via timer: current coordinate, current speed, etc.
function UpdateSliderData(obj, events, handles) global g_handleptr;%Define connection handle global g_nAxis;%Define axis number [res,~,runstate]=calllib('zauxdll','ZAux_Direct_GetIfIdle',g_handleptr,g_nAxis, 0); commandCheckHandler("ZAux_Direct_GetIfIdle",res); [res,~,curpos]=calllib('zauxdll','ZAux_Direct_GetDpos',g_handleptr, g_nAxis, 0); commandCheckHandler("ZAux_Direct_GetDpos",res); [res,~,curspeed]=calllib('zauxdll','ZAux_Direct_GetVpSpeed',g_handleptr, g_nAxis, 0); commandCheckHandler("ZAux_Direct_GetVpSpeed",res); str_curpos=num2str(curpos); str_curspeed=num2str(curspeed); if (runstate==0) set(handles.edit_runstate,'String',"Current motion state: Running"); else set(handles.edit_runstate,'String',"Current motion state: Stopped"); end set(handles.edit_curpos,'String',"Current axis coordinate: " +str_curpos); set(handles.edit_curspeed,'String',"Current axis speed: " +str_curspeed);
7. Set the axis parameters and start the motion by using the event handler function of the start button.
% --- Executes on button press in pushbutton_runmove. `function pushbutton_runmove_Callback(hObject, eventdata, handles)` % `hObject`: handle to pushbutton_runmove (see GCBO) % `eventdata`: reserved - to be defined in a future version of MATLAB % `handles` structure with handles and user data (see GUIDATA) `global g_handleptr;` % Defines the connection handle `global g_nAxis;` % Defines the axis number `global g_Dir;` % Defines the direction of motion `global g_Moveway;` % Defines the motion mode `calllib('zauxdll','ZAux_Direct_SetAtype',g_handleptr, g_nAxis,0);` `calllib('zauxdll','ZAux_Direct_SetUnits',g_handleptr,g_nAxis,str2num(get(handles.edit_units,'String')));` calllib('zauxdll','ZAux_Direct_SetLspeed',g_handleptr, g_nAxis, str2num(get(handles.edit_lspeed,'String'))); calllib('zauxdll','ZAux_Direct_SetSpeed',g_handleptr, g_nAxis, str2num(get(handles.edit_speed,'String'))); calllib('zauxdll','ZAux_Direct_SetAccel',g_handleptr, g_nAxis, str2num(get(handles.edit_acc,'String'))); calllib('zauxdll','ZAux_Direct_SetDecel',g_handleptr, g_nAxis, str2num(get(handles.edit_dec,'String'))); calllib('zauxdll','ZAux_Direct_SetSramp',g_handleptr, g_nAxis, str2num(get(handles.edit_sramp,'String'))); if (g_Moveway) %//Continuous motion calllib('zauxdll','ZAux_Direct_Single_Vmove',g_handleptr, g_nAxis,g_Dir); else % //Inching calllib('zauxdll','ZAux_Direct_Single_Move',g_handleptr, g_nAxis, g_Dir * str2num(get(handles.edit_step,'String'))); end
8. Stop the interpolation motion by using the event handler function of the stop button.
% --- Executes on button press in pushbutton_stopMove. Single-axis stop function pushbutton_stopMove_Callback(hObject, eventdata, handles) global g_handleptr; % Defines the connection handle global g_nAxis; calllib('zauxdll','ZAux_Direct_Single_Cancel',g_handleptr,g_nAxis,2);
IV. Debugging and Monitoring
1. Compile and run the routine, and connect to ZDevelop software for debugging to monitor the axis parameters and motion status of motion control.
(1) Connect the ZDevelop software and click "View" → "Oscilloscope" to open the oscilloscope and monitor the axis movement.
(2) Run the host computer software for debugging and monitoring.
2. Video demonstration.
This concludes our tutorial on developing applications of the EtherCAT motion control card using MATLAB.
This article is original content from Zheng Motion Technology. We welcome everyone to reprint it for mutual learning and to jointly improve China's intelligent manufacturing level. Copyright belongs to Zheng Motion Technology. Please indicate the source if you reprint this article.