Skip to content

Commit

Permalink
service file done for blackbox
Browse files Browse the repository at this point in the history
  • Loading branch information
RoseKapps committed Feb 29, 2024
1 parent d20e3d6 commit e60a09b
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 10 deletions.
13 changes: 13 additions & 0 deletions mission/blackbox/blackbox.sh
Original file line number Diff line number Diff line change
@@ -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"
149 changes: 149 additions & 0 deletions mission/blackbox/blackbox_startup copy.service
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions mission/blackbox/blackbox_startup.service
Original file line number Diff line number Diff line change
@@ -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 <path-to-home-directory>/<your-worskpace-name>/src/vortex-asv/mission/blackbox/blackbox.sh
WorkingDirectory=/home/vortex/
StandardOutput=journal+console
User=vortex
Restart=no
RestartSec=2

[Install]
WantedBy=multi-user.target
24 changes: 14 additions & 10 deletions mission/blackbox/record_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import rclpy
import subprocess
import time
import os
Expand All @@ -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',
Expand All @@ -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 <bag_file>"
# 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)
Expand All @@ -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)

Expand All @@ -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}")

Expand All @@ -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
Expand All @@ -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

Expand Down

0 comments on commit e60a09b

Please sign in to comment.