-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpc_biped_qp.cpp
257 lines (219 loc) · 8.19 KB
/
mpc_biped_qp.cpp
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <iostream>
#include <vector>
#include <eigen3/Eigen/Dense>
// Function to multiply matrix and vector
void matVecMult3(const double mat[3][3], const double vec[3], double result[3]) {
for (int i = 0; i < 3; ++i) {
result[i] = 0.0;
for (int j = 0; j < 3; ++j) {
result[i] += mat[i][j] * vec[j];
}
}
}
#include <qpOASES.hpp>
/** Example for qpOASES main function using the QProblem class. */
int main( )
{
USING_NAMESPACE_QPOASES
const double max_constraint = 0.17;
const double min_constraint = -max_constraint;
const double max_min_constraint = -0.03;
const double min_max_constraint = -max_min_constraint;
const double start_time = 2.5; // in s
const double end_time = 7.5; // in s
const double grav = 9.81; // gravity
const double h_com = 0.8; // height of CoM
const double dt = 0.005; // in s
const int N = 300; // lookahead
const double T = 9; // in s
const int period = 1 / dt; // in s
const int short_period = 0.8 / dt; // in s
const int diff_period = (period - short_period) / 2;
// Iteration dynamics
double dyn_mat[3][3] = {{1, dt, dt * dt / 2}, {0, 1, dt}, {0, 0, 1}};
double dyn_jerk[3] = {dt * dt * dt / 6, dt * dt / 2, dt};
double z_comp[3] = {1, 0, -h_com / grav};
// Function to compute next x hat given the new jerk
auto next_x = [&](const double x[3], const double x_jerk, double result[3]) {
double temp[3];
matVecMult3(dyn_mat, x, result);
temp[0] = x_jerk * dyn_jerk[0];
temp[1] = x_jerk * dyn_jerk[1];
temp[2] = x_jerk * dyn_jerk[2];
for (int i = 0; i < 3; ++i) {
result[i] += temp[i];
}
};
// Function to compute the new z dimension of the CoP given the current x hat
auto compute_z = [&](const double x[3]) -> double {
double sum = 0.0;
for (int i = 0; i < 3; ++i) {
sum += z_comp[i] * x[i];
}
return sum;
};
// Constraints/bounds
std::vector<double> z_max(static_cast<int>(T / dt) + N, max_constraint);
std::vector<double> z_min(static_cast<int>(T / dt) + N, min_constraint);
for (int i = static_cast<int>(start_time / dt); i < static_cast<int>(start_time / dt + short_period); ++i) {
z_max[i] = max_min_constraint;
}
for (int i = static_cast<int>(start_time / dt + short_period + period);
i < static_cast<int>(start_time / dt + 2 * short_period + period); ++i) {
z_max[i] = max_min_constraint;
}
for (int i = static_cast<int>(start_time / dt + 2 * short_period + 2 * period);
i < static_cast<int>(start_time / dt + 3 * short_period + 2 * period); ++i) {
z_max[i] = max_min_constraint;
}
for (int i = static_cast<int>(start_time / dt + short_period + diff_period);
i < static_cast<int>(start_time / dt + 2 * short_period + diff_period); ++i) {
z_min[i] = min_max_constraint;
}
for (int i = static_cast<int>(start_time / dt + 2 * short_period + period + diff_period);
i < static_cast<int>(start_time / dt + 3 * short_period + period + diff_period); ++i) {
z_min[i] = min_max_constraint;
}
for (int i = static_cast<int>(start_time / dt + 3 * short_period + 2 * period + diff_period);
i < static_cast<int>(start_time / dt + 4 * short_period + 2 * period + diff_period); ++i) {
z_min[i] = min_max_constraint;
}
real_t P_x[N * 3];
for (int i = 0; i < N; ++i) {
P_x[i * N + 0] = 1;
P_x[i * N + 1] = dt * (i + 1);
P_x[i * N + 2] = dt * dt * (i + 1) * (i + 1) / 2 - h_com / grav;
}
real_t P_u[N * N] = { 0.0 };
for (int i = 0; i < N; ++i) {
for (int j = 0; j <= i; ++j) {
double coef = i - j;
P_u[i * N + j] = (1 + 3 * coef + 3 * coef * coef) * dt * dt * dt / 6 - dt * h_com / grav;
}
}
real_t P_u_mat[N][N] = {{0.0}};
for (int i = 0; i < N; ++i) {
for (int j = 0; j <= i; ++j) {
double coef = i - j;
P_u_mat[i][j] = (1 + 3 * coef + 3 * coef * coef) * dt * dt * dt / 6 - dt * h_com / grav;
}
}
Eigen::Map<Eigen::Matrix<real_t, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> P_u_x(P_u_mat[0], N, N);
Eigen::MatrixXd P_u_x_inv = P_u_x.inverse();
real_t P_u_inv[N * N] = { 0.0 };
for (int i = 0; i < P_u_x_inv.rows(); ++i) {
for (int j = 0; j < P_u_x_inv.cols(); ++j) {
P_u_inv[i * N + j] = P_u_x_inv(i, j);
}
}
// Function to create eye matrix
auto eyeMat = [&](double result[N*N]) {
for (int i = 0; i < N; ++i) {
result[i + i * N] = 1.0;
}
};
// Function to matrix vector multiplication
auto mat_vec_mul = [&](const double mat[N*3], const double vec[3], double result[N]) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < 3; ++j) {
result[i] += mat[i * N + j] * vec[j];
}
}
};
// Function to matrix vector multiplication
auto mat_vec_mul_n = [&](const double mat[N*N], const double vec[N], double result[N]) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
result[i] += mat[i * N + j] * vec[j];
}
}
};
// Function to create lower and upper constraint
auto creat_constraint = [&](const int index, const double x_k[3], double lower[N], double upper[N]) {
double temp[N] = { 0.0 };
mat_vec_mul(P_x, x_k, temp);
for (int i = 0; i < N; ++i) {
upper[i] = z_max[index + i] - temp[i];
lower[i] = z_min[index + i] - temp[i];
}
};
// Function to create lower and upper constraint
auto creat_constraint_ = [&](const int index, const double x_k[3], double lower[N], double upper[N]) {
double temp[N] = { 0.0 };
double upper_[N] = { 0.0 };
double lower_[N] = { 0.0 };
mat_vec_mul(P_x, x_k, temp);
for (int i = 0; i < N; ++i) {
upper_[i] = z_max[index + i] - temp[i];
lower_[i] = z_min[index + i] - temp[i];
}
mat_vec_mul_n(P_u_inv, upper_, upper);
mat_vec_mul_n(P_u_inv, lower_, lower);
for (int i = 0; i < N; ++i){
if (lower[i] > upper[i]) {
double temp = lower[i];
lower[i] = upper[i];
upper[i] = temp;
}
}
std::cout << std::endl;
};
/* Setup data of first QP. */
real_t H[N*N] = { 0.0 };
eyeMat(H);
real_t A[N*N];
std::copy(std::begin(P_u), std::end(P_u), std::begin(A));
real_t x_k[3] = { 0.0 };
real_t g[N] = { 0.0 };
real_t lb[N] = { 0.0 };
real_t ub[N] = { 0.0 };
real_t lbA[N] = { 0.0 };
real_t ubA[N] = { 0.0 };
/* Setting up QProblem object. */
QProblem example( N,2 );
Options options;
options.printLevel = PL_NONE;
example.setOptions( options );
int_t nWSR = 10;
double cops[int(T / dt)] = { 0.0 };
double jerks[int(T / dt)] = { 0.0 };
for (int i = 0; i < int(T / dt); ++i) {
std::cout << "Current step: " << i << std::endl;
creat_constraint(i, x_k, lbA, ubA);
creat_constraint_(i, x_k, lb, ub);
if (i == 0) {
example.init( H,g,A,lb,ub,lbA,ubA, nWSR );
}
else {
example.hotstart( g,lb,ub,lbA,ubA, nWSR );
}
/* Get and print solution of first QP. */
real_t xOpt[N];
real_t yOpt[N + 2];
example.getPrimalSolution( xOpt );
example.getDualSolution( yOpt );
real_t jerk = yOpt[0];
if (i > 250) {
for (int i = 0; i < N + 2; ++i) {
std::cout << yOpt[0] << " ";
}
std::cout << std::endl;
std::string temp;
std::getline(std::cin, temp);
}
real_t new_x[3];
next_x(x_k, jerk, new_x);
std::copy(std::begin(new_x), std::end(new_x), std::begin(x_k));
cops[i] = compute_z(x_k);
jerks[i] = jerk;
}
for (int i = 0; i < int(T / dt); ++i) {
std::cout << cops[i] << " ";
}
std::cout << std::endl;
std::cout << std::endl;
for (int i = 0; i < int(T / dt); ++i) {
std::cout << jerks[i] << " ";
}
return 0;
}