话题包所在的终端输入 python 需要修复的包名,bag /需要修复的话题名
修复需要一定的时间,修复完成后输出finished。所在目录下会出现修复后的包名
#!/usr/bin/env python3
import rospy
import os
import sys
import rosbag
def print_usage():
print("Usage: fixbag.py <ros bag> <target topic>")
print("Usage: fixbag.py recorded.bag /robot/imu")
if __name__ == "__main__":
# 参数块需要输入三个,分别是一下三个
if len(sys.argv) != 3:
print_usage()
exit(-1)
bag_path = sys.argv[1] # 第一个是修复包的名称
target_topic = sys.argv[2] # 修复包里所包含的需要修复的话题
repair_path = bag_path.replace(".bag", "-repaired.bag") # 完成修复后输出包的名称加-reapired
# Open bag
bag = rosbag.Bag(bag_path, 'r')
fix_bag = rosbag.Bag(repair_path, "w") # Create a repaired bag
# Iterate through bag
for topic, msg, t in bag.read_messages():
# Add time back to the target message header
if topic == target_topic:
msg.header.stamp.secs = t.secs
msg.header.stamp.nsecs = t.nsecs
# Write message to bag
fix_bag.write(topic, msg, t)
# Close bag - Very important else you'll have to reindex it
fix_bag.close()
print("finished")