Skip to content

Commit

Permalink
IMPROVEMENT: Remove code duplication, make it easier to add modular c…
Browse files Browse the repository at this point in the history
…ommand line parameters
  • Loading branch information
amilcarlucas committed Apr 29, 2024
1 parent 5d931a3 commit 97af800
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 62 deletions.
30 changes: 3 additions & 27 deletions MethodicConfigurator/ardupilot_methodic_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from logging import info as logging_info
from logging import warning as logging_warning
from logging import error as logging_error
from os import getcwd as os_getcwd
from sys import exit as sys_exit

from backend_filesystem import LocalFilesystem
Expand Down Expand Up @@ -52,32 +51,9 @@ def argument_parser():
'flight controller. '
'When "Skip" is pressed, it skips to the next intermediate parameter file. '
'The process gets repeated for each intermediate parameter file.')
parser.add_argument('--device',
type=str,
default="",
help='MAVLink connection string to the flight controller. Defaults to autodetection'
)
parser.add_argument('-r', '--reboot-time',
type=int,
default=7,
help='Flight controller reboot time. '
'Default is %(default)s')
parser.add_argument('-t', '--vehicle-type',
choices=['AP_Periph', 'AntennaTracker', 'ArduCopter', 'ArduPlane',
'ArduSub', 'Blimp', 'Heli', 'Rover', 'SITL'],
default='',
help='The type of the vehicle. Defaults to ArduCopter')
parser.add_argument('--vehicle-dir',
type=str,
default=os_getcwd(),
help='Directory containing vehicle-specific intermediate parameter files. '
'Defaults to the current working directory')
parser.add_argument('--n',
type=int,
default=-1,
help='Start directly on the nth intermediate parameter file (skips previous files). '
'Default is to start on the file next to the last that you wrote to the flight controller.'
'If file does not exist, it will start on the first file.')
parser = FlightController.add_argparse_arguments(parser)
parser = LocalFilesystem.add_argparse_arguments(parser)
parser = ComponentEditorWindow.add_argparse_arguments(parser)
parser.add_argument('--loglevel',
type=str,
default='INFO',
Expand Down
20 changes: 20 additions & 0 deletions MethodicConfigurator/backend_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,23 @@ def get_start_file(self, explicit_index: int):
logging_warning("Last written file is the last file in the list. Starting from there.")
start_file_index = len(files) - 1
return files[start_file_index]

@staticmethod
def add_argparse_arguments(parser):
parser.add_argument('-t', '--vehicle-type',
choices=['AP_Periph', 'AntennaTracker', 'ArduCopter', 'ArduPlane',
'ArduSub', 'Blimp', 'Heli', 'Rover', 'SITL'],
default='',
help='The type of the vehicle. Defaults to ArduCopter')
parser.add_argument('--vehicle-dir',
type=str,
default=os_getcwd(),
help='Directory containing vehicle-specific intermediate parameter files. '
'Defaults to the current working directory')
parser.add_argument('--n',
type=int,
default=-1,
help='Start directly on the nth intermediate parameter file (skips previous files). '
'Default is to start on the file next to the last that you wrote to the flight controller.'
'If the file does not exist, it will start on the first file.')
return parser
15 changes: 15 additions & 0 deletions MethodicConfigurator/backend_flightcontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,3 +644,18 @@ def __classify_vehicle_type(mav_type_int):

# Return the classified vehicle type based on the MAV_TYPE enum
return mav_type_to_vehicle_type.get(mav_type_int, None)

@staticmethod
def add_argparse_arguments(parser):
parser.add_argument('--device',
type=str,
default="",
help='MAVLink connection string to the flight controller. If set to "none" no connection is made.'
' Defaults to autodetection'
)
parser.add_argument('-r', '--reboot-time',
type=int,
default=7,
help='Flight controller reboot time. '
'Default is %(default)s')
return parser
20 changes: 10 additions & 10 deletions MethodicConfigurator/frontend_tkinter_component_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,13 @@ def argument_parser():
argparse.Namespace: An object containing the parsed arguments.
"""
parser = ArgumentParser(description='')
parser.add_argument('--vehicle-dir',
type=str,
default=LocalFilesystem.getcwd(),
help='Directory containing vehicle-specific intermediate parameter files. '
'Defaults to the current working directory')
parser = LocalFilesystem.add_argparse_arguments(parser)
parser = ComponentEditorWindow.add_argparse_arguments(parser)
parser.add_argument('--loglevel',
type=str,
default='INFO',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
help='Logging level (default is INFO).')
parser.add_argument('-t', '--vehicle-type',
choices=['AP_Periph', 'AntennaTracker', 'ArduCopter', 'ArduPlane',
'ArduSub', 'Blimp', 'Heli', 'Rover', 'SITL'],
default='ArduCopter',
help='The type of the vehicle. Defaults to ArduCopter')
parser.add_argument('-v', '--version',
action='version',
version=f'%(prog)s {VERSION}',
Expand Down Expand Up @@ -186,6 +178,14 @@ def save_data(self):
logging_info("Data saved successfully.")
self.root.destroy()

@staticmethod
def add_argparse_arguments(parser):
parser.add_argument('--skip-component-editor',
action='store_true',
help='Skip the component editor window. Only use this if all components have been configured. '
'Default to false')
return parser


if __name__ == "__main__":
args = argument_parser()
Expand Down
11 changes: 1 addition & 10 deletions MethodicConfigurator/frontend_tkinter_connection_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,7 @@ def argument_parser():
"""
parser = ArgumentParser(description='This main is for testing and development only, '
'usually the ConnectionSelectionWidgets is called from another script')
parser.add_argument('--device',
type=str,
default="",
help='MAVLink connection string to the flight controller. Defaults to autodetection'
)
parser.add_argument('-r', '--reboot-time',
type=int,
default=7,
help='Flight controller reboot time. '
'Default is %(default)s')
parser = FlightController.add_argparse_arguments(parser)
parser.add_argument('--loglevel',
type=str,
default='INFO',
Expand Down
16 changes: 1 addition & 15 deletions MethodicConfigurator/frontend_tkinter_directory_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,21 +299,7 @@ def argument_parser():
"""
parser = ArgumentParser(description='This main is for testing and development only, '
'usually the VehicleDirectorySelectionWindow is called from another script')
parser.add_argument('-t', '--vehicle-type',
choices=['AP_Periph', 'AntennaTracker', 'ArduCopter', 'ArduPlane',
'ArduSub', 'Blimp', 'Heli', 'Rover', 'SITL'],
default='ArduCopter',
help='The type of the vehicle. Defaults to ArduCopter')
parser.add_argument('--vehicle-dir',
type=str,
default=LocalFilesystem.getcwd(),
help='Directory containing vehicle-specific intermediate parameter files. '
'Defaults to the current working directory')
parser.add_argument('--n',
type=int,
default=0,
help='Start directly on the nth intermediate parameter file (skips previous files). '
'Default is %(default)s')
parser = LocalFilesystem.add_argparse_arguments(parser)
parser.add_argument('--loglevel',
type=str,
default='INFO',
Expand Down

0 comments on commit 97af800

Please sign in to comment.