当前位置:   article > 正文

VirtualBox+Ubuntu22.10+Docker+ROS2

VirtualBox+Ubuntu22.10+Docker+ROS2

Docker

拉取ros2镜像

docker pull osrf/ros:foxy-desktop

运行

docker run -it --name=ros2 -p 50022:22 osrf/ros:foxy-desktop

进入容器安装组件

  1. apt-get update
  2. apt-get install vim
  3. apt-get install git
  4. apt-get install net-tools
  5. # 安装ssh
  6. apt-get install openssh-server
  7. 安装完成之后,还需要配置一下,否则不能正常连接。在容器的终端中输入:
  8. # 修改sshd_config,开放22端口,并允许密码登录
  9. echo "Port 22">>/etc/ssh/sshd_config
  10. echo "PermitRootLogin yes">>/etc/ssh/sshd_config
  11. # 启动ssh服务
  12. service ssh start
  13. # 查看ssh状态
  14. service ssh status
  15. # 进行修改容器的密码,否则之后使用ssh是连接不上的,在容器终端内输入:
  16. # 此处是修改密码的指令
  17. passwd
  18. 查看22端口是否处于监听状态。
  19. netstat -ap | grep ssh

下载ssh工具MobaXterm free Xserver and tabbed SSH client for Windows

设置连接

端口为50022,host为虚拟机的ip,username为root,密码为刚才设置的密码

输入密码后我们就会进入到docker中了。
要直接显示还需要重要的一步,我们还要设置docker中ros2镜像的DISPLAY的环境变量的值(将显示映射出来)
# export DISPLAY=宿主主机IP:0.0 宿主主机IP指的是当前自己电脑的ip
# 举个例子,当前我的是:
export DISPLAY=本地机器的ip:0.0 (安装了那个SSH工具的ip)
值得一提的是,该方法还可以在局域网下和其他的linux主机进行可视化交互,宿主ip改为linux主机ip就可以了。

输入命令

  1. root@a3919d23c03c:~# cd /opt/ros/foxy/setup.bash
  2. -bash: cd: /opt/ros/foxy/setup.bash: Not a directory
  3. root@a3919d23c03c:~# export DISPLAY=主机ip:0.0
  4. root@a3919d23c03c:~# source /opt/ros/foxy/setup.bash
  5. root@a3919d23c03c:~# ros run turtlesim_node
  6. -bash: ros: command not found
  7. root@a3919d23c03c:~# ros2 run turtlesim_node
  8. usage: ros2 run [-h] [--prefix PREFIX] package_name executable_name ...
  9. ros2 run: error: the following arguments are required: executable_name, argv
  10. root@a3919d23c03c:~# ros2 run turtlesim turtlesim_node
  11. QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'
  12. [INFO] [1716269474.027870513] [turtlesim]: Starting turtlesim with node name /turtlesim
  13. [INFO] [1716269474.076016003] [turtlesim]: Spawning turtle [turtle1] at x=[5.544445], y=[5.544445], theta=[0.000000]
  14. ^C[INFO] [1716269675.417040700] [rclcpp]: signal_handler(signal_value=2)
  15. root@a3919d23c03c:~# pwd
  16. /root
  17. root@a3919d23c03c:~# ros2 run turtlesim turtlesim_node
  18. QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'
  19. [INFO] [1716269710.118161429] [turtlesim]: Starting turtlesim with node name /turtlesim
  20. [INFO] [1716269710.122263683] [turtlesim]: Spawning turtle [turtle1] at x=[5.544445], y=[5.544445], theta=[0.000000]


设置完,我们运行个小demo,出现小乌龟,如果想要控制这个小乌龟,再打开一个终端连接

  1. root@a3919d23c03c:~# ll
  2. total 32
  3. drwx------ 1 root root 4096 May 21 05:26 ./
  4. drwxr-xr-x 1 root root 4096 May 21 02:44 ../
  5. -rw-r--r-- 1 root root 3106 Dec 5 2019 .bashrc
  6. drwx------ 2 root root 4096 May 21 05:31 .cache/
  7. drwxr-xr-x 4 root root 4096 Dec 9 03:58 .colcon/
  8. -rw-r--r-- 1 root root 161 Dec 5 2019 .profile
  9. drwxr-xr-x 1 root root 4096 May 21 05:31 .ros/
  10. root@a3919d23c03c:~# export DISPLAY=主机ip:0.0
  11. root@a3919d23c03c:~# source /opt/ros/foxy/setup.bash
  12. root@a3919d23c03c:~# ros2 run turtlesim turtle_teleop_key
  13. Reading from keyboard
  14. ---------------------------
  15. Use arrow keys to move the turtle.
  16. Use G|B|V|C|D|E|R|T keys to rotate to absolute orientations. 'F' to cancel a rotation.
  17. 'Q' to quit.

就可以用键盘的上下左右控制小乌龟走路了

可是实时查看消息传递信息 命令

ros2 topic echo /turtle1/cmd_vel

  1. root@a3919d23c03c:~# export DISPLAY=主机IP:0.0
  2. root@a3919d23c03c:~# source /opt/ros/foxy/setup.bash
  3. root@a3919d23c03c:~# ros2 topic echo /turtle1/cmd_vel
  4. WARNING: topic [/turtle1/cmd_vel] does not appear to be published yet
  5. Could not determine the type for the passed topic
  6. root@a3919d23c03c:~# ros2 topic echo /turtle1/cmd_vel
  7. linear:
  8. x: 2.0
  9. y: 0.0
  10. z: 0.0
  11. angular:
  12. x: 0.0
  13. y: 0.0
  14. z: 0.0
  15. ---
  16. linear:
  17. x: 0.0
  18. y: 0.0
  19. z: 0.0
  20. angular:
  21. x: 0.0
  22. y: 0.0
  23. z: 2.0
  24. ---
  25. linear:
  26. x: 2.0
  27. y: 0.0
  28. z: 0.0
  29. angular:
  30. x: 0.0
  31. y: 0.0
  32. z: 0.0
  33. ---
  34. linear:
  35. x: 0.0
  36. y: 0.0
  37. z: 0.0
  38. angular:
  39. x: 0.0
  40. y: 0.0
  41. z: 2.0
  42. ---
  43. linear:
  44. x: 0.0
  45. y: 0.0
  46. z: 0.0
  47. angular:
  48. x: 0.0
  49. y: 0.0
  50. z: 2.0
  51. ---

也可以打开rqt工具实时跟踪节点发送日志

root@e864c4185be5:~# ros2 run rqt_console rqt_console

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/649003
推荐阅读
相关标签
  

闽ICP备14008679号