Docker运行 ROS2 容器镜像 · 虚拟出一个 Ubuntu 20.04 环境 · 进行 ROS2开发

 

一、 Ubuntu/CentOS/win10上安装docker

Ubuntu

sudo apt-get update
sudo apt-get install -y docker.io
systemctl start docker             //安装完成后启动docker
systemctl enable docker         //设置开机启动
docker version                         //查看docker是否安装成功

CentOS (252条消息) Centos安装docker_三尺SCH的博客-CSDN博客
Windows 【全面详细】Windows10 Docker安装详细教程 - 知乎 (zhihu.com)

 

二、Docker 实例化 “Ubuntu:20.04:ROS2”容器镜像

这里我们没有从ubuntu基础镜像sudo docker pull ubuntu:20.04 方式,一步步开始,而是直接使用ROS的官方容器镜像。

ROS2, Docker Hub官网:        https://hub.docker.com/r/osrf/ros
MicroROS,  Docker Hub官网:   https://hub.docker.com/u/microros                               // libmicroros.a 生成容器镜像:https://hub.docker.com/r/microros/micro_ros_static_library_builder

若要二次生成容器镜像请参考:(253条消息) 基于osrf/ros定制打包ROS2 docker镜像_机器人梦想家的博客-CSDN博客

2.1 从docker hub上, 拉取ROS2官方提供的 foxy版本容器镜像

# sudo docker pull osrf/ros:foxy-desktop    //sudo docker pull --platform amd64 ros:foxy  拉取容器镜像
                                            //sudo docker pull osrf/ros:humble-desktop   //docker pull osrf/ros:humble-desktop-full
# docker images                             // docker image list  查看下载的镜像
# docker rmi <image-id/image-tag>           // 删除镜像

 

 2.2 运行镜像

sudo docker run -it -name ros2-foxy osrf/ros:foxy-desktop  // 运行镜像容器  
ctrl + p + q //退出容器伪终端,容器正常运行
sudo docker ps -a // 查看当前容器实例(-a表示所有:运行+退出) sudo docker stop <container-id> // 停止容器 sudo docker rm <container-id> // 删除容器

 2.3  进入容器实例伪终端

方式一:ros2 命令找不到

# docker exec -it <container-id/container-name> /bin/bash      //解决办法是执行:source /opt/ros/foxy/setup.bash  
//或者 1. echo 'source /opt/ros/foxy/setup.bash'>> ~/.bashrc 2. source ~/.bashrc
//当找不到动态库:echo 'export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH' >> ~/.bashrc 

vi ~/.bashrc 

 编辑~/.bashrc,  新增如下内容,并使用source 命令生效 

source /opt/ros/foxy/setup.bash
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu/qt5/plugins/platforms:$LD_LIBRARY_PATH
export export DISPLAY=10.20.0.165:0.0

source ~/.bashrc

 方式二:ros2 命令,正常运行

  

三、ROS2 docker可视化运行实例小乌龟

Refer: Using turtlesim, ros2, and rqt — ROS 2 Documentation: Foxy documentation

3.1 安装运行MobaXterm远程工具

确保你和你的安装docker的电脑在一个局域网里:

 

 3.2 容器伪终端中设置

export QT_DEBUG_PLUGINS=1
export DISPLAY=10.20.0.165:0.0 //设置docker中ros2镜像的DISPLAY的环境变量的值,注意这里的值就是上图 MobaXterm 红圈中DISPLAY的值

3.3 容器伪终端中启动小乌龟模拟器

ros2 pkg executables turtlesim   // 查看海龟仿真器是否安装

 

ros2 run turtlesim turtlesim_node     //启动小乌龟模拟器
ros2 run turtlesim turtle_teleop_key  //运行键盘控制节点

 3.4 查看小海龟使用了哪些功能

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

 

四、源码编译安装ROS2

Refer: Ubuntu (source) — ROS 2 Documentation: Foxy documentation

 

五、源码编译Micro ROS

 

Refer:ROS2与STM32入门教程-microROS的linux版本 - 创客智造/爱折腾智能机器人 (ncnynl.com)

 

 

附录:A    问题

A.1 libQt5Core.so.5: cannot open shared object file:

 解决办法如下:

sudo apt-get install libqt5core5a
sudo strip --remove-section=.note.ABI-tag  /usr/lib/x86_64-linux-gnu/libQt5Core.so.5

 记录:

root@b870757dab9e:/# ros2 run turtlesim turtlesim_node
/opt/ros/foxy/lib/turtlesim/turtlesim_node: error while loading shared libraries: libQt5Core.so.5: cannot open shared object file: No such file or directory root@b870757dab9e:/# ros2 run turtlesim turtlesim_node /opt/ros/foxy/lib/turtlesim/turtlesim_node: error while loading shared libraries: libQt5Core.so.5: cannot open shared object file: No such file or directory root@b870757dab9e:/# sudo strip --remove-section=.note.ABI-tag /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
root@b870757dab9e:/# ros2 run turtlesim turtlesim_node qt.qpa.xcb: could not connect to display qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb. root@b870757dab9e:/#

 

posted @ 2023-05-04 20:54  suntroop  阅读(1956)  评论(0)    收藏  举报