Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Commit 5c31a86

Browse files
authored
Merge pull request #35 from Cardinal-Space-Mining/recorder-fix
changed bag recording to be more resiliant to e-stop (hopefully)
2 parents cb94331 + 94a453c commit 5c31a86

3 files changed

Lines changed: 72 additions & 5 deletions

File tree

src/scripts/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# HOW TO RUN MULTIBAGGER SCRIPT:
2+
- From root, enter the directory you want the output to be in
3+
- Recommended: this directory (`cd src/scripts`)
4+
- Run the python script: `python3 multibagger.py`
5+
- It will save a new bag every 60 seconds
6+
- To end and ensure the final slice gets saved properly, **press the enter key** in the terminal <u>before</u> ^C or removing power from the robot

src/scripts/multibagger.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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()

src/scripts/recorder.bash

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@
33
# Specify the topics you want to record
44
TOPICS="/cloud_all_fields_fullframe /filtered_imu /filtered_cloud /ImageRight /ImageLeft /ImageCenter"
55

6-
# Specify the filename for the bag file
7-
BAG_FILE="my_bagfile"
8-
9-
# Record messages
10-
ros2 bag record -o $BAG_FILE $TOPICS
6+
# Record all specified topics, split into seperate files every 300 seconds, compress files
7+
ros2 bag record $TOPICS -d 60 --compression-mode file --compression-format zstd

0 commit comments

Comments
 (0)