Skip to content

Commit

Permalink
FEATURE: Remember last sucessfully written file to the FC and start f…
Browse files Browse the repository at this point in the history
…rom there on program start
  • Loading branch information
amilcarlucas committed Apr 24, 2024
1 parent 6cb630d commit f808d64
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
17 changes: 4 additions & 13 deletions MethodicConfigurator/ardupilot_methodic_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ def argument_parser():
'Defaults to the current working directory')
parser.add_argument('--n',
type=int,
default=0,
default=-1,
help='Start directly on the nth intermediate parameter file (skips previous files). '
'Default is %(default)s')
'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.add_argument('--loglevel',
type=str,
default='INFO',
Expand Down Expand Up @@ -126,17 +127,7 @@ def main():
vehicle_dir_window = VehicleDirectorySelectionWindow(local_filesystem)
vehicle_dir_window.root.mainloop()

# Get the list of intermediate parameter files files that will be processed sequentially
files = list(local_filesystem.file_parameters.keys())

start_file = None # pylint: disable=invalid-name
if files:
# Determine the starting file based on the --n command line argument
start_file_index = min(args.n, len(files) - 1) # Ensure the index is within the range of available files
if start_file_index != args.n:
logging_warning("Starting file index %s is out of range. Starting with file %s instead.",
args.n, files[start_file_index])
start_file = files[start_file_index]
start_file = local_filesystem.get_start_file(args.n)

component_editor_window = JsonEditorApp(VERSION, local_filesystem)
component_editor_window.root.mainloop()
Expand Down
53 changes: 53 additions & 0 deletions MethodicConfigurator/backend_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,56 @@ def copy_fc_values_to_file(self, selected_file: str, params: Dict[str, float]):
else:
logging_warning("Parameter %s not found in the current parameter file", param)
return ret

def write_last_written_filename(self, current_file: str):
try:
with open(os_path.join(self.vehicle_dir, 'last_written_filename.txt'), 'w', encoding='utf-8') as file:
file.write(current_file)
except Exception as e: # pylint: disable=broad-except
logging_error("Error writing last written filename: %s", e)

def __read_last_written_filename(self) -> str:
try:
with open(os_path.join(self.vehicle_dir, 'last_written_filename.txt'), 'r', encoding='utf-8') as file:
return file.read().strip()
except Exception as e: # pylint: disable=broad-except
logging_error("Error reading last written filename: %s", e)
return ""

def get_start_file(self, explicit_index: int):
# Get the list of intermediate parameter files files that will be processed sequentially
files = list(self.file_parameters.keys())

if explicit_index >= 0:
if not files:
return ""

# Determine the starting file based on the --n command line argument
start_file_index = explicit_index # Ensure the index is within the range of available files
if start_file_index >= len(files):
start_file_index = len(files) - 1
logging_warning("Starting file index %s is out of range. Starting with file %s instead.",
explicit_index, files[start_file_index])
return files[start_file_index]

last_written_filename = self.__read_last_written_filename()
if last_written_filename:
logging_info("Last written file was %s.", last_written_filename)
else:
logging_info("No last written file found. Starting with the first file.")
return files[0]

if last_written_filename not in files:
# Handle the case where last_written_filename is not found in the list
logging_warning("Last written file not found in the list of files. Starting with the first file.")
return files[0]

# Find the index of last_written_filename in files
last_written_index = files.index(last_written_filename)
# Check if there is a file following last_written_filename
start_file_index = last_written_index + 1
if start_file_index >= len(files):
# Handle the case where last_written_filename is the last file in the list
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]
11 changes: 1 addition & 10 deletions MethodicConfigurator/frontend_tkinter_directory_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,19 +337,10 @@ def main():
# Get the list of intermediate parameter files files that will be processed sequentially
files = list(local_filesystem.file_parameters.keys())

# pylint: disable=duplicate-code
if files:
# Determine the starting file based on the --n command line argument
start_file_index = min(args.n, len(files) - 1) # Ensure the index is within the range of available files
if start_file_index != args.n:
logging_warning("Starting file index %s is out of range. Starting with file %s instead.",
args.n, files[start_file_index])
else:
if not files:
logging_error("No intermediate parameter files found in %s.", args.vehicle_dir)
# show_no_param_files_error(args.vehicle_dir)
window = VehicleDirectorySelectionWindow(local_filesystem)
window.root.mainloop()
# pylint: enable=duplicate-code


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions MethodicConfigurator/frontend_tkinter_parameter_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ def write_selected_params(self, selected_params):
self.write_selected_params(selected_params)
else:
logging_info("All parameters written to the flight controller successfully")
self.local_filesystem.write_last_written_filename(self.current_file)

def on_skip_click(self, _event=None, force_focus_out_event=True):
if force_focus_out_event:
Expand Down

0 comments on commit f808d64

Please sign in to comment.