Python回调属性错误

Python callback attribute error

我编写了一个 python ROS 节点来订阅两个不同的主题,并使用 ApproximateTimeSynchroniser 基本上通过时间戳来匹配消息,并将这些消息捆绑到一个回调例程中。如果我使用回调来简单地打印出收到的两条消息,它就可以正常工作。

但是,我想在回调 routine.Using 下面的代码中使用接收到的 Pose 2D 数据的 x 和 y 位置来填充 geometry_msgs/Pose 消息,我已经通过制作一个回调例程中的空 Pose 对象:

  #!/usr/bin/env python


import message_filters
import rospy
from laser_line_extraction.msg import LineSegmentList
from geometry_msgs.msg import Pose,  Pose2D
from std_msgs.msg import Int32, Float32

rospy.init_node('simul-subscriber')

def callback(Pose2D, LineSegmentList): 
 pose = Pose()
 pose.position.x =  Pose2D.position.x 
 pose.position.y =  Pose2D.position.y 
 pose.position.z = 0
 #print(Pose2D,  LineSegmentList, pose)
 print(pose)
 print(LineSegmentList)

line_segment_sub = message_filters.Subscriber('/line_segments', LineSegmentList)
pose_2D_sub = message_filters.Subscriber('/pose2D',Pose2D)

ts = message_filters.ApproximateTimeSynchronizer([line_segment_sub, pose_2D_sub], 10, 0.1, allow_headerless=True)
ts.registerCallback(callback)

rospy.spin()

不幸的是,当 运行 这个节点时会出现奇怪的错误:

[ERROR] [1535552711.709928, 1381.743000]: bad callback: <bound method Subscriber.callback of <message_filters.Subscriber object at 0x7f7af3cee9d0>>
Traceback (most recent call last):
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
    cb(msg)
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/message_filters/__init__.py", line 75, in callback
    self.signalMessage(msg)
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/message_filters/__init__.py", line 57, in signalMessage
    cb(*(msg + args))
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/message_filters/__init__.py", line 287, in add
    self.signalMessage(*msgs)
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/message_filters/__init__.py", line 57, in signalMessage
    cb(*(msg + args))
  File "/home/elisabeth/catkin_ws/src/hector_quadrotor/hector_quadrotor_demo/python_scripts/simul-subscriber.py", line 14, in callback
    pose.position.x =  Pose2D.position.x
AttributeError: 'LineSegmentList' object has no attribute 'position'

这很奇怪,因为位置属性仅被 Pose 2D 引用,而不是 LineSegmentList 消息。我怀疑这是一个 python 问题而不是 ROS 问题,如果有人能提供任何帮助,我们将不胜感激!

我正在按照在 http://wiki.ros.org/message_filters 中找到的示例进行操作,他们将两个不同的主题 image 和 cameraInfo 传递给回调函数

  1 import message_filters
   2 from sensor_msgs.msg import Image, CameraInfo
   3 
   4 def callback(image, camera_info):
   5   # Solve all of perception here...
   6 
   7 image_sub = message_filters.Subscriber('image', Image)
   8 info_sub = message_filters.Subscriber('camera_info', CameraInfo)
   9 
  10 ts = message_filters.TimeSynchronizer([image_sub, info_sub], 10)
  11 ts.registerCallback(callback)
  12 rospy.spin()

您将 class 名称与对象名称混淆了。 对您的程序的修复应该是直截了当的。 我已将 Pose2D 更改为 pose2d,并将 LineSegmentList 更改为 linesegmentlist,并用 # --- Change ...:

进行了注释
#!/usr/bin/env python
import message_filters
import rospy
from laser_line_extraction.msg import LineSegmentList
from geometry_msgs.msg import Pose,  Pose2D
from std_msgs.msg import Int32, Float32

rospy.init_node('simul-subscriber')

# --- CHANGE: using proper object names instread of class names
def callback(pose2d, linesegmentlist): 
 pose = Pose()
 # --- CHANGE: using the object
 pose.position.x =  pose2d.position.x 
 pose.position.y =  pose2d.position.y 
 pose.position.z = 0
 #print(pose2d,  linesegmentlist, pose)
 print(pose)
 print(linesegmentlist)

line_segment_sub = message_filters.Subscriber('/line_segments', LineSegmentList)
pose_2D_sub = message_filters.Subscriber('/pose2D',Pose2D)

ts = message_filters.ApproximateTimeSynchronizer([line_segment_sub, pose_2D_sub], 10, 0.1, allow_headerless=True)
ts.registerCallback(callback)

rospy.spin()