Skip to content

Commit 13a65c2

Browse files
committed
modified spot arm script to plot the end effector reference
1 parent c34131a commit 13a65c2

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

mpx/examples/mjx_spot_arm.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import numpy as np
1818

1919
import mpx.config.config_spot_arm as config
20-
import mpx.utils.mpc_wrapper as mpc_wrapper
20+
# import mpx.utils.mpc_wrapper as mpc_wrapper
2121
import mpx.utils.sim as sim_utils
2222

2323
jax.config.update("jax_compilation_cache_dir", "./jax_cache")
@@ -32,7 +32,7 @@ def solve_mpc(mpc_data, qpos, qvel, foot, command, contact):
3232
mpc.initial_state
3333
.at[mpc.qpos_slice].set(qpos)
3434
.at[mpc.qvel_slice].set(qvel)
35-
.at[mpc.foot_slice].set(foot)
35+
.at[config.leg_slice].set(foot)
3636
)
3737
return mpc.run(mpc_data, x0, command, contact)
3838

@@ -49,7 +49,7 @@ def main(headless=False, steps=500, scene="flat"):
4949
model.opt.timestep = 1 / sim_frequency
5050

5151
contact_ids = sim_utils.geom_ids(model, config.contact_frame)
52-
mpc = mpc_wrapper.MPCWrapper(config, limited_memory=True)
52+
mpc = config.MpcWrapper(config, limited_memory=True)
5353
command_handle = sim_utils.KeyboardVelocityCommand()
5454
solve_mpc = _build_solve_fn(mpc)
5555
reset_mpc = jax.jit(mpc.reset)
@@ -62,7 +62,7 @@ def main(headless=False, steps=500, scene="flat"):
6262
mpc_data = reset_mpc(mpc.make_data(), data.qpos.copy(), data.qvel.copy(), foot)
6363

6464
warm_command = jnp.asarray(command_handle.mpc_input(config.robot_height))
65-
warm_contact = jnp.asarray(sim_utils.estimate_contacts(data, contact_ids))
65+
warm_contact = jnp.asarray(sim_utils.estimate_contacts(data, contact_ids[:4]))
6666
mpc_data, tau = solve_mpc(
6767
mpc_data,
6868
data.qpos.copy(),
@@ -73,7 +73,8 @@ def main(headless=False, steps=500, scene="flat"):
7373
)
7474
tau.block_until_ready()
7575
mpc_data = reset_mpc(mpc_data, data.qpos.copy(), data.qvel.copy(), foot)
76-
76+
arm_ref_fun = jax.jit(config.extra_ref_fun)
77+
arm_ref_data = {"foot": jnp.zeros((config.N + 1, 3))}
7778
period = int(sim_frequency / config.mpc_frequency)
7879
print(f"Controller period: {period} steps at {sim_frequency} Hz simulation frequency.")
7980
counter = 0
@@ -90,7 +91,7 @@ def step_controller():
9091
foot = jnp.asarray(sim_utils.geom_positions(data, contact_ids))
9192

9293
command = jnp.asarray(command_handle.mpc_input(config.robot_height))
93-
contact = jnp.asarray(sim_utils.estimate_contacts(data, contact_ids))
94+
contact = jnp.asarray(sim_utils.estimate_contacts(data, contact_ids[:4]))
9495
print(f"Contact: {contact}")
9596
print(foot)
9697
print(f"Command: {command}")
@@ -111,7 +112,6 @@ def step_controller():
111112
# The shifted warm start is the next joint target used by the PD stabilizer.
112113
q_ref = mpc_data.X0[0, 7 : 7 + config.n_joints]
113114
print(f"MPC time: {1e3 * (stop - start):.2f} ms")
114-
115115
data.ctrl = np.asarray(tau)
116116
mujoco.mj_step(model, data)
117117
counter += 1
@@ -120,19 +120,25 @@ def step_controller():
120120
for _ in range(steps):
121121
step_controller()
122122
return
123-
123+
arm_ref_sphere = []
124+
arm_pred_sphere = []
124125
with mujoco.viewer.launch_passive(
125126
model,
126127
data,
127128
key_callback=command_handle.key_callback,
128129
) as viewer:
129130
viewer.sync()
131+
130132
while viewer.is_running():
131133
overlay_text = command_handle.consume_overlay_text()
132134
tic = timer()
133135
if overlay_text is not None:
134136
viewer.set_texts((None, None, *overlay_text))
135137
step_controller()
138+
arm_ref_data["foot"] = jnp.zeros((config.N + 1, 1))
139+
arm_ref_data_new = arm_ref_fun(arm_ref_data,counter * model.opt.timestep)
140+
arm_ref_sphere = sim_utils.render_sphere_trajectory(viewer, arm_ref_data_new["foot"][:,1:],np.ones(config.N+1),0.03,np.array([0.0,1.0,0.0,1.0]),geom_ids = arm_ref_sphere)
141+
arm_pred_sphere = sim_utils.render_sphere_trajectory(viewer, mpc_data.X0[:,13+2*config.n_joints + 12 :13+2*config.n_joints+15],np.ones(config.N+1),0.03,geom_ids = arm_pred_sphere)
136142
toc = timer()
137143
if toc - tic < model.opt.timestep:
138144
sleep_time = model.opt.timestep - (toc - tic)

0 commit comments

Comments
 (0)