rosbag recorder,ros包录制脚本

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kdlangzi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值