Ask Your Question

rosbag API - Problems writing .bag files in a different thread

asked 2018-06-22 04:56:21 -0600

Eduardo Alvarado gravatar image

updated 2018-06-22 10:34:07 -0600

mgruhler gravatar image

I will try to explain briefly. I am programming a tool to cut .bag files, for example, according to an initial and ending time. Everything is handled by a GUI. In this interface, there is a progress-bar which shows the time-progress (which it may take a while if the .bag is large). Once the output .bag file is created with the desired messages in the new time range, it displays a description in the GUI.

To do so, I need multiple-threads. I have a thread where I perform the cutting process. The problem is that it does not write anything into the output .bag file. It obtains the Input .bag and creates an output .bag, but this last one is totally empty.

This is the thread, just part of my code:

class TaskThread(QtCore.QThread):
    # Signals to communicate with GUI
    taskFinished = QtCore.pyqtSignal()    
    outputdescriptionChanged = QtCore.pyqtSignal(str)

    def __init__(self):

    def __del__(self):

    def run(self):
            print "Executing functions within THREAD..."
            # Function to convert times to UNIX timestamps
            new_start_datetime_unix, new_end_datetime_unix = time_converter_to_unix(self.start_dt, self.start_dt) # --> Function outside the thread to obtains times from GUI (works fine)

            # Input/Output .bag files
            print ""
            print "Input .bag file path: ", self.input_dt
            print "Output .bag file path: ", self.output_dt

            # Create new .bag file and write every message between both times 
            print "Starting to write"
            with rosbag.Bag(self.output_dt, 'w') as outbag:
                for topic, msg, t in rosbag.Bag(self.input_dt).read_messages(topics = self.topics_dt, start_time = rospy.Time.from_sec(new_start_datetime_unix), end_time = rospy.Time.from_sec(new_end_datetime_unix)):
                    outbag.write(topic, msg, t)
                    print "Writing..."
            print "Writing finished"

            # Create .txt file with output .bag description
            output_txtfile(self, self.output_dt) # <--- Function outside the thread to write txt files (works fine)
            print "Finishing functions within THREAD..."

            print "Finishing thread..."

Result: The terminal prints "Starting to write..." and directly goess to "Writing finished". It creates even the output .bag (so the line with with rosbag.Bag(self.output_dt, 'w') as outbag: it works). But it does not write.

The same code without threads works perfectly and writes the messages correclty.

Is there some problem writing .bag files inside threads? Did you experience something similar using the rosbag API?


edit retag flag offensive close merge delete


just to make sure... The intendation starting from outbag.write(... is off. I guess this is just a copy'n'paste error? Also, Do you get the Writing... output?Otherwise, it might be you are not actually entering the for loop for whatever reason (maybe the times are off?)

mgruhler gravatar image mgruhler  ( 2018-06-22 05:52:07 -0600 )edit

Yes, it was a copy/paste error. outbag.write is inside the for topic, msg, t... loop, and next to him the Writing... This is not printed (it goes from Starting to write to Writing finished directly). So, it does not enter in the for, that is the problem, but I´m still don´t know why :(

Eduardo Alvarado gravatar image Eduardo Alvarado  ( 2018-06-22 06:00:59 -0600 )edit

Can you check whether it works without the topics, start_time and end_time arguments to read_messages?

kartikmohta gravatar image kartikmohta  ( 2018-06-22 21:39:10 -0600 )edit

Yes! It works fine. Pretty interesting. Why do you think the thread can´t access those arguments? For example, in topics = self.topics_dt, the topics_dt is a variable which I already decleared outside the thread, like many others (self.myThread.topics_dt = self.selected_topics). Thanks!

Eduardo Alvarado gravatar image Eduardo Alvarado  ( 2018-06-25 00:54:22 -0600 )edit

You might want to make sure those variables that are being passed to read_messages are set properly, specially the start and end times. rosbag info can tell you the start and end timestamps for messages in the bag.

kartikmohta gravatar image kartikmohta  ( 2018-06-25 01:05:32 -0600 )edit

Update: The error comes from both, start_time and end_time, the topics are fine. @kartikmohta: The same code used out of a thread works fine, so I think they are not being passed properly. I´ll investigate it. Thanks

Eduardo Alvarado gravatar image Eduardo Alvarado  ( 2018-06-25 01:06:33 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2018-06-25 01:16:16 -0600

Eduardo Alvarado gravatar image

updated 2018-06-25 03:24:03 -0600

jayess gravatar image

Problem solved! Pretty silly mistake.

new_start_datetime_unix, new_end_datetime_unix = time_converter_to_unix(self.start_dt, self.start_dt)

I was putting as start and end time the same one. Obviously there was no time frame to write. Now it works fine. Thank you guys!

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2018-06-22 04:56:21 -0600

Seen: 482 times

Last updated: Jun 25 '18