Skip to content

Commit

Permalink
Merge pull request #1 from rm-diego/file_arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
kiranj26 authored Nov 9, 2024
2 parents 4a3c7aa + 8e6c9d3 commit 557e0cc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: main.py",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/src/main.py",
"console": "integratedTerminal",
"args": [
"main",
"..\\data\\test_log.txt",
"..\\data\\test.dbc",
"SAF_SpeedTest"
],
"justMyCode": false
}
]
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ All required packages are listed in the `requirements.txt` file.
2. Run the main script with the appropriate arguments:

```bash
python src/main.py test SAF_SpeedTest
python src/main.py PATH_TO_LOG/example.txt PATH_TO_DBC/example.dbc SignalName StartTime EndTime
```

or
Example:

```bash
python src/main.py test SAF_SpeedTest 43.2 45.6
python src/main.py ../data/test_log.txt ../data/test.dbc SAF_SpeedTest 43.2 45.6
```

## Repository Structure
Expand Down
10 changes: 7 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import matplotlib.pyplot as plt
import argparse

from utils import validate_file

def parse_dbc(file_path):
"""
@brief Parse the DBC file to get CAN message definitions.
Expand Down Expand Up @@ -171,7 +173,9 @@ def plot_signals(parsed_data, signal_name, start_time, end_time):

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Plot CAN signal data")
parser.add_argument('mode', choices=['test', 'main'], help="Mode to run the script in")
#parser.add_argument('mode', choices=['test', 'main'], help="Mode to run the script in") # TODO: Not used for anything rn, see if can remove
parser.add_argument('log', type=str, help="Path to CAN log file")
parser.add_argument('dbc', type=str, help="Path to DBC file")
parser.add_argument('signal', type=str, help="Signal name to plot")
parser.add_argument('start', type=float, nargs='?', default=None, help="Start time for the plot")
parser.add_argument('end', type=float, nargs='?', default=None, help="End time for the plot")
Expand All @@ -180,8 +184,8 @@ def plot_signals(parsed_data, signal_name, start_time, end_time):

# Determine the paths to the DBC and log files based on the mode
script_dir = os.path.dirname(os.path.abspath(__file__))
dbc_file = os.path.join(script_dir, "..", "data", "test.dbc")
log_file = os.path.join(script_dir, "..", "data", "test_log.txt")
dbc_file = validate_file(os.path.join(script_dir, args.dbc), "DBC file")
log_file = validate_file(os.path.join(script_dir, args.log), "CAN log file")

# Parse the DBC file
db = parse_dbc(dbc_file)
Expand Down
15 changes: 15 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os
import sys

def validate_file(file_path, description):
"""
@brief Validate if the given file exists and is a valid file.
@param file_path Path to file
@param description Description of the file for error messages
@return Absolute path of the file if valid, else exits the program
"""
if not os.path.isfile(file_path):
print(f"Error: The {description} '{file_path}' does not exist or is not a file.")
sys.exit(1)
return os.path.abspath(file_path)

0 comments on commit 557e0cc

Please sign in to comment.