Skip to content

Commit 0bddce8

Browse files
Merge pull request #19 from Schulze18/spot
Refactor cost function definition + add Spot and Spot with Arm
2 parents a6bdf18 + b6d0545 commit 0bddce8

144 files changed

Lines changed: 3056428 additions & 375 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

mpx/config/config_acrobot_swingup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
I2 = m2 * l2 * l2 / 12.0
2626

2727
parameter = jnp.zeros(N + 1)
28-
reference = jnp.zeros(N + 1)
28+
reference = {"dummy": jnp.zeros(N + 1)}
2929

3030

3131
def dynamics(x, u, t, parameter):
@@ -70,7 +70,7 @@ def dynamics(x, u, t, parameter):
7070
Q = jnp.diag(jnp.array([1e-5 / dt, 1e-5 / dt, 1e-5 / dt, 1e-5 / dt]))
7171
R = jnp.diag(jnp.array([1e-4 / dt]))
7272
Q_f = jnp.diag(jnp.array([10.0, 10.0, 100.0, 100.0]))
73-
W = jnp.zeros((N, 1))
73+
W = {"dummy": jnp.zeros((N, 1))}
7474

7575

7676
def cost(W, reference, x, u, t):

mpx/config/config_aliengo.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import jax.numpy as jnp
22
import jax
33
import mpx.utils.models as mpc_dyn_model
4+
import mpx.utils.mpc_utils as mpc_utils
45
import mpx.utils.objectives as mpc_objectives
56
import os
67
from functools import partial
@@ -23,6 +24,7 @@
2324
step_height = 0.2 # Step height in meters
2425
initial_height = 0.1 # Initial height of the robot's base in meters
2526
robot_height = 0.36 # Height of the robot's base in meters
27+
clearance_speed = 0.2
2628

2729
# Initial positions, orientations, and joint angles
2830
p0 = jnp.array([0, 0, robot_height]) # Initial position of the robot's base
@@ -42,6 +44,8 @@
4244
n = 13 + 2*n_joints + 6*n_contact # Number of states (theta1, theta1_dot, theta2, theta2_dot)
4345
m = n_joints # Number of controls (F)
4446
grf_as_state = True
47+
foot_slice = slice(13 + 2 * n_joints, 13 + 2 * n_joints + 3 * n_contact)
48+
leg_slice = foot_slice
4549
# Reference torques and controls (using n_joints)
4650
u_ref = jnp.zeros(m) # Reference controls (concatenated torques)
4751

@@ -58,8 +62,7 @@
5862
# For the leg contact cost, repeat the unit cost for each contact point.
5963
Qleg = jnp.diag(jnp.tile(jnp.array([1e4,1e4,1e5]),n_contact))
6064

61-
# Combine all cost matrices into a block diagonal matrix
62-
W = jax.scipy.linalg.block_diag(Qp, Qrot, Qq, Qdp, Qomega, Qdq, Qleg, Qtau,Q_grf)
65+
W = {"pos": Qp, "rot": Qrot, "q": Qq, "vel": Qdp, "omega": Qomega, "dq": Qdq, "contact": Qleg, "tau": Qtau, "grf": Q_grf}
6366

6467
use_terrain_estimation = True # Flag to use terrain estimation
6568

@@ -68,8 +71,19 @@
6871
[p0, quat0, q0, jnp.zeros(6 + n_joints), p_legs0, jnp.zeros(_state_extra)]
6972
)
7073

71-
cost = partial(mpc_objectives.quadruped_wb_obj, True, n_joints, n_contact, N)
74+
cost = partial(mpc_objectives.quadruped_wb_obj, True, n_joints, n_contact, n_contact, N)
7275
hessian_approx = None
76+
reference_generator = partial(
77+
mpc_utils.reference_generator,
78+
use_terrain_estimation,
79+
N,
80+
dt,
81+
n_joints,
82+
n_contact,
83+
foot0=p_legs0,
84+
q0=q0,
85+
clearence_speed=clearance_speed,
86+
)
7387

