1.配置準(zhǔn)備
- 輸入
sudo apt-get install ros-$ROS_DISTRO-turtlesim
下載安裝turtlesim測(cè)試包狡逢,完成后首先運(yùn)行roscore
- 再打開(kāi)一個(gè)終端輸入
rosrun turtlesim turtlesim_node
奢浑,不出意外的話會(huì)出現(xiàn)一只烏龜
- 再另外一個(gè)終端輸入
rosrun turtlesim turtle_teleop_key
可以通過(guò)上下左右鍵控制烏龜運(yùn)動(dòng)了
2.多機(jī)通訊
想在PC上直接按鍵控制樹(shù)莓派上的烏龜運(yùn)動(dòng)印荔,要如何操作呢详羡?
主機(jī)與樹(shù)莓派信息如下:
Item | IP | Hostname |
---|---|---|
PC | 192.168.31.111 |
robot-dekstop |
樹(shù)莓派 | 192.168.31.107 |
pi-desktop |
相互ip可以通实柠,因?yàn)槲刺砑觝ostname ip映射窒盐,相互無(wú)法通過(guò)hostname聯(lián)系
- 在樹(shù)莓派上運(yùn)行
roscore
和啟動(dòng)turtlesim_node
- 在樹(shù)莓派上運(yùn)行
- PC上想要與之通訊首先必須設(shè)置
ROS_MASTER_URI
,export ROS_MASTER_URI='http:192.168.31.107:11311'
即可,在PC中斷輸入rostopic
list即可看到當(dāng)前的所有topic列表炕横,說(shuō)明PC已經(jīng)可以與ROS_MASTER建立通訊了
- PC上想要與之通訊首先必須設(shè)置
- 這時(shí)PC端啟動(dòng)
rosrun turtlesim turtle_teleop_key
無(wú)法控制烏龜運(yùn)動(dòng)份殿,為什么呢卿嘲?這里明明rostopic
明明可以看到/turtle/cmd_vel
拾枣。 鍵入rostopic info /turtle/cmd_vel
顯示如下
這就明顯了,主機(jī)從master里面取到的node發(fā)送/訂閱的topic
信息梅肤,node
地址為http://pi-desktop:46395
, PC根本不認(rèn)識(shí)pi-desktop
為何物凭语,自然無(wú)法發(fā)送數(shù)據(jù)撩扒。
- 這時(shí)PC端啟動(dòng)
- 如何修改搓谆?自然有2個(gè)方法:
- 這個(gè)我不認(rèn)識(shí)換個(gè)我認(rèn)識(shí)的來(lái)泉手,修改樹(shù)莓派端的
ROS_IP
環(huán)境變量export ROS_IP=192.168.31.107
再次查看topic
信息,
rosrun turtlesim turtle_teleop_key
節(jié)點(diǎn)榛做,發(fā)現(xiàn)仍然無(wú)法控制检眯。問(wèn)題是我認(rèn)識(shí)你昆淡,可是你不認(rèn)我啊,所有本地PC也需要export ROS_IP=192.168.31.111
,現(xiàn)在就正常了 - 這個(gè)我不認(rèn)識(shí)避凝,我現(xiàn)在認(rèn)識(shí)下不就行了恕曲,PC端修改
/etc/hosts
添加一個(gè)192.168.31.107 pi-desktop
,同時(shí)樹(shù)莓派端一樣修改/etc/hosts
添加192.168.31.111 robot-desktop
- 這個(gè)我不認(rèn)識(shí)換個(gè)我認(rèn)識(shí)的來(lái)泉手,修改樹(shù)莓派端的
- 如何修改搓谆?自然有2個(gè)方法:
3.PIBOT配置
PIBOT相關(guān)配置如下
ROS
主機(jī)(樹(shù)莓派等)
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
主機(jī)IP
4. 總結(jié)
多機(jī)通訊只能有一個(gè)master存在佩谣,所有都設(shè)置ROS_MASTER_URI
為http://MASTER_IP:11311, 各個(gè)終端要能相互通訊就必須設(shè)置自己的ROS_IP茸俭。
5. 一鍵配置腳本
PIBOT提供一鍵配置腳本安皱,只需要根據(jù)提示輸入即可配置上面參數(shù)
#!/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"
復(fù)制上面腳本保存至文件init_env.sh
,執(zhí)行bash init_env.sh
, 按照提示輸入
- 樹(shù)莓派輸入0缀踪,會(huì)自動(dòng)讀取樹(shù)莓派ip驴娃,寫(xiě)入變量至~/.piborc文件
- PC端輸入1循集, 再次輸入樹(shù)莓派的ip咒彤,寫(xiě)入變量至~/.piborc文件
source ~/.piborc
當(dāng)次生效或使用下面命令寫(xiě)入到bash的環(huán)境變量中
echo "source ~/.piborc" >> ~/.bashrc