硬件环境:M4传感采集板,Firefly控制板、笔记本电脑、Wifi
编程语言:Python
通信过程:
M4采集传感器信号,传感器信号通过M4的以太网接口通过网线传输至Firefly控制板,控制板接受M4的传感器信号后,对信号进行解码,发送返回信号回M4,并将数据发布在ROS指定话题上,同wifi局域网下的笔记本电脑LT-0079通过订阅话题信息获得传感器数据。
操作步骤:
1.通信环境配置:
1.1 Firefly端(主控板):
1.1.1设置Firefly的wifi网络IP地址
IPV4 Settings:Manual
Address:192.168.20.133
Netmask:255.255.255.0
Gateway:192.168.20.1
1.1.2 设置Firefly的host
sudo vim /etc/hosts
192.168.20.133 firefly # (本机ip),中间tab隔开
192.168.20.135 LT-0079 #(服务器IP)
1.1.3 设置firefly的ros环境(以LT-0079为主机)
vim ~/bashrc -->添加
export ROS_HOSTNAME=firefly
export ROS_MASTER_URI=http://LT-0079:11311
1.2 PC端:
1.2.1设置LT-0079的wifi网络IP地址
IPV4 Settings:Manual
Address:192.168.20.135
Netmask:255.255.255.0
Gateway:192.168.20.1
1.2.2 设置LT-0079的host
sudo vim /etc/hosts
192.168.20.133 firefly
192.168.20.135 LT-0079
1.2.3 设置LT-0079的ros环境(以LT-0079为主机)
vim ~/bashrc -->添加
export ROS_HOSTNAME=LT-0079
export ROS_MASTER_URI=http://LT-0079:11311
2.firefly上编写与M4网口通信程序
2.1.新建workspace
mkdir -p ~/ros_newlife/src
catkin_init_worksapce
2.2.创建package
catkin_creat_pkg M4_Ros rospy roscpp message_generation
2.3更改package.xml文件与CMakeList.txt
vim ~/workspace/src/package_name/package.xml
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
vim ~/workspace/src/package_name/CMakelist
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation)
generate_messages( DEPENDENCIES std_msgs)
add_message_files( FILES M4_Sensor.msg)
2.4 新建msg文件
mkdir msg
vim msg/M4_Sensor.msg
int32 Start
int32 Start
int12 Count
int12 Red_Dis1
int12 Red_Dis2
int12 Red_Dis3
int12 Red_Dis4
int12 Ult_Dis1
int12 Ult_Dis2
int12 Ult_Dis3
int12 Ult_Dis4
int12 Red_Hum1
int12 Red_Hum2
int12 Red_Hum3
int12 Red_Hum4
int12 Voice1
int12 Voice2
int12 Voice3
int12 Voice4
int12 Voice5
int12 Voice6
int12 Fsr_Head
int12 Fsr_Lwing
int12 Fsr_Rwing
int12 Fsr_Front
int12 Fsr_Back
int12 Check
2.5 编写主程序
vim src/M4Sensor_Publisher.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
#Author: zhouwei
#Date: 20160307
import time
import socket
import rospy
from M4_Ros.msg import M4_Sensor
import json
######################tcp socket addr setting
HOST='192.168.2.4'
PORT=8008
BUFFER=4096
#####################tcp socket start
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind((HOST,PORT))
sock.listen(5)
print 'i am listening'
con,addr=sock.accept()
################ros begining
rospy.init_node('M4Sensor_Pub',anonymous=0)
pub=rospy.Publisher('M4Sensor',M4_Sensor,queue_size=10)
while not rospy.is_shutdown():
try:
con.settimeout(5)
buf=con.recv(BUFFER)
if len(buf) != 0:
Sensor=M4_Sensor()
buf_array=json.loads(buf)
Sensor.Start=buf_array[0]
Sensor.Count=buf_array[1]
Sensor.Red_Dis1=buf_array[2]
Sensor.Red_Dis2=buf_array[3]
Sensor.Red_Dis3=buf_array[4]
Sensor.Red_Dis4=buf_array[5]
Sensor.Ult_Dis1=buf_array[6]
Sensor.Ult_Dis2=buf_array[7]
Sensor.Ult_Dis3=buf_array[8]
Sensor.Ult_Dis4=buf_array[9]
Sensor.Red_Hum1=buf_array[10]
Sensor.Red_Hum2=buf_array[11]
Sensor.Red_Hum3=buf_array[12]
Sensor.Red_Hum4=buf_array[13]
Sensor.Voice1=buf_array[14]
Sensor.Voice2=buf_array[15]
Sensor.Voice3=buf_array[16]
Sensor.Voice4=buf_array[17]
Sensor.Voice5=buf_array[18]
Sensor.Voice6=buf_array[19]
Sensor.Fsr_Head=buf_array[20]
Sensor.Fsr_Lwing=buf_array[21]
Sensor.Fsr_Rwing=buf_array[22]
Sensor.Fsr_Front=buf_array[23]
Sensor.Fsr_Back=buf_array[24]
Sensor.Check=buf_array[25]
rospy.loginfo('Data is %s'% Sensor)
pub.publish(Sensor)
con.send('yes')
else:
print('no data')
con.send('out of client')
con.close()
con,addr=sock.accept()
except socket.timeout:
print 'time out'
2.6 make文件
chmod +x ~/ros_newlife/src/M4_Ros/src/M4Sensor_Publisher.py
cd ~/ros_newlife | catkin_make
3. LT-0079上编写服务器接受程序
3.1.新建workspace
mkdir -p ~/ros_newlife/src
catkin_init_worksapce
3.2.创建package
catkin_creat_pkg V6demo rospy roscpp message_generation
3.3更改package.xml文件与CMakeList.txt
vim ~/ros_newlife/src/V6demo/package.xml
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
vim ~/ ros_newlife/src/V6demo/CMakelist
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation)
generate_messages( DEPENDENCIES std_msgs)
add_message_files( FILES M4_Sensor.msg)
3.4 新建msg文件
mkdir msg
vim msg/M4_Sensor.msg
"与firefly端一致"
3.5 编写主程序
vim src/M4Sensor_Subscriber.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import rospy
from V6demo.msg import M4_Sensor
def callback(data):
rospy.loginfo('i recv \n')
rospy.loginfo(data)
#rospy.loginfo(data.Ult_Dis1)
rospy.init_node('M4Sensor_Sub',anonymous=0)
rospy.Subscriber('M4Sensor',M4_Sensor,callback)
rospy.spin()
3.6 make文件
chmod +x ~/ros_newlife/src/V6demo/src/M4Sensor_Subscriber.py
cd ~/ros_newlife | catkin_make
4. 程序运行
LT-0079:(远程笔记本)
Terminal 1:
rosocre
Terminal 2:
cd ~/joey_newlife
source devel/setup.bash
rosrun V6demo M4Sensor_Subscriber.py
Firefly:(本地主控板)
Terminal 1:
cd ~/joey_newlife
source devel/setup.bash
rosrun V6demo M4Sensor_Publisher.py
M4:(传感采集板)
运行程序....