-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpc_core.cpp
More file actions
453 lines (368 loc) · 13.8 KB
/
mpc_core.cpp
File metadata and controls
453 lines (368 loc) · 13.8 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include <chrono>
#include <iostream>
#include "mpc/mpc_core.h"
#include "faster/termcolor.hpp"
JackalMPCCore::JackalMPCCore()
{
// _mpc_type = MPC_TYPE_CTE;
_mpc_type = MPC_TYPE_ACC;
// _mpc_type = MPC_TYPE_NLOPT;
// _mpc_type = MPC_TYPE_NLOPT_POS;
_mpc = std::make_unique<MPC_ACC>();
_curr_vel = 0;
_curr_ang_vel = 0;
_obstacle = Eigen::Vector3d(0, 0, 0);
}
JackalMPCCore::JackalMPCCore(const mpc_type &type)
{
_mpc_type = type;
if (_mpc_type == MPC_TYPE_CTE)
{
std::cout << termcolor::green << "[MPC Core] setting mpc type to track_vel_ipopt" << termcolor::reset << std::endl;
_mpc = std::make_unique<MPC>();
}
else if (_mpc_type == MPC_TYPE_ACC)
{
std::cout << termcolor::green << "[MPC Core] setting mpc type to track_acc_ipopt" << termcolor::reset << std::endl;
_mpc = std::make_unique<MPC_ACC>();
}
else if (_mpc_type == MPC_TYPE_NLOPT)
{
std::cout << termcolor::green << "[MPC Core] setting mpc type to track_acc_nlopt" << termcolor::reset << std::endl;
_mpc = std::make_unique<CBFHorizon>();
}
else if (_mpc_type == MPC_TYPE_NLOPT_POS)
{
std::cout << termcolor::green << "[MPC Core] setting mpc type to goto_pos_nlopt" << termcolor::reset << std::endl;
_mpc = std::make_unique<MPCNLOPT>();
}
_curr_vel = 0;
_curr_ang_vel = 0;
_obstacle = Eigen::Vector3d(0, 0, 0);
}
JackalMPCCore::~JackalMPCCore()
{
}
void JackalMPCCore::load_params(const std::map<std::string, double> ¶ms)
{
_dt = params.at("DT");
_max_anga = params.at("MAX_ANGA");
_max_linacc = params.at("MAX_LINACC");
_max_vel = params.at("LINVEL");
_max_ang_vel = params.at("ANGVEL");
_use_cbf = params.at("USE_CBF");
_mpc->LoadParams(params);
_filter.load_params(params);
}
void JackalMPCCore::set_obstacle(const Eigen::Vector3d &obstacle)
{
_obstacle = obstacle;
if (_mpc_type == MPC_TYPE_ACC)
{
MPC_ACC *mpc = dynamic_cast<MPC_ACC *>(_mpc.get());
mpc->updateObstacle(obstacle);
}
}
void JackalMPCCore::set_odom(const Eigen::Vector3d &odom)
{
_odom = odom;
}
void JackalMPCCore::set_goal(const Eigen::Vector2d &goal)
{
_goal = goal;
}
#ifdef FOUND_PYBIND11
void JackalMPCCore::set_odom(const std::vector<double> &odom)
{
_odom = Eigen::Vector3d(odom[0], odom[1], odom[2]);
}
void JackalMPCCore::set_goal(const std::vector<double> &goal)
{
_goal = Eigen::Vector2d(goal[0], goal[1]);
}
void JackalMPCCore::set_obstacle(const std::vector<double> &obstacle)
{
_obstacle = Eigen::Vector3d(obstacle[0], obstacle[1], obstacle[2]);
}
// reference comes in flattened, so we need to reshape it to (6, mpc_steps)
void JackalMPCCore::set_reference(const std::vector<double> &reference)
{
// divide size by 6 to get number of steps
int steps = reference.size() / 6;
Eigen::MatrixXd ref(6, steps);
for (int i = 0; i < steps; i++)
{
ref(0, i) = reference[0 * steps + i];
ref(1, i) = reference[1 * steps + i];
ref(2, i) = reference[2 * steps + i];
ref(3, i) = reference[3 * steps + i];
ref(4, i) = reference[4 * steps + i];
ref(5, i) = reference[5 * steps + i];
}
_reference = ref;
}
std::vector<double> JackalMPCCore::get_mpc_results()
{
return _mpc_results;
}
bool JackalMPCCore::set_dist_map(pybind11::dict grid_dict)
{
nav_msgs::OccupancyGrid grid;
// Deserialize the header
pybind11::dict header_dict = grid_dict["header"].cast<pybind11::dict>();
pybind11::dict stamp_dict = header_dict["stamp"].cast<pybind11::dict>();
grid.header.seq = header_dict["seq"].cast<uint32_t>();
grid.header.stamp.sec = stamp_dict["secs"].cast<int32_t>();
grid.header.stamp.nsec = stamp_dict["nsecs"].cast<int32_t>();
grid.header.frame_id = header_dict["frame_id"].cast<std::string>();
// Deserialize the info
pybind11::dict info_dict = grid_dict["info"].cast<pybind11::dict>();
grid.info.resolution = info_dict["resolution"].cast<float>();
grid.info.width = info_dict["width"].cast<uint32_t>();
grid.info.height = info_dict["height"].cast<uint32_t>();
pybind11::dict origin_dict = info_dict["origin"].cast<pybind11::dict>();
pybind11::dict position_dict = origin_dict["position"].cast<pybind11::dict>();
grid.info.origin.position.x = position_dict["x"].cast<double>();
grid.info.origin.position.y = position_dict["y"].cast<double>();
grid.info.origin.position.z = position_dict["z"].cast<double>();
pybind11::dict orientation_dict = origin_dict["orientation"].cast<pybind11::dict>();
grid.info.origin.orientation.x = orientation_dict["x"].cast<double>();
grid.info.origin.orientation.y = orientation_dict["y"].cast<double>();
grid.info.origin.orientation.z = orientation_dict["z"].cast<double>();
grid.info.origin.orientation.w = orientation_dict["w"].cast<double>();
// Deserialize the data
grid.data = grid_dict["data"].cast<std::vector<int8_t>>();
boost::shared_ptr<distmap::DistanceMapConverterBase> dist_map_conv;
dist_map_conv = distmap::make_distance_mapper("distmap/DistanceMapDeadReck");
if (!dist_map_conv->process(boost::make_shared<const nav_msgs::OccupancyGrid>(grid)))
{
std::cout << "Distance map failed to be set from occupancy grid" << std::endl;
return false;
}
_dist_grid_ptr = dist_map_conv->getDistanceFieldObstacle();
_filter.set_dist_map(_dist_grid_ptr);
if (_mpc_type == MPC_TYPE_ACC)
{
MPC_ACC *mpc = dynamic_cast<MPC_ACC *>(_mpc.get());
mpc->set_dist_map(_dist_grid_ptr);
}
if (_mpc_type == MPC_TYPE_NLOPT)
{
CBFHorizon *mpc = dynamic_cast<CBFHorizon *>(_mpc.get());
mpc->set_dist_map(_dist_grid_ptr);
}
std::cout << "Distance map set from occupancy grid" << std::endl;
return true;
}
std::vector<double> JackalMPCCore::get_dist_map_data(double x, double y)
{
if (_dist_grid_ptr == nullptr)
{
return {};
}
double dist = _dist_grid_ptr->atPositionSafe(x, y, true);
distmap::DistanceMap::Gradient grad = _dist_grid_ptr->gradientAtPosition(x, y, true);
double dx = grad.dx;
double dy = grad.dy;
double grad_norm = sqrt(dx * dx + dy * dy);
dx *= (-dist / grad_norm);
dy *= (-dist / grad_norm);
return {dist, dx, dy};
}
#endif
void JackalMPCCore::set_reference(const Eigen::MatrixXd &reference)
{
_reference = reference;
}
void JackalMPCCore::set_mpc_type(const mpc_type &type)
{
}
void JackalMPCCore::set_dist_map(const std::shared_ptr<distmap::DistanceMap> &dist_map)
{
_dist_grid_ptr = dist_map;
_filter.set_dist_map(dist_map);
if (_mpc_type == MPC_TYPE_ACC)
{
MPC_ACC *mpc = dynamic_cast<MPC_ACC *>(_mpc.get());
mpc->set_dist_map(dist_map);
}
if (_mpc_type == MPC_TYPE_NLOPT)
{
CBFHorizon *mpc = dynamic_cast<CBFHorizon *>(_mpc.get());
mpc->set_dist_map(dist_map);
}
}
std::vector<double> JackalMPCCore::solve()
{
if (_mpc_type == MPC_TYPE_NLOPT_POS)
{
MPCNLOPT *mpc = dynamic_cast<MPCNLOPT *>(_mpc.get());
std::cerr << "defining state" << std::endl;
Eigen::VectorXd state(4);
state << _odom(0), _odom(1), _odom(2), _curr_vel;
auto start = std::chrono::high_resolution_clock::now();
_mpc_results = mpc->Solve_gtg(state);
auto end = std::chrono::high_resolution_clock::now();
double time_to_solve = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
_curr_vel = limit(_curr_vel, _curr_vel + _mpc_results[1] * _dt, _max_linacc);
_curr_ang_vel = limit(_curr_ang_vel, _mpc_results[0], _max_anga);
// ensure vel is between -max and max and ang vel is between -max and max
_curr_vel = std::max(-_max_vel, std::min(_max_vel, _curr_vel));
_curr_ang_vel = std::max(-_max_ang_vel, std::min(_max_ang_vel, _curr_ang_vel));
return {_curr_vel, _curr_ang_vel};
}
if (_reference.cols() == 0)
return {};
double ref_head = atan2(_reference(4, 0), _reference(1, 0));
double cte = -1 * (_odom(0) - _reference(0, 0)) * sin(ref_head) + (_odom(1) - _reference(3, 0)) * cos(ref_head);
double etheta = _odom(2) - ref_head;
if (fabs(etheta) > M_PI)
{
int sign = (etheta >= 0) ? 1 : -1;
if (sign == 1)
{
etheta = 2 * M_PI - etheta;
}
else
{
etheta = 2 * M_PI + etheta;
}
etheta *= -sign;
}
// std::cout << "odometry is " << _odom.transpose() << std::endl;
double new_vel;
double time_to_solve = 0.;
if (_mpc_type == MPC_TYPE_CTE)
{
Eigen::VectorXd state(5);
state << _odom(0), _odom(1), _odom(2), cte, etheta;
// time the solve function in milliseconds
auto start = std::chrono::high_resolution_clock::now();
_mpc_results = _mpc->Solve(state, _reference);
auto end = std::chrono::high_resolution_clock::now();
time_to_solve = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
if (time_to_solve > _dt * 1000)
{
_mpc_results[0] = _curr_ang_vel;
_mpc_results[1] = _curr_vel;
}
new_vel = _mpc_results[1];
}
else if (_mpc_type == MPC_TYPE_ACC)
{
Eigen::VectorXd state(6);
state << _odom(0), _odom(1), _odom(2), _curr_vel, cte, etheta;
auto start = std::chrono::high_resolution_clock::now();
_mpc_results = _mpc->Solve(state, _reference);
auto end = std::chrono::high_resolution_clock::now();
time_to_solve = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "mpc wanted vel: " << _curr_vel + _mpc_results[1] * _dt << ", ang vel: " << _mpc_results[0] << std::endl;
std::cout << "mpc done, starting filter" << std::endl;
// if (_use_cbf)
// {
// start = std::chrono::high_resolution_clock::now();
// _mpc_results = _filter.Solve(state, Eigen::Vector2d(_mpc_results[0], _mpc_results[1]), _obstacle);
// end = std::chrono::high_resolution_clock::now();
// std::cout << "Filter time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << std::endl;
// std::cout << "filter wanted vel: " << _curr_vel + _mpc_results[1] * _dt << ", ang vel: " << _mpc_results[0] << std::endl;
// }
if (time_to_solve > _dt * 1000)
{
_mpc_results[0] = _curr_ang_vel;
_mpc_results[1] = 0;
}
new_vel = _curr_vel + _mpc_results[1] * _dt;
}
else if (_mpc_type == MPC_TYPE_NLOPT)
{
Eigen::VectorXd state(6);
state << _odom(0), _odom(1), _odom(2), _curr_vel, cte, etheta;
auto start = std::chrono::high_resolution_clock::now();
_mpc_results = _mpc->Solve(state, _reference);
auto end = std::chrono::high_resolution_clock::now();
time_to_solve = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
if (time_to_solve > _dt * 1000)
{
_mpc_results[0] = _curr_ang_vel;
_mpc_results[1] = 0;
}
new_vel = _curr_vel + _mpc_results[1] * _dt;
}
// std::cerr << "mpc_results: " << _mpc_results[0] << ", " << _mpc_results[1] << std::endl;
std::cout << "Solve time: " << time_to_solve << std::endl;
_curr_ang_vel = limit(_curr_ang_vel, _mpc_results[0], _max_anga);
_curr_vel = limit(_curr_vel, new_vel, _max_linacc);
// ensure vel is between -max and max and ang vel is between -max and max
_curr_vel = std::max(-_max_vel, std::min(_max_vel, _curr_vel));
_curr_ang_vel = std::max(-_max_ang_vel, std::min(_max_ang_vel, _curr_ang_vel));
std::cerr << "curr vel: " << _curr_vel << ", curr ang vel: " << _curr_ang_vel << std::endl;
return {_curr_vel, _curr_ang_vel};
}
std::vector<Eigen::VectorXd> JackalMPCCore::getHorizon()
{
std::vector<Eigen::VectorXd> ret;
if (_mpc_type == MPC_TYPE_CTE)
{
MPC *mpc = dynamic_cast<MPC *>(_mpc.get());
int sz = mpc->mpc_x.size();
for (int i = 0; i < sz; ++i)
{
Eigen::VectorXd state(3);
state << mpc->mpc_x[i], mpc->mpc_y[i], mpc->mpc_theta[i];
ret.push_back(state);
}
}
else if (_mpc_type == MPC_TYPE_ACC)
{
MPC_ACC *mpc = dynamic_cast<MPC_ACC *>(_mpc.get());
int sz = mpc->mpc_x.size();
double t = 0;
for (int i = 0; i < sz - 1; ++i)
{
t += _dt;
Eigen::VectorXd state(6);
state << t, mpc->mpc_x[i], mpc->mpc_y[i], mpc->mpc_theta[i], mpc->mpc_linvels[i], mpc->mpc_linaccs[i];
ret.push_back(state);
}
}
else if (_mpc_type == MPC_TYPE_NLOPT)
{
CBFHorizon *mpc = dynamic_cast<CBFHorizon *>(_mpc.get());
int sz = mpc->mpc_x.size();
double t = 0;
for (int i = 0; i < sz - 1; ++i)
{
t += _dt;
Eigen::VectorXd state(6);
state << t, mpc->mpc_x[i], mpc->mpc_y[i], mpc->mpc_theta[i], mpc->mpc_linvels[i], mpc->mpc_linaccs[i];
ret.push_back(state);
}
}
return ret;
}
double JackalMPCCore::limit(double prev_v, double input, double max_rate)
{
double ret = input;
if (fabs(prev_v - input) / _dt > max_rate)
{
if (input > prev_v)
ret = prev_v + max_rate * _dt;
else
ret = prev_v - max_rate * _dt;
}
return ret;
}
std::vector<double> JackalMPCCore::evaluateHFunction()
{
if (_mpc_type == MPC_TYPE_ACC)
{
MPC_ACC *mpc = dynamic_cast<MPC_ACC *>(_mpc.get());
return mpc->evaluateHFunction();
}
else if (_mpc_type == MPC_TYPE_NLOPT)
{
CBFHorizon *mpc = dynamic_cast<CBFHorizon *>(_mpc.get());
return {mpc->h_value, mpc->alpha_value};
}
return {0.0, 0.0};
}