DEV Community

Cover image for How to Develop EtherCAT Motion Controller on ROS (1)
ZMOTION CONTROLLER
ZMOTION CONTROLLER

Posted on

How to Develop EtherCAT Motion Controller on ROS (1)

It can be referred from the title, this article will talk about “How to Develop EtherCAT Motion Controller on ROS”. And here takes ZMC432-V2 EtherCAT Motion Controller as the example.

1. What Is ROS ?

ROS (robot operating system) is one operating system (secondary operating system) after opening source for robots. The main aim is to promote code reuse ration in the field of robotic R&D.

ROS is a processing frame of distributed type (also can be called as Nodes), then execution files can be designed independently and they can loosen couple while running. In addition, these processes can be encapsulated into data packages (Packages) and stack (Stacks) for sharing and distributing.

What's more, ROS supports joint system of code library, then cooperation also can be distributed. In this way, this kind of design (from file system level to community level) makes it possible to independently determine development and implement work. All above functions are achieved through ROS basic tools.

Simply, ROS as a kind of flexible operating system, it is really random on system nodes, they can be located in different PC, even in different Ethernet. Then, we can use one Arduino as one node to post message, and use one laptop as one node to subscribe above information and use one phone as one node to drive motor. Therefore, it can be seen ROS can be applied in all kinds of occasions.

Image description

(1) ROS Main Features

A. Distributed & Point to Point
The point to point design of ROS and mechanisms such as service and node managers can disperse the real-time computing pressure brought by functions of PC vision and audio recognition, which can adapt to the challenges encountered by multi-robots.

Image description

B. Multiple Program Languages
ROS supports several kinds of programming languages, such as, C++, Python, Octave, and LISP. Also, it includes multiple kinds of interfaces of other languages.

Image description

C. Simplification & Integration
Systems built by ROS are modular, and codes in each module can be compiled independently. And "simplification" theory can be easily achieved through "CMake" tool (for compiling).
And complex codes are encapsulated into the library by ROS. It only builds some application programs to show library, which means it allows simple codes that exceed prototype to be transplanted and reused.

D. Rich Tool Package
To manage complicated ROS software framework, we use a lot of small tools to compile and run all kinds of ROS components. Then, the kernel is designed, namely, it is not to build a huge development and running environment.

Image description

E. Free & Open Source
All source codes of ROS are released in public.

(2) ROS Applications

In recent years, domestic and foreign robot enterprises, driverless enterprises, and drone enterprises (DJI drones) all have joined the ROS. Now actually many enterprises need engineers who knows or masters ROS knowledge. Then it can be known ROS is popular increasingly. Please see below ROS typical applications.
A. “Boss" in robot controllers -- KEBA , their controllers support ROS.

Image description

B. Robonaut 2 developed based on ROS by USA NASA has been already working on the International Space Station.

Image description

C. The bottom layer of Baidu's Apollo unmanned vehicle is developed based on ROS. It can be found on github (apollo-platform).

Image description

D. ROS-I prepares to cooperate with Microsoft and BMW to develop a set of automation solutions (Reference: A ROS-Industrial Collaboration with Microsoft and BMW).

Image description

E. Many industrial robotic arms use ROS system. Among them, the 5th anniversary video officially released by ROS-I at the end of 2017 is full of robots in the four major families. The KUKA youbot robot in the picture below is developed by using the ROS system.

Image description

2. How to Install the Software ?

This part mainly talks about the detailed process of installing ROS Melodic on Ubuntu 18.04 (this article takes Ubuntu 18.04 as an example, the same is true for others, and it does not affect the use of motion controller).

(1) Set “sources.list”

$ sudo sh -c '. /etc/lsb-release && echo "deb
http://mirrors.ustc.edu.cn/ros/ubuntu/ $DISTRIB_CODENAME main" >
/etc/apt/sources.list.d/ros-latest.list'
Enter fullscreen mode Exit fullscreen mode

(2) Set “key” (key is updated)

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-key
421C365BD9FF1F717815A3895523BAEEB01FA116
Enter fullscreen mode Exit fullscreen mode

Image description

(3) Update “package” (maybe a long time, please wait a moment)

$ sudo apt-get update
At this time, it may meet following questions.

Image description

Solution is following, enter in end:
$sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key F42ED6FBAB17C654
$ sudo apt-get update

