-
Notifications
You must be signed in to change notification settings - Fork 548
/
Copy pathcontroller_helper.py
228 lines (201 loc) · 9 KB
/
controller_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python3
# 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 typing import TYPE_CHECKING, List
import numpy as np
import habitat.gym.gym_wrapper as gym_wrapper
from habitat_baselines.rl.hrl.utils import find_action_range
from habitat_hitl.environment.controllers.baselines_controller import (
MultiAgentBaselinesController,
SingleAgentBaselinesController,
clean_dict,
)
from habitat_hitl.environment.controllers.controller_abc import Controller
from habitat_hitl.environment.controllers.gui_controller import (
GuiHumanoidController,
GuiRobotController,
)
if TYPE_CHECKING:
from omegaconf import DictConfig
import habitat
from habitat.core.environments import GymHabitatEnv
class ControllerHelper:
"""ControllerHelper is a wrapper around the habitat env that allows for multiple agent controllers to be used."""
def __init__(
self,
gym_habitat_env: "GymHabitatEnv",
config: "DictConfig",
hitl_config,
gui_input,
recorder,
):
self._hitl_config = hitl_config
self._gym_habitat_env: GymHabitatEnv = gym_habitat_env
self._env: habitat.Env = gym_habitat_env.unwrapped.habitat_env
self.n_agents: int = len(self._env._sim.agents_mgr) # type: ignore[attr-defined]
self.n_user_controlled_agents: int = len(
hitl_config.gui_controlled_agents
)
assert self.n_user_controlled_agents <= self.n_agents
self.n_policy_controlled_agents: int = (
self.n_agents - self.n_user_controlled_agents
)
is_multi_agent: bool = self.n_agents > 1
if self.n_agents > 2:
raise ValueError("ControllerHelper only supports 1 or 2 agents.")
self.controllers: List[Controller] = []
if self.n_agents == self.n_policy_controlled_agents:
# all agents are policy controlled
if not is_multi_agent:
# single agent case
self.controllers.append(
SingleAgentBaselinesController(
0,
is_multi_agent,
config,
self._gym_habitat_env,
)
)
else:
# multi agent case (2 agents)
self.controllers.append(
MultiAgentBaselinesController(
is_multi_agent,
config,
self._gym_habitat_env,
)
)
else:
# some agents are gui controlled and the rest (if any) are policy controlled
for agent_index in range(
len(self._env.sim.habitat_config.agents_order)
):
gui_controlled_agent_config = (
self._find_gui_controlled_agent_config(agent_index)
)
if gui_controlled_agent_config:
agent_name: str = (
self._env.sim.habitat_config.agents_order[agent_index]
)
articulated_agent_type: str = (
self._env.sim.habitat_config.agents[
agent_name
].articulated_agent_type
)
gui_agent_controller: Controller
if articulated_agent_type == "KinematicHumanoid":
gui_agent_controller = GuiHumanoidController(
agent_idx=agent_index,
is_multi_agent=is_multi_agent,
gui_input=gui_input,
env=self._env,
walk_pose_path=hitl_config.walk_pose_path,
lin_speed=gui_controlled_agent_config.lin_speed,
ang_speed=gui_controlled_agent_config.ang_speed,
recorder=recorder.get_nested_recorder(
"gui_humanoid"
),
)
elif articulated_agent_type == "SpotRobot":
if is_multi_agent:
agent_k = f"agent_{agent_index}_"
else:
agent_k = ""
original_action_space = clean_dict(
self._gym_habitat_env.original_action_space,
agent_k,
)
action_space = gym_wrapper.create_action_space(
original_action_space
)
(
base_vel_action_idx,
base_vel_action_end_idx,
) = find_action_range(
original_action_space, "base_velocity"
)
assert len(action_space.shape) == 1
num_actions = action_space.shape[0]
articulated_agent = self._env._sim.agents_mgr[agent_index].articulated_agent # type: ignore[attr-defined]
# sloppy: derive turn scale. This is the change in yaw (in radians) corresponding to a base ang vel action of 1.0. See also Habitat-lab BaseVelAction.
turn_scale = (
config.habitat.simulator.ctrl_freq
/ config.habitat.task.actions[
f"{agent_k}base_velocity"
].ang_speed
)
gui_agent_controller = GuiRobotController(
agent_idx=agent_index,
is_multi_agent=is_multi_agent,
gui_input=gui_input,
articulated_agent=articulated_agent,
num_actions=num_actions,
base_vel_action_idx=base_vel_action_idx,
num_base_vel_actions=base_vel_action_end_idx
- base_vel_action_idx,
turn_scale=turn_scale,
)
else:
raise ValueError(
f"articulated agent type {articulated_agent_type} not supported"
)
self.controllers.append(gui_agent_controller)
else:
self.controllers.append(
SingleAgentBaselinesController(
agent_index,
is_multi_agent,
config,
self._gym_habitat_env,
)
)
def _find_gui_controlled_agent_config(self, agent_index):
for (
gui_controlled_agent_config
) in self._hitl_config.gui_controlled_agents:
if gui_controlled_agent_config.agent_index == agent_index:
return gui_controlled_agent_config
return None
def get_gui_agent_controllers(self) -> List[Controller]:
"""
Return list of controllers indexed by user index. Beware the difference between user index and agent index. For example, user 0 may control agent 1.
"""
gui_agent_controllers = []
for (
gui_controlled_agent_config
) in self._hitl_config.gui_controlled_agents:
gui_agent_controllers.append(
self.controllers[gui_controlled_agent_config.agent_index]
)
return gui_agent_controllers
def get_all_agent_controllers(self) -> List[Controller]:
"""
Return a list of controllers indexed by agent index.
"""
return self.controllers
def update(self, obs):
actions = []
for controller in self.controllers:
controller_action = controller.act(obs, self._env)
actions.append(controller_action)
if len(self.controllers) == 1:
action = actions.pop()
elif len(self.controllers) == 2:
# controllers don't necessarily act in the same order as the their agent index
# so we need to sort the actions by agent index
controlled_agent_idxs = [controller._agent_idx for controller in self.controllers] # type: ignore[attr-defined]
actions = [
action
for _, action in sorted(zip(controlled_agent_idxs, actions))
]
action = np.concatenate(actions, dtype=np.float32)
else:
raise ValueError(
"ControllerHelper only supports up to 2 controllers."
)
# convert action to float32
return np.float32(action)
def on_environment_reset(self):
for controller in self.controllers:
controller.on_environment_reset()