Skip to content

Commit

Permalink
merge start and stop button into single one
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimhroob committed Jul 24, 2024
1 parent 0cd9f0a commit 8b2a9ac
Showing 1 changed file with 64 additions and 29 deletions.
93 changes: 64 additions & 29 deletions src/ros2_topic_monitor/ros2_topic_monitor/record.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import sys
import subprocess
import threading
import os
import yaml
import rclpy
from rclpy.node import Node
import tkinter as tk
from tkinter import messagebox
from datetime import datetime
from rclpy import Parameter
import threading
import subprocess
import yaml
import rclpy
from rclpy.node import Node
from rclpy import Parameter
from std_msgs.msg import Empty
from ament_index_python.packages import get_package_share_directory

Expand All @@ -17,18 +16,18 @@ def __init__(self):
super().__init__('bag_recorder_node')

# Params
self.declare_parameter( 'cfg_pth' , Parameter.Type.STRING )
self.declare_parameter('cfg_pth', Parameter.Type.STRING)
self.cfg_pth = self.get_parameter_or('cfg_pth', Parameter('str', Parameter.Type.STRING, '')).value
package_share_directory = get_package_share_directory('ros2_topic_monitor')
cfg_pth_default = os.path.join(package_share_directory,'config', 'cfg_topics_record.yaml')
cfg_pth_default = os.path.join(package_share_directory, 'config', 'cfg_topics_record.yaml')
self.cfg_pth = self.cfg_pth or cfg_pth_default

with open(self.cfg_pth, 'r') as file:
config = yaml.safe_load(file)

self.topics = []
for category in config['topics'].values():
self.topics.extend(category)
self.topics.extend(category)

self.bag_process = None

Expand All @@ -41,7 +40,6 @@ def publish_message(self):
if self.recording_flag:
msg = Empty()
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "rosbag_recording" with Empty message')

def start_recording(self, output_path):
self.recording_flag = True
Expand All @@ -63,53 +61,90 @@ def stop_recording(self):
class BagRecorderGUI:
def __init__(self, recorder_node):
self.recorder_node = recorder_node
self.recording_in_progress = False

self.root = tk.Tk()
self.root.title('ROS2 Bag Recorder')

self.dir = tk.Label(self.root, text="Enter Bag dir:")
self.dir.pack(pady=5)
self.dir_label = tk.Label(self.root, text="Enter Bag Directory:")
self.dir_label.pack(pady=5)

self.bag_dir_entry = tk.Entry(self.root)
self.bag_dir_entry.pack(pady=5)

self.label = tk.Label(self.root, text="Enter Bag Name:")
self.label.pack(pady=5)
self.name_label = tk.Label(self.root, text="Enter Bag Name:")
self.name_label.pack(pady=5)

self.bag_name_entry = tk.Entry(self.root)
self.bag_name_entry.pack(pady=5)

self.start_button = tk.Button(self.root, text='Start Recording', command=self.start_recording)
self.start_button.pack(pady=10)

self.stop_button = tk.Button(self.root, text='Stop Recording', command=self.stop_recording)
self.stop_button.pack(pady=10)
self.toggle_button = tk.Button(self.root, text='Start Recording', command=self.toggle_recording)
self.toggle_button.pack(pady=10)

self.end_session_button = tk.Button(self.root, text='End Session', command=self.end_session)
self.end_session_button.pack(pady=10)

# Status frame to manage background color
self.status_frame = tk.Frame(self.root, width=300, height=10)
self.status_frame.pack(pady=10, fill=tk.X)
self.update_status_color()

def toggle_recording(self):
if self.recording_in_progress:
self.stop_recording()
else:
self.start_recording()

def start_recording(self):
bag_dir = self.bag_dir_entry.get()
bag_name = self.bag_name_entry.get()
bag_dir = self.bag_dir_entry.get().strip()
bag_name = self.bag_name_entry.get().strip()
if not bag_name:
bag_name = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")

if not os.path.isdir(bag_dir):
try:
os.makedirs(bag_dir)
except OSError as e:
messagebox.showerror("Error", f"Failed to create directory '{bag_dir}': {e}")
return

output_path = os.path.join(bag_dir, bag_name)

if os.path.exists(output_path):
proceed = messagebox.askyesno("Warning", f"The bag name '{bag_name}' already exists. Do you want to overwrite it?")
if not proceed:
return

self.recorder_node.start_recording(output_path)
messagebox.showinfo("Info", f"Started recording ROS2 bag to {output_path}.")
try:
self.recorder_node.start_recording(output_path)
self.recording_in_progress = True
self.update_status_color()
self.toggle_button.config(text='Stop Recording')
messagebox.showinfo("Info", f"Started recording ROS2 bag to {output_path}.")
except Exception as e:
messagebox.showerror("Error", f"Failed to start recording: {e}")

def stop_recording(self):
self.recorder_node.stop_recording()
messagebox.showinfo("Info", "Stopped recording ROS2 bag.")
try:
self.recorder_node.stop_recording()
self.recording_in_progress = False
self.update_status_color()
self.toggle_button.config(text='Start Recording')
messagebox.showinfo("Info", "Stopped recording ROS2 bag.")
except Exception as e:
messagebox.showerror("Error", f"Failed to stop recording: {e}")

def end_session(self):
self.stop_recording()
if self.recording_in_progress:
self.stop_recording()
self.root.quit()

def update_status_color(self):
if self.recording_in_progress:
self.status_frame.config(bg='green')
else:
self.status_frame.config(bg='red')

def run(self):
self.root.mainloop()

Expand Down

0 comments on commit 8b2a9ac

Please sign in to comment.