Skip to content

Commit

Permalink
HITL - Change key enums. (#1985)
Browse files Browse the repository at this point in the history
* Use KeyCode instead of KeyNS.

* Use MouseButton instead of MouseNS.

* Remove reference code for moving spot arm.
  • Loading branch information
0mdc authored Jun 25, 2024
1 parent 9dab8cd commit 8381bdc
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 170 deletions.
22 changes: 11 additions & 11 deletions examples/hitl/basic_viewer/basic_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

from habitat_hitl.app_states.app_service import AppService
from habitat_hitl.app_states.app_state_abc import AppState
from habitat_hitl.core.gui_input import GuiInput
from habitat_hitl.core.hitl_main import hitl_main
from habitat_hitl.core.hydra_utils import register_hydra_plugins
from habitat_hitl.core.key_mapping import KeyCode
from habitat_hitl.core.text_drawer import TextOnScreenAlignment
from habitat_hitl.environment.camera_helper import CameraHelper

Expand Down Expand Up @@ -46,17 +46,17 @@ def _update_lookat_pos(self):
# update lookat
move_delta = 0.1
move = mn.Vector3.zero_init()
if self._gui_input.get_key(GuiInput.KeyNS.W):
if self._gui_input.get_key(KeyCode.W):
move.x -= move_delta
if self._gui_input.get_key(GuiInput.KeyNS.S):
if self._gui_input.get_key(KeyCode.S):
move.x += move_delta
if self._gui_input.get_key(GuiInput.KeyNS.E):
if self._gui_input.get_key(KeyCode.E):
move.y += move_delta
if self._gui_input.get_key(GuiInput.KeyNS.Q):
if self._gui_input.get_key(KeyCode.Q):
move.y -= move_delta
if self._gui_input.get_key(GuiInput.KeyNS.J):
if self._gui_input.get_key(KeyCode.J):
move.z += move_delta
if self._gui_input.get_key(GuiInput.KeyNS.L):
if self._gui_input.get_key(KeyCode.L):
move.z -= move_delta

# align move forward direction with lookat direction
Expand Down Expand Up @@ -159,24 +159,24 @@ def on_environment_reset(self, episode_recorder_dict):
self._camera_helper.update(self._get_camera_lookat_pos(), dt=0)

def sim_update(self, dt, post_sim_update_dict):
if self._app_service.gui_input.get_key_down(GuiInput.KeyNS.ESC):
if self._app_service.gui_input.get_key_down(KeyCode.ESC):
self._app_service.end_episode()
post_sim_update_dict["application_exit"] = True

if (
self._env_episode_active()
and self._app_service.gui_input.get_key_down(GuiInput.KeyNS.P)
and self._app_service.gui_input.get_key_down(KeyCode.P)
):
self._paused = not self._paused

if self._app_service.gui_input.get_key_down(GuiInput.KeyNS.SPACE):
if self._app_service.gui_input.get_key_down(KeyCode.SPACE):
self._do_single_step = True
self._paused = True

is_paused_this_frame = self._paused and not self._do_single_step

if (
self._app_service.gui_input.get_key_down(GuiInput.KeyNS.M)
self._app_service.gui_input.get_key_down(KeyCode.M)
and self._episode_helper.next_episode_exists()
and not is_paused_this_frame
):
Expand Down
5 changes: 2 additions & 3 deletions examples/hitl/minimal/minimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
# Copyright (c) Meta Platforms, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import hydra
import magnum

from habitat_hitl.app_states.app_service import AppService
from habitat_hitl.app_states.app_state_abc import AppState
from habitat_hitl.core.gui_input import GuiInput
from habitat_hitl.core.hitl_main import hitl_main
from habitat_hitl.core.hydra_utils import register_hydra_plugins
from habitat_hitl.core.key_mapping import KeyCode


class AppStateMinimal(AppState):
Expand Down Expand Up @@ -40,7 +39,7 @@ def sim_update(self, dt, post_sim_update_dict):
)

# exit when the ESC key is pressed
if self._app_service.gui_input.get_key_down(GuiInput.KeyNS.ESC):
if self._app_service.gui_input.get_key_down(KeyCode.ESC):
post_sim_update_dict["application_exit"] = True