7488
def dynamics(model, mjx_model, contact_id, body_id):
7589
return partial(

mpx/config/config_aliengo_trot_two_step.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
step_height = 0.08
2121
initial_height = base.initial_height
2222
robot_height = base.robot_height
23+
clearance_speed = base.clearance_speed
2324

2425
p0 = base.p0
2526
quat0 = base.quat0
@@ -31,6 +32,8 @@
3132
n = base.n
3233
m = base.m
3334
grf_as_state = base.grf_as_state
35+
foot_slice = base.foot_slice
36+
leg_slice = base.leg_slice
3437
u_ref = base.u_ref
3538

3639
Qp = base.Qp
@@ -47,9 +50,20 @@
4750
use_terrain_estimation = False
4851
initial_state = base.initial_state
4952

50-
cost = partial(mpc_objectives.quadruped_wb_obj, True, n_joints, n_contact, N)
53+
cost = partial(mpc_objectives.quadruped_wb_obj, True, n_joints, n_contact, n_contact, N)
5154
hessian_approx = base.hessian_approx
5255
dynamics = base.dynamics
56+
reference_generator = partial(
57+
mpc_utils.reference_generator,
58+
use_terrain_estimation,
59+
N,
60+
dt,
61+
n_joints,
62+
n_contact,
63+
foot0=p_legs0,
64+
q0=q0,
65+
clearence_speed=clearance_speed,
66+
)
5367

5468
reference = partial(
5569
mpc_utils.reference_quadruped_trot_two_step,

mpx/config/config_barrel_roll.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
step_height = 0.12 # Step height in meters
2626
initial_height = 0.1 # Initial height of the robot's base in meters
2727
robot_height = 0.33 # Height of the robot's base in meters
28+
clearance_speed = 0.2
2829
grf_as_state = True
2930

3031
# Initial positions, orientations, and joint angles
@@ -57,6 +58,8 @@
5758
n = 13 + 2*n_joints + 6*n_contact # Number of states (theta1, theta1_dot, theta2, theta2_dot)
5859
m = n_joints # Number of controls (F)
5960
grf_as_state = True
61+
foot_slice = slice(13 + 2 * n_joints, 13 + 2 * n_joints + 3 * n_contact)
62+
leg_slice = foot_slice
6063
# Reference torques and controls (using n_joints)
6164
tau_ref = jnp.zeros(n_joints) # Reference torques (all zeros)
6265
# tau_ref = jnp.array([7.2171830e-02, -2.1473727e+00, 5.8485503e+00, 2.6923120e-03,
@@ -77,8 +80,7 @@
7780
# For the leg contact cost, repeat the unit cost for each contact point.
7881
Qleg = jnp.diag(jnp.tile(jnp.array([1e3,1e3,5e4]),n_contact))
7982

80-
# Combine all cost matrices into a block diagonal matrix
81-
W = jax.scipy.linalg.block_diag(Qp, Qrot, Qq, Qdp, Qomega, Qdq, Qleg, Qtau,Q_grf)
83+
W = {"pos": Qp, "rot": Qrot, "q": Qq, "vel": Qdp, "omega": Qomega, "dq": Qdq, "contact": Qleg, "tau": Qtau, "grf": Q_grf}
8284

8385
use_terrain_estimation = True # Flag to use terrain estimation
8486

@@ -87,8 +89,19 @@
8789
[p0, quat0, q0, jnp.zeros(6 + n_joints), p_legs0, jnp.zeros(_state_extra)]
8890
)
8991

90-
cost = partial(mpc_objectives.quadruped_wb_obj, False, n_joints, n_contact, N)
92+
cost = partial(mpc_objectives.quadruped_wb_obj, False, n_joints, n_contact, n_contact, N)
9193
hessian_approx = None
94+
reference_generator = partial(
95+
mpc_utils.reference_generator,
96+
use_terrain_estimation,
97+
N,
98+
dt,
99+
n_joints,
100+
n_contact,
101+
foot0=p_legs0,
102+
q0=q0,
103+
clearence_speed=clearance_speed,
104+
)
92105

93106
def dynamics(model, mjx_model, contact_id, body_id):
94107
return partial(
@@ -105,4 +118,4 @@ def dynamics(model, mjx_model, contact_id, body_id):
105118
# dynamics = mpc_dyn_model.quadruped_wb_dynamics_learned_contact_model
106119
# dynamics = mpc_dyn_model.quadruped_wb_dynamics_explicit_contact
107120
max_torque = 40
108-
min_torque = -40
121+
min_torque = -40

mpx/config/config_go2.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import jax.numpy as jnp
22
import jax
33
import mpx.utils.models as mpc_dyn_model
4+
import mpx.utils.mpc_utils as mpc_utils
45
import mpx.utils.objectives as mpc_objectives
56
import os
67
from functools import partial
@@ -23,6 +24,7 @@
2324
step_height = 0.065 # Step height in meters
2425
initial_height = 0.1 # Initial height of the robot's base in meters
2526
robot_height = 0.27 # Height of the robot's base in meters
27+
clearance_speed = 0.2
2628

2729
# Initial positions, orientations, and joint angles
2830
p0 = jnp.array([0, 0, robot_height]) # Initial position of the robot's base
@@ -43,6 +45,8 @@
4345
n = 13 + 2*n_joints + 6*n_contact # Number of states (theta1, theta1_dot, theta2, theta2_dot)
4446
m = n_joints # Number of controls (F)
4547
grf_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)
4751
u_ref = jnp.zeros(m) # Reference controls (concatenated torques)
4852

@@ -59,8 +63,7 @@
5963
# For the leg contact cost, repeat the unit cost for each contact point.
6064
Qleg = 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

6568
use_terrain_estimation = True # Flag to use terrain estimation
6669

@@ -69,8 +72,19 @@
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)
7376
hessian_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

