-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathMPCTest.cpp
333 lines (269 loc) · 11.4 KB
/
MPCTest.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* @file MPCTest.cpp
* @author Giulio Romualdi
* @copyright Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
* @date 2018
*/
// Catch2
#include <catch2/catch.hpp>
// OsqpEigen
#include <OsqpEigen/OsqpEigen.h>
// eigen
#include <Eigen/Dense>
#include <iostream>
#include <fstream>
#define _USE_MATH_DEFINES
#include <cmath>
// colors
#define ANSI_TXT_GRN "\033[0;32m"
#define ANSI_TXT_MGT "\033[0;35m" //Magenta
#define ANSI_TXT_DFT "\033[0;0m" //Console default
#define GTEST_BOX "[ cout ] "
#define COUT_GTEST ANSI_TXT_GRN << GTEST_BOX //You could add the Default
#define COUT_GTEST_MGT COUT_GTEST << ANSI_TXT_MGT
void setDynamicsMatrices(Eigen::Matrix<double, 12, 12> &a, Eigen::Matrix<double, 12, 4> &b)
{
a << 1., 0., 0., 0., 0., 0., 0.1, 0., 0., 0., 0., 0. ,
0., 1., 0., 0., 0., 0., 0., 0.1, 0., 0., 0., 0. ,
0., 0., 1., 0., 0., 0., 0., 0., 0.1, 0., 0., 0. ,
0.0488, 0., 0., 1., 0., 0., 0.0016, 0., 0., 0.0992, 0., 0. ,
0., -0.0488, 0., 0., 1., 0., 0., -0.0016, 0., 0., 0.0992, 0. ,
0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.0992,
0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0. ,
0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0. ,
0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0. ,
0.9734, 0., 0., 0., 0., 0., 0.0488, 0., 0., 0.9846, 0., 0. ,
0., -0.9734, 0., 0., 0., 0., 0., -0.0488, 0., 0., 0.9846, 0. ,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.9846;
b << 0., -0.0726, 0., 0.0726,
-0.0726, 0., 0.0726, 0. ,
-0.0152, 0.0152, -0.0152, 0.0152,
-0., -0.0006, -0., 0.0006,
0.0006, 0., -0.0006, 0.0000,
0.0106, 0.0106, 0.0106, 0.0106,
0, -1.4512, 0., 1.4512,
-1.4512, 0., 1.4512, 0. ,
-0.3049, 0.3049, -0.3049, 0.3049,
-0., -0.0236, 0., 0.0236,
0.0236, 0., -0.0236, 0. ,
0.2107, 0.2107, 0.2107, 0.2107;
}
void setInequalityConstraints(Eigen::Matrix<double, 12, 1> &xMax, Eigen::Matrix<double, 12, 1> &xMin,
Eigen::Matrix<double, 4, 1> &uMax, Eigen::Matrix<double, 4, 1> &uMin)
{
double u0 = 10.5916;
// input inequality constraints
uMin << 9.6 - u0,
9.6 - u0,
9.6 - u0,
9.6 - u0;
uMax << 13 - u0,
13 - u0,
13 - u0,
13 - u0;
// state inequality constraints
xMin << -M_PI/6,-M_PI/6,-OsqpEigen::INFTY,-OsqpEigen::INFTY,-OsqpEigen::INFTY,-1.,
-OsqpEigen::INFTY, -OsqpEigen::INFTY,-OsqpEigen::INFTY,-OsqpEigen::INFTY,
-OsqpEigen::INFTY,-OsqpEigen::INFTY;
xMax << M_PI/6,M_PI/6, OsqpEigen::INFTY,OsqpEigen::INFTY,OsqpEigen::INFTY,
OsqpEigen::INFTY, OsqpEigen::INFTY,OsqpEigen::INFTY,OsqpEigen::INFTY,
OsqpEigen::INFTY,OsqpEigen::INFTY,OsqpEigen::INFTY;
}
void setWeightMatrices(Eigen::DiagonalMatrix<double, 12> &Q, Eigen::DiagonalMatrix<double, 4> &R)
{
Q.diagonal() << 0, 0, 10., 10., 10., 10., 0, 0, 0, 5., 5., 5.;
R.diagonal() << 0.1, 0.1, 0.1, 0.1;
}
void castMPCToQPHessian(const Eigen::DiagonalMatrix<double, 12> &Q, const Eigen::DiagonalMatrix<double, 4> &R, int mpcWindow,
Eigen::SparseMatrix<double> &hessianMatrix)
{
hessianMatrix.resize(12*(mpcWindow+1) + 4 * mpcWindow, 12*(mpcWindow+1) + 4 * mpcWindow);
//populate hessian matrix
for(int i = 0; i<12*(mpcWindow+1) + 4 * mpcWindow; i++){
if(i < 12*(mpcWindow+1)){
int posQ=i%12;
float value = Q.diagonal()[posQ];
if(value != 0)
hessianMatrix.insert(i,i) = value;
}
else{
int posR=i%4;
float value = R.diagonal()[posR];
if(value != 0)
hessianMatrix.insert(i,i) = value;
}
}
}
void castMPCToQPGradient(const Eigen::DiagonalMatrix<double, 12> &Q, const Eigen::Matrix<double, 12, 1> &xRef, int mpcWindow,
Eigen::VectorXd &gradient)
{
Eigen::Matrix<double,12,1> Qx_ref;
Qx_ref = Q * (-xRef);
// populate the gradient vector
gradient = Eigen::VectorXd::Zero(12*(mpcWindow+1) + 4*mpcWindow, 1);
for(int i = 0; i<12*(mpcWindow+1); i++){
int posQ=i%12;
float value = Qx_ref(posQ,0);
gradient(i,0) = value;
}
}
void castMPCToQPConstraintMatrix(const Eigen::Matrix<double, 12, 12> &dynamicMatrix, const Eigen::Matrix<double, 12, 4> &controlMatrix,
int mpcWindow, Eigen::SparseMatrix<double> &constraintMatrix)
{
constraintMatrix.resize(12*(mpcWindow+1) + 12*(mpcWindow+1) + 4 * mpcWindow, 12*(mpcWindow+1) + 4 * mpcWindow);
// populate linear constraint matrix
for(int i = 0; i<12*(mpcWindow+1); i++){
constraintMatrix.insert(i,i) = -1;
}
for(int i = 0; i < mpcWindow; i++)
for(int j = 0; j<12; j++)
for(int k = 0; k<12; k++){
float value = dynamicMatrix(j,k);
if(value != 0){
constraintMatrix.insert(12 * (i+1) + j, 12 * i + k) = value;
}
}
for(int i = 0; i < mpcWindow; i++)
for(int j = 0; j < 12; j++)
for(int k = 0; k < 4; k++){
float value = controlMatrix(j,k);
if(value != 0){
constraintMatrix.insert(12*(i+1)+j, 4*i+k+12*(mpcWindow + 1)) = value;
}
}
for(int i = 0; i<12*(mpcWindow+1) + 4*mpcWindow; i++){
constraintMatrix.insert(i+(mpcWindow+1)*12,i) = 1;
}
}
void castMPCToQPConstraintVectors(const Eigen::Matrix<double, 12, 1> &xMax, const Eigen::Matrix<double, 12, 1> &xMin,
const Eigen::Matrix<double, 4, 1> &uMax, const Eigen::Matrix<double, 4, 1> &uMin,
const Eigen::Matrix<double, 12, 1> &x0,
int mpcWindow, Eigen::VectorXd &lowerBound, Eigen::VectorXd &upperBound)
{
// evaluate the lower and the upper inequality vectors
Eigen::VectorXd lowerInequality = Eigen::MatrixXd::Zero(12*(mpcWindow+1) + 4 * mpcWindow, 1);
Eigen::VectorXd upperInequality = Eigen::MatrixXd::Zero(12*(mpcWindow+1) + 4 * mpcWindow, 1);
for(int i=0; i<mpcWindow+1; i++){
lowerInequality.block(12*i,0,12,1) = xMin;
upperInequality.block(12*i,0,12,1) = xMax;
}
for(int i=0; i<mpcWindow; i++){
lowerInequality.block(4 * i + 12 * (mpcWindow + 1), 0, 4, 1) = uMin;
upperInequality.block(4 * i + 12 * (mpcWindow + 1), 0, 4, 1) = uMax;
}
// evaluate the lower and the upper equality vectors
Eigen::VectorXd lowerEquality = Eigen::MatrixXd::Zero(12*(mpcWindow+1),1 );
Eigen::VectorXd upperEquality;
lowerEquality.block(0,0,12,1) = -x0;
upperEquality = lowerEquality;
lowerEquality = lowerEquality;
// merge inequality and equality vectors
lowerBound = Eigen::MatrixXd::Zero(2*12*(mpcWindow+1) + 4*mpcWindow,1 );
lowerBound << lowerEquality,
lowerInequality;
upperBound = Eigen::MatrixXd::Zero(2*12*(mpcWindow+1) + 4*mpcWindow,1 );
upperBound << upperEquality,
upperInequality;
}
void updateConstraintVectors(const Eigen::Matrix<double, 12, 1> &x0,
Eigen::VectorXd &lowerBound, Eigen::VectorXd &upperBound)
{
lowerBound.block(0,0,12,1) = -x0;
upperBound.block(0,0,12,1) = -x0;
}
double getErrorNorm(const Eigen::Matrix<double, 12, 1> &x,
const Eigen::Matrix<double, 12, 1> &xRef)
{
// evaluate the error
Eigen::Matrix<double, 12, 1> error = x - xRef;
// return the norm
return error.norm();
}
TEST_CASE("MPCTest")
{
// open the ofstream
std::ofstream dataStream;
dataStream.open ("output.txt");
// set the preview window
int mpcWindow = 20;
// allocate the dynamics matrices
Eigen::Matrix<double, 12, 12> a;
Eigen::Matrix<double, 12, 4> b;
// allocate the constraints vector
Eigen::Matrix<double, 12, 1> xMax;
Eigen::Matrix<double, 12, 1> xMin;
Eigen::Matrix<double, 4, 1> uMax;
Eigen::Matrix<double, 4, 1> uMin;
// allocate the weight matrices
Eigen::DiagonalMatrix<double, 12> Q;
Eigen::DiagonalMatrix<double, 4> R;
// allocate the initial and the reference state space
Eigen::Matrix<double, 12, 1> x0;
Eigen::Matrix<double, 12, 1> xRef;
// allocate QP problem matrices and vectores
Eigen::SparseMatrix<double> hessian;
Eigen::VectorXd gradient;
Eigen::SparseMatrix<double> linearMatrix;
Eigen::VectorXd lowerBound;
Eigen::VectorXd upperBound;
// set the initial and the desired states
x0 << 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ;
xRef << 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0;
// set MPC problem quantities
setDynamicsMatrices(a, b);
setInequalityConstraints(xMax, xMin, uMax, uMin);
setWeightMatrices(Q, R);
// cast the MPC problem as QP problem
castMPCToQPHessian(Q, R, mpcWindow, hessian);
castMPCToQPGradient(Q, xRef, mpcWindow, gradient);
castMPCToQPConstraintMatrix(a, b, mpcWindow, linearMatrix);
castMPCToQPConstraintVectors(xMax, xMin, uMax, uMin, x0, mpcWindow, lowerBound, upperBound);
// instantiate the solver
OsqpEigen::Solver solver;
// settings
solver.settings()->setVerbosity(false);
solver.settings()->setWarmStart(true);
// set the initial data of the QP solver
solver.data()->setNumberOfVariables(12 * (mpcWindow + 1) + 4 * mpcWindow);
solver.data()->setNumberOfConstraints(2 * 12 * (mpcWindow + 1) + 4 * mpcWindow);
REQUIRE(solver.data()->setHessianMatrix(hessian));
REQUIRE(solver.data()->setGradient(gradient));
REQUIRE(solver.data()->setLinearConstraintsMatrix(linearMatrix));
REQUIRE(solver.data()->setLowerBound(lowerBound));
REQUIRE(solver.data()->setUpperBound(upperBound));
// instantiate the solver
REQUIRE(solver.initSolver());
// controller input and QPSolution vector
Eigen::Vector4d ctr;
Eigen::VectorXd QPSolution;
// number of iteration steps
int numberOfSteps = 50;
// profiling quantities
clock_t startTime, endTime;
double avarageTime = 0;
for (int i = 0; i < numberOfSteps; i++){
startTime = clock();
// solve the QP problem
REQUIRE(solver.solve());
// get the controller input
QPSolution = solver.getSolution();
ctr = QPSolution.block(12 * (mpcWindow + 1), 0, 4, 1);
// save data into file
auto x0Data = x0.data();
for(int j = 0; j < 12; j++)
dataStream << x0Data[j] << " ";
dataStream << std::endl;
// propagate the model
x0 = a * x0 + b * ctr;
// update the constraint bound
updateConstraintVectors(x0, lowerBound, upperBound);
REQUIRE(solver.updateBounds(lowerBound, upperBound));
endTime = clock();
avarageTime += static_cast<double>(endTime - startTime) / CLOCKS_PER_SEC;
}
// close the stream
dataStream.close();
std::cout << COUT_GTEST_MGT << "Avarage time = " << avarageTime / numberOfSteps
<< " seconds." << ANSI_TXT_DFT << std::endl;
REQUIRE(getErrorNorm(x0, xRef) <= 0.001);
}