ROS2官方文档
本教程为apt-get命令安装方式,官网教程有点问题,借鉴一下大佬的安装方式
1 安装ROS2
1.1 安装秘钥相关指令
sudo apt update && sudo apt install curl gnupg2 lsb-release
Reading package lists... Done
Building dependency tree
Reading state information... Done
lsb-release is already the newest version (9.20170808ubuntu1).
curl is already the newest version (7.58.0-2ubuntu3.16).
The following NEW packages will be installed:
gnupg2
0 upgraded, 1 newly installed, 0 to remove and 6 not upgraded.
Need to get 5,292 B of archives.
After this operation, 52.2 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://mirrors.aliyun.com/ubuntu bionic-updates/universe amd64 gnupg2 all 2.2.4-1ubuntu1.4 [5,292 B]
Fetched 5,292 B in 0s (37.9 kB/s)
Selecting previously unselected package gnupg2.
(Reading database ... 269610 files and directories currently installed.)
Preparing to unpack .../gnupg2_2.2.4-1ubuntu1.4_all.deb ...
Unpacking gnupg2 (2.2.4-1ubuntu1.4) ...
Setting up gnupg2 (2.2.4-1ubuntu1.4) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
1.2 授权秘钥
curl http://repo.ros2.org/repos.key | sudo apt-key add -
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1678 100 1678 0 0 2829 0 --:--:-- --:--:-- --:--:-- 2824
OK
1.3 添加ROS2软件源
sudo sh -c 'echo "deb [arch=amd64,arm64] http://packages.ros.org/ros2/ubuntu `lsb_release -cs` main" > /etc/apt/sources.list.d/ros2-latest.list'
sudo apt-get update
Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease
Hit:3 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease
Hit:4 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease
Get:5 http://packages.ros.org/ros2/ubuntu bionic InRelease [4,680 B]
Hit:6 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease
Hit:7 https://download.docker.com/linux/ubuntu bionic InRelease
Get:8 http://packages.ros.org/ros2/ubuntu bionic/main amd64 Packages [686 kB]
Ign:9 http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu bionic InRelease
Hit:10 http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu bionic Release
Get:12 http://packages.ros.org/ros2/ubuntu bionic/main arm64 Packages [658 kB]
Fetched 1,349 kB in 5s (288 kB/s)
Reading package lists... Done
1.4 安装
sudo apt install ros-dashing-desktop
- 补充:
本次安装直接安装了大礼包,ROS提供了两种默认搭配,如下:
#桌面版 : 包含ROS, RViz, demos, tutorials [推荐]
sudo apt install ros-dashing-desktop
#基础版:(包含通讯库、消息包、命令行工具,没有GUI工具)
sudo apt install ros-dashing-ros-base
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
cppcheck libxaw7-dev libxpm-dev pydocstyle pyflakes pyflakes3 python-funcsigs python-mock
python-pbr python-pyflakes python3-attr python3-bs4 python3-catkin-pkg-modules
.
.
.
Setting up ros-dashing-desktop (0.7.4-1bionic.20210521.235100) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.4) ...
ROS2到这里就安装完了,安装目录为/opt/ros/dashing
2 设置环境(可选但是推荐)
- 说明:不想设置的话,就每次启动终端手动运行一下
source /opt/ros/dashing/setup.bash
2.1 未安装ROS1 / 默认使用ROS2
echo "source /opt/ros/dashing/setup.bash" >> ~/.bashrc
source ~/.bashrc
2.2 装了ROS1,每次启动终端都手动选择版本
- 把下面的脚本添加到
~/.bashrc
,然后执行source ~/.bashrc
#source /opt/ros/melodic/setup.bash
#source /opt/ros/dashing/setup.bash
# ROS 1.0 melodic or ROS 2.0 Dashing
echo ROS 1.0 or ROS 2.0? 1=Melodic 2=Dashing
read ROS
if (($ROS==1));then
source /opt/ros/melodic/setup.bash
echo "Melodic"
elif (($ROS==2));then
source /opt/ros/dashing/setup.bash
echo "Dashing"
fi
3 命令行自动补全键入(可选但是推荐)
- 说明:ROS2按
Tab
键不会自动补全命令,如果希望有自动补全功能,需要安装一下
- 命令
sudo apt install python3-argcomplete
source /opt/ros/dashing/setup.sh
$ sudo apt install python3-argcomplete
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
python3-argcomplete
0 upgraded, 1 newly installed, 0 to remove and 6 not upgraded.
Need to get 26.9 kB of archives.
After this operation, 122 kB of additional disk space will be used.
Get:1 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 python3-argcomplete all 1.8.1-1ubuntu1 [26.9 kB]
Fetched 26.9 kB in 0s (70.1 kB/s)
Selecting previously unselected package python3-argcomplete.
(Reading database ... 287952 files and directories currently installed.)
Preparing to unpack .../python3-argcomplete_1.8.1-1ubuntu1_all.deb ...
Unpacking python3-argcomplete (1.8.1-1ubuntu1) ...
Setting up python3-argcomplete (1.8.1-1ubuntu1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
4 运行demo
跟ROS1一样,ROS2也提供了demo。
ros2 run demo_nodes_cpp listener
ros2 run demo_nodes_cpp talker
- 补充:
错误1:ros2: command not found
执行一下 2 设置环境变量
- 成功
listener终端:
$ ros2 run demo_nodes_cpp listener
[INFO] [listener]: I heard: [Hello World: 1]
[INFO] [listener]: I heard: [Hello World: 2]
[INFO] [listener]: I heard: [Hello World: 3]
[INFO] [listener]: I heard: [Hello World: 4]
[INFO] [listener]: I heard: [Hello World: 5]
talker终端:
$ ros2 run demo_nodes_cpp talker
[INFO] [talker]: Publishing: 'Hello World: 1'
[INFO] [talker]: Publishing: 'Hello World: 2'
[INFO] [talker]: Publishing: 'Hello World: 3'
[INFO] [talker]: Publishing: 'Hello World: 4'
[INFO] [talker]: Publishing: 'Hello World: 5'
5 安装ROS1 bridge(可选)
- 说明:ROS1 bridge提供了一个网桥,可以在 ROS 1 和 ROS 2 之间交换消息, 详情见官网
- 命令
sudo apt-get install ros-dashing-ros1-bridge
- demo:使用ROS1的方式运行talker,然后使用ROS2的方式运行listener
1 新开终端,启动roscore
source /opt/ros/melodic/setup.sh
roscore
2 新开终端,启动ROS1的talker
source /opt/ros/melodic/setup.sh
rosrun rospy_tutorials talker
3 新开终端,启动ROS2的listener
source /opt/ros/dashing/setup.sh
ros2 run demo_nodes_cpp listener
4 新开终端,启动ROS1 bridge
source /opt/ros/melodic/setup.sh
source /opt/ros/dashing/setup.sh
#设置默认地址,这一句可以不要
export ROS_MASTER_URI=http://localhost:11311
ros2 run ros1_bridge dynamic_bridge
6 附加RMW实现(可选)
- 说明:
1 ROS2相比ROS1,引入了DDS,DDS是一个国际标准,使用发布/订阅机制进行消息通讯
2 ROS2使用的默认中间件(RMW – ROS 2 middleware)是Cyclone DDS
,RMW可以更换,DDS和RM官网文档
3 如果安装了多个RMW,可以通过设置环境变量来选择不同的RMW,如:RMW_IMPLEMENTATION=rmw_connext_cpp
- 命令
sudo apt install ros-dashing-rmw-opensplice-cpp # for OpenSplice
sudo apt install ros-dashing-rmw-connext-cpp # for RTI Connext (requires license agreement)
7 安装colcon编译工具(推荐)
- 说明:ROS的编译工具经历了catkin、ament、colcon三个阶段,ROS2使用colcon,如果需要编译还是装一下
- 命令
sudo apt install python3-colcon-common-extensions
8 编译小乌龟demo
mkdir -p ~/dev_ws/src
cd ~/dev_ws/src
//克隆到刚刚创建的src目录下
git clone https://github.com/ros/ros_tutorials.git -b dashing-devel
- 检查依赖项,如果所有的依赖项都正确,则控制台显示如下
$ rosdep install -i --from-path src --rosdistro dashing -y
#All required rosdeps installed successfully
$ colcon build
Starting >>> turtlesim
[Processing: turtlesim]
Finished <<< turtlesim [30.7s]
Summary: 1 package finished [30.8s]