**
ROS自定义数组[]消息类型 学习与使用中的一些问题
**
本帖用来记录ros自定义消息类型的学习过程以及目前遇到的一些问题:
- 创建自定义的数组 [ ] 消息类型以及使用
- 以自定义的消息类型 Distance.msg 为基础,再次定义消息类型 distance.msg 以及使用
- 以自定义的消息类型Distance.msg 为基础,再次定义数组 [ ] 消息类型Sj.msg
(尚未解决使用问题)
注:Ubantu 16.04
**
一、创建自定义的数组 [ ] 消息类型以及使用
**
创建
在创建好的功能包下新建msg文件夹,在msg文件夹中创建Test.msg文件:
float64 w
float32[] a
编译
Test.msg文件创建完成后,需要分别在package.xml文件以及CMakeList.txt中添加如下代码:
package.xml:
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
CMakeList.txt:
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation)
--------------------------------------------------------------
## Generate messages in the 'msg' folder
add_message_files(
FILES
Test.msg
)
--------------------------------------------------------------
## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs )
--------------------------------------------------------------
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES test
CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
# DEPENDS system_lib)
添加结束后,在工作空间下进行编译:
ryj@ryj-System-Product-Name:~/test_ws$ catkin_make
验证及使用
验证test.msg 数组 [ ] 消息类型:
test.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###########################################
####### 本历程用于验证test.msg消息结构 ######
######## 时间:2020.11.01 #########
###########################################
import rospy
from test.msg import Distance
from test.msg import Test
from random import random
rospy.init_node('test_publisher')
pub=rospy.Publisher('test',Test,queue_size=5)
rate =rospy.Rate(2)
while not rospy.is_shutdown():
msg = Test()
msg.w = random()
msg.a=[random(),random(),random()
pub.publish(msg)
rate.sleep()
运行.py文件后验证 Test.msg [ ]消息类型:
ryj@ryj-System-Product-Name:~$ rostopic echo /test
w: 0.64976981224
a: [0.28443363308906555, 0.24585898220539093, 0.3781258463859558]
---
w: 0.88999320791
a: [0.04934848099946976, 0.477643758058548, 0.7934481501579285]
---
w: 0.743219493552
a: [0.7222915291786194, 0.8820058703422546, 0.9646686911582947]
---
w: 0.819877121465
a: [0.34416118264198303, 0.5286455750465393, 0.6777477264404297]
---
w: 0.671419291345
a: [0.9397015571594238, 0.6959425806999207, 0.4939687252044678]
---
**
二、以自定义的消息类型 Distance.msg 为基础,再次定义消息类型 distance.msg 以及使用
**
创建
在创建好的功能包下新建msg文件夹,在msg文件夹中创建Distance.msg文件:
float64 lfdistance
float64 rfdistance
float64 lbdistance
float64 rbdistance
以及distance.msg文件:
Distance ccc
编译
Distance.msg 和 distance.msg 文件创建完成后,需要分别在package.xml文件以及CMakeList.txt中添加如下代码:
package.xml:
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
CMakeList.txt:
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation)
--------------------------------------------------------------
## Generate messages in the 'msg' folder
add_message_files(
FILES
Test.msg
Distance.msg
distance.msg
)
--------------------------------------------------------------
## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs )
--------------------------------------------------------------
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES test
CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
# DEPENDS system_lib)
添加结束后,在工作空间下进行编译:
ryj@ryj-System-Product-Name:~/test_ws$ catkin_make
验证和使用
验证Distance.msg数据类型:
distance_publisher_test_1.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
############################################
### 本历程用于验证Distance.msg消息结构##########
##### 时间:2020.11.01 ########
########################################
import rospy
from test.msg import Distance
from random import random
rospy.init_node('distance_publisher')
pub=rospy.Publisher('distance',Distance,queue_size=5)
rate =rospy.Rate(2)
while not rospy.is_shutdown():
msg = Distance()
msg.lfdistance = random()
msg.rfdistance = random()
msg.lbdistance = random()
msg.rbdistance = random()
pub.publish(msg)
rate.sleep()
运行.py文件后以验证Distance.msg数据结构,查看消息内容:
ryj@ryj-System-Product-Name:~$ rostopic echo /distance
lfdistance: 0.294549089667
rfdistance: 0.574421169485
lbdistance: 0.188829316564
rbdistance: 0.645022166847
---
lfdistance: 0.301771062692
rfdistance: 0.184317243424
lbdistance: 0.555854273084
rbdistance: 0.106270681606
---
lfdistance: 0.345642084176
rfdistance: 0.176619587316
lbdistance: 0.186552889047
rbdistance: 0.503750507188
---
lfdistance: 0.57470141537
rfdistance: 0.324531003314
lbdistance: 0.0593479686516
rbdistance: 0.390910208963
验证distance.msg数据类型:
distance_publisher_test_2.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###########################################
### 本历程用于验证distance.msg消息结构#########
##### 时间:2020.11.01 ########
#########################################
import rospy
from test.msg import Distance
#from test.msg import distance
from test.msg import distance
from random import random
rospy.init_node('sj_publisher')
pub=rospy.Publisher('sj',distance,queue_size=5)
rate =rospy.Rate(2)
while not rospy.is_shutdown():
msg = distance()
msg.ccc.lfdistance =random()
pub.publish(msg.ccc)
rate.sleep()
运行.py文件后以验证distance.msg数据结构,查看消息内容:
ryj@ryj-System-Product-Name:~$ rostopic echo /sj
ccc:
lfdistance: 0.493087003315
rfdistance: 0.0
lbdistance: 0.0
rbdistance: 0.0
---
ccc:
lfdistance: 0.645757335999
rfdistance: 0.0
lbdistance: 0.0
rbdistance: 0.0
---
ccc:
lfdistance: 0.414857667022
rfdistance: 0.0
lbdistance: 0.0
rbdistance: 0.0
**
三、以自定义的消息类型Distance.msg 为基础 再次定义数组 [ ] 消息类型Sj.msg (尚未解决使用问题)
创建
**
在这里我依据上述已经得到验证的两种数据类型创建了如下Sj.msg:
Distance[] aaaa
编译
按照上述流程进行编译.
验证(使用问题尚未解决)
查询当前数据结构为:
ryj@ryj-System-Product-Name:~$ rosmsg show Sj.msg
[test/Sj]:
test/Distance[] ccc
float64 lfdistance
float64 rfdistance
float64 lbdistance
float64 rbdistance
验证文件:
distance_publisher_3.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###########################################
######## 本历程用于验证Sj.msg消息结构 #########
####### 时间:2020.11.01 ########
###########################################
import rospy
from test.msg import Distance
#from test.msg import distance
from test.msg import Sj
from random import random
rospy.init_node('sj_publisher')
pub=rospy.Publisher('sj',Sj,queue_size=5)
rate =rospy.Rate(2)
while not rospy.is_shutdown():
msg = Sj()
msg.ccc =[(random(),random(),random(),random()),(random(),random(),random(),random())]
pub.publish(msg.ccc)
rate.sleep()
令笔者不解的是,在这个数据结构下,在验证程序中调用ccc总是失败,尚未找到解决办法,如果你有好的解决方案欢迎评论或者私信,非常感谢!!!
ryj@ryj-System-Product-Name:~$ rosrun test distance_publisher_3.py
Traceback (most recent call last):
File "/home/ryj/test_ws/src/test/scripts/distance_publisher_3.py", line 24, in <module>
pub.publish(msg.ccc)
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 882, in publish
self.impl.publish(data)
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 1066, in publish
serialize_message(b, self.seq, message)
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/msg.py", line 152, in serialize_message
msg.serialize(b)
File "/home/ryj/test_ws/devel/lib/python2.7/dist-packages/test/msg/_Sj.py", line 65, in serialize
buff.write(_get_struct_4d().pack(_x.lfdistance, _x.rfdistance, _x.lbdistance, _x.rbdistance))
AttributeError: 'tuple' object has no attribute 'lfdistance'