Robotics StackExchange | Archived questions

ROSNODE Subscriber for custom datatype Python

I have a rostopic names /scanpoints of type datatypes/scan_point_2018.

This is the custom datatype. The scan_point_2018 is defined in some path /path/..../scan_point_2018.hpp.

I need to write a subscriber for this topic and I tried the following code:

#!/usr/bin/env python import rospy from std_msgs.msg import String

 def callback(data):
     rospy.loginfo("%s Header: %d" % (data.header))

 def listener():
     rospy.init_node('scan_listener', anonymous=True)
     rospy.Subscriber("/scanpoints", String, callback)
     rospy.spin()

 if __name__ == '__main__':
     listener()

As expected it through error:

 [ERROR] [1526286071.031024461]: 
 Client [/scan_listener_6042_1526286069827] wants topic /scan_listener to have datatype/md5sum [std_msgs/String/992ce8a1687cec8c8bd883ec73ca41d1],but our version has [datatypes/scan_point_2018/9af20afc1058845793165634a89c8aa7]. Dropping connection.

I need to know how to call this custom datatype in my code? Do I need to include path of the file scanpoint2018.hpp or any other suggestions please!

Asked by ARM on 2018-05-14 03:47:06 UTC

Comments

Answers

I recommend working through the custom message publisher and subscriber tutorials for python first. The .hpp file you refer to is a C++ header file so it's not what you need if your writing a python script.

You'll need to find the package which defines the message type put it in your workspace if it isn't already and add a dependency to it in. Then you'll need to import it into your script with a line like:

from <package_name>.msg import <message_type_name>

You'll need to look through your packages for a file at a path that looks like:

/.../<package_name>/msgs/scan_point_2018.msg

Hope this helps.

Asked by PeteBlackerThe3rd on 2018-05-14 04:36:30 UTC

Comments

The best and most convenient way is to use Python API. Where you do not need to play the bag file instead call it using Python. Following code I took from ROS Official Site.

import rosbag
bag = rosbag.Bag('test.bag')
for topic, msg, t in bag.read_messages(topics=['chatter', 'numbers']):
    print msg
bag.close()

Asked by ARM on 2018-05-16 13:33:26 UTC

Comments