7589
def 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
87101
max_torque = 25
88-
min_torque = -25
102+
min_torque = -25

mpx/config/config_h1.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import jax.numpy as jnp
22
import jax
33
import mpx.utils.models as mpc_dyn_model
4+
import mpx.utils.mpc_utils as mpc_utils
45
import mpx.utils.objectives as mpc_objectives
56
import os
67
import sys
@@ -25,6 +26,7 @@
2526
step_height = 0.08 # Step height in meters
2627
initial_height = 0.9 # Initial height of the robot's base in meters
2728
robot_height = 0.9 # Height of the robot's base in meters
29+
clearance_speed = 0.2
2830

2931
# Initial positions, orientations, and joint angles
3032
p0 = jnp.array([0, 0, 0.9]) # Initial position of the robot's base
@@ -48,6 +50,8 @@
4850
n = 13 + 2*n_joints + 3*n_contact + 3*n_contact # Number of states
4951
m = n_joints # Number of controls (F)
5052
grf_as_state = True
53+
foot_slice = slice(13 + 2 * n_joints, 13 + 2 * n_joints + 3 * n_contact)
54+
leg_slice = foot_slice
5155
# Reference torques and controls (using n_joints)
5256
u_ref = jnp.zeros(m) # Reference controls (concatenated torques)
5357

@@ -71,8 +75,7 @@
7175
Qleg = jnp.diag(jnp.tile(jnp.array([1e3,1e3,1e5]),n_contact))
7276
Qgrf = jnp.diag(jnp.ones(3*n_contact))*1e-3
7377

74-
# Combine all cost matrices into a block diagonal matrix
75-
W = jax.scipy.linalg.block_diag(Qp, Qrot, Qq, Qdp, Qomega, Qdq, Qleg, Qtau, Qgrf)
78+
W = {"pos": Qp, "rot": Qrot, "q": Qq, "vel": Qdp, "omega": Qomega, "dq": Qdq, "contact": Qleg, "tau": Qtau, "grf": Qgrf}
7679

7780
use_terrain_estimation = False # Flag to use terrain estimation
7881

@@ -83,6 +86,17 @@
8386

8487
cost = partial(mpc_objectives.h1_wb_obj, n_joints, n_contact, N)
8588
hessian_approx = None
89+
reference_generator = partial(
90+
mpc_utils.reference_generator,
91+
use_terrain_estimation,
92+
N,
93+
dt,
94+
n_joints,
95+
n_contact,
96+
foot0=p_legs0,
97+
q0=q0,
98+
clearence_speed=clearance_speed,
99+
)
86100

