一、apt方式安装
安装
说起ROS,可能大家现在或多或少都有所了解。现如今世界机器人发展之迅猛犹如几十年前计算机行业一样,机器人也逐渐进入到千家万户,大到工业机器人,小到家用的服务型机器人,各式各样,为各种人们生活所需的机器人以计算机技术的发展为基础的机器人也是如雨后春笋。机器人可主要分为硬件层和软件层两个大的主要方向。每一种类型的机器人都需要以硬件的实际情况编写符合用户需要的功能,渐渐的人们发现,这样的机器人代码的复用率很低,大大阻碍了机器人的发展。因此ROS便是为机器人在研发的过程中的代码复用提供支持.ROS是Robot Operating System(机器人操作系统)的简称.ROS开始于2007年,在斯坦福大学人工智能实验室斯坦福AI机器人项目的支持下开发了ROS。从2010年3月2日发布的第一版ROS Box Turtle至今(截止到2018年8月)已有12个版本。对 ROS 兼容性最好的当属 Ubuntu 操作系统了。其中三个长期支持版本,并对应着的Ubuntu的三个LTS版本具体如下:
ROS版本 发行时间 截止支持时间 对应的Ubuntu的版本
ROS Indigo 2014年7月22 2019年4月 Ubuntu 14.04
ROS Kinetic 2016年5月23 2021年4月 Ubuntu 16.04
ROS Melodic 2018年5月23 2023年4月 Ubuntu 18.04
ROS Noetic 2020年5月 2025年5月 Ubuntu 20.04
ROS支持的Python,C ++,JAVA等编程语言。因为ROS主要支持Ubuntu的操作系统,因此本教程也是在Ubuntu的系统下安装的。这里比较建议安装的Ubuntu系统或者的Windows的双系统,这样可以更便于ROS学习接 下来为大家详细讲解ROS的安装。
(1)配置的Ubuntu的系统
“软件和更新”界面中,打开后按照下图进行配置(确保你的"restricted", "universe," 和 "multiverse."前是打上勾的),最好将源设置为中国。
(2)打开终端,设置计算机可以从ROS.org网站上接收软件。
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
(3)设置秘钥
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
设置结果
leisp@leisp-desktop:~$ sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
Executing: /tmp/apt-key-gpghome.OUZemtSxm4/gpg.1.sh --keyserver hkp://keyserver.ubuntu.com:80 --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
gpg: 密钥 F42ED6FBAB17C654:“Open Robotics <info@osrfoundation.org>”未改变
gpg: 合计被处理的数量:1
gpg: 未改变:1
leisp@leisp-desktop:~$
(4)安装
现在读者已经将ROS版本库地址放在代码列表中,然后更新下软件包。
重新定向ROS服务器
sudo apt-get update
sudo apt-get upgrade
然后选择你想安装“多少部分”的ROS:
-
完整桌面版安装(Desktop-Full,推荐):除了桌面版的全部组件外,还包括2D/3D模拟器(simulator)和2D/3D 感知包(perception package)。
sudo apt-get install ros-melodic-desktop-full
类似的“E: 无法定位软件包 ros-melodic-desktop-full”的错误,是由于ROS的版本不对应导致的,每个不同的ubuntu系统对应着不同的ROS版本,如果装错了就会出现上述问题,在Ubuntu18.04.1环境下可以安装的是melodic版本的,安装教程可以参考官网安装教程,ROS有Melodic、Lunar、Kinetic不同的种类对应着不同的ubuntu版本,
Melodic 主要对应:Ubuntu Artful (17.10), Bionic (18.04 LTS) 以及Debian Stretch
Kinetic 主要对应:Ubuntu Wily (15.10) and Ubuntu Xenial (16.04 LTS)
安装完成后,可以用下面的命令来查看可使用的包:
apt-cache search ros-melodic
(5)初始化ROS
在使用ROS之前,必须先安装和初始化rosdep命令行工具。这样便可以使你轻松的安装库和源代码时的系统依赖。与此同时,ROS中的一些核心组件也需要rosdep,因此rosdep默认安装在ROS中。可以使用下面的命令安装和初始化rosdep。
sudo apt install python-rosdep
sudo apt install rosbash
sudo rosdep init
报错,但可以ping通 raw.githubusercontent.com:
leisp@leisp-desktop:~$ sudo rosdep init
ERROR: cannot download default sources list from:
https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d/20-default.list
Website may be down.
leisp@leisp-desktop:~$ ping raw.githubusercontent.com
PING raw.githubusercontent.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.022 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.034 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.049 ms
解决方法
该解决方法是针对由于以下两个网址无法正常访问,但可以ping通,于是修改hosts文件,加入以下两个网址的IP地址实现访问。
sudo gedit /etc/hosts
#199.232.28.133 raw.githubusercontent.com
151.101.84.133 raw.githubusercontent.com
151.101.228.133 raw.github.com
修改完成后,在终端执行
sudo rosdep init
rosdep update
leisp@leisp-desktop:/etc$ sudo rosdep init
Wrote /etc/ros/rosdep/sources.list.d/20-default.list
Recommended: please run
rosdep update
leisp@leisp-desktop:/etc$ rosdep update
reading in sources list data from /etc/ros/rosdep/sources.list.d
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/osx-homebrew.yaml
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/base.yaml
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/python.yaml
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/ruby.yaml
ERROR: unable to process source [https://raw.githubusercontent.com/ros/rosdistro/master/releases/fuerte.yaml]:
Failed to download target platform data for gbpdistro:
<urlopen error [Errno 111] Connection refused>
Query rosdistro index https://raw.githubusercontent.com/ros/rosdistro/master/index-v4.yaml
ERROR: error loading sources list:
<urlopen error <urlopen error timed out> (https://raw.githubusercontent.com/ros/rosdistro/master/index-v4.yaml)>
输入指令rosdep update,重复试还是超时报错,解决办法:
参考这篇博客:https://blog.csdn.net/nanianwochengshui/article/details/105702188
第一步
首先进入github.com/ros/rosdistro去把这个包下载下来。
第二步
修改这个包中rosdep/source.list.d/下的文件20-default.list,将这个文件中指向raw.githubusercontent.com的url地址全部修改为指向本地文件的地址,也就是该下载好的包的地址:以下是我修改好的样例:
# os-specific listings first
yaml file:///home/leisp/rosdistro/rosdep/osx-homebrew.yaml osx
# generic
yaml file:///home/leisp/rosdistro/rosdep/base.yaml
yaml file:///home/leisp/rosdistro/rosdep/python.yaml
yaml file:///home/leisp/rosdistro/rosdep/ruby.yaml
gbpdistro file:///home/leisp/rosdistro/releases/fuerte.yaml fuerte
# newer distributions (Groovy, Hydro, ...) must not be listed anymore, they are being fetched from the rosdistro index.yaml instead
***注意:***在py语言中:url本地文件地址格式是:file://+文件地址,后面更改其他文件中地址的时候也一样。
第三步
修改/usr/lib/python2.7/dist-packages/rosdep2/main.py中的默认的url的地址,但是经过代码阅读,该文件中并不存在指向raw.githubusercontent.com的代码。而且经过对代码的分析,提示报错的代码就在该文件中
def command_init(options):
try:
data = download_default_sources_list()
except URLError as e:
print('ERROR: cannot download default sources list from:\n%s\nWebsite may be down.' % (DEFAULT_SOURCES_LIST_URL))
return 4
except DownloadFailure as e:
print('ERROR: cannot download default sources list from:\n%s\nWebsite may be down.' % (DEFAULT_SOURCES_LIST_URL))
由于其调用了download_default_sources_list()这个函数
而该函数就在/usr/lib/python2.7/dist-packages/rosdep2该文件夹下面的sources_list.py文件里面。而这个文件里面的代码则进行了访问raw.githubusercontent.com的操作,因此修改该默认url即可。
# default file to download with 'init' command in order to bootstrap
# rosdep
DEFAULT_SOURCES_LIST_URL = 'file:///home/leisp/rosdistro/rosdep/sources.list.d/20-default.list'
# seconds to wait before aborting download of rosdep data
总结:该步实际并不是修改 main.py 文件里面默认url的指向地址,而是修改同文件夹下的sources_list.py文件里面的代码
第四步
修改以下两个文件里面的代码:
/usr/lib/python2.7/dist-packages/rosdep2/rep3.py
/usr/lib/python2.7/dist-packages/rosdistro/__init__.py
下面是我分别修改后的样例:
/usr/lib/python2.7/dist-packages/rosdep2/rep3.py文件:
# location of targets file for processing gbpdistro files
REP3_TARGETS_URL = 'file:///home/leisp/rosdistro/releases/targets.yaml'
# seconds to wait before aborting download of gbpdistro data
/usr/lib/python2.7/dist-packages/rosdistro/__init__.py的文件:
# index information
DEFAULT_INDEX_URL = 'file:///home/leisp/rosdistro/index-v4.yaml'
def get_index_url():
然后进行
sudo rosdep init
rosdep update
leisp@leisp-desktop:~/catkin/catkin_ws$ rosdep update
reading in sources list data from /etc/ros/rosdep/sources.list.d
Hit file:///home/leisp/rosdistro/rosdep/osx-homebrew.yaml
Hit file:///home/leisp/rosdistro/rosdep/base.yaml
Hit file:///home/leisp/rosdistro/rosdep/python.yaml
Hit file:///home/leisp/rosdistro/rosdep/ruby.yaml
Hit file:///home/leisp/rosdistro/releases/fuerte.yaml
Query rosdistro index file:///home/leisp/rosdistro/index-v4.yaml
Skip end-of-life distro "ardent"
Skip end-of-life distro "bouncy"
Skip end-of-life distro "crystal"
Add distro "dashing"
Skip end-of-life distro "eloquent"
Add distro "foxy"
Skip end-of-life distro "groovy"
Skip end-of-life distro "hydro"
Skip end-of-life distro "indigo"
Skip end-of-life distro "jade"
Add distro "kinetic"
Skip end-of-life distro "lunar"
Add distro "melodic"
Add distro "noetic"
Add distro "rolling"
updated cache in /home/leisp/.ros/rosdep/sources.cache
(6)初始化环境变量
你已经成功的安装了ROS系统了。但是为了每次打开新的终端不用执行脚本来配置环境变量,需要在.bashrc中的脚本文件结束时添加脚本,用下面这个命令来完成。
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
要是配置生效,必须使用下面的命令来执行这个文件
source ~/.bashrc
上面两句非常重要,很多小伙伴在日常的开发过程中,有的找不到 Package,有的找不到node, 很多情况下都是没有添加source。
(7)安装rosinstall
rosinstall是一种常用的命令行工具,使你可以使用一个命令轻松下载ROS包的许多源。
sudo apt-get install python-rosinstall python-rosinstall-generator python-wstool build-essential
(8) 安装rosdep
rosdep使我们能够轻松地为要编译的源安装系统依赖性,并且是运行ROS中某些核心组件所必需的
sudo apt install python-rosdep
如何输入roscore指令,遇报错:
usrname:~$ roscore
Command 'roscore' not found, but can be installed with:
sudo apt install python-roslaunch
按照提示进行的操作,结果依然报错:
usrname:~$ sudo apt install python-roslaunch
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
有一些软件包无法被安装。如果您用的是 unstable 发行版,这也许是
因为系统无法达到您要求的状态造成的。该版本中可能会有一些您需要的软件
包尚未被创建或是它们已被从新到(Incoming)目录移出。
下列信息可能会对解决问题有所帮助:
下列软件包有未满足的依赖关系:
python-roslaunch : 依赖: python-roslib 但是它将不会被安装
E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系。
运行如下命令修复:
usrname:~$ git clone https://github.com/ros/catkin.git
usrname:~$ cd catkin
usrname:~$ git branch melodic-devel
usrname:~$ mkdir build
usrname:~$ cd build
usrname:~$ cmake ..
usrname:~$ make
usrname:~$ sudo make install
usrname:~$ cd ..
usrname:~$ sudo python2 setup.py install
##创建ROS工作空间
usrname:~$ mkdir -p catkin_ws/src
usrname:~$ cd catkin_ws
usrname:~$ catkin_make
usrname:~$ sudo apt install ros-melodic-desktop-full
usrname:~$ sudo apt install python-rosdep
usrname:~$ rosdep update
usrname:~$ source ~/.bashrc
到此为止,你已经在电脑上安装了一个较为完整的ROS系统了。
卸载ROS
sudo apt-get purge ros-*
sudo rm -rf /etc/ros
sudo gedit ~/.bashrc
二、测试
为了保险,重启一下,测试测试我们的ROS吧....
ROS是依靠终端来运行的系统。所以还需要打开终端。这里给大家演示一个基本的案例--turtlesim小海龟。运行下面的命令你便可以看到一个有小海龟的界面。
设置环境变量
source ~/catkin_ws/devel/setup.bash
然后,输入指令:
roscore
至此,说明电脑就能够运行ROS了!
再输入指令:
rosrun turtlesim turtlesim_node
该行命令需要重新打开一个终端来输入,因为你会发现当输入roscore运行后终端是无法输入有效的命令来运行的。
如何使用键盘使小海龟运动起来呢?就需要输入下面的命令。
rosrun turtlesim turtle_teleop_key
参考:
https://my.oschina.net/u/4308169/blog/3371002?hmsr=kaifa_aladdin