-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathreach_target.py
More file actions
176 lines (144 loc) · 5.35 KB
/
reach_target.py
File metadata and controls
176 lines (144 loc) · 5.35 KB
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
"""Set of reach target tasks."""
from abc import ABC
from dataclasses import dataclass, field
import numpy as np
from gymnasium import spaces
from mojo import Mojo
from mojo.elements import Body, Geom
from mojo.elements.consts import GeomType
from bigym.bigym_env import BiGymEnv
from bigym.const import HandSide
from bigym.robots.robot import Robot
@dataclass
class TargetConfig:
"""Target Config."""
target_hands: list[HandSide]
reset_position: np.ndarray = field(default_factory=lambda: np.zeros(3))
size: np.ndarray = field(default_factory=lambda: np.array([0.05, 0.05, 0.05]))
color_default: np.ndarray = field(default_factory=lambda: np.array([1, 0, 0, 1]))
color_highlight: np.ndarray = field(default_factory=lambda: np.array([1, 0, 0, 1]))
class Target:
"""Target sphere."""
def __init__(self, mojo: Mojo, robot: Robot, config: TargetConfig):
"""Init."""
self._robot = robot
self._config = config
self.body = Body.create(mojo)
self.geom: Geom = Geom.create(
mojo,
parent=self.body,
geom_type=GeomType.SPHERE,
size=config.size,
color=config.color_default,
mass=0,
)
self.geom.set_collidable(False)
def reset_position(self, offset: np.ndarray = np.zeros(3)):
"""Reset position of the target."""
self.body.set_position(self._config.reset_position + offset)
def distance(self, pos: np.ndarray) -> float:
"""Get distance to target."""
return float(np.linalg.norm(self.body.get_position() - pos))
def is_reached(self, tolerance: float) -> bool:
"""Check if target is reached."""
for side in self._config.target_hands:
if side not in self._robot.grippers:
continue
hand_pos = self._robot.get_hand_pos(side)
is_reached = self.distance(hand_pos) <= tolerance
self.set_highlight(is_reached)
if is_reached:
return True
return False
def set_highlight(self, highlight: bool):
"""Toggle target highlight."""
self.geom.set_color(
self._config.color_highlight if highlight else self._config.color_default
)
class _ReachTargetEnv(BiGymEnv, ABC):
"""Base reach target environment."""
TARGET_CONFIGS = [
TargetConfig(
target_hands=[HandSide.LEFT, HandSide.RIGHT],
reset_position=np.array([0.5, 0, 1]),
color_default=np.array([0.3, 0, 0, 1]),
color_highlight=np.array([1, 0, 0, 1]),
)
]
POSITION_BOUNDS = np.array([0.1, 0.1, 0.1])
TOLERANCE = 0.1
def _initialize_env(self):
self.targets: list[Target] = []
for config in self.TARGET_CONFIGS:
self.targets.append(Target(self._mojo, self.robot, config))
def _on_reset(self):
for target in self.targets:
offset = np.random.uniform(-self.POSITION_BOUNDS, self.POSITION_BOUNDS)
target.reset_position(offset)
target.set_highlight(False)
def _success(self) -> bool:
for target in self.targets:
if not target.is_reached(self.TOLERANCE):
return False
return True
def _on_step(self):
"""Highlight spheres even in fast mode."""
self._success()
class ReachTarget(_ReachTargetEnv):
"""Reach the target with either left or right wrist."""
def _get_task_privileged_obs_space(self):
return {
"target_position": spaces.Box(
low=-np.inf, high=np.inf, shape=(3,), dtype=np.float32
)
}
def _get_task_privileged_obs(self):
return {
"target_position": np.array(
self.targets[0].body.get_position(), np.float32
).copy()
}
class ReachTargetSingle(ReachTarget):
"""Reach the target with specific wrist."""
TARGET_CONFIGS = [
TargetConfig(
target_hands=[HandSide.LEFT],
reset_position=np.array([0.5, 0, 1]),
color_default=np.array([0.3, 0, 0, 1]),
color_highlight=np.array([1, 0, 0, 1]),
)
]
class ReachTargetDual(_ReachTargetEnv):
"""Reach 2 targets, one with each arm."""
TARGET_CONFIGS = [
TargetConfig(
target_hands=[HandSide.LEFT],
reset_position=np.array([0.5, 0.2, 1]),
color_default=np.array([0.3, 0, 0, 1]),
color_highlight=np.array([1, 0, 0, 1]),
),
TargetConfig(
target_hands=[HandSide.RIGHT],
reset_position=np.array([0.5, -0.2, 1]),
color_default=np.array([0, 0.3, 0, 1]),
color_highlight=np.array([0, 1, 0, 1]),
),
]
def _get_task_privileged_obs_space(self):
return {
"target_position_left": spaces.Box(
low=-np.inf, high=np.inf, shape=(3,), dtype=np.float32
),
"target_position_right": spaces.Box(
low=-np.inf, high=np.inf, shape=(3,), dtype=np.float32
),
}
def _get_task_privileged_obs(self):
return {
"target_position_left": np.array(
self.targets[0].body.get_position(), np.float32
).copy(),
"target_position_right": np.array(
self.targets[1].body.get_position(), np.float32
).copy(),
}