Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions examples/camera_feeds.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import random
import threading
import time

import cv2

from stretch_mujoco.enums.actuators import Actuators
from stretch_mujoco.enums.stretch_cameras import StretchCameras
from stretch_mujoco.stretch_mujoco_simulator import StretchMujocoSimulator


def show_camera_feeds_sync(
sim: StretchMujocoSimulator,
print_fps: bool
):
def show_camera_feeds_sync(sim: StretchMujocoSimulator, print_fps: bool):
"""
Pull camera data from the simulator and display it using OpenCV.

Expand All @@ -20,8 +19,10 @@ def show_camera_feeds_sync(
camera_data = sim.pull_camera_data()

if print_fps:
print(f"Physics fps: {sim.pull_status().fps}. Camera FPS: {camera_data.fps}. {sim.pull_status().sim_to_real_time_ratio_msg}")

print(
f"Physics fps: {sim.pull_status().fps}. Camera FPS: {camera_data.fps}. {sim.pull_status().sim_to_real_time_ratio_msg}"
)

for camera_name, pixels in camera_data.get_all(use_depth_color_map=True).items():
cv2.imshow(camera_name.name, pixels)

Expand All @@ -33,6 +34,7 @@ def my_control_loop():
sim.move_to(Actuators.lift, random.random())
time.sleep(3)


if __name__ == "__main__":
# You can use all the camera's, but it takes longer to render, and may affect the overall simulation FPS.
# cameras_to_use = StretchCameras.all()
Expand All @@ -49,4 +51,4 @@ def my_control_loop():
show_camera_feeds_sync(sim, True)

except KeyboardInterrupt:
sim.stop()
sim.stop()
16 changes: 9 additions & 7 deletions examples/draw_circles.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import threading
import time

import numpy as np

from examples.camera_feeds import show_camera_feeds_sync
Expand All @@ -9,11 +9,11 @@
from stretch_mujoco.stretch_mujoco_simulator import StretchMujocoSimulator


def draw_circle(n, diameter_m, arm_init, lift_init, sim:StretchMujocoSimulator):
def draw_circle(n, diameter_m, arm_init, lift_init, sim: StretchMujocoSimulator):
"""
From https://forum.hello-robot.com/t/creating-smooth-motion-using-trajectories/671
"""
t = np.linspace(0, 2*np.pi, n, endpoint=True)
t = np.linspace(0, 2 * np.pi, n, endpoint=True)
x = (diameter_m / 2) * np.cos(t) + arm_init
y = (diameter_m / 2) * np.sin(t) + lift_init
circle_mat = np.c_[x, y]
Expand All @@ -25,10 +25,11 @@ def draw_circle(n, diameter_m, arm_init, lift_init, sim:StretchMujocoSimulator):
sim.wait_until_at_setpoint(Actuators.arm)
sim.wait_until_at_setpoint(Actuators.lift)


def _run_draw_circle():
time.sleep(2)
try:
while sim.is_running():
while sim.is_running():
sim.move_to(Actuators.head_tilt, -1.5707)
sim.move_to(Actuators.head_pan, -0.7853)

Expand All @@ -39,13 +40,14 @@ def _run_draw_circle():

sim.move_to(Actuators.gripper, pos=-0.15)
sim.wait_until_at_setpoint(Actuators.gripper)

status = sim.pull_status()
draw_circle(25, 0.2, status.arm.pos, status.lift.pos, sim)
time.sleep(1)
sim.home()
time.sleep(2)
except ConnectionError: ...
except ConnectionError:
...


if __name__ == "__main__":
Expand All @@ -65,4 +67,4 @@ def _run_draw_circle():
show_camera_feeds_sync(sim, True)

except KeyboardInterrupt:
sim.stop()
sim.stop()
8 changes: 4 additions & 4 deletions examples/gamepad_teleop.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

import click
import cv2
from examples.camera_feeds import show_camera_feeds_sync
from gamepad_controller import GamePadController

from examples.camera_feeds import show_camera_feeds_sync
from stretch_mujoco import StretchMujocoSimulator
from stretch_mujoco.enums.stretch_cameras import StretchCameras
from stretch_mujoco.enums.actuators import Actuators
from stretch_mujoco.enums.stretch_cameras import StretchCameras

sim:StretchMujocoSimulator
gamepad:GamePadController
sim: StretchMujocoSimulator
gamepad: GamePadController

button_mapping = {
"top_pad_pressed": ["wrist_pitch", 1, 0.05],
Expand Down
21 changes: 13 additions & 8 deletions examples/keyboard_teleop.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from time import sleep
from pynput import keyboard
from pprint import pprint
from time import sleep

import click
from pynput import keyboard

from examples.camera_feeds import show_camera_feeds_sync
from examples.laser_scan import show_laser_scan
Expand Down Expand Up @@ -95,7 +95,14 @@ def on_release(key, sim: StretchMujocoSimulator):
@click.option("--imagery", is_flag=True, help="Show all the cameras' imagery")
@click.option("--lidar", is_flag=True, help="Show the lidar scan in Matplotlib")
@click.option("--print-ratio", is_flag=True, help="Print the sim-to-real time ratio to the cli.")
def main(scene_xml_path: str|None, robocasa_env: bool, imagery_nav: bool, imagery: bool, lidar:bool, print_ratio:bool):
def main(
scene_xml_path: str | None,
robocasa_env: bool,
imagery_nav: bool,
imagery: bool,
lidar: bool,
print_ratio: bool,
):
cameras_to_use = StretchCameras.all() if imagery else []
if imagery_nav:
cameras_to_use = [StretchCameras.cam_nav_rgb]
Expand All @@ -110,9 +117,7 @@ def main(scene_xml_path: str|None, robocasa_env: bool, imagery_nav: bool, imager
model, xml, objects_info = model_generation_wizard()

sim = StretchMujocoSimulator(
model=model,
scene_xml_path=scene_xml_path,
cameras_to_use=cameras_to_use
model=model, scene_xml_path=scene_xml_path, cameras_to_use=cameras_to_use
)

try:
Expand Down Expand Up @@ -140,8 +145,8 @@ def main(scene_xml_path: str|None, robocasa_env: bool, imagery_nav: bool, imager

try:
show_laser_scan(scan_data=sensor_data.get_data(StretchSensors.base_lidar))
except: ...

except:
...

listener.stop()

Expand Down
27 changes: 16 additions & 11 deletions examples/laser_scan.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import time

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import matplotlib

from stretch_mujoco.enums.stretch_cameras import StretchCameras
from stretch_mujoco.enums.stretch_sensors import StretchSensors
from stretch_mujoco.stretch_mujoco_simulator import StretchMujocoSimulator

try:
# Some machines seem to need this for matplotlib to work.
matplotlib.use("TkAgg")
except: ...
except:
...


def show_laser_scan(scan_data: np.ndarray):

Expand All @@ -23,20 +27,20 @@ def show_laser_scan(scan_data: np.ndarray):

if len(filtered_distance) == 0:
return time.sleep(1 / 15)

degrees = np.array(range(len(scan_data)))
degrees = np.radians(degrees)

degrees_filtered = degrees[mask_lower & mask_upper]

x = filtered_distance * np.cos(degrees_filtered) * -1
y = filtered_distance * np.sin(degrees_filtered) * -1

degrees_filtered = np.rad2deg(degrees_filtered)
front_idx = (degrees_filtered >= 150) & (degrees_filtered <= 210) # ~180
back_idx = (degrees_filtered >= 330) | (degrees_filtered <= 30) # ~0
right_idx= (degrees_filtered >= 60) & (degrees_filtered <= 120) # ~90
left_idx = (degrees_filtered >= 240) & (degrees_filtered <= 300) # ~270
front_idx = (degrees_filtered >= 150) & (degrees_filtered <= 210) # ~180
back_idx = (degrees_filtered >= 330) | (degrees_filtered <= 30) # ~0
right_idx = (degrees_filtered >= 60) & (degrees_filtered <= 120) # ~90
left_idx = (degrees_filtered >= 240) & (degrees_filtered <= 300) # ~270

plt.scatter(x, y, color="r", s=5)
plt.scatter(x[front_idx], y[front_idx], color="g", s=5)
Expand All @@ -45,8 +49,8 @@ def show_laser_scan(scan_data: np.ndarray):
plt.scatter(x[right_idx], y[right_idx], color="c", s=5)
max_x = np.abs(x).max()
max_y = np.abs(y).max()
plt.xlim([-max_x-1, max_x+1])
plt.ylim([-max_y-1, max_y+1])
plt.xlim([-max_x - 1, max_x + 1])
plt.ylim([-max_y - 1, max_y + 1])
plt.legend(["All", "Front", "Back", "Left", "Right"])

plt.pause(1 / 15)
Expand All @@ -70,7 +74,8 @@ def show_laser_scan(scan_data: np.ndarray):

try:
show_laser_scan(scan_data=sensor_data.get_data(StretchSensors.base_lidar))
except: ...
except:
...

current_position = status.base.x

Expand Down
16 changes: 11 additions & 5 deletions examples/move_joints.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import random

import numpy as np

from stretch_mujoco.enums.actuators import Actuators
from stretch_mujoco.stretch_mujoco_simulator import StretchMujocoSimulator


def lift_sequence():
LIFT_START_POS = 0.1
MOVE_LIFT_BY = 0.5
Expand All @@ -14,15 +16,19 @@ def lift_sequence():
start_lift_position = sim.pull_status().lift.pos

if not np.isclose(start_lift_position, LIFT_START_POS, atol=0.05):
print(f"The lift did not move to the starting position. Should be at {LIFT_START_POS}, but is at {start_lift_position:.2f} instead.")
print(
f"The lift did not move to the starting position. Should be at {LIFT_START_POS}, but is at {start_lift_position:.2f} instead."
)

sim.move_by(Actuators.lift, MOVE_LIFT_BY)
sim.wait_while_is_moving(Actuators.lift)

current_lift_position = sim.pull_status().lift.pos

if not np.isclose(start_lift_position, current_lift_position, atol=0.05):
print(f"The lift did not move by the specified amount. Asked to move from {start_lift_position:.4f} by {MOVE_LIFT_BY}, but ended up at {current_lift_position:.4f}. Should be {start_lift_position + MOVE_LIFT_BY :.4f}")
print(
f"The lift did not move by the specified amount. Asked to move from {start_lift_position:.4f} by {MOVE_LIFT_BY}, but ended up at {current_lift_position:.4f}. Should be {start_lift_position + MOVE_LIFT_BY :.4f}"
)


if __name__ == "__main__":
Expand Down Expand Up @@ -51,8 +57,8 @@ def lift_sequence():
target *= -1
sim.set_base_velocity(v_linear=5.0, omega=30)

sim.move_to(Actuators.head_pan, random.random()-0.5)
sim.move_to(Actuators.head_tilt, random.random()-0.5)
sim.move_to(Actuators.head_pan, random.random() - 0.5)
sim.move_to(Actuators.head_tilt, random.random() - 0.5)
sim.wait_until_at_setpoint(Actuators.head_pan)
sim.wait_until_at_setpoint(Actuators.head_tilt)

Expand Down
2 changes: 1 addition & 1 deletion examples/robocasa_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def main(task: str, layout: int, style: int, write_to_file):
# You can use all the camera's, but it takes longer to render, and may affect the overall simulation FPS.
# cameras_to_use = StretchCameras.all()
cameras_to_use = [StretchCameras.cam_d405_rgb]

model, xml, objects_info = model_generation_wizard(
task=task,
layout=layout,
Expand Down
16 changes: 7 additions & 9 deletions examples/test_one_process.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from multiprocessing import Manager
import signal
import threading
from multiprocessing import Manager

from stretch_mujoco.mujoco_server import MujocoServer

from stretch_mujoco.mujoco_server import MujocoServerProxies
from stretch_mujoco.mujoco_server import MujocoServer, MujocoServerProxies

_manager = Manager()
data_proxies = MujocoServerProxies.default(_manager)
Expand All @@ -14,11 +12,11 @@
signal.signal(signal.SIGINT, lambda num, frame: event.set())

MujocoServer.launch_server(
scene_xml_path=None,
model=None,
camera_hz=30,
scene_xml_path=None,
model=None,
camera_hz=30,
show_viewer_ui=True,
stop_mujoco_process_event=event,
stop_mujoco_process_event=event,
data_proxies=data_proxies,
cameras_to_use=[]
cameras_to_use=[],
)
14 changes: 7 additions & 7 deletions examples/test_one_process_passive.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from multiprocessing import Manager
import signal
import threading
from multiprocessing import Manager

from stretch_mujoco.mujoco_server import MujocoServerProxies
from stretch_mujoco.mujoco_server_passive import MujocoServerPassive
Expand All @@ -13,11 +13,11 @@
signal.signal(signal.SIGINT, lambda num, frame: event.set())

MujocoServerPassive.launch_server(
scene_xml_path=None,
model=None,
camera_hz=30,
scene_xml_path=None,
model=None,
camera_hz=30,
show_viewer_ui=True,
stop_mujoco_process_event=event,
stop_mujoco_process_event=event,
data_proxies=data_proxies,
cameras_to_use=[]
)
cameras_to_use=[],
)
10 changes: 3 additions & 7 deletions launch_sim.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import stretch_mujoco
import click
import cv2

import stretch_mujoco
from stretch_mujoco.enums.stretch_cameras import StretchCameras


@click.command()
@click.option("--scene-xml-path", help="Path to a scene xml file")
@click.option("--headless", is_flag=True, help="Run the simulation headless")
@click.option("--imagery", is_flag=True, help="Show the cameras' imagery")
def main(
scene_xml_path: str,
headless: bool,
imagery: bool
) -> None:
def main(scene_xml_path: str, headless: bool, imagery: bool) -> None:
cameras_to_use = StretchCameras.all() if imagery else []
sim = stretch_mujoco.StretchMujocoSimulator(scene_xml_path,cameras_to_use=cameras_to_use)
sim = stretch_mujoco.StretchMujocoSimulator(scene_xml_path, cameras_to_use=cameras_to_use)
sim.start(headless=headless)
try:
while sim.is_running():
Expand Down
8 changes: 4 additions & 4 deletions stretch_mujoco/datamodels/status_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ class StatusCommand:

move_to: dict[str, CommandMove] = field(default_factory=dict)
move_by: dict[str, CommandMove] = field(default_factory=dict)
base_velocity: CommandBaseVelocity = field(default_factory=lambda:CommandBaseVelocity(0, 0, False))
keyframe: CommandKeyframe = field(default_factory=lambda:CommandKeyframe("", False))
base_velocity: CommandBaseVelocity = field(
default_factory=lambda: CommandBaseVelocity(0, 0, False)
)
keyframe: CommandKeyframe = field(default_factory=lambda: CommandKeyframe("", False))
coordinate_frame_arrows_viz: list[CommandCoordinateFrameArrowsViz] = field(default_factory=list)



def set_move_to(self, command: CommandMove):
"""Sends a move_to command and removes the move_by command."""
self.move_to[command.actuator_name] = command
Expand Down
Loading