Ubuntu20.04同时安装ROS1和ROS2共存
文章目录
每版的Ubuntu系统版本都有与之对应ROS版本的,每一版ROS都有其对应版本的Ubuntu版本,不可随便装,ubuntu20.04对应ROS1 noetic和ROS2 foxy ROS2 Galactic版本。当然最方便的是使用鱼香ROS一键安装:
wget http://fishros.com/install -O fishros && . fishros
但我们接下来使用手动安装。
一、换源
老规矩,先换源
1.1 通过软件更新
打开软件更新
选择Download from->Other,找到China,选择源。关闭界面等待自动更新源
1.2 通过修改源文件(建议)
(1)备份源文件:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.old
(2)打开文件:
sudo gedit /etc/apt/sources.list
(3)换源:推荐清华或者阿里云的源(根据自己的网去尝试,选择最快的一个):清华源地址,选择Ubuntu版本(查看ubuntu版本命令:cat /etc/issue
)并复制源内容,覆盖sources.list
文件原来的内容:
(4)换过源之后要更新:
sudo apt-get update
二、安装ROS1(noetic)
参考 详细介绍如何在ubuntu20.04中安装ROS系统,超快完成安装(最新版教程) 讲解的非常详细,安装过程中很多错误都给出了解决方法,这里写出大致流程
(1)添加ROS软件源
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)添加密钥
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
(3)更新软件源
sudo apt update
以上有问题换用:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F42ED6FBAB17C654
(4)安装ROS noetic
sudo apt install ros-noetic-desktop-full
(5)初始化rosdep
sudo rosdep init
- 如果找不到命令:
sudo apt install python-rosdep2
# 或者
sudo apt install python3-rosdep2
ERROR: cannot download default sources list from:https : //raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d/20-default.listWebsite may be down.
国外的网站无法链接,有以下三种办法(推荐第三种,我新发现的方法):
- 有条件就科学上网
- 修改hosts
sudo gedit /etc/hosts
打开的hosts文件中添加如下的内容,然后保存退出
199.232.28.133 raw.githubusercontent.com
如果依然出现ERROR: cannot download default sources list from
,访问 https://www.ipaddress.com/ 查询 raw.githubusercontent.com 的ip地址
将199.232.28.133 raw.githubusercontent.com换为:
185.199.108.133 raw.githubusercontent.com
ERROR: default sources list file already exists: /etc/ros /rosdep/sources.list.d/20-default.listPlease delete if you wish to re-initialize
sudo rm /etc/ros/rosdep/sources.list.d/20-default.list
然后
rosdep update
手动模拟 rosdep init
sudo mkdir -p /etc/ros/rosdep/sources.list.d/
sudo curl -o /etc/ros/rosdep/sources.list.d/20-default.list https://mirrors.tuna.tsinghua.edu.cn/github-raw/ros/rosdistro/master/rosdep/sources.list.d/20-default.list
为 rosdep update 换源
export ROSDISTRO_INDEX_URL=https://mirrors.tuna.tsinghua.edu.cn/rosdistro/index-v4.yaml
# 后面的参数是为了不跳过ROS版本
rosdep update --include-eol-distros
每次 rosdep update 之前,均需要增加该环境变量为了持久化该设定,可以将其写入 .bashrc 中:
echo 'export ROSDISTRO_INDEX_URL=https://mirrors.tuna.tsinghua.edu.cn/rosdistro/index-v4.yaml' >> ~/.bashrc
(6)安装rosinstall
sudo apt install python3-rosinstall python3-rosinstall-generator python3-wstool
(7)配置环境
sudo gedit ~/.bashrc
# 末尾添加
source /opt/ros/noetic/setup.bash
# 保存退出
source ~/.bashrc
(8)验证
roscore
Command ‘roscore’ not found, but can be installed with:sudo apt install python3-roslaunch
,按照提示安装:
sudo apt install python3-roslaunch
Resorce not found: roslaunch
说明之前的安装没有安装全,继续安装
sudo apt install ros-noetic-desktop-full
(9)小乌龟测试ROS:
roecore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
三、安装ROS2(foxy)
参考ubuntu20.04安装ros2,并与ros1共存
(1)设置编码UTF-8
终端输入:locale
检查是否支持 UTF-8
如果不支持:
sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
export LANG=en_US.UTF-8
(2)检查是否启用ubuntu Universe存储库
apt-cache policy | grep universe
## 输出应如下:
500 http://security.ubuntu.com/ubuntu focal-security/universe i386 Packages
release v=20.04,o=Ubuntu,a=focal-security,n=focal,l=Ubuntu,c=universe,b=i386
500 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages
release v=20.04,o=Ubuntu,a=focal-security,n=focal,l=Ubuntu,c=universe,b=amd64
注意使用1.2换源已经默认启用,但是界面上并没有勾选,不用管,若没有启用不会有输出,则:
sudo apt install software-properties-common
sudo add-apt-repository universe
(3)设置密钥
sudo apt update && sudo apt install curl gnupg2 lsb-release
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(source /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
- 如果报错
ailed to connect to raw.githubusercontent.com port 443 after 13 ms: 拒绝连接
,参考上一节的方法在/etc/hosts
文件中添加raw.githubusercontent.com 的IP; - 如果没有反应就多等一会
(4)安装
sudo apt update
# 推荐桌面版,注意安装时间较长,耐心等待
sudo apt install ros-foxy-desktop
(5)配置环境
echo "source /opt/ros/foxy/setup.bash" >> ~/.bashrc
source .bashrc
(6)官方demo测试
ros2 run demo_nodes_cpp talker
ros2 run demo_nodes_cpp listener
(7)小乌龟,不再需要master
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key
四、安装ROS2(Galactic)
为了调试Autoware.universe,必须要安装Galactic,如果需要首先卸载foxy
sudo apt remove ros-foxy-* && sudo apt autoremove
(1)测试语言环境,上一章已经处理;
(2)检查是否启用ubuntu Universe存储库,上一章已经处理;
(3)添加 ROS 2 apt 存储库
sudo apt update && sudo apt install curl
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
(4)安装
sudo apt update
sudo apt install ros-galactic-desktop
sudo apt install ros-galactic-ros-base
sudo apt install ros-dev-tools
(5)配置环境
echo "source /opt/ros/galactic/setup.bash" >> ~/.bashrc
source .bashrc
(6)小乌龟验证
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key
五、ROS1和ROS2混合配置环境
参考Ubuntu20.04 ROS1和ROS2同时安装及使用
如果只安装一个版本的ROS,这一节不需要再进行,否则需要ROS1和ROS2混合配置
sudo gedit ~/.bashrc
打开.bashrc文件,删除前面两节的配置内容:
source /opt/ros/noetic/setup.bash
source /opt/ros/foxy/setup.bash
source /opt/ros/galactic/setup.bash
后在末尾加上如下代码,完成环境变量设置,在使用过程中可以根据需要切换ros版本,记得每次打开一个新的终端都需要通过按键“1”或“2”来选择ros版本(galactic同理)
echo "ros noetic(1) or ros2 foxy(2)?"
read edition
if [ "$edition" -eq "1" ];then
source /opt/ros/noetic/setup.bash
else
source /opt/ros/foxy/setup.bash
fi
六、ROS常用命令以及区别
6.1 常用命令
(1)编译
# ROS1
catkin_create_pkg <package_name> [depend1] [depend2] [depend3]...
# catkin_create_pkg test_pkg std_msgs rospy roscpp
catkin_make
catkin_make -DCATKIN_WHITELIST_PACKAGES=""
# ROS2
colcon build
# 只编译指定包
colcon build --packages-select PACKAGE_NAME
# 忽略指定包
colcon build --packages-ignore PACKAGE_NAME
# 遇到编译错误继续编译其他模块
colcon build --continue-on-error
(2)工作空间配置bashrc
source ~/catkin_ws/devel/setup.bash
source ~/colcon_ws/install/setup.bash
(3)terminal 命令
rosrun --> ros2 run
rosnode --> ros2 node
roslaunch --> ros2 launch
rosparam --> ros2 param
rospkg --> ros2 pkg
rosservice --> ros2 service
rossrv --> ros2 srv
rostopic --> ros2 topic
rosaction --> ros2 action
6.2 其他命令(我的笔记)
(1)通过 topic_tools/throttle 改变话题的发布频率、发布带宽
# 参数解释:intopic: 订阅的输入话题、msgs_per_sec: 每秒允许通过的最大messages数、outtopic: 发布的输出话题 (default: intopic_throttle)
throttle messages <intopic> <msgs_per_sec> [outtopic]
# 举例,将原来200Hz的IMU数据降低至100
rosrun topic_tools throttle messages /imu_raw 100 /imu_raw_low_freq
roslaunch写法:
<node pkg="topic_tools" type="throttle" name="camera_throttle" args="messages /imu_raw 100 /imu_raw_low_freq" output="screen" respawn="true"/>
(2)bag的录制和播放
# 1 录制指定话题数据并保存,-O需要大写
rosbag record -O <bagname> <topic_name1> <topic_name2> <topic_name3> ...
# 2 查看数据包中包含的话题名称,话题数量,话题消息类型等信息
rosbag info <bagname>
# 3 bag播放
rosbag play <bagname> # 回放指定bag
rosbag play * # 回放当前目录下所有bag
rosbag play -s n <bagname> # 从指定时间(n = 指定时间s)开始回放bag
rosbag play -s n -u m <bagname> # 从指定时间(n = 开始时间s)开始播放指定时间(m = 指定播放时间s)的bag
rosbag play -r n <bagname> # 按指定倍率(n = 倍率)回放bag
rosbag play <topic_name1>:=<topic_name2> # 某个话题重命名播放
# 4 回放不成功,提示 reindex ,则:
rosbag reindex <bagname>
(3)rostopic echo --noarr查看激光雷达数据详细信息(如frame_id)
用rostopic echo /激光雷达话题名查看激光雷达返回的数据,除了数字什么也显示不出来。想要查看激光雷达数据的frame_id等信息,加上–noarr参数即可,排除话题数据中的数组信息。
rostopic echo --noarr /velodyne_points
(4)通过rqt_bag命令将bag进行可视化
rqt_bag <bagname>