From e60a09b703d98497ad556261c6b890b6c449f862 Mon Sep 17 00:00:00 2001 From: RoseKapps Date: Thu, 29 Feb 2024 18:59:30 +0100 Subject: [PATCH] service file done for blackbox --- mission/blackbox/blackbox.sh | 13 ++ .../blackbox/blackbox_startup copy.service | 149 ++++++++++++++++++ mission/blackbox/blackbox_startup.service | 15 ++ mission/blackbox/record_data.py | 24 +-- 4 files changed, 191 insertions(+), 10 deletions(-) create mode 100644 mission/blackbox/blackbox.sh create mode 100644 mission/blackbox/blackbox_startup copy.service create mode 100644 mission/blackbox/blackbox_startup.service diff --git a/mission/blackbox/blackbox.sh b/mission/blackbox/blackbox.sh new file mode 100644 index 00000000..0f3d5c12 --- /dev/null +++ b/mission/blackbox/blackbox.sh @@ -0,0 +1,13 @@ +#!/bin/bash +# Determine the directory where the script is located +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" + +# Source ROS 2 setup +source /opt/ros/humble/setup.bash + +# Export ROS environment variables +export ROS_DOMAIN_ID=0 +export ROS_LOCALHOST_ONLY=0 + +# Execute the Python script, using the script's directory to find it +exec python3 "$SCRIPT_DIR/record_data.py" \ No newline at end of file diff --git a/mission/blackbox/blackbox_startup copy.service b/mission/blackbox/blackbox_startup copy.service new file mode 100644 index 00000000..d9decadf --- /dev/null +++ b/mission/blackbox/blackbox_startup copy.service @@ -0,0 +1,149 @@ +[Unit] +Description=launch recording of data +After=network.target + +[Service] +ExecStart= python3 /home/vortex/rose_test_ws/src/vortex-asv/mission/blackbox/record_data.py +RemainAfterExit=yes +WorkingDirectory=/home/vortex/rose_test_ws +StandardOutput=journal+console + + +Environment="HOME=root" +Environment="ROS_DOMAIN_ID=1" +Environment="ROS_LOCALHOST_ONLY=1" +Environment="_colcon_cd_root=/top/ros/rolling/" + +User=vortex + +Restart=no +RestartSec=2 + +[Install] +WantedBy=multi-user.target + +-------------------------------------------------------------------------------------------- + +[Unit] +Description=launch recording of data +After=network.target + +[Service] +ExecStart=/usr/bin/python3 /home/vortex/rose_test_ws/src/vortex-asv/mission/blackbox/record_data.py +RemainAfterExit=yes +WorkingDirectory=/home/vortex/rose_test_ws +StandardOutput=journal+console + + +Environment="HOME=root" +Environment="ROS_DOMAIN_ID=1" +Environment="ROS_LOCALHOST_ONLY=1" +Environment="_colcon_cd_root=/top/ros/rolling/" + +User=vortex + +Restart=no +RestartSec=2 + +[Install] +WantedBy=multi-user.target + +--------------------------------------------------------------------------------------------- + +[Unit] +Description=launch recording of data +After=network.target + +[Service] +ExecStart=/bin/bash -c 'source /opt/ros/humble/setup.bash; source /home/vortex/david_test_ws/install/setup.bash; python3 record_data.py' +RemainAfterExit=yes +WorkingDirectory=/home/vortex/rose_test_ws +StandardOutput=journal+console + + +Environment="HOME=root" +Environment="ROS_DOMAIN_ID=1" +Environment="ROS_LOCALHOST_ONLY=1" +Environment="_colcon_cd_root=/top/ros/rolling/" + +User=vortex + +Restart=no +RestartSec=2 + +[Install] +WantedBy=multi-user.target + +--------------------------------------------------------------------------------------------- + +[Unit] +Description=launch ros2 bms node +After=network.target + +[Service] +ExecStart=/bin/bash -c 'source /opt/ros/humble/setup.bash; source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash; source /usr/share/colcon_cd/function/colcon_cd.sh colcon build; source /home/vortex/david_test_ws/install/setup.bash; ros2 launch bms bms_launch.py; sleep 60' +RemainAfterExit=yes +WorkingDirectory=/home/vortex/david_test_ws +StandardOutput=journal+console + + +Environment="HOME=root" +Environment="ROS_DOMAIN_ID=1" +Environment="ROS_LOCALHOST_ONLY=1" +Environment="_colcon_cd_root=/top/ros/rolling/" + +User=vortex + +Restart=no +RestartSec=2 + +[Install] +WantedBy=multi-user.target +--------------------------------------------------------------------------------------------- + +[Unit] +Description=My Custom Service +After=network.target + +[Service] +ExecStart=/path/to/your/executable +Restart=always + +[Install] +WantedBy=default.target + + + + + + + + + + + +------------------------------------------------------- + +[Unit] +Description=launch recording of data +After=network.target + +[Service] +ExecStart=/bin/bash -c 'source /opt/ros/humble/setup.bash; source /home/vortex/rose_test_ws/install/setup.bash; python3 /home/vortex/rose_test_ws/src/vortex-asv/mission/blackbox/record_data.py' +RemainAfterExit=yes +WorkingDirectory=/home/vortex/rose_test_ws +StandardOutput=journal+console + + +Environment="HOME=root" +Environment="ROS_DOMAIN_ID=1" +Environment="ROS_LOCALHOST_ONLY=1" +Environment="_colcon_cd_root=/top/ros/rolling/" + +User=vortex + +Restart=no +RestartSec=2 + +[Install] +WantedBy=multi-user.target diff --git a/mission/blackbox/blackbox_startup.service b/mission/blackbox/blackbox_startup.service new file mode 100644 index 00000000..1c7b666d --- /dev/null +++ b/mission/blackbox/blackbox_startup.service @@ -0,0 +1,15 @@ +[Unit] +Description=Launch recording of ROS 2 data +After=network.target + +[Service] +# Use the wrapper script for ExecStart +ExecStart=/bin/bash //src/vortex-asv/mission/blackbox/blackbox.sh +WorkingDirectory=/home/vortex/ +StandardOutput=journal+console +User=vortex +Restart=no +RestartSec=2 + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/mission/blackbox/record_data.py b/mission/blackbox/record_data.py index bc11a88b..c777e065 100644 --- a/mission/blackbox/record_data.py +++ b/mission/blackbox/record_data.py @@ -1,4 +1,3 @@ -import rclpy import subprocess import time import os @@ -8,8 +7,10 @@ class ROSMonitor: def __init__(self): # timestamp variable is a string that represents the current date and time in the format 'YYYY-MM-DD HH:MM:SS' - timestamp = time.strftime('%Y-%m-%d_%H:%M:%S') - self.bag_file = 'rosdata_' + timestamp + '.bag' + timestamp = time.strftime('%Y-%m-%d_%H:%M:%S') + self.bag_directory = '/home/vortex/rose_test_ws/src/vortex-asv/mission/blackbox/' + self.bag_name = 'rosdata_' + timestamp + '.bag' + self.bag_file = self.bag_directory + self.bag_name self.topics = [ #indicate there all the topics you want to record '/joy', '/joystick/joy', @@ -33,16 +34,19 @@ def __init__(self): '/tf', '/tf_static' ] + + #subprocess.run(['source', '/opt/ros/humble/setup.bash'], text=True) #to source the ros setup files def monitor_nodes_and_topics(self): # Record bag data -> then to see the data we just recorded we use the command "ros2 bag play " + # subprocess.run(['/home/vortex/rose_test_ws/src/vortex-asv/mission/blackbox']) subprocess.run(['ros2', 'bag', 'record', '-o', self.bag_file] + self.topics, text=True) #the name of the bagfile is the date and hour in the format 'rosdata_YYYY-MM-DD_HH:MM:SS.bag' - def delete_old_bags(self, days=1, max_size_kb=300): #adjust the max size before you start deleting old files (1 000 000 kb = 1 000 mb = 1 gb) + def delete_old_bags(self, days=1, max_size_kb=3000000): #adjust the max size before you start deleting old files (1 000 000 kb = 1 000 mb = 1 gb) print('Entering the delete_old_bags function') current_time = datetime.now() older_than_time = current_time - timedelta(days=days) @@ -52,7 +56,7 @@ def delete_old_bags(self, days=1, max_size_kb=300): #adjust the max size # List all directories in the current directory - directories_in_current_directory = [d for d in os.listdir('.') if os.path.isdir(d)] + directories_in_current_directory = [d for d in os.listdir(self.bag_directory) if os.path.isdir(os.path.join(self.bag_directory, d))] print('List of directories in the current directory:') print(directories_in_current_directory) @@ -70,7 +74,7 @@ def delete_old_bags(self, days=1, max_size_kb=300): #adjust the max size continue if directory_time < older_than_time: - directory_path = os.path.join('.', directory_name) + directory_path = os.path.join(self.bag_directory, directory_name) shutil.rmtree(directory_path) print(f"Deleted old directory: {directory_path}") @@ -83,7 +87,7 @@ def delete_old_bags(self, days=1, max_size_kb=300): #adjust the max size # Update the list of directories after deleting old directories - directories_in_current_directory = [d for d in os.listdir('.') if os.path.isdir(d)] + directories_in_current_directory = [d for d in os.listdir(self.bag_directory) if os.path.isdir(os.path.join(self.bag_directory, d))] # Calculate total size of remaining directories ON total_size_kb = sum(self.get_directory_size(d) for d in directories_in_current_directory if (d.startswith('rosdata_') and d.endswith('.bag')) ) / 1024 @@ -102,18 +106,18 @@ def delete_old_bags(self, days=1, max_size_kb=300): #adjust the max size break oldest_directory = sorted_directories[0] - oldest_directory_path = os.path.join('.', oldest_directory) + oldest_directory_path = os.path.join(self.bag_directory, oldest_directory) print(f"Deleting the oldest directory: {oldest_directory_path}") shutil.rmtree(oldest_directory_path) # Update the list of directories and total size - directories_in_current_directory = [d for d in os.listdir('.') if os.path.isdir(d)] + directories_in_current_directory = [d for d in os.listdir(self.bag_directory) if os.path.isdir(os.path.join(self.bag_directory, d))] total_size_kb = sum(self.get_directory_size(d) for d in directories_in_current_directory if (d.startswith('rosdata_') and d.endswith('.bag')) ) / 1024 print(f'Now the total size of directories is: {total_size_kb:.2f} KB') def get_directory_size(self, directory): - directory_path = os.path.join('.', directory) + directory_path = os.path.join(self.bag_directory, directory) total_size = sum(os.path.getsize(os.path.join(directory_path, file)) for file in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, file))) return total_size