ROS2已经出了好几个版本,本文尝试安装最新的版本并跑通一个简单的测试
1. 安装ROS2
我们直接按照官方的文档使用安装包安装(我们修改了使用国内的源加快安装速度)
sudo sh -c 'echo "deb https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu/ focal main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
sudo apt-get update
ROS_DISTRO=galactic
sudo apt-get -y --allow-unauthenticated install ros-${ROS_DISTRO}-ros-base
2. 测试
官方的文档提供了直接可以跑的talker和listener的demo, 我们自己编译运行测试下
cd ~
git clone https://gitee.com/pibot/ros2_tutorials.git
source /opt/ros/galactic/setup.bash
sudo apt-get install ros-galactic-example-interfaces python3-colcon-common-extensions
colcon build --symlink-install
编译有点慢,输出如下
? ros2_tutorials git:(master) colcon build --symlink-install
Starting >>> cpp_parameters
Starting >>> cpp_pubsub
Starting >>> cpp_srvcli
Starting >>> more_interfaces
Finished <<< cpp_srvcli [22.4s]
Starting >>> my_package
Finished <<< cpp_parameters [22.6s]
Starting >>> py_pubsub
Finished <<< cpp_pubsub [32.5s]
Starting >>> py_srvcli
Finished <<< my_package [11.8s]
Starting >>> python_parameters
Finished <<< more_interfaces [35.5s]
Starting >>> tutorial_interfaces
Finished <<< py_pubsub [14.7s]
Finished <<< py_srvcli [4.79s]
Finished <<< python_parameters [3.84s]
Finished <<< tutorial_interfaces [18.1s]
Summary: 9 packages finished [54.1s]
分别打开2个终端启动listener 和talker 两个node
. install/setup.bash
ros2 run cpp_pubsub listener
. install/setup.bash
ros2 run cpp_pubsub talker
输入结果如下
? ros2_tutorials git:(master) ros2 run cpp_pubsub talker
[INFO] [1651157786.201203089] [minimal_publisher]: Publishing: 'Hello, world! 0'
[INFO] [1651157786.701139979] [minimal_publisher]: Publishing: 'Hello, world! 1'
[INFO] [1651157787.201177175] [minimal_publisher]: Publishing: 'Hello, world! 2'
[INFO] [1651157787.701187811] [minimal_publisher]: Publishing: 'Hello, world! 3'
[INFO] [1651157788.201121074] [minimal_publisher]: Publishing: 'Hello, world! 4'
? ros2_tutorials git:(master) ros2 run cpp_pubsub listener
[INFO] [1651157786.202114153] [minimal_subscriber]: I heard: 'Hello, world! 0'
[INFO] [1651157786.701859978] [minimal_subscriber]: I heard: 'Hello, world! 1'
[INFO] [1651157787.201796211] [minimal_subscriber]: I heard: 'Hello, world! 2'
[INFO] [1651157787.701764906] [minimal_subscriber]: I heard: 'Hello, world! 3'
[INFO] [1651157788.201778743] [minimal_subscriber]: I heard: 'Hello, world! 4'
查看topic以及输出
? ros2_tutorials git:(master) ros2 topic list
/parameter_events
/rosout
/topic
? ros2_tutorials git:(master) ros2 topic echo /topic
data: Hello, world! 1
---
data: Hello, world! 2
---
data: Hello, world! 3
---
data: Hello, world! 4
---
3. 总结
| ROS1 | ROS2 |
---|
环境变量 | source /opt/ros/${ROS_DISTO}/setup.bash | source /opt/ros/${ROS_DISTO}/setup.bash | 编译 | catkin_make | colcon build | 启动node | rosrun package node | ros2 run package node | 查看topic | rostop echo /topic | ros2 topich echo /topic | master | roscore | 无 |
|