87101
def dynamics(model, mjx_model, contact_id, body_id):
88102
return partial(

mpx/config/config_h1_jump_forward.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
step_height = base.step_height
2121
initial_height = base.initial_height
2222
robot_height = base.robot_height
23+
clearance_speed = base.clearance_speed
2324

2425
p0 = base.p0
2526
quat0 = base.quat0
@@ -31,6 +32,8 @@
3132
n = base.n
3233
m = base.m
3334
grf_as_state = base.grf_as_state
35+
foot_slice = base.foot_slice
36+
leg_slice = base.leg_slice
3437
u_ref = base.u_ref
3538
W = base.W
3639
use_terrain_estimation = False
@@ -44,6 +47,17 @@
4447
hessian_approx = base.hessian_approx
4548
dynamics = base.dynamics
4649
MPCWrapper = base.MPCWrapper
50+
reference_generator = partial(
51+
mpc_utils.reference_generator,
52+
use_terrain_estimation,
53+
N,
54+
dt,
55+
n_joints,
56+
n_contact,
57+
foot0=p_legs0,
58+
q0=q0,
59+
clearence_speed=clearance_speed,
60+
)
4761

4862
reference = partial(
4963
mpc_utils.reference_humanoid_jump_forward,

mpx/config/config_h1_kinodynamic.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import jax.numpy as jnp
66

77
import mpx.utils.models as mpc_dyn_model
8+
import mpx.utils.mpc_utils as mpc_utils
89
import mpx.utils.mpc_wrapper as base_mpc_wrapper
910
import mpx.utils.objectives as mpc_objectives
1011

@@ -24,6 +25,7 @@
2425
step_height = 0.08
2526
initial_height = 0.9
2627
robot_height = 0.9
28+
clearance_speed = 0.2
2729

2830
p0 = jnp.array([0.0, 0.0, 0.9])
2931
quat0 = jnp.array([1.0, 0.0, 0.0, 0.0])
@@ -47,6 +49,8 @@
4749
n = 13 + 2 * n_joints + 3 * n_contact
4850
m = n_joints + 3 * n_contact
4951
grf_as_state = False
52+
foot_slice = slice(13 + 2 * n_joints, 13 + 2 * n_joints + 3 * n_contact)
53+
leg_slice = foot_slice
5054
u_ref = jnp.zeros(m)
5155

5256
Qp = jnp.diag(jnp.array([0.0, 0.0, 1e4]))
@@ -64,7 +68,7 @@
6468
Qleg = jnp.diag(jnp.tile(jnp.array([1e5, 1e5, 1e5]), n_contact))
6569
Qdq_cmd = jnp.diag(jnp.ones(n_joints)) * 1e-2
6670
Qgrf = jnp.diag(jnp.ones(3 * n_contact)) * 1e-3
67-
W = jax.scipy.linalg.block_diag(Qp, Qrot, Qq, Qdp, Qomega, Qdq, Qleg, Qdq_cmd, Qgrf)
71+
W = {"pos": Qp, "rot": Qrot, "q": Qq, "vel": Qdp, "omega": Qomega, "dq": Qdq, "contact": Qleg, "dq_cmd": Qdq_cmd, "grf": Qgrf}
6872

6973
use_terrain_estimation = False
7074
initial_state = jnp.concatenate([p0, quat0, q0, jnp.zeros(6 + n_joints), p_legs0])
@@ -94,6 +98,17 @@
9498

9599
cost = partial(mpc_objectives.h1_kinodynamic_obj, n_joints, n_contact, N)
96100
hessian_approx = None
101+
reference_generator = partial(
102+
mpc_utils.reference_generator,
103+
use_terrain_estimation,
104+
N,
105+
dt,
106+
n_joints,
107+
n_contact,
108+
foot0=p_legs0,
109+
q0=q0,
110+
clearence_speed=clearance_speed,
111+
)
97112

98113

99114
def dynamics(model, mjx_model, contact_id, body_id):

0 commit comments

Comments
 (0)