Files
old-lab_ui/src/bagger.py

50 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python
import rospy
from std_msgs.msg import String
import rospkg
import subprocess
import os
import signal
class Bagger(object):
def __init__(self):
rospy.init_node("bagger", anonymous=True)
rospy.Subscriber("bag_publisher", String, self.callback)
self.pub = rospy.Publisher("bag_notifier", String, queue_size=10)
self.proc = None
rospack = rospkg.RosPack()
self.data_path = os.path.join(rospack.get_path("rosbridge_gui_example"), "data")
def callback(self, msg):
if msg.data == "STOP" and self.proc is not None:
os.killpg(self.proc.pid, signal.SIGINT)
self.pub.publish("STOPPED")
else:
msg_data = msg.data.split()
self.pub.publish(str(len(msg_data)))
self.pub.publish(str(msg_data))
if len(msg_data) == 0: # We only got the name of the bag file and not any topics
bag_file_name = os.path.join(self.data_path, "bag_file")
self.proc = subprocess.Popen(["rosbag", "record",
"--all", "-o", bag_file_name], preexec_fn=os.setsid)
self.pub.publish("STARTED")
elif len(msg_data) == 1: # We only got the name of the bag file and not any topics
bag_file_name = os.path.join(self.data_path, msg_data)
self.proc = subprocess.Popen(["rosbag", "record",
"--all", "-o", bag_file_name], preexec_fn=os.setsid)
self.pub.publish("STARTED")
else: #we posibly have a bag file name and a list of topics
if msg_data[0].startswith("/"): #then we know there is no bag file name
msg_data = ["bagfile"] + msg_data
msg_data[0] = os.path.join(self.data_path, msg_data[0])
processList = ["rosbag", "record", "-o"] + msg_data
self.pub.publish(" ".join(processList))
self.proc = subprocess.Popen( processList, preexec_fn=os.setsid)
self.pub.publish("STARTED")
if __name__ == '__main__':
Bagger()
rospy.spin()