Today, Zheng Motion Technology will share with you "Motion Control Card Application Development Tutorial: ROS (Part 1)".
This article begins with the background of ROS applications, then details the installation process of ROS Melodic on Ubuntu 18.04, and finally uses a ROS programming case—message publishing and subscription—to illustrate the application development of the ROS motion control card.
Before we begin our formal learning, let's first understand the motion control cards ECI2418 and ECI2618 from Zheng Motion Technology. These two products are 4-axis and 6-axis motion control cards, respectively.
The ECI2418 supports 4-axis pulse input and encoder feedback, with 24 onboard inputs, 16 onboard outputs, 2 AD, 2 DA, and a handwheel interface. Some output ports support high-speed PWM control.
The ECI2618 supports 6-axis pulse input and encoder feedback, with 24 onboard inputs, 16 onboard outputs, 2 AD converters, 2 DA converters, and a handwheel interface. Some output ports support high-speed PWM control.
Both ECI2418 and ECI2618 use the same set of API functions and support development languages such as C, C++, C#, LabVIEW, Python, and Delphi. They also support platforms such as VC6.0, VB6.0, Qt, and .Net, and operating systems such as Windows, Linux, WinCE, and iMac.
The following is the first part of the ROS application development tutorial.
ROS Application Background
ROS (Robot Operating System) is an open-source secondary operating system for robots. Its primary design goal is to improve code reusability in the field of robotics development. ROS is a distributed processing framework (also known as Nodes). This allows executables to be designed independently and loosely coupled at runtime. These processes can be encapsulated in packages and stacks for easy sharing and distribution. ROS also supports federated systems of code repositories, enabling collaboration to be distributed as well. This design, from the file system level to the community level, makes it possible to independently decide on development and implementation work. All of the above functionalities are implemented using ROS's underlying tools.
In short, ROS, as a flexible operating system, allows for a high degree of flexibility in the nodes running on it. These nodes can reside on different computers or even on different networks. We can use an Arduino as a node to publish information, a laptop as a node to subscribe to such information, and a mobile phone as a node to drive a motor, etc. This flexibility allows ROS to adapt to many different application scenarios.
1. The main characteristics of ROS can be summarized as follows:
(1) Distributed peer-to-peer design
ROS's point-to-point design, along with its service and node manager mechanisms, can distribute the real-time computational load from functions such as computer vision and speech recognition, enabling it to adapt to the challenges faced by multiple robots.
(2) Multilingual support
ROS supports many different languages, such as C++, Python, Octave, and LISP, and also includes various interface implementations for other languages.
(3) Streamlining and Integration
ROS systems are modular, with code in each module compiling independently. The CMake tool makes it easy to achieve a streamlined approach. ROS essentially encapsulates complex code within libraries, creating only small applications to demonstrate the functionality of the ROS libraries. This allows for the porting and reuse of simpler code beyond its original prototype.
(4) Abundant toolkit
To manage the complex ROS software framework, we utilized a large number of small tools to compile and run a wide variety of ROS components, thus designing it as a kernel rather than building a massive development and runtime environment.
(5) Free and open source
All of ROS's source code is publicly available.
2. Some applications of ROS in various industries
In recent years, domestic and international robotics companies, autonomous driving companies, and drone companies (DJI) have all joined the ROS camp. Furthermore, in recruitment advertisements from major companies such as Tencent, Pony.ai, Alibaba's Cainiao Network, and Baidu Apollo, positions such as autonomous driving planning algorithm engineers, autonomous navigation engineers, robot perception algorithm engineers, and system platform R&D architects all require "familiarity with ROS" or "preference will be given to candidates with ROS development experience." From this, we can see the development trend of the ROS system, which is gradually dominating the Chinese market in the robotics industry. Below are some typical ROS application scenarios:
(1) KEBA, a leading player in robot controllers, has controllers that already support ROS:
(2) NASA's Robonaut 2, developed based on ROS, is already working on the International Space Station:
(3) The underlying architecture of Baidu Apollo autonomous vehicles is based on ROS, and it was almost changed to ROS 2. It can be found on GitHub (apollo-platform):
(4) ROS-I is currently preparing to collaborate with Microsoft and BMW to develop an automation solution (see: A ROS-Industrial Collaboration with Microsoft and BMW):
(5) Many industrial robotic arms have begun using the ROS system, and the official ROS-I fifth-anniversary video released at the end of 2017 featured robots from the four major robotic families. The KUKA Youbot robot shown below was developed using the ROS system:
II. Software Installation Process
This section mainly describes the detailed process of installing ROS Melodic on Ubuntu 18.04 (this article uses Ubuntu 18.04 as an example, but the same applies to other systems, and it does not affect the use of the motion control card).
1. Configure 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'
2. Set the key (public key has been updated)
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116
3. Update package (this will take a while, please be patient).
$ sudo apt-get update
At this point, the following problems may occur:
The solution is as follows: Enter the following in the terminal:
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key F42ED6FBAB17C654
$ sudo apt-get update
When you see "Reading package lists... Done", it means the update is complete.
4. Install the full version of ROS melodic
$ 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
Renderings:
If you encounter the following problem when entering the command `sudo rosdep init`:
(1) sudo: rosdep: command not found problem
In the terminal, type: sudo apt install rospack-tools
(2)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.
① In the terminal, type: sudo gedit /etc/hosts to open the hosts file. The content is as follows:
② Add 199.232.28.133 raw.githubusercontent.com to the file. The file after adding the address will look like this:
③Then save the file and exit;
④ Execute the following command in the terminal: sudo rosdep init, and the problem is solved.
6. Test if ROS is installed successfully.
$ roscore
The page opened successfully as shown in the image below:
Three ROS Programming Cases
This section mainly discusses two ROS programming examples: "message publishing" and "subscription".
1. Create a ROS workspace
$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/
$ catkin_make
2. Create a ROS package (zmotion is the package name, but it can also be defined as something else).
$ cd ~/catkin_ws/src
$ catkin_create_pkg zmotion std_msgs roscpp
$ cd ~/catkin_ws
$ catkin_make
3. After creating the package, you need to source ~/catkin_ws/devel/setup.bash every time you run it, otherwise the file cannot be found.
Solve the problem of having to source the method every time:
Enter the following in the terminal:
Edit ~/.bashrc
Add the following to the end of the file and save it:
$ source ~/catkin_ws/devel/setup.bash
4. Create a new node file
Create a src directory within the zmotion package directory:
$ cd ~/catkin_ws/src/zmotion // Enter the zmotion application package path
`$ mkdir -p src` // Creates a new folder named `src` to store C++ programs.
$ cd src/ // Enter the src folder path
$ touch talker.cpp // Create a new cpp file for publishing messages.
$ touch listener.cpp // Create a new message subscription node cpp file
5. Write and publish the node (talker.cpp)
Functionality:
(1) Initialize the ROS system;
(2) Publish std_msgs/Float64 messages on the chatter topic;
(3) Messages are sent at a frequency of 10 times per second.
#include "ros/ros.h"
#include "std_msgs/Float64.h"
#include
int main(int argc, char **argv)
{
// Initialize ROS, name remapping (unique), must be the base name, without '/'
ros::init(argc, argv, "talker");
// Create a handle for the process's node; the first created NodeHandle initializes the node.
ros::NodeHandle n;
// Tell the host to publish a std_msgs message on the chatter topic.
The host will subscribe to all chatter topic nodes; the parameter indicates the size of the publishing queue (first-in, first-out).
ros::Publisher chatter_pub = n.advertise
ros::Rate loop_rate(10); // Self-loop frequency
int count = 0;
while (ros::ok())
{
std_msgs::Float64 msg;
msg.data = count * 1.0;
// Output, used to replace prinf/cout
ROS_INFO("Position is: %f", msg.data);
chatter_pub.publish(msg);
ros::spinOnce();
//Sleep mode to allow the output frequency to be 10Hz
loop_rate.sleep();
++count;
}
return 0;
}
6. Write the subscription node (listener.cpp)
Functionality:
(1) Initialize the ROS system;
(2) Subscribe to chatter topic messages;
(3) Enter a self-loop and wait for a message to arrive;
(4) When the message arrives, the chatterCallback() function is called.
#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 the master that it needs to subscribe to chatter topic messages.
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
ros::spin(); // Self-loop
return 0;
}
7. Modify CMakeLists.txt
(1) Open the path of the program package zmotion (catkin_ws/src/zmotion) and find the CMakeLists.txt file.
(2) The minimum version has been changed to 2.8.3.
cmake_minimum_required(VERSION 2.8.3)
(3) Locate find_package and add the dependency package message_generation.
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
message_generation
)
(4) Ensure that generate_message is called.
generate_messages(
DEPENDENCIES
std_msgs
)
(5) Ensure that the dependency for this message is also added to the runtime dependencies.
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES beginner_tutorials
# CATKIN_DEPENDS roscpp std_msgs
# DEPENDS system_lib
CATKIN_DEPENDS message_runtime
)
(6) Modify the settings under build
① Comment out the include directive in include_directories.
include_directories(
include
${catkin_INCLUDE_DIRS}
)
② Add the following code to the specified location. The code below can be added arbitrarily to the build. I added it to the appropriate location for aesthetic purposes. As long as it is added to the build, it will take effect.
add_executable(talker src/talker.cpp)
add_executable(listener src/listener.cpp)
add_dependencies(talker $zmotion_generate_messages_cpp) // zmotion is the package name
target_link_libraries(talker
${catkin_LIBRARIES}
)
target_link_libraries(listener
${catkin_LIBRARIES}
)
(7) Finally, list the complete 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}
)
8. Modify the package.xml file
<
build_depend > message_generation
build_depend >
<
build_export_depend > message_generation
build_export_depend >
<
exec_depend > message_runtime
exec_depend >9. Compilation
$ cd ~/catkin_ws
$ catkin_make
10. Run the program
(1) Open a new terminal and start the ROS system;
$ roscore
(2) Open a new terminal and run the publish node;
$ cd ~/catkin_ws // Enter the working directory
$ rosrun zmotion talker //Run the publish node program
(3) Open a new terminal and run the subscription node;
$ cd ~/catkin_ws
$ rosrun zmotion listener
(4) Results.
Results of running the release node program:
[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
Results of running the subscription node program:
[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
References:
1.ROS Wiki: http://wiki.ros.org/
2.https://www.ncnynl.com/archives/201609/838.html
3.https://blog.csdn.net/qq_42145185/article/details/80413380?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFrom MachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control
4.https://www.zhihu.com/question/29025926?sort=created
This concludes our tutorial on "Motion Control Card Application Development Tutorial: ROS (Part 1)" from Zheng Motion Technology. For more exciting content, please follow the " Zheng Motion Assistant " WeChat official account.
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.