Image description

When “Reading package lists … Done” appears, which means updating succeeds.

(4) Install “ROS melodic” (full version)

$ sudo apt-get install ros-melodic-desktop-full

(5) Initialize “rosdep”

$sudo rosdep init
$rosdep update
$echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
$ source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Effects:

Image description

Image description

If you meet following problems when input the instruction "sudo rosdep init":
A. sudo: rosdep: command not found
Enter in the terminal: sudo apt install rospack-tools
B. ERROR: cannot download default sources list from: https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d/20-default.list Website may be down.
--enter "sudo gedit /etc/hosts" in the terminal, then open hosts file--

Image description

--add "199.232.28.133 raw.githubusercontent.com" in the file, and new file added as follow--

Image description

--save file and exit--

Image description

--execute “sudo rosdep init” in the terminal, then problem is solved--

(6) test whether ROS is installed successfully or not

$ roscore
If it is successful, like this:

Image description

3. ROS Programming Examples

There are mainly two ROS routines, “message publish” and “subscription”.

(1) Build One ROS Work Space

$mkdir -p ~/catkin_ws/src
$cd ~/catkin_ws/
$ catkin_make
Enter fullscreen mode Exit fullscreen mode

(2) Create One ROS Program Package (zmotion is the package name, it can be defined as others)

$cd ~/catkin_ws/src
$catkin_create_pkg zmotion std_msgs roscpp
$cd ~/catkin_ws
$ catkin_make
Enter fullscreen mode Exit fullscreen mode

(3) After Creating the Program Package, “source ~ / catkin_ws / devel / setup.bash is required in each running, otherwise the file cannot be found.

Enter in the terminal:
$ gedit ~/.bashrc
add at the end of the file and save
$ source ~/catkin_ws/devel/setup.bash
Enter fullscreen mode Exit fullscreen mode

(4) New Build One Node File

Create a src directory in the zmotion package directory:

$ cd ~/catkin_ws/src/zmotion   //enter the ZMotion package path
$ mkdir -p src   //new build the src folder for saving c++ program
$ cd src/   // enter the src folder path
$ touch talker.cpp   //create a new publishment message node cpp file
$ touch listener.cpp   //create a new subscription message node cpp file
Enter fullscreen mode Exit fullscreen mode

(5) Edite “Publish Node” (talker.cpp)

Functions to be achieved:
--initialize ROS system
--publish message of std_msgs/Float64 on chatter topic
--publish the message in 10 times/s

#include "ros/ros.h"
#include "std_msgs/Float64.h"
#include <sstream>
int main(int argc, char **argv)
{
//Initialize ROS, name remapping (unique), it must be base name, excluding/
ros::init(argc, argv, "talker"); 
//build one handle for node of processing, the first built NodeHandle is the initialization node.
ros::NodeHandle n;
//tell host computer publish one std_msgs message in chatter topic
//main computer will subscribe all nodes of chatter topic, parameter means the size of published array (input firstly, then output)
ros::Publisher chatter_pub = n.advertise("chatter", 1000);
ros::Rate loop_rate(10);  //self-cycle frequency
int count = 0;
while (ros::ok())
{
std_msgs::Float64 msg;
             msg.data = count*1.0;
             //output, replace prinf/cout
             ROS_INFO("Position is: %f", msg.data); 
chatter_pub.publish(msg);
ros::spinOnce();
             //sleep, make publish frequency is 10Hz
             loop_rate.sleep();
             ++count;
           }
           return 0;
         }
Enter fullscreen mode Exit fullscreen mode

(6) Edit “Subscript Node” (listener.cpp)

Functions to be achieved:
--initialize ROS system
--subscript message of chatter topic
--enter self-cycle, waiting for message
--message arrived, call chatterCallback() function.

#include "ros/ros.h"
#include "std_msgs/Float64.h"
//callback function
void chatterCallback(const std_msgs::Float64::ConstPtr& msg)
{
ROS_INFO("I heard: %f", msg->data);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
//tell master to subscribe chatter topic message.
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
ros::spin(); //self-cycle frequency
return 0;
}
Enter fullscreen mode Exit fullscreen mode

(7) Modify “CMakeLists.txt”

A. Open the path of program package "zmotion(catkin_ws/src/zmotion)", find the file "CMakeLists.txt".

Image description