Expand Down
38 changes: 17 additions & 21 deletions examples/hitl/pick_throw_vr/pick_throw_vr.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
from habitat.datasets.rearrange.navmesh_utils import get_largest_island_index
from habitat_hitl.app_states.app_service import AppService
from habitat_hitl.app_states.app_state_abc import AppState
from habitat_hitl.core.gui_input import GuiInput
from habitat_hitl.core.hitl_main import hitl_main
from habitat_hitl.core.hydra_utils import register_hydra_plugins
from habitat_hitl.core.key_mapping import KeyCode, MouseButton
from habitat_hitl.core.text_drawer import TextOnScreenAlignment
from habitat_hitl.environment.avatar_switcher import AvatarSwitcher
from habitat_hitl.environment.camera_helper import CameraHelper
Expand Down Expand Up @@ -149,10 +149,10 @@ def get_sim(self):

def get_grasp_keys_by_hand(self, hand_idx):
if hand_idx == 0:
return [GuiInput.KeyNS.ZERO, GuiInput.KeyNS.ONE]
return [KeyCode.ZERO, KeyCode.ONE]
else:
assert hand_idx == 1
return [GuiInput.KeyNS.TWO, GuiInput.KeyNS.THREE]
return [KeyCode.TWO, KeyCode.THREE]

def _try_grasp_remote(self):
assert not self._held_target_obj_idx
Expand Down Expand Up @@ -350,7 +350,7 @@ def _update_grasping_and_set_act_hints_local(self):
self._has_grasp_preview = False

if self._held_target_obj_idx is not None:
if self._app_service.gui_input.get_key(GuiInput.KeyNS.SPACE):
if self._app_service.gui_input.get_key(KeyCode.SPACE):
if self._recent_reach_pos:
# Continue reaching towards the recent reach position (the
# original position of the grasped object before it was snapped
Expand All @@ -368,7 +368,7 @@ def _update_grasping_and_set_act_hints_local(self):
# also preview throw
_ = self._throw_helper.viz_and_get_humanoid_throw()

if self._app_service.gui_input.get_key_up(GuiInput.KeyNS.SPACE):
if self._app_service.gui_input.get_key_up(KeyCode.SPACE):
if self._recent_reach_pos:
# this spacebar release means we've finished the reach-and-grasp motion
self._recent_reach_pos = None
Expand Down Expand Up @@ -413,9 +413,7 @@ def _update_grasping_and_set_act_hints_local(self):
)
self._has_grasp_preview = True

if self._app_service.gui_input.get_key_down(
GuiInput.KeyNS.SPACE
):
if self._app_service.gui_input.get_key_down(KeyCode.SPACE):
self._recent_reach_pos = (
self._get_target_object_position(min_i)
)
Expand All @@ -430,9 +428,7 @@ def _update_grasping_and_set_act_hints_local(self):
walk_dir = None
distance_multiplier = 1.0

if self._app_service.gui_input.get_mouse_button(
GuiInput.MouseNS.RIGHT
):
if self._app_service.gui_input.get_mouse_button(MouseButton.RIGHT):
(
candidate_walk_dir,
candidate_distance_multiplier,
Expand Down Expand Up @@ -614,7 +610,7 @@ def _get_camera_lookat_pos(self):
return lookat

def sim_update(self, dt, post_sim_update_dict):
if self._app_service.gui_input.get_key_down(GuiInput.KeyNS.ESC):
if self._app_service.gui_input.get_key_down(KeyCode.ESC):
self._app_service.end_episode()
post_sim_update_dict["application_exit"] = True

Expand All @@ -624,11 +620,11 @@ def sim_update(self, dt, post_sim_update_dict):
episode_id_by_scene_index = ["0", "5", "10", "15", "20"]
for scene_idx in range(num_fetch_scenes):
key_map = [
GuiInput.KeyNS.ONE,
GuiInput.KeyNS.TWO,
GuiInput.KeyNS.THREE,
GuiInput.KeyNS.FOUR,
GuiInput.KeyNS.FIVE,
KeyCode.ONE,
KeyCode.TWO,
KeyCode.THREE,
KeyCode.FOUR,
KeyCode.FIVE,
]
key = key_map[scene_idx]
if self._app_service.gui_input.get_key_down(key):
Expand All @@ -637,10 +633,10 @@ def sim_update(self, dt, post_sim_update_dict):
)
self._app_service.end_episode(do_reset=True)

