ROS2 tutorial by GuyueHome
Version information:
Different:
- ROS2: No need of
rosmaster
anymore. UsingDDS
communication mechanism. - ROS2: More OOP-style programming.
Similar:
- Similar framework and communication concepts: topic, service, action etc.
Check the official documentation
What if I have to install ROS2 with ROS already in my PC?
- Settings for ROS2 coexistence with ROS.
Open two terminals, first source the ros2 to both of them, then
ros2 run demo_nodes_cpp talker
for a demo-publisher wrote with C++ on one terminal and
ros2 run demo_nodes_py listener
for a demo-subscriber wrote with Python on the other terminal.
Open 2 terminals, first source the ros2, then
ros2 run turtlesim turtlesim_node
to launch the turtlesim node and
ros2 run turtlesim turtle_teleop_key
to run the keyboard control node.
ros2 node list
check the active nodes;ros2 node info /node-name
check the information of a certain node;
ros2 topic list
ros2 topic info /topic-name
ros2 topic echo /topic-name
print the message from a topic in real-time;ros2 topic pub --rate 1 /topic-name message-type "{title1: {x: val, y: val, z: val}, ...}"
publish a message to the topic;
eg. Publish a velocity command of typegeometry_msgs/msg/Twist
to turtlesim's topic/turtle1/cmd_vel
:
ros2 topic pub --rate 1 /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"
ros2 service call /service-name service-type {service-parameters}
eg. Add another turtle into the turtlesim
ros2 service call /spawn turtlesim/srv/Spawn "{x: 2, y: 2, theta: 0.2, name: ''}"
ros2 bag record /topic-name
eg. record the/turtle1/cmd_vel
ros2 bag record /turtle1/cmd_vel
The recorded data would be saved to the current path of the terminal.
ros2 bag play bag-name
eg. play the recorded/turtle1/cmd_vel
ros2 bag play rosbag2_2022_10_03-12_49_13
Here replace the bag name with yours correspondingly.
rosdepc
is a tool that can help to install all the dependencies for a workspace automatically.
sudo pip3 install rosdepc
sudo rosdepc init & rosdepc update
# cd to the workspace root path
rosdepc install -i --from-path src --rosdistro galactic -y
mkdir -p ~/colcon_ws/src # create workspace and src folders
cd colcon_ws
colcon build # compile the workspace
after colcon build
the folder structure would be like
colcon_ws
├── build
│ └── COLCON_IGNORE
├── install
│ ├── COLCON_IGNORE
│ ├── local_setup.sh # source install/local_setup.sh
│ ├── _local_setup_util_sh.py
│ └── setup.sh
├── log
│ └── ...
└── src # contains the packages
ros2 pkg create --build-type <build-type> <package_name>
create a package
cd ~/colcon_ws/src
ros2 pkg create --build-type ament_cmake learning_pkg_c # C++
ros2 pkg create --build-type ament_python learning_pkg_python # Python
for c++ package, there should be package.xml
and CMakeLists.txt
.
for python package, there should be package.xml
, setup.cfg
and setup.py
.
The information of dependencies for rosdepc
is exactly provided by package.xml
.
- execute a certain process of the App;
- an independent
.exe
file; - compliable for multiple programming languages, e.g. cooperation of a
talker.cpp
and alistener.py
; - can be launched on differnent host machines distributedly.
- be managed with a sole node name.
Sometimes we might encounter problems like "Import rclpy cannot be resolved". This can happen when we didn't link to ROS2's shell environment correctly, especially we have ROS and ROS2 coexist and any of them is NOT sourced defaultly to our terminal.
Actually, the necessary libraries rclpy
and rclcpp
are supposed to be installed along with ros2.
- (If not, install the library
rclpy
manuelly: https://index.ros.org/r/rclpy/)
According to the official guide, we can avoid VS Code's resolving shell environment phase by launching VS Code directly from a fully initialized terminal:
source /opt/ros/galactic/setup.bash # link to ROS2's shell environment
cd colcon_ws/src
code .
- create package
- create script
- add this at the begining of the
.py
script#!/usr/bin/env python3 # -*- coding: utf-8 -*-
- add this at the begining of the
- make executable
.exe
file- C++: configure
CMakeList.txt
- Python: via the command
chmod +x node.py
- C++: configure
Configure the setup.py
: add the node entry in entry_points
:
e.g.:
entry_points={
'console_scripts': [
'node_helloworld = learning_node.node_helloworld:main',
...
],
},
Note that every time we've modified the .py
script we need to re-build the workspace so that the changes could be updated, which is different from ROS1.
- for ROS1, the Python node we would run is exactly the script we've modified in
/src
- for ROS2, the Python node is in
/install
, generated from the script via$ colcon build
.
We MUST source the setup.sh
before running a ros node, which is likely to what we did with ROS1
cd colcon_ws
source install/setup.sh # for ROS1, it was $ source devel/setup.bash
ros2 run learning_node node_helloworld
Command syntax:
$ ros2 run <pkg_name> <node_name>
<node_name>
stands for an .exe
file which was built by CMake
during compiling;
<node_name>
, or exactly <node_name.py>
is an .exe
file directly converted from the .py
script.