对于Ubuntu18.04系统,对应的ros1和ros2的版本号为:
- ros1:
melodic
- ros2:
eloquent
1 添加密钥和ros2下载
1、添加密钥
sudo apt update && sudo apt install curl gnupg2 lsb-release
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
2、添加ros2的下载源
sudo sh -c 'echo "deb [arch=amd64] http://packages.ros.org/ros2/ubuntu bionic main" > /etc/apt/sources.list.d/ros2-latest.list'
2 更新源和安装ros-eloquent-desktop及其依赖
1、更新源
sudo apt update
2、安装
和ros1一样,ros2也有两个版本
可以选,根据个人需求选取一个版本进行安装
- 1)
桌面版
:安装包含:ROS, RViz, demos, tutorials
等内容比较全面,推荐按照这个版本
sudo apt install ros-eloquent-desktop
- 2)
基本版
:安装仅包含:通讯库,消息包,命令行工具
。此版本没有GUI工具,只使用于资源比较限制的计算机按照
sudo apt install ros-eloquent-ros-base
注意:
如果计算机资源允许,建议安装:桌面版
3 配置环境
3.1 添加环境变量
安装好ros2之后,需要把source /opt/ros/eloquent/setup.bash
添加到环境变量中,才可以在命令行下使用ros2的相关命令
echo "source /opt/ros/eloquent/setup.bash" >> ~/.bashrc
3.2 如何让ros1和ros2切换使用
1、如果你电脑中已经安装了ros1版本
,在你添加上面的环境变量的时候
,不出意外的话就会弹一个下面的警告,如下:
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
当然这个错误你可以暂时忽略,不用管,但是如何做到在命令行中随心切换ros1和ros2呢?
2、如何让ros1和ros2切换使用参考)
ros1和ros2的安装目录是明确区分开的:
- ros1的安装路径:
/opt/ros/melodic
- ros2的安装路径:
/opt/ros/eloquent
我们只要在~/.bashrc
配置文件中,添加如下的判断环境变量即可,每次打开一个新的终端之后,只要通过输入1
或2
就可以进行
echo "ros melodic(1) or ros2 eloquent(2)?"
read edition
if [ "$edition" -eq "1" ];then
source /opt/ros/melodic/setup.bash
else
source /opt/ros/eloquent/setup.bash
fi
注意:
[ ]
和内部的字符之间一定要空一个字符
,否则就会报错
3.3 安装自动补全命令
ros2的命令可以通过argcomplete
完成自动补全,按照下面软件获得自动补全功能:
sudo apt install python3-argcomplete
3.4 安装ros2bag命令
上面已经安装好了ros2,但是我在使用ros2去播放rosbag2包的时候,发现没有命令,ros2的bag命令需要单独安装:
sudo apt-get install ros-eloquent-ros2bag
1、然后可以查看ros2录制包的信息
(base) shl@zhihui-mint:/media/shl/sd/shl_ros2_bags$ ros2 bag info rosbag2_2021_05_12-11_34_36
Files: rosbag2_2021_05_12-11_34_36/rosbag2_2021_05_12-11_34_36_0.db3
Bag size: 7.0 GiB
Storage id: sqlite3
Duration: 203.404s
Start: May 12 2021 11:34:37.99 (1620790477.99)
End May 12 2021 11:38:00.503 (1620790680.503)
Messages: 4070
Topic information: Topic: /velodyne_points | Type: sensor_msgs/msg/PointCloud2 | Count: 2035 | Serialization Format: cdr
Topic: /image | Type: sensor_msgs/msg/Image | Count: 2035 | Serialization Format: cdr
(base) shl@zhihui-mint:/media/shl/sd/shl_ros2_bags$
2、但是,在播放包的时候又会报错:[ERROR] [rosbag2_storage]: Could not load/open plugin with storage id 'sqlite3'.
从错误的信息可以发现,好像是缺少插件
导致的!
解决错误方式,安装如下插件即可:
sudo apt install -y ros-eloquent-rosbag2-bag-v2-plugins ros-eloquent-rosbag2-converter-default-plugins ros-eloquent-rosbag2-storage ros-eloquent-rosbag2-transport ros-eloquent-rosbag2-storage-default-plugins ros-eloquent-ros2bag
安装好之后,在播放ros2的bag包就没有问题了:
(base) shl@zhihui-mint:/media/shl/sd/shl_ros2_bags$ ros2 bag play rosbag2_2021_05_12-11_34_36
[INFO] [rosbag2_storage]: Opened database 'rosbag2_2021_05_12-11_34_36/rosbag2_2021_05_12-11_34_36_0.db3' for READ_ONLY.
4 测试ros2的安装环境
下面我们通过简单的一个例子进行测试
首先我们打开一个终端
运行一个C++
的talker
如果运行报错记得source一下
ros2 run demo_nodes_cpp talker
我们再打开一个终端运行
一个Python
的listener
如果运行报错记得source一下
ros2 run demo_nodes_py listener
这时候在talker
模块会出现
[INFO] [talker]: Publishing: 'Hello World: 1'
[INFO] [talker]: Publishing: 'Hello World: 2'
[INFO] [talker]: Publishing: 'Hello World: 3'
这时候在listener
模块会出现
[INFO] [listener]: I heard: [Hello World: 3]
[INFO] [listener]: I heard: [Hello World: 4]
[INFO] [listener]: I heard: [Hello World: 5]
好了到现在就把ros2的C++和Python接口都测试了
5 安装ros1的桥,建立ros2和ros1之间的通讯
sudo apt update
sudo apt install ros-eloquent-ros1-bridge
6 ros2的常用命令
ros2的一些命令几乎和ros1是一样的,只是相当于命令进行了一个拆分,比如:播放包:
- ros1:rosbag play xxx.bag
- ros2:ros2 bag play xxx-dir
其他的命令类似上述拆分:
6.1 ros2的bag相关命令
1、ros2查看包的信息
ros2 bag info rosbag2-xxx-dir
2、ros2播放包
ros2 bag play rosbag2-xxx-dir
3、ros2录制包
指定自己要录制包的topic即可!
ros2 bag record /topic1 /topic2
录制所有的topic:
ros2 bag record -a
6.2 ros2的topic相关的命令
先查看一下:ros2 topic
的参数:
(base) shl@zhihui-mint:~$ ros2 topic -h
usage: ros2 topic [-h] [--include-hidden-topics]
Call `ros2 topic <command> -h` for more detailed usage. ...
Various topic related sub-commands
optional arguments:
-h, --help show this help message and exit
--include-hidden-topics
Consider hidden topics as well
Commands:
bw Display bandwidth used by topic
delay Display delay of topic from timestamp in header
echo Output messages from a topic
find Output a list of available topics of a given type
hz Print the average publishing rate to screen
info Print information about a topic
list Output a list of available topics
pub Publish a message to a topic
type Print a topic's type
Call `ros2 topic <command> -h` for more detailed usage.
(base) shl@zhihui-mint:~$
1、查看topic的频率
ros2 topic hz /topic1
2、打印topic的数据message
ros2 topic echo /topic1
3、查看topic的frame_id
ros2 topic echo /topic1 | grep frame_id