ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

How to get topic from a message

asked 2013-08-01 12:00:04 -0600

lourd gravatar image

I'm trying to write a custom bagging node for my ROS installation. The node's requirements are to only bag topics that are published on the local machine (my ROS installation runs on a 3-computer setup), to throttle the bagging rate based on system load and free disk space, and to only bag topics specific to the state of the robot. This means that subscriptions are being created and destroyed on-the-fly, and therein lies my problem.

For creating and destroying the subscriptions, I have these 2 lines of code:

map(lambda x: x.unregister(), SUBSCRIPTIONS)
SUBSCRIPTIONS = map(lambda topic: rospy.Subscriber(topic, AnyMsg, bag), TOPICS)

The top line unsubscribes everything and the bottom one makes all new ones. As you can see, each subscription has a callback method of bag. In that method, I was hoping to just be able to write the message to a bag, independent of the topic. I didn't realize until now that rosbag.Bag.write() requires both the message _and_ the topic.

So, what's the easiest way of getting the message publisher's topic type? In addition, is there any easier way of doing what I've described?

Thanks for your help!
-lourd

edit retag flag offensive close merge delete

Comments

2 Answers

Sort by ยป oldest newest most voted
2

answered 2013-08-01 16:22:57 -0600

forrestv gravatar image

Make bag take two arguments, the topic name and the message, and then change that code to:

map(lambda x: x.unregister(), SUBSCRIPTIONS)
SUBSCRIPTIONS = map(lambda topic: rospy.Subscriber(topic, AnyMsg, lambda msg: bag(topic, msg)), TOPICS)
edit flag offensive delete link more

Comments

Ah, yes, beautiful! Thank you!

lourd gravatar image lourd  ( 2013-08-01 16:51:48 -0600 )edit
0

answered 2013-08-05 05:29:57 -0600

lourd gravatar image

For future viewer reference, the second line could also be written as:

SUBSCRIPTIONS = map(lambda topic: rospy.Subscriber(topic, AnyMsg, callback=bag, callback_args=topic), TOPICS)

with or without the "callback" and "callback_args". I would say that this follows the ROS convention more closely than the previous answer, although it is excellent and also works.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-08-01 12:00:04 -0600

Seen: 1,023 times

Last updated: Aug 05 '13