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

using rosbag in callback

asked 2018-12-18 12:28:34 -0500

hsrbrchat gravatar image

For my project I am interested in many frames of reference contained in a list called frame_list, each with Pose messages published to their own topic e.g. 'frame_transform'. I am trying to subscribe to all of these topics and use rosbag to save the Poses as they are published. The code below creates the .bag file, but does not write any data into it even though messages are being published to the topics. Is this a good way to achieve what I want? Can anybody spot where I am going wrong? Thanks.

#!/usr/bin/env python  
import roslib
import rospy
import rosbag
from geometry_msgs.msg import Pose

def callback(data, args):

    bag = rosbag.Bag('data.bag', 'w')
    bag.write(str(args), data)

if __name__ == '__main__':

    if rospy.has_param('init_complete'): #check if initialisation is complete
        rospy.init_node('rosbag_writer') #initiate node
        frame_list = rospy.get_param('frame_list') #import list of frames from rosparam
        for frame in frame_list:
            rospy.Subscriber(str(frame) + '_transform', Pose, callback, frame) #check for frame transforms
        rospy.spin()
edit retag flag offensive close merge delete

Comments

Why are you using the rosbag Python API instead of just using rosbag from the command line?

jayess gravatar image jayess  ( 2018-12-18 15:01:03 -0500 )edit

My project is to create a robot task-learning program which is accessible to those with no knowledge of ros or Python, therefore stuff like this must be done using the Python API to minimise user input.

hsrbrchat gravatar image hsrbrchat  ( 2018-12-18 15:05:51 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-12-18 15:34:57 -0500

jayess gravatar image

You could use launch files to start rosbag and your other nodes such as in #q175133 and #q52773, for example.

What I'm seeing from your code, however, is that you're not storing your subscribers at all, just instantiating them in a for loop and then spinning. If this is how you want to do it, try storing them in a list. Something like

subscribers = []
for frame in frame_list:
    subscriber = rospy.Subscriber(str(frame) + '_transform', Pose, callback, frame)
    subscribers.append(subscriber)
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2018-12-18 12:28:34 -0500

Seen: 1,013 times

Last updated: Dec 18 '18