99
1010
1111import jax
12+
13+ jax .config .update ("jax_enable_x64" , True )
14+
1215import jax .numpy as jnp
1316import mujoco
1417import mujoco .viewer
@@ -36,18 +39,29 @@ def solve_mpc(mpc_data, qpos, qvel, command):
3639
3740 return solve_mpc
3841
39- def sample_reachable_position (rng , model , data , end_effector_id , qpos ):
40- joint_range = model .jnt_range [: config .n_joints ]
41- sample_qpos = rng .uniform (
42- low = joint_range [:, 0 ],
43- high = joint_range [:, 1 ],
44- )
45- data .qpos [:] = sample_qpos
46- data .qvel [:] = 0.0
47- mujoco .mj_forward (model , data )
48- if data .geom_xpos [end_effector_id ][2 ] < 0.4 :
49- return sample_reachable_position (rng , model , data , end_effector_id , qpos )
50- return data .geom_xpos [end_effector_id ].copy ()
42+ # def sample_reachable_position(rng, model, data, end_effector_id, qpos):
43+ # joint_range = model.jnt_range[: config.n_joints]
44+ # sample_qpos = rng.uniform(
45+ # low=joint_range[:, 0],
46+ # high=joint_range[:, 1],
47+ # )
48+ # data.qpos[:] = sample_qpos
49+ # data.qvel[:] = 0.0
50+ # mujoco.mj_forward(model, data)
51+ # if data.geom_xpos[end_effector_id][2] < 0.4:
52+ # return sample_reachable_position(rng, model, data, end_effector_id, qpos)
53+ # return data.geom_xpos[end_effector_id].copy()
54+ def sample_position (indx ,obstacle_center ,obstacle_radius ):
55+ if indx == 0 :
56+ return np .array ([0 ,0 ,obstacle_radius + 0.01 ]) + obstacle_center
57+ elif indx == 1 :
58+ return np .array ([0.0 ,obstacle_radius + 0.01 ,0.0 ]) + obstacle_center
59+ elif indx == 2 :
60+ return np .array ([0.0 ,0.0 ,- obstacle_radius - 0.01 ]) + obstacle_center
61+ elif indx == 3 :
62+ return np .array ([0.0 ,- obstacle_radius - 0.01 ,0.0 ]) + obstacle_center
63+ else :
64+ raise ValueError ("Invalid index for sample_position" )
5165
5266def main ():
5367 model = mujoco .MjModel .from_xml_path (dir_path + "/../data/piper_l/scene_flat.xml" )
@@ -65,9 +79,8 @@ def main():
6579 end_effector_id = mujoco .mj_name2id (
6680 model , mujoco .mjtObj .mjOBJ_GEOM , config .contact_frame [0 ]
6781 )
68- desired_position = sample_reachable_position (
69- rng , model , target_data , end_effector_id , config .q0
70- )
82+ des_pos_idx = 0
83+ desired_position = sample_position (des_pos_idx , config .obstacle_center , config .obstacle_radius )
7184
7285 data .qpos = np .asarray (config .q0 )
7386 mujoco .mj_forward (model , data )
@@ -86,7 +99,7 @@ def main():
8699 q = jnp .zeros (config .n_joints )
87100 dq = jnp .zeros (config .n_joints )
88101 def step_controller ():
89- nonlocal counter , tau , mpc_data , desired_position , target_start_counter , q , dq
102+ nonlocal counter , tau , mpc_data , desired_position , target_start_counter , q , dq , des_pos_idx
90103
91104 qpos = data .qpos .copy ()
92105 qvel = data .qvel .copy ()
@@ -107,9 +120,8 @@ def step_controller():
107120 counter += 1
108121 timed_out = counter - target_start_counter >= max_steps_per_target
109122 if timed_out :
110- desired_position = sample_reachable_position (
111- rng , model , target_data , end_effector_id , data .qpos
112- )
123+ des_pos_idx = (des_pos_idx + 1 ) % 4
124+ desired_position = sample_position (des_pos_idx , config .obstacle_center , config .obstacle_radius )
113125 target_start_counter = counter
114126 mpc_data = reset_mpc (
115127 mpc .make_data (), data .qpos .copy (), data .qvel .copy ()
@@ -120,13 +132,19 @@ def step_controller():
120132 model ,
121133 data
122134 ) as viewer :
123- viewer . sync ()
135+
124136 desired_position_geom_id = sim_utils .render_sphere (
125137 viewer ,
126138 desired_position ,
127139 diameter = 0.05 ,
128140 color = np .array ([0.0 , 0.0 , 1.0 , 0.5 ]))
129-
141+ obstacle_geom_id = sim_utils .render_sphere (
142+ viewer ,
143+ config .obstacle_center ,
144+ diameter = config .obstacle_radius * 2 ,
145+ color = np .array ([1.0 , 0.0 , 0.0 , 0.5 ])
146+ )
147+ viewer .sync ()
130148 while viewer .is_running ():
131149 tic = timer ()
132150 step_controller ()
0 commit comments