Skip to content

Commit

Permalink
Merge pull request #34 from michel-aractingi/pushcubeloop-26-09-2024
Browse files Browse the repository at this point in the history
Added new teleoperation script + PushCubeLoop env + small refactoring of the environments.
  • Loading branch information
perezjln authored Oct 20, 2024
2 parents d084937 + 48e38c5 commit f270bba
Show file tree
Hide file tree
Showing 18 changed files with 648 additions and 105 deletions.
2 changes: 1 addition & 1 deletion examples/dynamixel_mujoco_leader.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@ def do_sim(robot_id="6dof"):
parser.add_argument('--protocol_version', type=float, default=2.0, help='Protocol version (e.g., 1.0 or 2.0)')
args = parser.parse_args()

do_sim(args.robot)
do_sim(args.robot)
2 changes: 1 addition & 1 deletion examples/mujoco_interactive_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def do_interactive_sim(robot_id):
if robot_id == "6dof":
m = mujoco.MjModel.from_xml_path("gym_lowcostrobot/assets/low_cost_robot_6dof/reach_cube.xml")
m = mujoco.MjModel.from_xml_path("gym_lowcostrobot/assets/low_cost_robot_6dof/push_cube_loop.xml")#reach_cube.xml")

data = mujoco.MjData(m)

Expand Down
62 changes: 62 additions & 0 deletions examples/teleoperate_leader_lerobot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import argparse

import mujoco
import numpy as np
import matplotlib.pyplot as plt
import gymnasium as gym
import gym_lowcostrobot # Import the low-cost robot environments

from lerobot.common.robot_devices.motors.dynamixel import DynamixelMotorsBus

def do_sim(args):

env = gym.make(args.env_name, render_mode="human")

offsets = [0, -np.pi/2, -np.pi/2, 0, -np.pi/2, 0]
counts_to_radians = np.pi * 2. / 4096.
# get the start pos from .cache/calibration directory in your local lerobot
start_pos = [2072, 2020, 1063, 3966, 3053, 1938]
axis_direction = [-1, -1, 1, -1, -1, -1]
joint_commands = [0,0,0,0,0,0]
leader_arm = DynamixelMotorsBus(
port=args.device,
motors={
# name: (index, model)
"shoulder_pan": (1, "xl330-m077"),
"shoulder_lift": (2, "xl330-m077"),
"elbow_flex": (3, "xl330-m077"),
"wrist_flex": (4, "xl330-m077"),
"wrist_roll": (5, "xl330-m077"),
"gripper": (6, "xl330-m077"),
},
)

if not leader_arm.is_connected:
leader_arm.connect()

env.reset()
rewards = []
timesteps = []

while env.viewer.is_running():
positions = leader_arm.read("Present_Position")
assert len(joint_commands) == len(positions)
for i in range(len(joint_commands)):
joint_commands[i] = axis_direction[i] * \
(positions[i] - start_pos[i]) * counts_to_radians + offsets[i]

ret = env.step(joint_commands)

rewards.append(ret[1])
timesteps.append(env.data.time)

plt.plot(timesteps, rewards)
plt.show()

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Choose between 5dof and 6dof lowcost robot simulation.")
parser.add_argument('--device', type=str, default='/dev/ttyACM0', help='Port name (e.g., COM1, /dev/ttyUSB0, /dev/tty.usbserial-*)')
parser.add_argument('--env-name', type=str, default='PushCubeLoop-v0', help='Specify the gym-lowcost robot env to test.')
args = parser.parse_args()

do_sim(args)
7 changes: 7 additions & 0 deletions gym_lowcostrobot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
__version__ = "0.0.1"

ASSETS_PATH = os.path.join(os.path.dirname(__file__), "assets", "low_cost_robot_6dof")
BASE_LINK_NAME = "link_1"

register(
id="LiftCube-v0",
Expand Down Expand Up @@ -35,3 +36,9 @@
entry_point="gym_lowcostrobot.envs:StackTwoCubesEnv",
max_episode_steps=500,
)

register(
id="PushCubeLoop-v0",
entry_point="gym_lowcostrobot.envs:PushCubeLoopEnv",
max_episode_steps=500,
)
14 changes: 7 additions & 7 deletions gym_lowcostrobot/assets/low_cost_robot_6dof/follower.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

