Docker
拉取ros2镜像
docker pull osrf/ros:foxy-desktop
运行
docker run -it --name=ros2 -p 50022:22 osrf/ros:foxy-desktop
进入容器安装组件
apt-get update
apt-get install vim
apt-get install git
apt-get install net-tools
# 安装ssh
apt-get install openssh-server
安装完成之后,还需要配置一下,否则不能正常连接。在容器的终端中输入:
# 修改sshd_config,开放22端口,并允许密码登录
echo "Port 22">>/etc/ssh/sshd_config
echo "PermitRootLogin yes">>/etc/ssh/sshd_config
# 启动ssh服务
service ssh start
# 查看ssh状态
service ssh status
# 进行修改容器的密码,否则之后使用ssh是连接不上的,在容器终端内输入:
# 此处是修改密码的指令
passwd
查看22端口是否处于监听状态。
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就可以了。
输入命令
root@a3919d23c03c:~# cd /opt/ros/foxy/setup.bash
-bash: cd: /opt/ros/foxy/setup.bash: Not a directory
root@a3919d23c03c:~# export DISPLAY=主机ip:0.0
root@a3919d23c03c:~# source /opt/ros/foxy/setup.bash
root@a3919d23c03c:~# ros run turtlesim_node
-bash: ros: command not found
root@a3919d23c03c:~# ros2 run turtlesim_node
usage: ros2 run [-h] [--prefix PREFIX] package_name executable_name ...
ros2 run: error: the following arguments are required: executable_name, argv
root@a3919d23c03c:~# ros2 run turtlesim turtlesim_node
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'
[INFO] [1716269474.027870513] [turtlesim]: Starting turtlesim with node name /turtlesim
[INFO] [1716269474.076016003] [turtlesim]: Spawning turtle [turtle1] at x=[5.544445], y=[5.544445], theta=[0.000000]
^C[INFO] [1716269675.417040700] [rclcpp]: signal_handler(signal_value=2)
root@a3919d23c03c:~# pwd
/root
root@a3919d23c03c:~# ros2 run turtlesim turtlesim_node
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'
[INFO] [1716269710.118161429] [turtlesim]: Starting turtlesim with node name /turtlesim
[INFO] [1716269710.122263683] [turtlesim]: Spawning turtle [turtle1] at x=[5.544445], y=[5.544445], theta=[0.000000]
设置完,我们运行个小demo,出现小乌龟,如果想要控制这个小乌龟,再打开一个终端连接
root@a3919d23c03c:~# ll
total 32
drwx------ 1 root root 4096 May 21 05:26 ./
drwxr-xr-x 1 root root 4096 May 21 02:44 ../
-rw-r--r-- 1 root root 3106 Dec 5 2019 .bashrc
drwx------ 2 root root 4096 May 21 05:31 .cache/
drwxr-xr-x 4 root root 4096 Dec 9 03:58 .colcon/
-rw-r--r-- 1 root root 161 Dec 5 2019 .profile
drwxr-xr-x 1 root root 4096 May 21 05:31 .ros/
root@a3919d23c03c:~# export DISPLAY=主机ip:0.0
root@a3919d23c03c:~# source /opt/ros/foxy/setup.bash
root@a3919d23c03c:~# ros2 run turtlesim turtle_teleop_key
Reading from keyboard
---------------------------
Use arrow keys to move the turtle.
Use G|B|V|C|D|E|R|T keys to rotate to absolute orientations. 'F' to cancel a rotation.
'Q' to quit.
就可以用键盘的上下左右控制小乌龟走路了
可是实时查看消息传递信息 命令
ros2 topic echo /turtle1/cmd_vel
root@a3919d23c03c:~# export DISPLAY=主机IP:0.0
root@a3919d23c03c:~# source /opt/ros/foxy/setup.bash
root@a3919d23c03c:~# ros2 topic echo /turtle1/cmd_vel
WARNING: topic [/turtle1/cmd_vel] does not appear to be published yet
Could not determine the type for the passed topic
root@a3919d23c03c:~# ros2 topic echo /turtle1/cmd_vel
linear:
x: 2.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.0
---
linear:
x: 0.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 2.0
---
linear:
x: 2.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.0
---
linear:
x: 0.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 2.0
---
linear:
x: 0.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 2.0
---
也可以打开rqt工具实时跟踪节点发送日志
root@e864c4185be5:~# ros2 run rqt_console rqt_console