Skip to content

Commit

Permalink
Added headless control
Browse files Browse the repository at this point in the history
  • Loading branch information
rbonghi committed Jan 27, 2025
1 parent fb4de9e commit 1da5284
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ dependencies = { file = ["requirements.txt"] }
[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools]
editable = true

[project.scripts]
nanosaur = "nanosaur.main:main"

Expand Down
5 changes: 4 additions & 1 deletion src/nanosaur/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,10 @@ def robot_display(platform, params: Params, args):
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')
try:
subprocess.run(f'source {bash_file} && {command}', shell=True, executable='/bin/bash')
except KeyboardInterrupt:
print(TerminalFormatter.color_text("Keyboard interrupt received, stopping robot display", color='yellow'))
return True
elif selected_location == 'docker':
# Run from docker container
Expand Down
36 changes: 32 additions & 4 deletions src/nanosaur/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ def parser_simulation_menu(subparsers: argparse._SubParsersAction, params: Param
parser_simulation_set = simulation_subparsers.add_parser(
'set', help="Select the simulator you want to use")
parser_simulation_set.set_defaults(func=simulation_set)

# Add simulation headless subcommand
parser_simulation_headless = simulation_subparsers.add_parser(
'headless', help="Set the simulation in headless mode")
parser_simulation_headless.set_defaults(func=simulation_set_headless)
return parser_simulation


Expand Down Expand Up @@ -194,7 +199,7 @@ def simulation_robot_start_debug(params):
return False


def simulation_start_debug(simulation_ws_path, simulation_tool, isaac_sim_path=None):
def simulation_start_debug(simulation_ws_path, simulation_tool, headless, isaac_sim_path=None):
"""Install the simulation tools."""

bash_file = f'{simulation_ws_path}/install/setup.bash'
Expand All @@ -204,9 +209,11 @@ def simulation_start_debug(simulation_ws_path, simulation_tool, isaac_sim_path=N
return False

command = simulation_tools[simulation_tool]['simulator']
command += f" headless:={str(headless).lower()}"
# add isaac_sim_path if available
if isaac_sim_path:
command = f"{command} isaac_sim_path:={isaac_sim_path} headless:=false"
command = f"{command} isaac_sim_path:={isaac_sim_path}"
logger.debug(command)
try:
# Combine sourcing the bash file with running the command
process = subprocess.Popen(
Expand Down Expand Up @@ -262,7 +269,8 @@ def simulation_start(platform, params: Params, args):
if debug_mode == 'host':
nanosaur_ws_path = workspace.get_workspace_path(params, 'ws_simulation_name')
simulator_tool = params['simulation_tool']
return simulation_start_debug(nanosaur_ws_path, simulator_tool, isaac_sim_path=params.get('isaac_sim_path', None))
headless = params.get('simulation_headless', False)
return simulation_start_debug(nanosaur_ws_path, simulator_tool, headless, isaac_sim_path=params.get('isaac_sim_path', None))
elif debug_mode == 'docker':
# Run from docker container
return docker_simulator_start(platform, params, args)
Expand Down Expand Up @@ -319,4 +327,24 @@ def simulation_set(platform, params: Params, args):
else:
print(TerminalFormatter.color_text(f"Selected {answers['simulation_tool']}", color='green'))
return True
# EOF

def simulation_set_headless(platform, params: Params, args):
# Get the current simulation tool
headless_mode = params.get('simulation_headless', False)
# Ask the user if they want to run in headless mode
question = [
inquirer.List(
'headless',
message="Select if you want run the simulation in headless mode",
choices=['Yes', 'No'],
default='Yes' if headless_mode else 'No'
)
]
# Get the user's answer
answer = inquirer.prompt(question, theme=GreenPassion())
if answer is None:
return False
# Save the headless mode setting
params['simulation_headless'] = (answer['headless'] == 'Yes')
print(TerminalFormatter.color_text(f"Headless mode set to: {answer['headless']}", color='green'))
return True
2 changes: 2 additions & 0 deletions src/nanosaur/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ def build_env_file(params):
if 'simulation_tool' in params:
simulation_tool = params['simulation_tool'].lower().replace(' ', '-')
env_file.write(f"SIMULATION={simulation_tool}\n")
if 'simulation_headless' in params:
env_file.write(f"SIMULATION_HEADLESS={params['simulation_headless']}\n")
# Pass robot ros commands
env_file.write(f"COMMANDS={robot.config_to_ros()}\n")

Expand Down
3 changes: 2 additions & 1 deletion src/nanosaur/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ def debug_simulation(params: utilities.Params, args):
if selected_launcher == 'robot':
return simulation_robot_start_debug(params)
isaac_sim_path = params.get('isaac_sim_path', None)
return simulation_start_debug(simulation_ws_path, selected_launcher, isaac_sim_path=isaac_sim_path)
headless = params.get('simulation_headless', False)
return simulation_start_debug(simulation_ws_path, selected_launcher, headless, isaac_sim_path=isaac_sim_path)
elif selected_location == 'docker':
# Set the volumes
volumes = [
Expand Down

0 comments on commit 1da5284

Please sign in to comment.