<default>
<default class="follower">
<joint armature="0.1" damping="1" actuatorfrcrange="-10 10"/>
<position kp="1000" kv="100" inheritrange="1"/>
<joint armature="0.1" damping="1." actuatorfrcrange="-10 10"/>
<position kp="1000" kv="10" inheritrange="1"/>
<default class="visual">
<geom type="mesh" group="2" contype="0" conaffinity="0"/>
<geom type="mesh" group="2" contype="1" conaffinity="1"/>
</default>
<default class="collision">
<geom type="mesh" group="3" mass="0" density="0"/>
<default class="finger">
<geom type="mesh" priority="1" condim="6" solimp="0.015 1 0.036" friction="0.8 0.02 0.01"/>
<geom type="mesh" priority="1" condim="6" solimp="0.015 1 0.036" friction="1.5"/>
</default>
</default>
<site size="0.001" rgba="0.5 0.5 0.5 0.3" group="4"/>
Expand Down Expand Up @@ -48,12 +48,12 @@
</asset>

<worldbody>
<body name="base_link" childclass="follower">
<body name="base_link" childclass="follower" quat="-0.707 0. 0. 0.707">
<geom mesh="base_link" class="visual" material="white"/>
<geom mesh="base_link_motor" class="visual" material="black"/>
<geom mesh="base_link_collision" class="collision"/>

<body name="link_1" pos="0.012 0 0.0409">
<body name="link_1" pos="0.012 0 0.0409" >
<inertial pos="0.011924 -0.00048792 0.013381" quat="-0.0190903 0.705417 0.0178052 0.708312" mass="0.05014" diaginertia="1.44921e-05 1.2371e-05 7.59138e-06"/>
<joint name="joint_1" pos="0 0 0" axis="0 0 -1" range="-3.14 3.14"/>
<geom mesh="link_1" class="visual" material="white"/>
Expand All @@ -69,7 +69,7 @@

<body name="link_3" pos="-0.0148 0.0065 0.1083">
<inertial pos="-0.05537 0.014505 0.0028659" quat="8.17663e-05 0.710999 -4.16983e-05 0.703193" mass="0.06379" diaginertia="2.45081e-05 2.2231e-05 7.34061e-06"/>
<joint name="joint_3" pos="0 0 0" axis="0 1 0" range="-3.14 3.14"/>
<joint name="joint_3" pos="0 0 0" axis="0 -1 0" range="-3.14 3.14"/>
<geom mesh="link_3" class="visual" material="white"/>
<geom mesh="link_3_motor" class="visual" material="black"/>
<geom mesh="link_3_collision" class="collision"/>
Expand Down
10 changes: 5 additions & 5 deletions gym_lowcostrobot/assets/low_cost_robot_6dof/lift_cube.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<mujoco model="low_cost_robot scene">
<!-- The timestep has a big influence on the contacts stability -->
<option cone="elliptic" impratio="10" timestep="0.001"/>
<option cone="elliptic" impratio="10" timestep="0.005"/>

<include file="follower.xml"/>

Expand All @@ -22,14 +22,14 @@
<worldbody>
<light pos="0 0 3" dir="0 0 -1" directional="false"/>
<geom name="floor" size="0 0 0.05" type="plane" material="groundplane" pos="0 0 0" friction="0.1"/>
<body name="box" pos="0.0 0.2 0.01">
<body name="cube" pos="0.0 0.2 0.01">
<freejoint name="red_box_joint"/>
<inertial pos="0 0 0" mass="0.1" diaginertia="0.00016667 0.00016667 0.00016667"/>
<geom friction="0.5" condim="3" pos="0 0 0" size="0.015 0.015 0.015" type="box" name="red_box" rgba="0.5 0 0 1" priority="1"/>
<geom friction="0.5" condim="4" pos="0 0 0" size="0.015 0.015 0.015" type="box" name="red_box" rgba="0.5 0 0 1" priority="1"/>
</body>

<camera name="camera_front" pos="0.049 0.888 0.317" xyaxes="-0.998 0.056 -0.000 -0.019 -0.335 0.942"/>
<camera name="camera_top" pos="0 0 1" euler="0 0 0" mode="fixed"/>
<camera name="camera_front" pos="0.049 0.5 0.225" xyaxes="-0.998 0.056 -0.000 -0.019 -0.335 0.942"/>
<camera name="camera_top" pos="0 0.1 0.6" euler="0 0 0" mode="fixed"/>
<camera name="camera_vizu" pos="-0.1 0.6 0.3" quat="-0.15 -0.1 0.6 1"/>

