• ROS安装


    1:检查Unbuntu的软件和更新源

    在这里插入图片描述

    2:设置Ros的下载源

    sudo apt update
    
    # 将 sources.list 拷贝到桌面
    cp /etc/apt/sources.list ~/Desktop 
    
    # 打开 sources.list 进行编辑
    sudo gedit /etc/apt/sources.list
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    20.04LTS
    # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
    
    # 预发布软件源,不建议启用
    # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.1设置中科大源: 在终端输入如下命令后回车

    sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.ustc.edu.cn/ros/ubuntu/ `lsb_release -cs` main" > /etc/apt/sources.list.d/ros-latest.list'
    
    ##### qinghua
    
    ```bash
    sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ $DISTRIB_CODENAME main" > /etc/apt/sources.list.d/ros-latest.list'
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.2 设置公钥: 在终端输入如下命令后回车

    sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
    
    
    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F42ED6FBAB17C654
     
     
    sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
     
     
    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.3 更新最新可用软件包列表:在终端输入以下命令后回车

    sudo apt-get update 
    sudo apt-get upgrade
    
    • 1
    • 2

    3.安装ROS

    在终端输入以下命令后回车

    Ubuntu 18.04
    sudo apt-get install ros-melodic-desktop-full
    sudo apt-get install ros-melodic-rqt*
    
    • 1
    • 2
    Ubuntu 20.04
    sudo apt install ros-noetic-desktop-full
    
    • 1

    4.设置环境变量

    在终端输入以下命令后回车

    echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
    source ~/.bashrc
    
    • 1
    • 2

    5.下载其他功能组件

    在终端输入以下命令后回车

    sudo apt install python-rosdep python-rosinstall python-rosinstall-generator python-wstool build-essential
    
    • 1

    20.04

    sudo apt install python3-rosdep2
    
    • 1

    6.rosdep init 初始化

    6.1.在终端输入以下命令后回车
    sudo chmod 777 /etc
    sudo rosdep init
    
    
    • 1
    • 2
    • 3
    6.2.常见报错

    报错:
    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.

    6.3.原因:初始化的这一步将会从外国的网站raw.githubusercontent.com拉取一些信息和文件。这个网站现在被墙了,导致这一步出错。

    解决: 修改hosts文件

    sudo gedit /etc/hosts
    
    • 1
    把下面这些地址拷贝到hosts文件末尾,然后保存。
    
        192.30.253.118 gist.github.com
        185.199.110.153 github.io
        151.101.113.194 github.global.ssl.fastly.net
        52.216.227.168 github-cloud.s3.amazonaws.com
        52.74.223.119 github.com
        199.232.28.133 avatars1.githubusercontent.com
        199.232.28.133 avatars2.githubusercontent.com
        199.232.28.133 avatars0.githubusercontent.com
        199.232.28.133 avatars3.githubusercontent.com
        199.232.28.133 raw.githubusercontent.com
        199.232.28.133 user-images.githubusercontent.com
        199.232.28.133 avatars.githubusercontent.com
        199.232.28.133 github.map.fastly.net
        199.232.28.133 avatars7.githubusercontent.com
        54.239.31.69 aws.amazon.com
        54.239.30.25 console.aws.amazon.com
        54.239.96.90 ap-northeast-1.console.aws.amazon.com
        54.240.226.81 ap-southeast-1.console.aws.amazon.com
        54.240.193.125 ap-southeast-2.console.aws.amazon.com
        54.239.54.102 eu-central-1.console.aws.amazon.com
        177.72.244.194 sa-east-1.console.aws.amazon.com
        176.32.114.59 eu-west-1.console.aws.amazon.com
        54.239.31.128 us-west-1.console.aws.amazon.com
        54.240.254.230 us-west-2.console.aws.amazon.com
        54.239.38.102 s3-console-us-standard.console.aws.amazon.com
        54.231.49.3 s3.amazonaws.com
        52.219.0.4 s3-ap-northeast-1.amazonaws.com
        54.231.242.170 s3-ap-southeast-1.amazonaws.com
        54.231.251.21 s3-ap-southeast-2.amazonaws.com
        54.231.193.37 s3-eu-central-1.amazonaws.com
        52.218.16.140 s3-eu-west-1.amazonaws.com
        52.92.72.2 s3-sa-east-1.amazonaws.com
        54.231.236.6 s3-us-west-1.amazonaws.com
        54.231.168.160 s3-us-west-2.amazonaws.com
        52.216.80.48 github-cloud.s3.amazonaws.com
        54.231.40.3 github-com.s3.amazonaws.com
        52.216.20.171 github-production-release-asset-2e65be.s3.amazonaws.com
        52.216.228.168 github-production-user-asset-6210df.s3.amazonaws.com
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    在这里插入图片描述

    执行sudo gedit /etc/resolv.conf,将原有的nameserver这一行注释,并添加以下两行:

    sudo gedit /etc/resolv.conf
    
    • 1
    nameserver 8.8.8.8 #google域名服务器
    nameserver 8.8.4.4 #google域名服务器
    
    • 1
    • 2

    再次执行sudo rosdep init,这次会报一个问题,就是文件已存在:

    sudo rosdep init
    
    • 1
    ERROR: default sources list file already exists: /etc/ros/rosdep/sources.list.d/20-default.list
    Please delete if you wish to re-initialize
    
    • 1
    • 2

    这时候直接删除这个文件sudo rm /etc/ros/rosdep/sources.list.d/20-default.list,再次执行sudo rosdep init,我至今操作过很多次,没有出现不成功的状态,如果实在不成功,试试换手机热点,多执行几次,最后输出如下信息就OK了:

    sudo rm /etc/ros/rosdep/sources.list.d/20-default.list
    
    • 1
    sudo rosdep init
    
    • 1

    显示如下信息,则rosdep初始化成功

        wrote /etc/ros/rosdep/sources.list.d/20-default.list
        Recommended: please run
        rosdep update
    
    • 1
    • 2
    • 3

    7:解决 rosdep update 问题

    常见报错信息就是各种The read operation timed out.。
    解决,直接从对应网址保存所有需要的文件,然后把网络路径改成本地文件路径:
    
    • 1
    • 2
    链接: https://pan.baidu.com/s/1JGFlI0hYo6tVEicQXnSj7w
    提取码: xiao 
    
    • 1
    • 2
    下载所有的文件,整理好文件夹,对于主要的yaml文件的路径都在20-default.list这个里面有,基本套路
    就是https://raw.githubusercontent.com/ros/rosdistro/master/XXX后面的xxx根据不同文件替换一下,
    打开index-v4.yaml能看到所有需要的其他子文件夹,yaml文件和gz 压缩包:
    
    • 1
    • 2
    • 3

    1:将下载的文件夹移动到etc/ros/ros_github/为你自己提取安放的路径

    在这里插入图片描述

    直接输进浏览器地址栏,都能直接打开对应的yaml和.gz文件,ctrl+s保存即可,最后形成这么一个文件夹,文件夹的名字自己随便起都可以,里面的子文件夹一定和index-v4.yaml保持一致,如下图所示(我整理好了,大家也可以直接用)。
    在这里插入图片描述

    修改 index-v4.yaml的路径和20-default.list文件里面的路径:

    2.7

     sudo gedit /usr/lib/python2.7/dist-packages/rosdistro/__init__.py
    
    • 1

    3.0 cpp

     sudo gedit /usr/lib/python3/dist-packages/rosdistro/__init__.py
    
    • 1

    比如我最后放那些yaml的文件夹放在/etc/ros/ros_github,将网址地址改为file:///etc/ros/ros_github/index-v4.yaml如下图所示
    在这里插入图片描述
    2:修改 /etc/ros/rosdep/sources.list.d/20-default.list文件。

    sudo gedit /etc/ros/rosdep/sources.list.d/20-default.list
    
    • 1

    把里面指向网址的路径改为本地,比如我最后放那些yaml的文件夹放在/etc/ros/ros_github,

    在这里插入图片描述
    修改如下:

    # os-specific listings first
    yaml file:///etc/ros/ros_github/osx-homebrew.yaml osx
    # generic
    yaml file:///etc/ros/ros_github/base.yaml
    yaml file:///etc/ros/ros_github/python.yaml
    yaml file:///etc/ros/ros_github/ruby.yaml
    gbpdistro file:///etc/ros/ros_github/fuerte.yaml fuerte
    # newer distributions (Groovy, Hydro, ...) must not be listed anymore, they are being fetched from the rosdistro index.yaml instead
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3:再次执行rosdep update命令

    检查安装情况:运行小海龟和rviz

    建议再执行以下环境配置命令(有的同学可能漏了,且再来一次无妨):
    如果出现下面的错误,那就多rosdep update几次,因为这个问题的本质是网络不通畅导致的。
    ERROR: unable to process source [file:///etc/ros/ros_github/fuerte.yaml]:
    Failed to download target platform data for gbpdistro:
    (‘The read operation timed out’,)
    实在不行就参考https://blog.csdn.net/weixin_53660567/article/details/120607176

    echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
    source ~/.bashrc
    
    • 1
    • 2

    4:再次执行

    rosdep update
    
    • 1

    如过报错,则从新开一个命令框进行更新

    8:创建小海龟

    1.终端输入命令回车:

    roscore
    
    • 1

    出现以上提示则代表安装成功。

    在这里插入图片描述输入以下命令,把没安装的安装一下就好了(如果显示有几个安装包无法下载,是因为网络的原因重新输入以下命令就行了)

    	sudo apt install ros-noetic-desktop-full
    
    • 1

    2.检查基础功能
    在终端成功输入roscore后,开启第二个终端输入

    	rosrun turtlesim turtlesim_node
    
    • 1

    3:会出现一个小海龟,最后开启第三个终端输入

    rosrun turtlesim turtle_teleop_key 
    
    • 1

    参考:https://blog.csdn.net/sinat_25923849/article/details/1079764

    9 安装ros2

    添加源
    检查是否已已启用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
    • 3
    • 4
    • 5
    • 6

    如果没有看到上面的输出信息,执行下面这两条命令

    sudo apt install software-properties-common
    sudo add-apt-repository universe
    
    • 1
    • 2

    (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
     
    
    • 1
    • 2
    • 3
    • 4
    报错:E: 仓库 “http://ppa.launchpad.net/rock-core/qt4/ubuntu bionic Release” 没有 Release 文件。

    在这里插入图片描述

    我的是qt4所以我删除了
    sudo rm rock-core-ubuntu-qt4-bionic.list.save
    sudo rm rock-core-ubuntu-qt4-bionic.list
    
    • 1
    • 2

    在这里插入图片描述

    如果没有设置密钥,在执行ros2安装命令的时候会出现无法定位到软件包的问题。
    如果遇到报错“ailed to connect to raw.githubusercontent.com port 443 after 13 ms: 拒绝连接”
    解决方法:在/etc/hosts文件中添加如下IP
    sudo gedit /etc/hosts
    
    #这两行是原来就有的
    127.0.0.1	localhost
    127.0.1.1	iron-virtual-machine
    ###这一行是需要新增加的
    185.199.108.133  raw.githubusercontent.com
         
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    安装

    sudo apt update
    sudo apt upgrade
    ## 推荐桌面版,比较推荐。
    sudo apt install ros-eloquent-desktop
    
    • 1
    • 2
    • 3
    • 4

    设置环境变量

    这是~/.bashrc中原先的ros环境变量
    sudo gedit ~/.bashrc
    
    • 1
    添加下列代码:
    echo "ROS noetic (1) or ROS2 foxy (2)?"
    read edition
    if [ "$edition" -eq "1" ];then
      source /opt/ros/noetic/setup.bash  #这是ros1根目录环境变量
      source ~/catkin_ws/devel/setup.bash#这是ros1工作空间环境变量
      echo using ros noetic
    else
      source /opt/ros/eloquent/setup.bash    #这是ros2系统环境变量
      source ~/colcon_ws/install/local_setup.sh #添加在这里***************
    #  echo using eloquent foxy
    fi
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    同时有ros1和ros2会有这样的提示

    在这里插入图片描述好像目前没有发现忽略这个问题出现的bug
    解决方法:

    修改环境变量,避免两个版本冲突

    ros1

    sudo vim /opt/ros/melodic/share/ros_environment/catkin_env_hook/1.ros_distro.sh
    
    • 1

    注释

    # generated from ros_environment/env-hooks/1.ros_distro.sh.in
    #if [ -n "$ROS_DISTRO" -a "$ROS_DISTRO" != "melodic" ]; then
    #  echo "ROS_DISTRO was set to '$ROS_DISTRO' before. Please make sure that the environment does not mix paths from different distributions."
    #fi
    #export ROS_DISTRO=melodic
    
    • 1
    • 2
    • 3
    • 4
    • 5

    ros2

    sudo vim /opt/ros/eloquent/share/ros_environment/environment/1.ros_distro.sh
    
    • 1

    也注释

    # generated from ros_environment/env-hooks/1.ros_distro.sh.in
    #if [ -n "$ROS_DISTRO" -a "$ROS_DISTRO" != "dashing" ]; then
    #  echo "ROS_DISTRO was set to '$ROS_DISTRO' before. Please make sure that the environment does not mix paths from different distributions."
    #fi
    #export ROS_DISTRO=dashing
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    解决功能包依赖

    sudo apt install python3-pip
    sudo pip3 install rosdepc      -i https://pypi.doubanio.com/simple
    sudo rosdepc init
    rosdepc update
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    sudo rosdep fix-permissions
    
    • 1

    安装colcon编译工具

    sudo apt-get install python3-colcon-ros
    
    • 1

    验证安装结果
    1、查看环境变量printenv | grep -i ROS

    printenv | grep -i ROS
    LD_LIBRARY_PATH=/opt/ros/eloquent/opt/yaml_cpp_vendor/lib:/opt/ros/eloquent/opt/rviz_ogre_vendor/lib:/opt/ros/eloquent/lib/x86_64-linux-gnu:/opt/ros/eloquent/lib
    AMENT_PREFIX_PATH=/opt/ros/eloquent
    ROS_VERSION=2
    ROS_LOCALHOST_ONLY=0
    ROS_PYTHON_VERSION=3
    PYTHONPATH=/opt/ros/eloquent/lib/python3.6/site-packages
    PATH=/opt/ros/eloquent/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
    ROS_DISTRO=eloquent
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    运行经典小乌龟

    安装turtlesim包为您的ROS 2分配:
    sudo apt update
     
    sudo apt install ros-eloquent-turtlesim
    
    • 1
    • 2
    • 3
    检查包:
    ros2 pkg executables turtlesim
    
    • 1

    在这里插入图片描述
    ros2命令的自动补全

    sudo apt install python3-argcomplete
    
    • 1
    turtlesim开始,输入以下命令
    ros2 run turtlesim turtlesim_node
    
    • 1

    新开一个窗口

    ros2 run turtlesim turtle_teleop_key
    
    • 1

    在打开一个窗口,依次使用下面命令,和ROS1下就很多类似。

    ros2 node list
    ros2 topic list
    ros2 service list
    ros2 action list
    
    • 1
    • 2
    • 3
    • 4

    打印输出:

    ros2 node list
        /teleop_turtle
        /turtlesim
    ros2 topic list
        /parameter_events
        /rosout
        /turtle1/cmd_vel
        /turtle1/color_sensor
        /turtle1/pose
    ros2 service list
        /clear
        /kill
        /reset
        /spawn
        /teleop_turtle/describe_parameters
        /teleop_turtle/get_parameter_types
        /teleop_turtle/get_parameters
        /teleop_turtle/list_parameters
        /teleop_turtle/set_parameters
        /teleop_turtle/set_parameters_atomically
        /turtle1/set_pen
        /turtle1/teleport_absolute
        /turtle1/teleport_relative
        /turtlesim/describe_parameters
        /turtlesim/get_parameter_types
        /turtlesim/get_parameters
        /turtlesim/list_parameters
        /turtlesim/set_parameters
        /turtlesim/set_parameters_atomically
    ros2 action list
        /turtle1/rotate_absolute
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    实验通信

    开一个终端执行

    ros2 run demo_nodes_cpp talker
    
    • 1

    开另外一个终端执行

    ros2 run demo_nodes_py listener
    
    • 1

    使用ros2

    1:创建ros2工作空间

    mkdir -p ~/ros2_ws/src
    cd ~/ros2_ws/src
    
    • 1
    • 2

    2:创建功能包

    ros2 pkg create --build-type ament_cmake testcpp   # c++功能包
    ros2 pkg create --build-type ament_python testpython  # python功能包
    
    • 1
    • 2

    这里与ros1有区别,创建功能包需要指定语言

    robot@ubuntu:~/ros2_ws/src$ ros2 pkg create --build-type ament_python testpython
        going to create a new package
        package name: testpython
        destination directory: /home/robot/ros2_ws/src
        package format: 3
        version: 0.0.0
        description: TODO: Package description
        maintainer: ['robot ']
        licenses: ['TODO: License declaration']
        build type: ament_python
        dependencies: []
        creating folder ./testpython
        creating ./testpython/package.xml
        creating source folder
        creating folder ./testpython/testpython
        creating ./testpython/setup.py
        creating ./testpython/setup.cfg
        creating folder ./testpython/resource
        creating ./testpython/resource/testpython
        creating ./testpython/testpython/__init__.py
        creating folder ./testpython/test
        creating ./testpython/test/test_copyright.py
        creating ./testpython/test/test_flake8.py
        creating ./testpython/test/test_pep257.py
    robot@ubuntu:~/ros2_ws/src$ ros2 pkg create --build-type ament_cmake testcpp
        going to create a new package
        package name: testcpp
        destination directory: /home/robot/ros2_ws/src
        package format: 3
        version: 0.0.0
        description: TODO: Package description
        maintainer: ['robot ']
        licenses: ['TODO: License declaration']
        build type: ament_cmake
        dependencies: []
        creating folder ./testcpp
        creating ./testcpp/package.xml
        creating source and include folder
        creating folder ./testcpp/src
        creating folder ./testcpp/include/testcpp
        creating ./testcpp/CMakeLists.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    3:编译

    cd ~/ros2_ws/
    colcon build
    
    • 1
    • 2

    参考:https://blog.csdn.net/cau_weiyuhu/article/details/128666548

  • 相关阅读:
    2022牛客蔚来杯第三场A C J
    力扣 889. 根据前序和后序遍历构造二叉树
    基于 .NET 6 的轻量级 Webapi 框架 FastEndpoints
    四年 android开发 转 web前端 开发3个月的一点点工作、生活感悟
    2024年数学建模美赛详细总结以及经验分享
    java常用的几个图片处理工具对Tiff文件的支持
    Oracle 高可用 阅读笔记
    JS(二)
    debug技巧之本地调试
    下载zip源码并使用交叉编译工具进行编译
  • 原文地址:https://blog.csdn.net/qq_46107892/article/details/127751617