if self._app_service.gui_input.get_key_down(GuiInput.KeyNS.P):
if self._app_service.gui_input.get_key_down(KeyCode.P):
self._paused = not self._paused

if self._app_service.gui_input.get_key_down(GuiInput.KeyNS.H):
if self._app_service.gui_input.get_key_down(KeyCode.H):
self._hide_gui_text = not self._hide_gui_text

# toggle remote/local under certain conditions:
Expand All @@ -650,7 +646,7 @@ def sim_update(self, dt, post_sim_update_dict):
self._app_service.hitl_config.networking.enable
and self._held_target_obj_idx is None
and (
self._app_service.gui_input.get_key_down(GuiInput.KeyNS.T)
self._app_service.gui_input.get_key_down(KeyCode.T)
or (
not self._is_remote_active_toggle
and self._app_service.remote_client_state.get_gui_input().get_any_key_down()
Expand All @@ -659,7 +655,7 @@ def sim_update(self, dt, post_sim_update_dict):
):
self._is_remote_active_toggle = not self._is_remote_active_toggle

if self._app_service.gui_input.get_key_down(GuiInput.KeyNS.TAB):
if self._app_service.gui_input.get_key_down(KeyCode.TAB):
self._avatar_switch_helper.switch_avatar()

if not self._paused:
Expand Down
16 changes: 6 additions & 10 deletions examples/hitl/rearrange/rearrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
from habitat_hitl.app_states.app_service import AppService
from habitat_hitl.app_states.app_state_abc import AppState
from habitat_hitl.app_states.app_state_tutorial import AppStateTutorial
from habitat_hitl.core.gui_input import GuiInput
from habitat_hitl.core.hitl_main import hitl_main
from habitat_hitl.core.hydra_utils import register_hydra_plugins
from habitat_hitl.core.key_mapping import KeyCode, MouseButton
from habitat_hitl.core.text_drawer import TextOnScreenAlignment
from habitat_hitl.environment.camera_helper import CameraHelper
from habitat_hitl.environment.controllers.gui_controller import (
Expand Down Expand Up @@ -142,7 +142,7 @@ def _update_grasping_and_set_act_hints(self):
mn.Color3(255 / 255, 255 / 255, 0),
)

if self._app_service.gui_input.get_key_down(GuiInput.KeyNS.SPACE):
if self._app_service.gui_input.get_key_down(KeyCode.SPACE):
translation = self._get_agent_translation()
dist_to_obj = np.linalg.norm(goal_position - translation)
if dist_to_obj < self._can_grasp_place_threshold:
Expand All @@ -153,9 +153,7 @@ def _update_grasping_and_set_act_hints(self):
if self._held_target_obj_idx is None:
assert not self._gui_agent_ctrl.is_grasped
# pick up an object
if self._app_service.gui_input.get_key_down(
GuiInput.KeyNS.SPACE
):
if self._app_service.gui_input.get_key_down(KeyCode.SPACE):
translation = self._get_agent_translation()

min_dist = self._can_grasp_place_threshold
Expand Down Expand Up @@ -188,9 +186,7 @@ def _update_grasping_and_set_act_hints(self):
) = self._nav_helper.get_humanoid_walk_hints_from_ray_cast(
visualize_path=True
)
if self._app_service.gui_input.get_mouse_button(
GuiInput.MouseNS.RIGHT
):
if self._app_service.gui_input.get_mouse_button(MouseButton.RIGHT):
walk_dir = candidate_walk_dir
distance_multiplier = candidate_distance_multiplier

Expand Down Expand Up @@ -438,12 +434,12 @@ def _get_camera_lookat_pos(self):
return lookat

def sim_update(self, dt, post_sim_update_dict):
if self._app_service.gui_input.get_key_down(GuiInput.KeyNS.ESC):
if self._app_service.gui_input.get_key_down(KeyCode.ESC):
self._app_service.end_episode()
post_sim_update_dict["application_exit"] = True

if (
self._app_service.gui_input.get_key_down(GuiInput.KeyNS.M)
self._app_service.gui_input.get_key_down(KeyCode.M)
and self._episode_helper.next_episode_exists()
):
self._app_service.end_episode(do_reset=True)
Expand Down
12 changes: 6 additions & 6 deletions examples/hitl/rearrange_v2/rearrange_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
AverageRateTracker,
)
from habitat_hitl.app_states.app_service import AppService
from habitat_hitl.core.gui_input import GuiInput
from habitat_hitl.core.key_mapping import KeyCode
from habitat_hitl.core.text_drawer import TextOnScreenAlignment
from habitat_hitl.core.user_mask import Mask, Users
from habitat_hitl.environment.camera_helper import CameraHelper
Expand Down Expand Up @@ -274,10 +274,10 @@ def update(self, dt: float):
self.end_episode_form.step()
return

