-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
157 lines (131 loc) · 3.96 KB
/
controller.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from physics import foot_J_func
from einops import einsum, rearrange
import numpy as np
import casadi as ca
import sys
import os
def optimize_traj(init_q, init_qd, ref_traj, stance, params, dt=0.005):
# Unpack params
m_b, I, m_l, g = params
# Find the first stance phase
stance_start = np.min(np.argwhere(stance.squeeze()))
stance_end = (
np.min(np.argwhere(~stance[stance_start:].squeeze())) + stance_start + 1
)
# Grab just the first stance phases
stance = np.zeros_like(stance.squeeze())
stance[stance_start:stance_end] = True
n_stance_control_times = np.sum(stance)
# Extract x and ys from ref_traj
(
thetas,
xs,
ys,
theta_dotx,
x_dots,
y_dots,
) = ref_traj.T
x_ref = rearrange(ref_traj[stance], "t i -> (t i)")
x_0 = np.concatenate([init_q[0:3], init_qd[0:3]])
ls = np.zeros_like(xs)
qs = np.stack([thetas, xs, ys, ls], axis=-1)
# Construct A_qp and B_qp
eye = np.eye(3)
zero = np.zeros((3, 3))
A = rearrange(
np.stack(
[
[eye, eye * dt],
[zero, eye],
]
),
"br bc i j -> (br i) (bc j)",
**{"br": 2, "bc": 2},
)
m_inv = np.diag([1 / I, 1 / (m_b + m_l), 1 / (m_b + m_l)])
M_inv = rearrange(
np.stack(
[
[zero, zero],
[zero, m_inv],
]
),
"br bc i j -> (br i) (bc j)",
**{"br": 2, "bc": 2},
)
jacs = np.stack([foot_J_func(q).T[0:3] for q in qs[stance]])
J = np.concatenate(
[
np.broadcast_to(
np.zeros([3, 2]),
jacs.shape,
),
jacs,
],
axis=1,
)
bs = np.concatenate([einsum(M_inv, J, "i j, t j k -> t i k")]) * dt
A_qp_stack = np.stack(
[np.linalg.matrix_power(A, i) for i in range(n_stance_control_times)]
)
A_qp = rearrange(A_qp_stack, "p i j -> (p i) j")
A_qp_pad = np.concatenate([np.zeros([1, 6, 6]), A_qp_stack])
A_ind_mat = np.clip(
np.arange(n_stance_control_times, 0, -1)[None, :]
- np.arange(n_stance_control_times, 0, -1)[:, None],
0,
None,
)
A_qp_mat = A_qp_pad[A_ind_mat]
B_qp = einsum(A_qp_mat, bs, "bi bj i j, bj j k -> bi bj i k")
B_qp = rearrange(B_qp, "bi bj i j -> (bi i) (bj j)")
# Weight Matrix
# TODO: Tune this
L = np.kron(
np.diag(np.arange(n_stance_control_times)),
np.diag(np.array([0.0, 1.0, 1.0, 0.0, 250.0, 250.0])),
)
K = np.eye(n_stance_control_times * 2) * 1e-2
H = 2 * (B_qp.T @ L @ B_qp + K)
g = 2 * B_qp.T @ L @ (A_qp @ x_0 - x_ref)
# Constraints
c_single = np.array(
[
[0, 1], # No pulling constraint
[-1.0, 0.4], # Friction cone constraint
[1.0, 0.4], # Friction cone constraint
]
)
c_lower_single = np.array(
[
[0.05], # No pulling constraint
[0], # Friction cone constraint
[0], # Friction cone constraint
]
)
C = np.kron(np.eye(n_stance_control_times), c_single)
c_lower = np.tile(c_lower_single, (n_stance_control_times, 1))
# Silence casadi
stdout = sys.stdout
stderr = sys.stderr
sys.stdout = sys.stderr = open(os.devnull, "w")
# Define optimization variables
U = ca.MX.sym("U", n_stance_control_times * 2)
# Solve QP
H_ca = ca.DM(H)
g_ca = ca.DM(g)
C_ca = ca.DM(C)
c_lower_ca = ca.DM(c_lower)
# Cost
cost = 0.5 * ca.mtimes(U.T, ca.mtimes(H_ca, U)) + ca.mtimes(g_ca.T, U)
# Solver
qp = {"x": U, "f": cost, "g": ca.vertcat(C_ca @ U)}
opts = {"print_time": 0, "printLevel": "none", "verbose": False}
solver = ca.qpsol("solver", "qpoases", qp, opts)
# Solve the QP
res = solver(lbg=c_lower_ca, ubg=ca.inf)
U_opt = res["x"]
# Un-silence casadi
sys.stdout = stdout
sys.stderr = stderr
return U_opt