1.配置准备
- 输入
sudo apt-get install ros-$ROS_DISTRO-turtlesim
下载安装turtlesim测试包,完成后首先运行roscore
- 再打开一个终端输入
rosrun turtlesim turtlesim_node
,不出意外的话会出现一只乌龟
- 再另外一个终端输入
rosrun turtlesim turtle_teleop_key
可以通过上下左右键控制乌龟运动了
2.多机通讯
想在PC上直接按键控制树莓派上的乌龟运动,要如何操作呢?
主机与树莓派信息如下:
Item | IP | Hostname |
---|---|---|
PC | 192.168.31.111 |
robot-dekstop |
树莓派 | 192.168.31.107 |
pi-desktop |
相互ip可以通,因为未添加hostname ip映射,相互无法通过hostname联系
- 在树莓派上运行
roscore
和启动turtlesim_node
- 在树莓派上运行
- PC上想要与之通讯首先必须设置
ROS_MASTER_URI
,export ROS_MASTER_URI='http:192.168.31.107:11311'
即可,在PC中断输入rostopic
list即可看到当前的所有topic列表,说明PC已经可以与ROS_MASTER建立通讯了
- PC上想要与之通讯首先必须设置
- 这时PC端启动
rosrun turtlesim turtle_teleop_key
无法控制乌龟运动,为什么呢?这里明明rostopic
明明可以看到/turtle/cmd_vel
。 键入rostopic info /turtle/cmd_vel
显示如下
这就明显了,主机从master里面取到的node发送/订阅的topic
信息,node
地址为http://pi-desktop:46395
, PC根本不认识pi-desktop
为何物,自然无法发送数据。
- 这时PC端启动
- 如何修改?自然有2个方法:
- 这个我不认识换个我认识的来,修改树莓派端的
ROS_IP
环境变量export ROS_IP=192.168.31.107
再次查看topic
信息,
rosrun turtlesim turtle_teleop_key
节点,发现仍然无法控制。问题是我认识你,可是你不认我啊,所有本地PC也需要export ROS_IP=192.168.31.111
,现在就正常了 - 这个我不认识,我现在认识下不就行了,PC端修改
/etc/hosts
添加一个192.168.31.107 pi-desktop
,同时树莓派端一样修改/etc/hosts
添加192.168.31.111 robot-desktop
- 这个我不认识换个我认识的来,修改树莓派端的
- 如何修改?自然有2个方法:
3.PIBOT配置
PIBOT相关配置如下
ROS
主机(树莓派等)
source /opt/ros/indigo/setup.bash
LOCAL_IP=`hostname -I`
export ROS_IP=`echo $LOCAL_IP`
export ROS_HOSTNAME=`echo $LOCAL_IP`
export PIBOT_MODEL=apollo
export PIBOT_LIDAR=rplidar
export ROS_MASTER_URI=`echo http://$ROS_IP:11311`
source ~/pibot_ros/ros_ws/devel/setup.bash
PC
source /opt/ros/indigo/setup.bash
LOCAL_IP=`hostname -I`
export ROS_IP=`echo $LOCAL_IP`
export ROS_HOSTNAME=`echo $LOCAL_IP`
export PIBOT_MODEL=apollo
export PIBOT_LIDAR=rplidar
export ROS_MASTER_URI=`echo http://xxx.xxx.xx.xx:11311`
source ~/pibot_ros/ros_ws/devel/setup.bash
xxx.xxx.xx.xx为
ROS
主机IP
4. 总结
多机通讯只能有一个master存在,所有都设置ROS_MASTER_URI
为http://MASTER_IP:11311, 各个终端要能相互通讯就必须设置自己的ROS_IP。
5. 一键配置脚本
PIBOT提供一键配置脚本,只需要根据提示输入即可配置上面参数
#!/bin/bash
if [ ! -z "$PIBOT_HOME" ]; then
PIBOT_HOME_DIR=$PIBOT_HOME
else
PIBOT_HOME_DIR=~/pibot_ros
fi
code_name=$(lsb_release -sc)
if [ "$code_name" = "trusty" ]; then
ros_version="indigo"
elif [ "$code_name" = "xenial" ]; then
ros_version="kinetic"
elif [ "$code_name" = "bionic" ] || [ "$code_name" = "stretch" ]; then
ros_version="melodic"
else
echo -e "\033[1;31m PIBOT not support "$code_name"\033[0m"
exit
fi
content="#!/bin/bash
#source ros
if [ ! -f /opt/ros/${ros_version}/setup.bash ]; then
echo \"please run cd $PIBOT_HOME_DIR && ./pibot_install_ros.sh to install ros sdk\"
else
source /opt/ros/${ros_version}/setup.bash
fi
"
echo "${content}" > ~/.pibotrc
#LOCAL_IP=`ifconfig eth0|grep "inet addr:"|awk -F":" '{print $2}'|awk '{print $1}'`
#LOCAL_IP=`ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | awk -F"/" '{print $1}'`
#if [ ! ${LOCAL_IP} ]; then
# echo "please check network"
# exit
#fi
LOCAL_IP=`ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | awk -F"/" '{print $1}'`
echo "LOCAL_IP=\`ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print \$2}' | awk -F"/" '{print \$1}'\`" >> ~/.pibotrc
if [ ! ${LOCAL_IP} ]; then
echo -e "\033[1;31muse 127.0.0.1 as local ip\033[0m"
LOCAL_IP=127.0.0.1
fi
echo "export ROS_IP=\`echo \$LOCAL_IP\`" >> ~/.pibotrc
echo "export ROS_HOSTNAME=\`echo \$LOCAL_IP\`" >> ~/.pibotrc
echo -e "\033[1;34mplease specify the current machine(ip:$LOCAL_IP) type:\033[1;32m
(0: pibot board,
\033[31m1: control PC or Virtual PC\033[1;34m)\033[1;33m"
read PIBOT_INPUT
if [ "$PIBOT_INPUT" = "0" ]; then
ROS_MASTER_IP_STR="\`echo \$LOCAL_IP\`"
ROS_MASTER_IP=`echo $LOCAL_IP`
else
echo -e "\033[1;34mplase specify the pibot board ip for commnication(e.g. 192.168.12.1):"
read -p "" PIBOT_INPUT
ROS_MASTER_IP_STR=`echo $PIBOT_INPUT`
ROS_MASTER_IP=`echo $PIBOT_INPUT`
fi
echo "export ROS_MASTER_URI=`echo http://${ROS_MASTER_IP_STR}:11311`" >> ~/.pibotrc
echo -e "\033[1;35m*****************************************************************"
echo "local_ip: " ${LOCAL_IP}
echo "onboard_ip: " ${ROS_MASTER_IP}
echo ""
echo -e "please execute \033[1;36;4msource ~/.bashrc\033[1;35m to make the configure effective\033[0m"
echo -e "\033[1;35m*****************************************************************\033[0m"
复制上面脚本保存至文件init_env.sh
,执行bash init_env.sh
, 按照提示输入
- 树莓派输入0,会自动读取树莓派ip,写入变量至~/.piborc文件
- PC端输入1, 再次输入树莓派的ip,写入变量至~/.piborc文件
source ~/.piborc
当次生效或使用下面命令写入到bash的环境变量中
echo "source ~/.piborc" >> ~/.bashrc