</worldbody>
Expand Down
11 changes: 6 additions & 5 deletions gym_lowcostrobot/assets/low_cost_robot_6dof/pick_place_cube.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<mujoco model="low_cost_robot scene">
<compiler angle="radian" autolimits="true" convexhull="false"/>
<!-- The timestep has a big influence on the contacts stability -->
<option cone="elliptic" impratio="10" timestep="0.001" gravity="0 0 -9.81"/>
<option cone="elliptic" impratio="10" timestep="0.005" gravity="0 0 -9.81"/>

<include file="follower.xml"/>

Expand All @@ -23,16 +23,17 @@
<worldbody>
<light pos="0 0 3" dir="0 0 -1" directional="false"/>
<geom name="floor" size="0 0 0.05" type="plane" material="groundplane" pos="0 0 0" friction="0.1"/>
<body name="box" pos="0.0 0.2 0.01">
<body name="cube" pos="0.0 0.2 0.01">
<freejoint name="red_box_joint"/>
<inertial pos="0 0 0" mass="10" diaginertia="0.00016667 0.00016667 0.00016667"/>
<geom friction="0.5" condim="3" pos="0 0 0" size="0.015 0.015 0.015" type="box" name="red_box" rgba="0.5 0 0 1" priority="1"/>
<geom friction="0.5" condim="4" pos="0 0 0" size="0.015 0.015 0.015" type="box" name="red_box" rgba="0.5 0 0 1" priority="1"/>
</body>

<camera name="camera_front" pos="0.049 0.888 0.317" xyaxes="-0.998 0.056 -0.000 -0.019 -0.335 0.942"/>
<camera name="camera_top" pos="0 0 1" euler="0 0 0" mode="fixed"/>
<camera name="camera_front" pos="0.049 0.5 0.225" xyaxes="-0.998 0.056 -0.000 -0.019 -0.335 0.942"/>
<camera name="camera_top" pos="0 0.1 0.6" euler="0 0 0" mode="fixed"/>
<camera name="camera_vizu" pos="-0.1 0.6 0.3" quat="-0.15 -0.1 0.6 1"/>

<geom name="target_region" type="cylinder" pos=".06 .135 0.005" size="0.035 0.01" rgba="0 0 1 0.3" contype="0" conaffinity="0" />
</worldbody>

</mujoco>
12 changes: 6 additions & 6 deletions gym_lowcostrobot/assets/low_cost_robot_6dof/push_cube.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<mujoco model="low_cost_robot scene">
<!-- The timestep has a big influence on the contacts stability -->
<option cone="elliptic" impratio="10" timestep="0.001"/>
<option cone="elliptic" impratio="10" timestep="0.005"/>

<include file="follower.xml"/>

Expand All @@ -22,17 +22,17 @@
<worldbody>
<light pos="0 0 3" dir="0 0 -1" directional="false"/>
<geom name="floor" size="0 0 0.05" type="plane" material="groundplane" pos="0 0 0" friction="0.1"/>
<body name="box" pos="0.0 0.2 0.01">
<body name="cube" pos="0.0 0.2 0.01">
<freejoint name="red_box_joint"/>
<inertial pos="0 0 0" mass="0.1" diaginertia="0.00016667 0.00016667 0.00016667"/>
<geom friction="0.5" condim="3" pos="0 0 0" size="0.015 0.015 0.015" type="box" name="red_box" rgba="0.5 0 0 1" priority="1"/>
<geom friction="0.5" condim="4" pos="0 0 0" size="0.015 0.015 0.015" type="box" name="red_box" rgba="0.5 0 0 1" priority="1"/>
</body>

<camera name="camera_front" pos="0.049 0.888 0.317" xyaxes="-0.998 0.056 -0.000 -0.019 -0.335 0.942"/>
<camera name="camera_top" pos="0 0 1" euler="0 0 0" mode="fixed"/>
<camera name="camera_front" pos="0.049 0.5 0.225" xyaxes="-0.998 0.056 -0.000 -0.019 -0.335 0.942"/>
<camera name="camera_top" pos="0 0.1 0.6" euler="0 0 0" mode="fixed"/>
<camera name="camera_vizu" pos="-0.1 0.6 0.3" quat="-0.15 -0.1 0.6 1"/>

