Skip to content

Commit 9f08fca

Browse files
add init lasa dataset
1 parent 0d74b07 commit 9f08fca

File tree

6 files changed

+120
-29
lines changed

6 files changed

+120
-29
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "mujoco_robot_environments/mujoco_controllers"]
2+
path = mujoco_robot_environments/mujoco_controllers
3+
url = [email protected]:peterdavidfagan/mujoco_controllers.git

mujoco_robot_environments/config/robots/arm/controller_config/osc.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ controller_params:
44
control_dt: ${control_dt}
55
gains:
66
position:
7-
kp: 250.0
8-
kd: 30.0
7+
kp: 350.0
8+
kd: 20.0
99
orientation:
1010
kp: 500.0
1111
kd: 100.0
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""Collecting robot demonstrations of LASA drawing dataset."""
2+
3+
import numpy as np
4+
import mujoco
5+
6+
import pyLasaDataset as lasa
7+
import h5py
8+
9+
from mujoco_robot_environments.tasks.lasa_draw import LasaDrawEnv
10+
11+
import hydra
12+
from hydra import compose, initialize
13+
14+
15+
if __name__=="__main__":
16+
# clear hydra global state to avoid conflicts with other hydra instances
17+
hydra.core.global_hydra.GlobalHydra.instance().clear()
18+
initialize(version_base=None, config_path="./config", job_name="rearrangement")
19+
20+
# add task configs
21+
COLOR_SEPARATING_CONFIG = compose(
22+
config_name="rearrangement",
23+
overrides=[
24+
"arena/props=colour_splitter",
25+
"simulation_tuning_mode=True"
26+
]
27+
)
28+
29+
# instantiate color separation task
30+
env = LasaDrawEnv(viewer=True, cfg=COLOR_SEPARATING_CONFIG)
31+
32+
# Leverage demonstrations from LASA dataset
33+
import pyLasaDataset as lasa
34+
s_data = lasa.DataSet.Sshape
35+
demos = s_data.demos
36+
37+
def preprocess_demo(demo_data):
38+
pos = demo_data.pos
39+
vel = demo_data.vel
40+
41+
# scale position data
42+
pos_scaled = (pos / 200) + 0.2
43+
pos_scaled = pos_scaled[:,::4]
44+
positions = np.vstack([pos_scaled[1,:] + 0.2, -pos_scaled[0,:] + 0.2, np.repeat(0.55, pos_scaled.shape[1])]).T
45+
46+
# scale velocity data
47+
vel_scaled = (vel / 800)
48+
vel_scaled = vel_scaled[:,::4]
49+
velocities = np.vstack([vel_scaled[1,:], vel_scaled[0,:], np.repeat(0.0, pos_scaled.shape[1])]).T
50+
51+
return positions, velocities
52+
53+
# interactive control of robot with mocap body
54+
_, _, _, obs = env.reset()
55+
data = {}
56+
for demo_idx, demo in enumerate(demos):
57+
positions, velocities = preprocess_demo(demo)
58+
joint_positions, joint_velocities = [], []
59+
data[f"trajectory_{demo_idx}"] = {}
60+
61+
for idx, (target_pos, target_vel) in enumerate(zip(positions, velocities)):
62+
while True:
63+
pos, vel = env.move_to_draw_target(target_pos, target_vel)
64+
65+
if idx != 0:
66+
joint_positions.append(pos)
67+
joint_velocities.append(vel)
68+
69+
# check if target is reached
70+
if env._robot.arm_controller.current_position_error() < 5e-3:
71+
break
72+
73+
with env.passive_view.lock():
74+
env.passive_view.user_scn.ngeom += 1
75+
mujoco.mjv_initGeom(
76+
env.passive_view.user_scn.geoms[env.passive_view.user_scn.ngeom-1],
77+
type=mujoco.mjtGeom.mjGEOM_SPHERE,
78+
size=[0.001, 0, 0],
79+
pos=target_pos,
80+
mat=np.eye(3).flatten(),
81+
rgba=[1, 0, 0, 1]
82+
)
83+
env.passive_view.sync()
84+
85+
data[f"trajectory_{demo_idx}"]["joint_positions"] = np.vstack(joint_positions)
86+
data[f"trajectory_{demo_idx}"]["joint_velocities"] = np.vstack(joint_velocities)
87+
88+
# Save to HDF5
89+
with h5py.File("robot_trajectories.h5", "w") as f:
90+
for traj_name, data in data.items():
91+
group = f.create_group(traj_name)
92+
group.create_dataset("position", data=data["joint_positions"], compression="gzip")
93+
group.create_dataset("velocity", data=data["joint_velocities"], compression="gzip")
94+
95+
env.close()
96+

mujoco_robot_environments/tasks/lasa_draw.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from mujoco_robot_environments.environment.prop_initializer import PropPlacer
2828
from mujoco_robot_environments.models.robot_arm import RobotArm
2929

30+
import h5py
3031

3132
def generate_default_config():
3233
hydra.core.global_hydra.GlobalHydra.instance().clear()
@@ -40,7 +41,7 @@ def generate_default_config():
4041
)
4142

4243

43-
class PushEnv(dm_env.Environment):
44+
class LasaDrawEnv(dm_env.Environment):
4445
"""MuJoCo powered robotics environment with dm_env interface."""
4546

4647
def __init__(
@@ -134,7 +135,7 @@ def __init__(
134135
conaffinity=0 # no influence on collision detection
135136
)
136137

137-
# visualise plane for drawing
138+
# add a plane for drawing task
138139
self.draw_plane_center=self._arena.mjcf_model.worldbody.add(
139140
'body',
140141
name="draw_plane",
@@ -305,17 +306,17 @@ def interactive_tuning(self):
305306
if self.passive_view is not None:
306307
self.passive_view.sync()
307308

308-
def test_board(self, target_position):
309+
def move_to_draw_target(self, target_position, target_velocity):
309310
"""
310-
Interactively control arm to tune simulation parameters.
311+
Move to position and velocity target for drawing task.
311312
"""
312313
mocap_quat = self._physics.data.mocap_quat[0]
313314

314315
# update control target
315316
self._robot.arm_controller.set_target(
316-
position=target_position + [0.0, 0.0, 0.1],
317+
position=target_position + [0.0, 0.0, 0.1], # account for height of toolpiece
317318
quat=mocap_quat,
318-
velocity=np.zeros(3),
319+
velocity=target_velocity,
319320
angular_velocity=np.zeros(3),
320321
)
321322

@@ -328,6 +329,9 @@ def test_board(self, target_position):
328329
if self.passive_view is not None:
329330
self.passive_view.sync()
330331

332+
# return joint data for recording
333+
return self._physics.bind(self._robot.arm_joints).qpos, self._physics.bind(self._robot.arm_joints).qvel
334+
331335

332336
if __name__=="__main__":
333337
# clear hydra global state to avoid conflicts with other hydra instances
@@ -346,26 +350,11 @@ def test_board(self, target_position):
346350
)
347351

348352
# instantiate color separation task
349-
env = PushEnv(viewer=True, cfg=COLOR_SEPARATING_CONFIG)
350-
positions = [
351-
np.array([0.6, 0.3, 0.55]),
352-
np.array([0.6, -0.3, 0.55]),
353-
np.array([0.3, -0.3, 0.55]),
354-
np.array([0.3, 0.3, 0.55]),
355-
np.array([0.6, -0.3, 0.55]),
356-
]
353+
env = LasaDrawEnv(viewer=True, cfg=COLOR_SEPARATING_CONFIG)
357354

358355
# interactive control of robot with mocap body
359356
_, _, _, obs = env.reset()
360-
for target in positions:
361-
while True:
362-
env.test_board(target)
363-
364-
# check if target is reached
365-
if env._robot.arm_controller.current_position_error() < 1e-3:
366-
break
367-
368-
# env.interactive_tuning()
369-
357+
while True:
358+
env.interactive_tuning()
370359
env.close()
371360

pyproject.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ mujoco_robot_environments = ['*.png']
3737
[tool.poetry.dependencies]
3838
python = "3.10.6"
3939
numpy = "^1.16.0"
40-
mujoco = "3.2.1"
41-
mujoco_controllers = "^0.0.2"
40+
mujoco = "3.2.6"
41+
mujoco_controllers = {path="./mujoco_robot_environments/mujoco_controllers", develop=true}
4242
brax = "^0.10.4"
43-
dm-control = "1.0.22"
43+
dm-control = "1.0.26"
4444
pillow = "10.0.0"
4545
matplotlib = "^3.7.2"
4646
hydra-core = "^1.3.2"
@@ -62,6 +62,8 @@ envlogger = {extras = ["tfds"], version = "^1.2", markers = "sys_platform == 'li
6262
rlds = {version="^0.1.7", markers = "sys_platform == 'linux'"}
6363
robot-descriptions = "^1.10.0"
6464
mink = "^0.0.5"
65+
pylasadataset = "^0.1.1"
66+
h5py = "^3.12.1"
6567

6668
[tool.black]
6769
line-length = 120

0 commit comments

Comments
 (0)