【ROS2 入门】虚拟机环境 ubuntu 18.04 ROS2 安装

         大家好,我是虎哥,从今天开始,我将花一段时间,开始将自己从ROS1切换到ROS2,做为有别于ROS1的版本,做了很多更新和改变,我还是很期待自己逐步去探索ROS2中的惊喜。在安装过程中我也遇到的一些坑,我自己也尝试填了一部分,未必是全部,但是相信集合大家发布的各种安装教程,你也可以顺利安装。接下来开始我们的ROS初步探索吧。

目录

一、虚拟机系统安装ubuntu 18.04 系统

二、初始化系统

三、安装ROS2 eloquent 版本

3.1 官方部署说明

3.2添加ros2 软件源

3.3安装ros-eloquent和相关依赖

3.3安装python3库

3.4环境变量设置

1、ROS2单独存在

2、ROS1和ROS2共存(我没有实践这条)

四、ROS2依赖配置

4.1解决功能包依赖

4.2安装colcon编译工具

4.3 验证安装结果

1、查看环境变量printenv | grep -i ROS

2、运行经典小乌龟

3、实验通信

五、使用ros2

5.1创建ros2工作空间

5.2创建功能包

5.3编译

一、虚拟机系统安装ubuntu 18.04 系统

开始安装

 需要等待系统安装完毕。

 我虚拟机硬件配置为:

二、初始化系统

关闭自动更新

 确认系统版本

#更新

sudo apt-get update
#更换源
sudo apt-get install -y  wget
sudo apt-get install apt-transport-https -y
#18.04
sudo cp -a /etc/apt/sources.list /etc/apt/sources.list.bak
#华为源 18.04 
sudo sed -i "s@http://.*archive.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list
sudo sed -i "s@http://.*security.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list
​
sudo apt-get update

 还有报错,这个是华为源的BUG,可以先不用理。

#安装常用工具组件
sudo apt-get install -y \
    vim \
    lrzsz \
    curl 

三、安装ROS2 eloquent 版本

3.1 官方部署说明

官网教程入口:ROS 2 Eloquent Elusor Installation — ROS 2 Documentation: Eloquent documentation

我们综合一下,国内的教程和官方教程,亲自测试了如下教程

3.2添加ros2 软件源

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 -
curl http://repo.ros2.org/repos.key | sudo apt-key add -
​
sudo sh -c 'echo "deb [arch=amd64] http://packages.ros.org/ros2/ubuntu bionic main" > /etc/apt/sources.list.d/ros2-latest.list'

3.3安装ros-eloquent和相关依赖

sudo apt update
sudo apt install ros-eloquent-desktop

 开始漫长等待

 注意,你的网还是好一点,就少出点问题,我之前切换到了我手机热点上。

 下载完后就开始安装了。

 安装完毕。

3.3安装python3库

sudo apt install -y libpython3-dev python3-pip
pip3 install -U argcomplete

安装RMW implementation(跳过 体积太大,费时间)

sudo apt update
sudo apt install ros-eloquent-rmw-connext-cpp

3.4环境变量设置

1、ROS2单独存在

echo "source /opt/ros/eloquent/setup.bash" >> ~/.bashrc
source ~/.bashrc

2、ROS1和ROS2共存(我没有实践这条)

echo "ros melodic(1) or ros2 dashing(2)?"
read edition
if [ "$edition" -eq "1" ];then
  source /opt/ros/melodic/setup.bash
  #在这里继续添加source ROS1的工作空间
else
  source /opt/ros/eloquent/setup.bash
  #source ~/ros2_ws/install/local_setup.bash
fi
推荐内容
 

四、ROS2依赖配置

4.1解决功能包依赖

sudo pip3 install rosdepc
sudo rosdepc init
rosdepc update

 如果出现如下错误:

sudo rosdep fix-permissions

4.2安装colcon编译工具

sudo apt-get install python3-colcon-ros

4.3 验证安装结果

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

2、运行经典小乌龟

Install the turtlesim package for your ROS 2 distro:

sudo apt update

sudo apt install ros-eloquent-turtlesim

Check that the package installed:

ros2 pkg executables turtlesim

To start turtlesim, enter the following command in your terminal:

ros2 run turtlesim turtlesim_node

新开一个窗口

ros2 run turtlesim turtle_teleop_key

使用键盘前后左右键就可以控制小乌龟运动了。感觉很亲切。

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

ros2 node list
ros2 topic list
ros2 service list
ros2 action list

打印输出:

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

3、实验通信

开一个终端执行

ros2 run demo_nodes_cpp talker

开另外一个终端执行

ros2 run demo_nodes_py listener

五、使用ros2

5.1创建ros2工作空间

mkdir -p ~/ros2_ws/src
cd ~/ros2_ws/src

5.2创建功能包

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

这里与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 <robot@todo.todo>']
    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 <robot@todo.todo>']
    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

5.3编译

cd ~/ros2_ws/
colcon build

纠错,疑问,交流: 911946883@qq.com  

  • 14
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机器人虎哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值