From 03e6a9a572714810b02b5154c576a41b18757b9f Mon Sep 17 00:00:00 2001 From: Raffaello Bonghi Date: Wed, 22 Jan 2025 13:18:24 +0000 Subject: [PATCH] Improve nanosaur commands --- src/nanosaur/main.py | 42 ++++++++++++++++++++++++++++----------- src/nanosaur/ros.py | 4 +--- src/nanosaur/workspace.py | 36 +++++++++++++++++++++++++-------- 3 files changed, 59 insertions(+), 23 deletions(-) diff --git a/src/nanosaur/main.py b/src/nanosaur/main.py index 2eef48b..0b28f88 100644 --- a/src/nanosaur/main.py +++ b/src/nanosaur/main.py @@ -27,6 +27,8 @@ import argparse import argcomplete import sys +import inquirer +from inquirer.themes import GreenPassion from jtop import jtop, JtopException from nanosaur import __version__ @@ -37,10 +39,11 @@ from nanosaur.swarm import parser_swarm_menu from nanosaur.prompt_colors import TerminalFormatter from nanosaur.utilities import RobotList -import inquirer + +NANOSAUR_INSTALL_OPTIONS = ['Simple', 'Developer', 'Maintainer'] NANOSAUR_CONFIG_FILE_NAME = 'nanosaur.yaml' -NANOSAUR_HOME_NAME = 'nanosaur_test' +NANOSAUR_HOME_NAME = 'nanosaur' # Define default parameters DEFAULT_PARAMS = { @@ -106,23 +109,38 @@ def install_old(platform, params: Params, args, password=None): def install(platform, params: Params, args, password=None): + # Determine the device type + device_type = "robot" if platform['Machine'] == 'jetson' else "desktop" + # Questions to ask the user questions = [ inquirer.List( 'choice', message="What would you like to install?", - choices=['Developer Workspace', 'Simulation Tools', 'Robot Configuration'], + choices=NANOSAUR_INSTALL_OPTIONS, ), + inquirer.Confirm( + 'confirm', + message="Are you sure you want to install this?", + default=False + ) ] - - answers = inquirer.prompt(questions) - - if answers['choice'] == 'Developer Workspace': + # Ask the user to select an install type + answers = inquirer.prompt(questions, theme=GreenPassion()) + if answers is None: + return False + # Check if the user wants to continue + if answers['confirm'] is False: + print(TerminalFormatter.color_text("Installation cancelled", color='red')) + return False + # Get the selected install type + install_type = answers['choice'] + print(f"Installing {install_type} workspace...") + if install_type == 'Developer': workspace.create_developer_workspace(platform, params, args) - elif answers['choice'] == 'Simulation Tools': - print(TerminalFormatter.color_text("Simulation Tools installation is not implemented yet", color='red')) - elif answers['choice'] == 'Robot Configuration': - print(TerminalFormatter.color_text("Robot Configuration installation is not implemented yet", color='red')) - + elif install_type == 'Maintainer': + workspace.create_maintainer_workspace(platform, params, args) + elif install_type == 'Simple': + print(TerminalFormatter.color_text(f"Not implemented yet {device_type}", color='red')) def main(): # Load the parameters diff --git a/src/nanosaur/ros.py b/src/nanosaur/ros.py index 47ab338..0a233e4 100644 --- a/src/nanosaur/ros.py +++ b/src/nanosaur/ros.py @@ -44,9 +44,7 @@ NANOSAUR_DOCKERFILE_SUFFIX = "nanosaur" -def run_dev_script(platform, params: Params, args): - - perception_path = get_workspace_path(params, params['ws_perception_name']) +def run_dev_script(params, perception_path): isaac_ros_common_path = os.path.join(perception_path, 'src', 'isaac_ros_common') # Get the path to the Isaac ROS common package os.chdir(isaac_ros_common_path) diff --git a/src/nanosaur/workspace.py b/src/nanosaur/workspace.py index 1823f3c..992f615 100644 --- a/src/nanosaur/workspace.py +++ b/src/nanosaur/workspace.py @@ -60,14 +60,24 @@ def add_workspace_subcommand(name, help_text, func): # Add workspace clean subcommand add_workspace_subcommand('clean', "Clean the workspace", clean) add_workspace_subcommand('update', "Update the workspace", update) + add_workspace_subcommand('run', "Run the workspace", run_developer_workspace) add_workspace_subcommand('deploy', "Deploy workspace to docker image", deploy) - # Add workspace perception subcommand - parser_workspace_perception = workspace_subparsers.add_parser( - 'perception', help="Start the Isaac ROS docker container") - parser_workspace_perception.set_defaults(func=ros.run_dev_script) return parser_workspace +def run_developer_workspace(platform, params: Params, args, password=None): + if args.workspace is not None: + workspace = args.workspace + else: + workspace = "robot" if platform['Machine'] == 'jetson' else "desktop" + + if workspace == 'perception': + perception_ws_name = params['ws_perception_name'] + perception_ws_path = get_workspace_path(params, perception_ws_name) + ros.run_dev_script(params, perception_ws_path) + else: + print(TerminalFormatter.color_text(f"I cannot run {workspace}", color='red')) + def get_workspaces_path(params: Params) -> bool: nanosaur_home_path = get_nanosaur_home(params['nanosaur_home']) # Add all workspaces that exist in the Nanosaur home folder @@ -91,7 +101,7 @@ def get_workspace_path(params: Params, ws_name) -> str: return None -def create_workspace(nanosaur_home_path, ws_name) -> str: +def create_workspace(nanosaur_home_path, ws_name, skip_create_colcon_setting=False) -> str: ws_name_path = os.path.join(nanosaur_home_path, ws_name) ws_name_path_src = os.path.join(ws_name_path, "src") # Check if folder exists, if not, create it @@ -101,8 +111,9 @@ def create_workspace(nanosaur_home_path, ws_name) -> str: else: print(TerminalFormatter.color_text(f"Workspace '{ws_name}' already exists.", color='yellow')) # Save the default colcon settings - with open(f"{ws_name_path}/colcon_defaults.yaml", 'w') as file: - yaml.dump(COLCON_DEFAULTS, file) + if not skip_create_colcon_setting: + with open(f"{ws_name_path}/colcon_defaults.yaml", 'w') as file: + yaml.dump(COLCON_DEFAULTS, file) return ws_name_path @@ -195,6 +206,15 @@ def build_workspace(nanosaur_raw_github_repo, branch, workspace_path, rosinstall return True +def create_developer_workspace(platform, params: Params, args, password=None): + # Get the Nanosaur home folder and branch + nanosaur_home = params['nanosaur_home'] + # Create the Nanosaur home folder + nanosaur_home_path = create_nanosaur_home(nanosaur_home) + # Create developer workspace + create_workspace(nanosaur_home_path, params['ws_developer_name'], skip_create_colcon_setting=True) + + @require_sudo_password def create_maintainer_workspace(platform, params: Params, args, password=None): # determine the device type @@ -243,7 +263,7 @@ def create_maintainer_workspace(platform, params: Params, args, password=None): # Make the perception workspace ws_name_path = create_workspace(nanosaur_home_path, params['ws_perception_name']) - build_workspace(branch, ws_name_path, 'perception', password, skip_rosdep=True, skip_build=True) + build_workspace(nanosaur_raw_github_repo, branch, ws_name_path, 'perception', password, skip_rosdep=True, skip_build=True) # Set params in maintainer mode params['mode'] = 'maintainer'