Robotics StackExchange | Archived questions

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

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):
        QtCore.QThread.__init__(self)

    def __del__(self):
        self.wait()

    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..."
            self.taskFinished.emit()

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?

Thanks,

Asked by Eduardo Alvarado on 2018-06-22 04:56:21 UTC

Comments

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?)

Asked by mgruhler on 2018-06-22 05:52:07 UTC

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 :(

Asked by Eduardo Alvarado on 2018-06-22 06:00:59 UTC

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

Asked by kartikmohta on 2018-06-22 21:39:10 UTC

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!

Asked by Eduardo Alvarado on 2018-06-25 00:54:22 UTC

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.

Asked by kartikmohta on 2018-06-25 01:05:32 UTC

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

Asked by Eduardo Alvarado on 2018-06-25 01:06:33 UTC

Answers

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!

Asked by Eduardo Alvarado on 2018-06-25 01:16:16 UTC

Comments