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,
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 theWriting...
output?Otherwise, it might be you are not actually entering thefor
loop for whatever reason (maybe the times are off?)Yes, it was a copy/paste error.
outbag.write
is inside thefor topic, msg, t...
loop, and next to him theWriting...
This is not printed (it goes fromStarting to write
toWriting finished
directly). So, it does not enter in the for, that is the problem, but I´m still don´t know why :(Can you check whether it works without the
topics
,start_time
andend_time
arguments toread_messages
?Yes! It works fine. Pretty interesting. Why do you think the thread can´t access those arguments? For example, in
topics = self.topics_dt
, thetopics_dt
is a variable which I already decleared outside the thread, like many others (self.myThread.topics_dt = self.selected_topics
). Thanks!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.Update: The error comes from both,
start_time
andend_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