with Bag('test.bag') as bag: for topic, msg, t in bag.read_messages(topics=['chatter', 'numbers']): print msg
It also adds in its own rosh-isms, which are described below.
Retrieving messages by topic
The topics attribute gives you a tab-completable index of topics within a bag file. You can iterate over a topic to pull messages from the bag and you can also get the message class of a topic.
In : b = Bag('chatter.bag') In : type(b.topics.chatter) Out: <class 'rosh.impl.bag.BagTopic'> In : rostype(b.topics.chatter) Out: <class 'std_msgs.msg._String.String'> In : for topic, message, t in b.topics.chatter: ...: print message, t data: hello world 1286482774.5 1286482774501392502 data: hello world 1286482774.5 1286482774501435969 data: hello world 1286482774.6 1286482774601234579 data: hello world 1286482774.6 1286482774601377595 data: hello world 1286482774.7 1286482774701203314 data: hello world 1286482774.7 1286482774701237932
Recording topics (Experimental)
EXPERIMENTAL: this does not have the same performance as rosbag record and should only be used for low-data topics.
You can setup a bag file to record a ROSH topic. You can start and stop the recording as you please.
bag = Bag('test.bag', 'w') bag.record(topics.chatter) bag.record(topics.rosout_agg) bag.stop() bag.start() bag.close()