11import jax .numpy as jnp
22import jax
33import mpx .utils .models as mpc_dyn_model
4+ import mpx .utils .mpc_utils as mpc_utils
45import mpx .utils .objectives as mpc_objectives
56import os
67from functools import partial
2324step_height = 0.065 # Step height in meters
2425initial_height = 0.1 # Initial height of the robot's base in meters
2526robot_height = 0.27 # Height of the robot's base in meters
27+ clearance_speed = 0.2
2628
2729# Initial positions, orientations, and joint angles
2830p0 = jnp .array ([0 , 0 , robot_height ]) # Initial position of the robot's base
4345n = 13 + 2 * n_joints + 6 * n_contact # Number of states (theta1, theta1_dot, theta2, theta2_dot)
4446m = n_joints # Number of controls (F)
4547grf_as_state = True
48+ foot_slice = slice (13 + 2 * n_joints , 13 + 2 * n_joints + 3 * n_contact )
49+ leg_slice = foot_slice
4650# Reference torques and controls (using n_joints)
4751u_ref = jnp .zeros (m ) # Reference controls (concatenated torques)
4852
5963# For the leg contact cost, repeat the unit cost for each contact point.
6064Qleg = jnp .diag (jnp .tile (jnp .array ([1e4 ,1e4 ,1e5 ]),n_contact ))
6165
62- # Combine all cost matrices into a block diagonal matrix
63- W = jax .scipy .linalg .block_diag (Qp , Qrot , Qq , Qdp , Qomega , Qdq , Qleg , Qtau ,Q_grf )
66+ W = {"pos" : Qp , "rot" : Qrot , "q" : Qq , "vel" : Qdp , "omega" : Qomega , "dq" : Qdq , "contact" : Qleg , "tau" : Qtau , "grf" : Q_grf }
6467
6568use_terrain_estimation = True # Flag to use terrain estimation
6669
6972 [p0 , quat0 , q0 , jnp .zeros (6 + n_joints ), p_legs0 , jnp .zeros (_state_extra )]
7073)
7174
72- cost = partial (mpc_objectives .quadruped_wb_obj , True , n_joints , n_contact , N )
75+ cost = partial (mpc_objectives .quadruped_wb_obj , True , n_joints , n_contact , n_contact , N )
7376hessian_approx = None
77+ reference_generator = partial (
78+ mpc_utils .reference_generator ,
79+ use_terrain_estimation ,
80+ N ,
81+ dt ,
82+ n_joints ,
83+ n_contact ,
84+ foot0 = p_legs0 ,
85+ q0 = q0 ,
86+ clearence_speed = clearance_speed ,
87+ )
7488
7589def dynamics (model , mjx_model , contact_id , body_id ):
7690 return partial (
@@ -85,4 +99,4 @@ def dynamics(model, mjx_model, contact_id, body_id):
8599# dynamics = mpc_dyn_model.quadruped_wb_dynamics_learned_contact_model
86100# dynamics = mpc_dyn_model.quadruped_wb_dynamics_explicit_contact
87101max_torque = 25
88- min_torque = - 25
102+ min_torque = - 25
0 commit comments