<site name="cube_target" pos="1 0 0" size="0.01" rgba="1 0 0 1" />
<geom name="target_region" type="cylinder" pos=".06 .135 0.005" size="0.035 0.01" rgba="0 0 1 0.3" contype="0" conaffinity="0" />

</worldbody>

Expand Down
51 changes: 51 additions & 0 deletions gym_lowcostrobot/assets/low_cost_robot_6dof/push_cube_loop.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<mujoco model="bd1 scene">
<option timestep="0.005"/>
<option gravity="0 0 -9.81" />

<compiler angle="radian" autolimits="true"/>

<include file="follower.xml"/>

<asset>
<texture type="skybox" builtin="gradient" rgb1="0.3 0.5 0.7" rgb2="0 0 0" width="512" height="3072" />
<texture type="2d" name="groundplane" builtin="checker" mark="edge" rgb1="0.2 0.3 0.4" rgb2="0.1 0.2 0.3" markrgb="0.8 0.8 0.8" width="300" height="300" />
<material name="groundplane" texture="groundplane" texuniform="true" texrepeat="5 5" reflectance="0.2" />
</asset>

<visual>
<headlight diffuse="0.6 0.6 0.6" ambient="0.3 0.3 0.3" specular="0 0 0" />
<rgba haze="0.15 0.25 0.35 1" />
<global azimuth="150" elevation="-20" offheight="640" />
</visual>

<worldbody>
<light pos="0 0 3" dir="0 0 -1" directional="false" />
<body name="floor">
<geom pos="0 0 0" name="floor" size="0 0 .125" type="plane" material="groundplane" solref="0.0 0.0"/>
</body>

<body name="cube" pos="0.06 0.135 0.017">
<freejoint name="cube"/>
<inertial pos="0 0 0" mass=".05" diaginertia="0.00001125 0.00001125 0.00001125"/>
<geom friction="1.5 1.5 1.5" condim="4" pos="0 0 0" size="0.015 0.015 0.015" type="box" name="cube" rgba="0.5 0 0 1" priority="1"/>
</body>

<camera name="camera_front" pos="0.049 0.5 0.225" xyaxes="-0.998 0.056 -0.000 -0.019 -0.335 0.942"/>
<camera name="camera_top" pos="0 0.1 0.6" euler="0 0 0" mode="fixed"/>
<camera name="camera_vizu" pos="-0.1 0.6 0.3" quat="-0.15 -0.1 0.6 1"/>

<!-- Goal Region 1 -->
<geom name="goal_region_1" type="box" pos=".06 .135 0.01" size="0.035 0.045 0.007" rgba="1 1 0 0.3" contype="0" conaffinity="0" />

<!-- Goal Region 2 -->
<geom name="goal_region_2" type="box" pos="-.06 .135 0.01" size="0.035 0.045 0.007" rgba="1 0 1 0.3" contype="0" conaffinity="0"/>

<!-- Define the rails that contain the cube -->
<geom name="left_wall" type="box" pos="-0.125 0.135 0.005" size="0.01 0.055 0.007" rgba="1 1 1 1" />
<geom name="right_wall" type="box" pos="0.125 0.135 0.005" size="0.01 0.055 0.007" rgba="1 1 1 1" />
<geom name="top_wall" type="box" pos="0 0.09 0.005" size="0.125 0.01 0.007" rgba="1 1 1 1" />
<geom name="bottom_wall" type="box" pos="0 0.18 0.005" size="0.125 0.01 0.007" rgba="1 1 1 1" />


</worldbody>
</mujoco>
9 changes: 5 additions & 4 deletions gym_lowcostrobot/assets/low_cost_robot_6dof/reach_cube.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<mujoco model="low_cost_robot scene">
<option timestep="0.005"/>
<include file="follower.xml"/>

<statistic center="0 0 0.2" extent=".8"/>
Expand All @@ -19,14 +20,14 @@
<worldbody>
<light pos="0 0 3" dir="0 0 -1" directional="false"/>
<geom name="floor" size="0 0 0.05" type="plane" material="groundplane" pos="0 0 0" friction="0.1"/>
<body name="box" pos="0.0 0.2 0.01">
<body name="cube" pos="0.0 0.2 0.01">
<freejoint name="cube"/>
<inertial pos="0 0 0" mass="0.1" diaginertia="0.00016667 0.00016667 0.00016667"/>
<geom friction="0.5" condim="3" pos="0 0 0" size="0.015 0.015 0.015" type="box" name="red_box" rgba="0.5 0 0 1" priority="1"/>
<geom friction="0.5" condim="4" pos="0 0 0" size="0.015 0.015 0.015" type="box" name="red_box" rgba="0.5 0 0 1" priority="1"/>
</body>

