-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmujoco_ball_catcher.py
82 lines (61 loc) · 2.31 KB
/
mujoco_ball_catcher.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
import time
import math
import robot
import numpy as np
import mujoco
import mujoco.viewer
m = mujoco.MjModel.from_xml_path('world/arm_ball_catcher.xml')
d = mujoco.MjData(m)
with mujoco.viewer.launch_passive(m, d) as viewer:
start = time.time()
# Ball information
ball_r = 0.03
ball_tolerance = ball_r*0.01
ball_p = np.zeros(3)
ball_v = np.zeros(3)
ball_destination_p = np.array([0.5, -0.5, 0.15])
# Setting up the Arm Robot
joint_names = ["base", "shoulder", "elbow"]
arm = robot.RobotModel(d, joint_names)
# Set End Effector Estimator Function
arm.set_update_transform_method(robot.update_transform_method)
# Setting up the task scheduler
ts = robot.task_scheduler.RobotTaskScheduler()
# go_to_ball
robot.ball_catcher_tasks.task_go_to_ball.robot = arm
ts.add(robot.ball_catcher_tasks.task_go_to_ball)
# grab_ball
robot.ball_catcher_tasks.task_grab_ball.robot = arm
ts.add(robot.ball_catcher_tasks.task_grab_ball)
# move_ball_to_destination
robot.ball_catcher_tasks.task_move_ball_to_destination.robot = arm
ts.add(robot.ball_catcher_tasks.task_move_ball_to_destination)
# ungrab_ball
robot.ball_catcher_tasks.task_ungrab_ball.robot = arm
ts.add(robot.ball_catcher_tasks.task_ungrab_ball)
while viewer.is_running():
step_start = time.time()
# mj_step can be replaced with code that also evaluates
# a policy and applies a control signal before stepping the physics.
mujoco.mj_step(m, d)
# Pick up changes to the physics state, apply perturbations, update options from GUI.
viewer.sync()
# Update robot model observation
arm.observe()
ball_p = d.geom('ball').xpos
ball_v = d.sensor('ball_vx').data
# Update arm End Effector
arm.update()
# Actuate using the task scheduler
if not ts.empty():
if ts.current_task().name == "go_to_ball":
ts.current_task().target = ball_p
elif ts.current_task().name == "grab_ball":
ts.current_task().target = float(d.time)
elif ts.current_task().name == "move_ball_to_destination":
ts.current_task().target = ball_destination_p
ts.spin_once(d.time)
# Rudimentary time keeping, will drift relative to wall clock.
time_until_next_step = m.opt.timestep - (time.time() - step_start)
if time_until_next_step > 0:
time.sleep(time_until_next_step)