Batch rosbag record from python
I'm trying to batch process bagfiles using rosbag from within python.
My process is - use multiprocess to open two processes - one that calls the roslaunch I want to record output from, and one that calls rosbag record multiple times to capture different sections of the output.
However, this does not work because I can't seem to kill rosbag record nicely from python.
Here's the minimal code to show an example of what I'm trying to do:
#!/usr/bin/env python
from rosbag import rosbag_main
from multiprocessing import Process
import time
import sys
import os
import signal
import rospy
if __name__ == "__main__":
#if no roscore is initialized, start one
if not rospy.core.is_initialized():
print "No rosmaster found, "
roscore = Process(target = os.system, args = ["roscore"])
roscore.start()
time.sleep(1)
num_run = 0
roslaunch = Process(target = os.system, args = ["roslaunch testing_scripts elapsed_time.launch"])
roslaunch.start()
while num_run<3:
start_bag_time = time.time()
print "Starting on "+str(num_run)
write = Process(target = rosbag_main.record_cmd, args = [['-a']])
#start the rosbag record process
write.start()
while time.time()-start_bag_time<5:
time.sleep(0.01)
while write.is_alive():
time.sleep(5)
os.kill(int(write.pid),signal.SIGKILL)
print("waiting for write to die")
print("killed write")
rospy.logwarn("done with "+str(num_run))
num_run += 1
#kill the roslaunch process
if roslaunch.is_alive():
roslaunch.terminate()
print "killing publisher"
#kill the roscore process, if you started it
if roscore.is_alive():
roscore.terminate()
print "killing roscore"
elapsed_launch.launch just publishes a string message containing the elapsed time since the node was initiated once every 100 ms.
I would expect that this script would create three bag files, one for each 5 second chunk in the total operation time of the script.
However, I can't seem to kill the rosbag record process nicely. I've tried SIGINT, it doesn't kill the node. SIGKILL allows the script to continue, however, this does not actually kill the node. At the end of the script, if I look at my processes, I have:
cholloway@nb-cholloway:~/workspace/blackwidow$ ps aux | grep record
chollow+ 4931 0.4 0.0 1306408 11576 pts/3 Sl 17:04 0:00 /opt/ros/indigo/lib/rosbag/record --buffsize 256 --chunksize 768 --all
chollow+ 5026 0.3 0.0 1306408 11332 pts/3 Sl 17:04 0:00 /opt/ros/indigo/lib/rosbag/record --buffsize 256 --chunksize 768 --all
chollow+ 5188 0.3 0.0 1306408 11428 pts/3 Sl 17:05 0:00 /opt/ros/indigo/lib/rosbag/record --buffsize 256 --chunksize 768 --all
How can I nicely kill rosbag record? Is there an easier way to achieve this batch processing of rosbags?
Asked by CatherineH on 2015-07-29 16:14:59 UTC
Answers
This doesn't answer your question directly, but if you want bags split up by time, you can use the --split
and --duration
options of rosbag record
.
Asked by Dan Lazewatsky on 2015-07-29 17:04:31 UTC
Comments
yeah, unfortunately this is not the answer I'm looking for. Splitting things up by time is just an example for demonstration purposes, I actually want to trigger an end to rosbag record at arbitrary times.
Asked by CatherineH on 2015-07-30 07:49:56 UTC
Comments