<camera name="camera_front" pos="0.049 0.888 0.317" xyaxes="-0.998 0.056 -0.000 -0.019 -0.335 0.942"/>
<camera name="camera_top" pos="0 0 1" euler="0 0 0" mode="fixed"/>
<camera name="camera_front" pos="0.049 0.5 0.225" xyaxes="-0.998 0.056 -0.000 -0.019 -0.335 0.942"/>
<camera name="camera_top" pos="0 0.1 0.6" euler="0 0 0" mode="fixed"/>
<camera name="camera_vizu" pos="-0.2 0.6 0.3" quat="-0.15 -0.1 0.6 1"/>

</worldbody>
Expand Down
20 changes: 10 additions & 10 deletions gym_lowcostrobot/assets/low_cost_robot_6dof/stack_two_cubes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<mujoco model="low_cost_robot scene">
<!-- The timestep has a big influence on the contacts stability -->
<option cone="elliptic" impratio="10" timestep="0.001"/>
<option cone="elliptic" impratio="10" timestep="0.005"/>

<include file="follower.xml"/>

Expand All @@ -23,19 +23,19 @@
<light pos="0 0 3" dir="0 0 -1" directional="false"/>
<geom name="floor" size="0 0 0.05" type="plane" material="groundplane" pos="0 0 0" friction="0.1"/>
<body name="cube_red" pos="0.1 0.1 0.01">
<freejoint name="cube_red"/>
<inertial pos="0 0 0" mass="0.1" diaginertia="0.00001125 0.00001125 0.00001125"/>
<geom friction="0.5" condim="3" pos="0 0 0" size="0.015 0.015 0.015" type="box" name="cube_red" rgba="0.5 0 0 1" priority="1"/>
</body>
<freejoint name="cube_red"/>
<inertial pos="0 0 0" mass="0.1" diaginertia="0.00001125 0.00001125 0.00001125"/>
<geom friction="0.5" condim="4" pos="0 0 0" size="0.015 0.015 0.015" type="box" name="cube_red" rgba="0.5 0 0 1" priority="1"/>
</body>

<body name="cube_blue" pos="-0.1 -0.1 0.01">
<body name="cube_blue" pos="-0.1 -0.1 0.01">
<freejoint name="cube_blue"/>
<inertial pos="0 0 0" mass="0.1" diaginertia="0.00001125 0.00001125 0.00001125"/>
<geom friction="0.5" condim="3" pos="0 0 0" size="0.015 0.015 0.015" type="box" name="cube_blue" rgba="0 0 0.5 1" priority="1"/>
</body>
<geom friction="0.5" condim="4" pos="0 0 0" size="0.015 0.015 0.015" type="box" name="cube_blue" rgba="0 0 0.5 1" priority="1"/>
</body>

<camera name="camera_front" pos="0.049 0.888 0.317" xyaxes="-0.998 0.056 -0.000 -0.019 -0.335 0.942"/>
<camera name="camera_top" pos="0 0 1" euler="0 0 0" mode="fixed"/>
<camera name="camera_front" pos="0.049 0.5 0.225" xyaxes="-0.998 0.056 -0.000 -0.019 -0.335 0.942"/>
<camera name="camera_top" pos="0 0.1 0.6" euler="0 0 0" mode="fixed"/>
<camera name="camera_vizu" pos="-0.1 0.6 0.3" quat="-0.15 -0.1 0.6 1"/>

</worldbody>
Expand Down
3 changes: 2 additions & 1 deletion gym_lowcostrobot/envs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
from .push_cube_env import PushCubeEnv
from .reach_cube_env import ReachCubeEnv
from .stack_two_cubes_env import StackTwoCubesEnv
from .push_cube_loop_env import PushCubeLoopEnv

__all__ = ["LiftCubeEnv", "PickPlaceCubeEnv", "PushCubeEnv", "ReachCubeEnv", "StackTwoCubesEnv"]
__all__ = ["LiftCubeEnv", "PickPlaceCubeEnv", "PushCubeEnv", "ReachCubeEnv", "StackTwoCubesEnv", "PushCubeLoopEnv"]
Loading

0 comments on commit f270bba

Please sign in to comment.