rosbag recorder for ros1;
循环录制rosbag包,python3.x,运行于ros noetic,测试通过;
有任何问题,请联系zw_1520@163.com
#! /usr/bin/env python3
import rosbag
import rospy
import argparse
import logging
from datetime import datetime
import roslib.message
from threading import Lock
import signal, sys
import os
import zipfile
logging.basicConfig(level=logging.DEBUG)
class RosbagRecorder(object):
def __init__(self, output_bag, topic_infos):
self.__output_bag = output_bag
self.__topic_infos = topic_infos
self.__subscribers = []
self.__Lock = Lock()
self.__bag = rosbag.Bag(self.__output_bag, "w")
signal.signal(signal.SIGINT, self.signal_handler)
self.__is_recorder = True
# msg must be placed before topic !!
def topic_callback(self, msg, topic):
if not self.__is_recorder:
return
self.__Lock.acquire()
try:
self.__bag.write(topic, msg, rospy.Time.now())
except Exception as e:
rospy.logerr("%s" % e)
self.__Lock.release()
def start_recorder(self):
self.__is_recorder = True
for topic, topic_type in self.__topic_infos:
msg_class = roslib.message.get_message_class(topic_type)
#rospy.loginfo(topic)
#rospy.loginfo(topic_type)
self.__subscribers.append(
rospy.Subscriber(topic, msg_class, self.topic_callback, topic))
def signal_handler(self, sig, frame):
rospy.loginfo("ctrl+c, close bag")
self.__Lock.acquire()
self.__bag.close()
self.__Lock.release()
sys.exit(0)
def stop_recorder(self):
rospy.loginfo("stop record, close bag")
self.__Lock.acquire()
self.__is_recorder = False
self.__bag.close()
self.__Lock.release()
def compress_file(self, file_path, zip_path):
"""zip file"""
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
zipf.write(file_path, arcname=file_path)
rosbag_record_save_num = 10
rosbag_record_time = 15 * 60
if __name__ == "__main__":
rospy.init_node("bag_recorder")
bag_record_list = []
bag_zip_record_list = []
while True:
time_string = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
output_bag = time_string + ".bag"
output_bag_dir = os.environ['HOME'] + "/rosbag_data/"
if not os.path.exists(output_bag_dir):
os.makedirs(output_bag_dir)
output_bag = output_bag_dir + output_bag
zip_file_path = os.environ[
'HOME'] + "/rosbag_data/" + time_string + ".zip"
bag_record_list.append(output_bag)
bag_zip_record_list.append(zip_file_path)
if len(bag_record_list) > rosbag_record_save_num + 1:
rospy.loginfo(bag_record_list)
del_file = bag_record_list.pop(0)
rospy.loginfo("del bag: " + del_file)
if os.path.exists(del_file):
os.remove(del_file)
rospy.loginfo("remove bag: " + del_file)
if len(bag_zip_record_list) > rosbag_record_save_num + 1:
del_file = bag_zip_record_list.pop(0)
if os.path.exists(del_file):
os.remove(del_file)
rospy.loginfo("remove zip: " + del_file)
rospy.loginfo(output_bag)
topic_infos = []
topic_infos = rospy.get_published_topics()
#rospy.loginfo(topic_infos)
rospy.loginfo("start recorder...")
recorder = RosbagRecorder(output_bag, topic_infos)
recorder.start_recorder()
rospy.sleep(rosbag_record_time)
recorder.stop_recorder()
recorder.compress_file(output_bag, zip_file_path)
rospy.spin()