if self.gui_input.get_key_down(GuiInput.KeyNS.H):
if self.gui_input.get_key_down(KeyCode.H):
self.show_gui_text = not self.show_gui_text

if self.gui_input.get_key_down(GuiInput.KeyNS.ZERO):
if self.gui_input.get_key_down(KeyCode.ZERO):
self.end_episode_form.show()

if self.client_helper:
Expand Down Expand Up @@ -668,14 +668,14 @@ def sim_update(self, dt: float, post_sim_update_dict):
# Server GUI exit.
if (
not self._app_service.hitl_config.networking.enable
and self._server_gui_input.get_key_down(GuiInput.KeyNS.ESC)
and self._server_gui_input.get_key_down(KeyCode.ESC)
):
self._app_service.end_episode()
post_sim_update_dict["application_exit"] = True
return

# Skip the form when changing the episode from the server.
if self._server_gui_input.get_key_down(GuiInput.KeyNS.ZERO):
if self._server_gui_input.get_key_down(KeyCode.ZERO):
server_user = self._user_data[self._server_user_index]
if (
server_user.agent_data.episode_completion_status
Expand All @@ -685,7 +685,7 @@ def sim_update(self, dt: float, post_sim_update_dict):

# Switch the server-controlled user.
if self._num_users > 0 and self._server_gui_input.get_key_down(
GuiInput.KeyNS.TAB
KeyCode.TAB
):
self._server_user_index = (
self._server_user_index + 1
Expand Down
2 changes: 1 addition & 1 deletion habitat-hitl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class AppStateMinimal(AppState):
)
# exit when the ESC key is pressed
if self._app_service.gui_input.get_key_down(GuiInput.KeyNS.ESC):
if self._app_service.gui_input.get_key_down(KeyCode.ESC):
post_sim_update_dict["application_exit"] = True
Expand Down
7 changes: 3 additions & 4 deletions habitat-hitl/habitat_hitl/app_states/app_state_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
# Copyright (c) Meta Platforms, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from habitat_hitl.app_states.app_state_abc import AppState
from habitat_hitl.core.gui_input import GuiInput
from habitat_hitl.core.key_mapping import KeyCode
from habitat_hitl.core.text_drawer import TextOnScreenAlignment
from habitat_hitl.environment.hitl_tutorial import Tutorial, generate_tutorial

Expand Down Expand Up @@ -61,10 +60,10 @@ def _sim_update_tutorial(self, dt: float):

self._tutorial.update(dt)

if self._app_service.gui_input.get_key_down(GuiInput.KeyNS.SPACE):
if self._app_service.gui_input.get_key_down(KeyCode.SPACE):
self._tutorial.skip_stage()

if self._app_service.gui_input.get_key_down(GuiInput.KeyNS.Q):
if self._app_service.gui_input.get_key_down(KeyCode.Q):
while not self._tutorial.is_completed():
self._tutorial.skip_stage()

Expand Down
3 changes: 0 additions & 3 deletions habitat-hitl/habitat_hitl/core/gui_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ class GuiInput:
This class isn't usable by itself for getting input from the underlying OS. I.e. it won't self-populate from underlying OS input APIs. See also gui_application.py InputHandlerApplication.
"""

KeyNS = KeyCode
MouseNS = MouseButton

def __init__(self):
self._key_held = set()
self._mouse_button_held = set()
Expand Down
8 changes: 4 additions & 4 deletions habitat-hitl/habitat_hitl/core/remote_client_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ def __init__(

# temp map VR button to key
self._button_map = {
0: GuiInput.KeyNS.ZERO,
1: GuiInput.KeyNS.ONE,
2: GuiInput.KeyNS.TWO,
3: GuiInput.KeyNS.THREE,
0: KeyCode.ZERO,
1: KeyCode.ONE,
2: KeyCode.TWO,
3: KeyCode.THREE,
}

@property
Expand Down
Loading

0 comments on commit 8381bdc

Please sign in to comment.