本博文实现了:
- 一条简单命令控制3个机器人(开启roscore的同时,运行所需的所有的ros命名)
- ssh的同时输入密码并且运行命令
在进行ros开发时,不可避免的需要开多个终端,运行n条命令。特别是做多机器人开发。要ssh到不同的机器人然后分别运行命令,在本机上也是如此。
当然,在ros中,提供了launch文件给我们进行命令的封装。可是ssh应该是无法写入roslaunch中。而且ssh后还得输入密码。对于正常的开发当然没有问题,可是如果要做展示就特别麻烦了。比如,本人做的多机器人操作中,需要运行的命令如下:
首先必须运行
roscore
ssh远程连接三个机器人
ssh ubuntu@192.168.11.152 1
ssh ubuntu@192.168.11.157 2
ssh ubuntu@192.168.11.159 0
然后在三个机器人上分别运行如下命令
ROS_NAMESPACE=tb_1 roslaunch turtlebot3_bringup turtlebot3_robot.launch multi_robot_name:="tb_1" set_lidar_frame_id:="tb_1/base_scan"
ROS_NAMESPACE=tb_2 roslaunch turtlebot3_bringup turtlebot3_robot.launch multi_robot_name:="tb_2" set_lidar_frame_id:="tb_2/base_scan"
ROS_NAMESPACE=tb_0 roslaunch turtlebot3_bringup turtlebot3_robot.launch multi_robot_name:="tb_0" set_lidar_frame_id:="tb_0/base_scan"
再次ssh到三个机器人,再分别运行如下命令
rosrun mvcam mvcam_ROI_max_flag __ns:=tb_1
rosrun mvcam mvcam_ROI_max_flag __ns:=tb_2
rosrun mvcam mvcam_ROI_max_flag __ns:=tb_0
然后在电脑终端运行
roslaunch single_led EKF.launch
如果要开启键盘操作,还需要运行
roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch
这些命令下来,差不多10个窗口,特别麻烦。
而本博文采用sh脚本来实现。只需要在.bashrc文件加入
alias open='cd ~/demo && sh launch_top.sh'
然后简单的在终端运行open,所有的命令都自动运行。生成入下图所示的rqt
创建脚本如下(此处直接粘贴代码):
#! /bin/bash
gnome-terminal --window -e 'bash -c "roscore;exec bash"'
###############################################################
gnome-terminal --window -e 'bash -c "expect rb1_launch.sh;exec bash"'
#gnome-terminal --window -e 'bash -c "expect rb1_launch_cam.sh;exec bash"'
gnome-terminal --window -e 'bash -c "expect rb1_launch_mvcam.sh;exec bash"'
###############################################################
gnome-terminal --window -e 'bash -c "expect rb2_launch.sh;exec bash"'
#gnome-terminal --window -e 'bash -c "expect rb2_launch_cam.sh;exec bash"'
gnome-terminal --window -e 'bash -c "expect rb2_launch_mvcam.sh;exec bash"'
###############################################################
sleep 10s
gnome-terminal --window -e 'bash -c "roslaunch single_led EKF.launch;exec bash"'
#!/usr/bin/expect
set user "ubuntu"
set ip "10.79.111.186"
set password "ubuntu"
set timeout 20
#opensend a ssh link, the comment must begin at a new line
spawn ssh $user@$ip
expect {
"(yes/no)?" { send "yes\r"; exp_continue;}
"password:" { send "$password\r"; exp_continue;}
"*@*" { send "ROS_NAMESPACE=tb_1 roslaunch turtlebot3_bringup turtlebot3_robot.launch multi_robot_name:=\"tb_1\" set_lidar_frame_id:=\"tb_1/base_scan\"\r"; }
}
interact
#!/usr/bin/expect
set user "ubuntu"
set ip "10.79.111.186"
set password "ubuntu"
set timeout 20
#opensend a ssh link, the comment must begin at a new line
spawn ssh $user@$ip
expect {
"(yes/no)?" { send "yes\r"; exp_continue;}
"password:" { send "$password\r"; exp_continue;}
"*@*" { send "ROS_NAMESPACE=tb_1 roslaunch usb_cam usb_cam.launch\r"; }
}
interact
#!/usr/bin/expect
set user "ubuntu"
set ip "10.79.111.186"
set password "ubuntu"
set timeout 20
#opensend a ssh link, the comment must begin at a new line
spawn ssh $user@$ip
expect {
"(yes/no)?" { send "yes\r"; exp_continue;}
"password:" { send "$password\r"; exp_continue;}
"*@*" { send "rosrun mvcam mvcam_ROI_max_flag __ns:=tb_1\r"; }
}
interact
#!/usr/bin/expect
set user "ubuntu"
set ip "10.79.13.213"
set password "ubuntu"
set timeout 20
#opensend a ssh link, the comment must begin at a new line
spawn ssh $user@$ip
expect {
"(yes/no)?" { send "yes\r"; exp_continue;}
"password:" { send "$password\r"; exp_continue;}
"*@*" { send "ROS_NAMESPACE=tb_2 roslaunch turtlebot3_bringup turtlebot3_robot.launch multi_robot_name:=\"tb_2\" set_lidar_frame_id:=\"tb_2/base_scan\"\r"; }
}
interact
#!/usr/bin/expect
set user "ubuntu"
set ip "10.79.13.213"
set password "ubuntu"
set timeout 20
#opensend a ssh link, the comment must begin at a new line
spawn ssh $user@$ip
expect {
"(yes/no)?" { send "yes\r"; exp_continue;}
"password:" { send "$password\r"; exp_continue;}
"*@*" { send "ROS_NAMESPACE=tb_2 roslaunch usb_cam usb_cam.launch\r"; }
}
interact
#!/usr/bin/expect
set user "ubuntu"
set ip "10.79.13.213"
set password "ubuntu"
set timeout 20
#opensend a ssh link, the comment must begin at a new line
spawn ssh $user@$ip
expect {
"(yes/no)?" { send "yes\r"; exp_continue;}
"password:" { send "$password\r"; exp_continue;}
"*@*" { send "rosrun mvcam mvcam_ROI_max_flag __ns:=tb_2\r"; }
}
interact