1+ import subprocess
2+ import os
3+ from datetime import datetime
4+ import threading
5+ import psutil
6+
7+ TOPICS = "/cloud_all_fields_fullframe /filtered_imu /filtered_cloud /ImageRight /ImageLeft /ImageCenter"
8+ ROS_BAG_PROC_INIT = ["ros2" , "wtf" , "bag" , "record" , TOPICS , "--compression-mode" , "file" , "--compression-format" , "zstd" ]
9+
10+ SECONDS_PER_BAG = 10
11+ OVERLAP_SECONDS = 5
12+
13+ recorders = []
14+ running = True
15+
16+ def create_next_bag (bag_num , last_bag ):
17+ global running
18+ if not running :
19+ return
20+ BagRecorder (bag_num , SECONDS_PER_BAG )
21+ print (f"Bag number { bag_num } started, press enter to cancel" )
22+
23+ killer_timer = threading .Timer (OVERLAP_SECONDS , last_bag .end_proc )
24+ killer_timer .start ()
25+
26+
27+ class BagRecorder :
28+ def __init__ (self , num , slice_time ):
29+ recorders .append (self )
30+ self .num = num
31+
32+ # self.sp = subprocess.Popen["ros2", "wtf", "bag", "record", TOPICS, "--compression-mode", "file", "--compression-format", "zstd", "-o", f"ros-bag-slice-{num}"], shell=True)
33+ self .proc = subprocess .Popen (["ros2" , "bag" , "record" ] + TOPICS .split (' ' ) + ["--compression-mode" , "file" , "--compression-format" , "zstd" , "-o" , f"ros-bag-slice-{ num } " ], stdout = subprocess .PIPE )
34+
35+ self .timer = threading .Timer (slice_time , create_next_bag , (num + 1 , self ))
36+ self .timer .start ()
37+
38+ def end_proc (self ):
39+ if self .proc .poll () is None :
40+ self .proc .terminate ()
41+ print (f"Bag number { self .num } killed successfully" )
42+
43+ def cleanup ():
44+ for br in recorders :
45+ br .end_proc ()
46+
47+
48+ def main ():
49+ global running
50+ # folder is created of the format "rosbag-collection-ddmmyy-HHMMSS"
51+ folder_name : str = f"rosbag-collection-" + datetime .now ().strftime ("%d%m%y-%H%M%S" )
52+
53+ subprocess .call (["mkdir" , folder_name ])
54+ os .chdir (folder_name )
55+
56+ BagRecorder (0 , 1 )
57+
58+ input ("Bag recording started, press enter to exit program\n " )
59+ running = False
60+ cleanup ()
61+ print ("Exited successfully" )
62+
63+ if __name__ == "__main__" :
64+ main ()
0 commit comments