In last article, we have known ROS background, how to install ROS Melodic by Ubuntu18.04, and two ROS programming examples. Please visit “How to Develop EtherCAT Motion Controller on ROS (1)” for details.
Then, today, the second part is coming, that is, how to configure Zmotion dynamic library environment, and how one single-axis of EtherCAT motion controller on ROS is.
Still, the hardware takes ZMC432-V2 & ZMC408CE. They can be used in offline or online occasions.
For development, both use one same set of API function, C, C++, C#, LabVIEW, Python, Delphi all can be used, and mainstream platforms and operation systems all are valid, which means it is easy and convenient to develop.
1. Configure Zmotion Dynamic Link Library Environment
(1) Add Dynamic Link Library
New build folder "lib" under "content" -- zmotion(catkin_ws/src/zmotion/) of program package, and save dynamic link library "libzmotion.so".
A. add the third party path (under build) in CMakeLists.txt
link_directories(
lib
${catkin_LIB_DIRS}
)
B. also, link dynamic link library in CMakeLists,txt file(when calling the link libaray, remember to remove lib and .so)
target_link_libraries(talker ${catkin_LIBRARIES} zmotion)
(2) Add Library Function “zmcaux.cpp”, “zmotion.h”, “zmcaux.h”
A. add zmcaux.cpp file into "catkin_ws/src/zmotion/src"
B. add head file "zmotion.h and zmcaux.h" into "catkin_ws/src/zmotion/include/ zmotion"
C. and add these three library files in CMakeLists.txt:
add_executable(talker
src/talker.cpp
src/zmcaux.cpp
include/zmotion/zmotion.h
include/zmotion/zmcaux.h
)
D. modify head file's quote, like below image (need to fill corresponding address of "include" file, zmotion is the name of program package)
#include "zmotion/zmotion.h"
#include "zmotion/zmcaux.h"
2. Zmotion Controller Single-Axis Motion by ROS
“talker” node achieves axis 0 motion, and real time sends the position to “listener” node, then modifies "talker.cpp":
(1) Add Handle & Head File
#include "zmotion/zmotion.h"
#include "zmotion/zmcaux.h"
ZMC_HANDLE g_handle=NULL;
(2) Connect to Motion Controller Through EtherNET
ZMC_LinuxLibInit();
//EtherNET (Ethernet) to link
char ipaddr[16] = {"192.168.0.11"};
int x =ZAux_OpenEth(ipaddr,&g_handle); //***ZMC
ROS_INFO("connect to controller through Ethernet:%d",x);//Return 0 -- success
(3) Single-Axis Motion
ZAux_Direct_SetSpeed(g_handle, 0, 200); //set axis 0 motion speed as 200units/s
ZAux_Direct_SetAccel(g_handle, 0, 2000); //set axis 0 acceleration as 2000units/s/s
ZAux_Direct_SetDecel(g_handle, 0, 2000); //set axis 0 deceleration as 2000units/s/s
ZAux_Direct_SetSramp(g_handle, 0, 100); //set axis 0 S curve time as 100ms
ZAux_Direct_Single_Move(g_handle, 0, 300); //axis 0 moves 100 units that relates to current position
(4) Send Real-Time Position to “listener” Node
float piValue;
while (ros::ok())
{
std_msgs::Float64 msg;
ZAux_Direct_GetMpos(g_handle, 0, & piValue);//read time
msg.data = piValue;
//output, used to replace prinf/cout
ROS_INFO("Position is: %f", msg.data);
chatter_pub.publish(msg);
ros::spinOnce();
//sleep, make publishment frequency as 10Hz
loop_rate.sleep();
}
(5) Compile
cd ~/catkin_ws/
catkin_make
(6) Run the Program
//open one new end station
roscore
//open another new end station
cd ~/catkin_ws/
rosrun zmotion talker
//open another new end station
cd ~/catkin_ws/
rosrun zmotion listener
Following is the running effect, real-time output position:
In addition, it can be seen that axis 0 is running S curve motion from the oscilloscope.
That's all, thank you for your reading -- How to Develop EtherCAT Motion Controller on ROS (2)
Have a good day, best wishes, see you next time.
Top comments (0)