diff --git a/src/nanosaur/docker.py b/src/nanosaur/docker.py index a1cdf10..cdacf9e 100644 --- a/src/nanosaur/docker.py +++ b/src/nanosaur/docker.py @@ -66,6 +66,39 @@ def create_simple(platform, params: Params, args) -> bool: return True +def docker_robot_run_command(platform, params: Params, command, name=None): + """Run a command in the robot container.""" + + if not docker.compose.is_installed(): + print(TerminalFormatter.color_text("Please install Docker and Docker Compose before running the simulation.", color='red')) + return False + + workspace_type = "robot" if platform['Machine'] == 'jetson' else "simulation" + docker_compose = f"docker-compose.{workspace_type}.yml" + nanosaur_home_path = get_nanosaur_home() + # Create the full file path + docker_compose_path = os.path.join(nanosaur_home_path, docker_compose) + robot = RobotList.get_robot(params) + + # Check which simulation tool is selected only if robot.simulation is true + if robot.simulation and 'simulation_tool' not in params: + print(TerminalFormatter.color_text("No simulation tool selected. Please run simulation set first.", color='red')) + return False + + # Build env file + if not is_env_file(): + print(TerminalFormatter.color_text("Creating the environment file...", color='green')) + build_env_file(params) + + # Create a DockerClient object with the docker-compose file + nanosaur_compose = DockerClient(compose_files=[docker_compose_path]) + try: + nanosaur_compose.compose.run(service='nanosaur_simulator', command=command, remove=True, tty=True, name=name) + except DockerException as e: + print(TerminalFormatter.color_text(f"Error running the command: {e}", color='red')) + return False + + def docker_robot_start(platform, params: Params, args): """Start the docker container.""" diff --git a/src/nanosaur/robot.py b/src/nanosaur/robot.py index 83df2cb..77aefd8 100644 --- a/src/nanosaur/robot.py +++ b/src/nanosaur/robot.py @@ -27,6 +27,7 @@ from inquirer.themes import GreenPassion import argparse import subprocess +import shlex from nanosaur import workspace from nanosaur import docker from nanosaur.prompt_colors import TerminalFormatter @@ -272,26 +273,32 @@ def robot_reset(platform, params: Params, args): def control_keyboard(platform, params: Params, args): """Control the robot using the keyboard.""" - nanosaur_ws_path = workspace.get_workspace_path(params, 'ws_simulation_name') - bash_file = f'{nanosaur_ws_path}/install/setup.bash' - # Read the robot name robot = RobotList.get_robot(params) - print(TerminalFormatter.color_text(f"Control the robot {robot.name} using the keyboard", color='green')) - subprocess.run(f'source {bash_file} && ros2 run teleop_twist_keyboard teleop_twist_keyboard --ros-args --remap /cmd_vel:=/{robot.name}/key_vel', - shell=True, executable='/bin/bash') - + command = f"ros2 run teleop_twist_keyboard teleop_twist_keyboard --ros-args --remap /cmd_vel:=/{robot.name}/key_vel" + # Run from local machine + if params.get('mode', '') in ['Maintainer','Raffo']: + nanosaur_ws_path = workspace.get_workspace_path(params, 'ws_simulation_name') + bash_file = f'{nanosaur_ws_path}/install/setup.bash' + # Read the robot name + print(TerminalFormatter.color_text(f"Control the robot {robot.name} using the keyboard", color='green')) + subprocess.run(f'source {bash_file} && {command}', shell=True, executable='/bin/bash') + return True + # Run from docker container + docker.docker_robot_run_command(platform, params, shlex.split(command), name=f"{robot.name}-keyboard") + return True def robot_display(platform, params: Params, args): """Display the robot configuration.""" - nanosaur_ws_path = workspace.get_workspace_path(params, 'ws_simulation_name') - bash_file = f'{nanosaur_ws_path}/install/setup.bash' - # Read the robot name robot = RobotList.get_robot(params) - print(TerminalFormatter.color_text(f"Display the robot {robot.name}", color='green')) - try: - subprocess.run(f'source {bash_file} && ros2 launch nanosaur_visualization robot_display.launch.py robot_name:={robot.name}', - shell=True, executable='/bin/bash') - except KeyboardInterrupt: - print(TerminalFormatter.color_text("Process interrupted by user", color='yellow')) + command = f"ros2 launch nanosaur_visualization robot_display.launch.py robot_name:={robot.name}" + # Run from local machine + if params.get('mode', '') in ['Maintainer', 'Raffo']: + nanosaur_ws_path = workspace.get_workspace_path(params, 'ws_simulation_name') + bash_file = f'{nanosaur_ws_path}/install/setup.bash' + print(TerminalFormatter.color_text(f"Display the robot {robot.name}", color='green')) + subprocess.run(f'source {bash_file} && {command}', shell=True, executable='/bin/bash') + return True + # Run from docker container + docker.docker_robot_run_command(platform, params, shlex.split(command), name=f"{robot.name}-rviz") return True # EOF