B. The lowest version is modified as 2.8.3
cmake_minimum_required(VERSION 2.8.3)

C. Find the file "find_package", and add the dependent package "message_generation".

find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
message_generation
)
Enter fullscreen mode Exit fullscreen mode

D. Make sure "generate_message" that is called.

generate_messages(
DEPENDENCIES
std_msgs
)
Enter fullscreen mode Exit fullscreen mode

E. Make sure dependent of this message is added in dependent when running.

catkin_package(
# INCLUDE_DIRS include
# LIBRARIES beginner_tutorials
# CATKIN_DEPENDS roscpp std_msgs
# DEPENDS system_lib
CATKIN_DEPENDS message_runtime
)
Enter fullscreen mode Exit fullscreen mode

F. Modify the configuration under "build".

Image description

--comment "include" in include_directories.

Image description

include_directories(
include
${catkin_INCLUDE_DIRS}
)
Enter fullscreen mode Exit fullscreen mode

--add below codes in assigned position, following codes can be added in "build" randomly. It is better to add in relative positions for good appearance. It takes effect as long as it is added in the "build".

add_executable(talker src/talker.cpp)
add_executable(listener src/listener.cpp)
add_dependencies(talker $zmotion_generate_messages_cpp)//Zmotion is package name
target_link_libraries(talker
${catkin_LIBRARIES}
)
target_link_libraries(listener
${catkin_LIBRARIES}
)
Enter fullscreen mode Exit fullscreen mode

Image description

a. at last, list full CMakeLists.txt.

cmake_minimum_required(VERSION 2.8.3)
project(zmotion)
find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
  message_generation
)
## Generate added messages and services with any dependencies listed here
generate_messages(
   DEPENDENCIES
   std_msgs
)
###################################
## catkin specific configuration ##
###################################
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES zmotion
# CATKIN_DEPENDS roscpp std_msgs
# DEPENDS system_lib
CATKIN_DEPENDS message_runtime
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)
## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
add_executable(talker src/talker.cpp)
add_executable(listener src/listener.cpp)
## Rename C++ executable without prefix
add_dependencies(talker $zmotion_generate_messages_cpp)
## Specify libraries to link a library or executable target against
target_link_libraries(talker
   ${catkin_LIBRARIES}
)
target_link_libraries(listener
   ${catkin_LIBRARIES}
)

b. modify “package.xml” file.
<!-- build dependent. Select one between following two, or both are entered -->
<build_depend>message_generation</build_depend>
<build_export_depend>message_generation</build_export_depend>
<!-- Execute dependent, below must be added: -->
<exec_depend>message_runtime</exec_depend>
Enter fullscreen mode Exit fullscreen mode

c. Compile

$ cd ~/catkin_ws
$ catkin_make
Enter fullscreen mode Exit fullscreen mode

d. Run the Program
--open one new end station, turn on the ROS system--
$ roscore
--open one new end station, run “publish node”--

$ cd ~/catkin_ws   //enter work widget content
$ rosrun zmotion talker   //run “publish node” program
Enter fullscreen mode Exit fullscreen mode

--open one new end station, run “subscript node”--

$ cd ~/catkin_ws
$ rosrun zmotion listener
Enter fullscreen mode Exit fullscreen mode

--Result--
results of “Publish Node”

[INFO] [WallTime: 1314931831.774057] Position is: 1.000000
[INFO] [WallTime: 1314931832.775497] Position is: 2.000000
[INFO] [WallTime: 1314931833.778937] Position is: 3.000000
[INFO] [WallTime: 1314931834.782059] Position is: 4.000000
[INFO] [WallTime: 1314931835.784853] Position is: 5.000000
Enter fullscreen mode Exit fullscreen mode

results of “Subscript Node”

[INFO] [WallTime: 1314931970.262246] I heard: 1.000000
[INFO] [WallTime: 1314931971.266348] I heard: 2.000000
[INFO] [WallTime: 1314931972.270429] I heard: 3.000000
[INFO] [WallTime: 1314931973.274382] I heard: 4.000000
[INFO] [WallTime: 1314931974.277694] I heard: 5.000000
Enter fullscreen mode Exit fullscreen mode

ABOUT ZMOTION
That's all, thank you for your reading -- How to Develop EtherCAT Motion Controller on ROS (1)

Top comments (0)