/
non_uniform_bspline.cpp
executable file
·571 lines (468 loc) · 16.6 KB
/
non_uniform_bspline.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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#include "grad_replanner/non_uniform_bspline.h"
#include <ros/ros.h>
// control points is a (n+1)x3 matrix
NonUniformBspline::NonUniformBspline(Eigen::MatrixXd points, int order,
double interval, bool zero) {
this->p = order;
control_points = points;
this->n = points.rows() - 1;
this->m = this->n + this->p + 1;
// calculate knots vector
this->interval = interval;
this->u = Eigen::VectorXd::Zero(this->m + 1);
for (int i = 0; i <= this->m; ++i) {
if (i <= this->p)
this->u(i) = double(-this->p + i) * this->interval;
else if (i > this->p && i <= this->m - this->p) {
this->u(i) = this->u(i - 1) + this->interval;
} else if (i > this->m - this->p) {
this->u(i) = this->u(i - 1) + this->interval;
}
}
// show the result
// cout << "p: " << p << " n: " << n << " m: " << m << endl;
// cout << "control pts:\n" << control_points << "\nknots:\n" <<
// this->u.transpose() << endl; cout << "M3:\n" << M[0] << "\nM4:\n" << M[1]
// << "\nM5:\n" << M[2] << endl;
}
NonUniformBspline::~NonUniformBspline() {}
void NonUniformBspline::setParameterAuto() {
ros::param::get("/local_replanner/limit_ratio", limit_ratio);
ros::param::get("/local_replanner/max_vel", limit_vel);
ros::param::get("/local_replanner/max_acc", limit_acc);
}
void NonUniformBspline::setDynamicsLimit(double vel, double acc) {
limit_vel = vel;
limit_acc = acc;
}
void NonUniformBspline::setKnot(Eigen::VectorXd knot) { this->u = knot; }
Eigen::VectorXd NonUniformBspline::getKnot() { return this->u; }
void NonUniformBspline::getRegion(double &um, double &um_p) {
um = this->u(this->p);
um_p = this->u(this->m - this->p);
}
Eigen::VectorXd NonUniformBspline::getU(double u) {
Eigen::VectorXd uv = Eigen::VectorXd::Zero(this->p + 1);
uv(0) = 1.0;
for (int i = 1; i <= this->p; ++i) {
uv(i) = uv(i - 1) * u;
}
return uv;
}
Eigen::MatrixXd NonUniformBspline::getPi(int idx) {
Eigen::MatrixXd pi = control_points.block(idx - p, 0, p + 1, 3);
return pi;
}
Eigen::Vector3d NonUniformBspline::evaluateDeBoor(double t) {
if (t < this->u(this->p) || t > this->u(this->m - this->p)) {
//cout << "Out of trajectory range." << endl;
// return Eigen::Vector3d::Zero(3);
if (t < this->u(this->p)) t = this->u(this->p);
if (t > this->u(this->m - this->p)) t = this->u(this->m - this->p);
}
// determine which [ui,ui+1] lay in
int k = this->p;
while (true) {
if (this->u(k + 1) >= t) break;
++k;
}
/* deBoor's alg */
vector<Eigen::Vector3d> d;
for (int i = 0; i <= p; ++i) {
d.push_back(control_points.row(k - p + i));
// cout << d[i].transpose() << endl;
}
for (int r = 1; r <= p; ++r) {
for (int i = p; i >= r; --i) {
double alpha = (t - u[i + k - p]) / (u[i + 1 + k - r] - u[i + k - p]);
// cout << "alpha: " << alpha << endl;
d[i] = (1 - alpha) * d[i - 1] + alpha * d[i];
}
}
return d[p];
}
Eigen::MatrixXd NonUniformBspline::getDerivativeControlPoints() {
// The derivative of a b-spline is also a b-spline, its order become p-1
// control point Qi = p*(Pi+1-Pi)/(ui+p+1-ui+1)
Eigen::MatrixXd ctp = Eigen::MatrixXd::Zero(control_points.rows() - 1, 3);
for (int i = 0; i < ctp.rows(); ++i) {
ctp.row(i) = p * (control_points.row(i + 1) - control_points.row(i)) /
(u(i + p + 1) - u(i + 1));
}
return ctp;
}
NonUniformBspline NonUniformBspline::getDerivative() {
Eigen::MatrixXd ctp = this->getDerivativeControlPoints();
NonUniformBspline derivative =
NonUniformBspline(ctp, p - 1, this->interval, false);
/* cut the first and last knot */
Eigen::VectorXd knot(this->u.rows() - 2);
knot = this->u.segment(1, this->u.rows() - 2);
derivative.setKnot(knot);
return derivative;
}
bool NonUniformBspline::checkFeasibility(bool show) {
bool fea = true;
// SETY << "[Bspline]: total points size: " << control_points.rows() << REC;
Eigen::MatrixXd P = control_points;
/* check vel feasibility and insert points */
double max_vel = -1.0;
for (int i = 0; i < P.rows() - 1; ++i) {
Eigen::Vector3d vel =
p * (P.row(i + 1) - P.row(i)) / (u(i + p + 1) - u(i + 1));
if (fabs(vel(0)) > limit_vel + 0.1 || fabs(vel(1)) > limit_vel + 0.1 ||
fabs(vel(2)) > limit_vel + 0.1) {
/* insert mid point */
if (show)
SETR << "[Check]: Infeasible vel " << i << " :" << vel.transpose()
<< REC;
fea = false;
max_vel = max(max_vel, fabs(vel(0)));
max_vel = max(max_vel, fabs(vel(1)));
max_vel = max(max_vel, fabs(vel(2)));
}
}
/* acc feasibility */
double max_acc = -1.0;
for (int i = 0; i < P.rows() - 2; ++i) {
Eigen::Vector3d acc =
p * (p - 1) *
((P.row(i + 2) - P.row(i + 1)) / (u(i + p + 2) - u(i + 2)) -
(P.row(i + 1) - P.row(i)) / (u(i + p + 1) - u(i + 1))) /
(u(i + p + 1) - u(i + 2));
if (fabs(acc(0)) > limit_acc + 0.1 || fabs(acc(1)) > limit_acc + 0.1 ||
fabs(acc(2)) > limit_acc + 0.1) {
/* insert mid point */
if (show)
SETR << "[Check]: Infeasible acc " << i << " :" << acc.transpose()
<< REC;
fea = false;
max_acc = max(max_acc, fabs(acc(0)));
max_acc = max(max_acc, fabs(acc(1)));
max_acc = max(max_acc, fabs(acc(2)));
double ma = max(fabs(acc(0)), fabs(acc(1)));
ma = max(ma, fabs(acc(2)));
double ratio = sqrt(ma / limit_acc) + 1e-4;
// cout << "ratio: " << ratio << endl;
}
}
// cout << "max vel:" << max_vel << ", max acc:" << max_acc << endl;
double ratio = max(max_vel / limit_vel, sqrt(fabs(max_acc) / limit_acc));
//SETY << "check ratio:" << ratio << REC;
return fea;
}
bool NonUniformBspline::reallocateTime(bool show) {
// SETY << "[Bspline]: total points size: " << control_points.rows() << REC;
// cout << "origin knots:\n" << u.transpose() << endl;
bool fea = true;
// double tm, tmp;
// getRegion(tm, tmp);
// double to = tmp - tm;
// cout << "origin duration: " << to << endl;
Eigen::MatrixXd P = control_points;
/* check vel feasibility and insert points */
double max_vel = -1.0;
const int der_num_v = 4;
const int der_num_a = 3;
for (int i = der_num_v; i < P.rows() - 1 - der_num_v; ++i) {
Eigen::Vector3d vel =
p * (P.row(i + 1) - P.row(i)) / (u(i + p + 1) - u(i + 1));
if (fabs(vel(0)) > limit_vel + 1e-4 || fabs(vel(1)) > limit_vel + 1e-4 ||
fabs(vel(2)) > limit_vel + 1e-4) {
fea = false;
max_vel = -1.0;
max_vel = max(max_vel, fabs(vel(0)));
max_vel = max(max_vel, fabs(vel(1)));
max_vel = max(max_vel, fabs(vel(2)));
if (show)
SETR << "[Realloc]: Infeasible vel " << i << " :" << vel.transpose()
<< REC;
double ratio = max_vel / limit_vel + 1e-4;
if (ratio > limit_ratio) ratio = limit_ratio;
double time_ori = u(i + p + 1) - u(i + 1);
double time_new = ratio * time_ori;
double delta_t = time_new - time_ori;
double t_inc = delta_t / double(p);
for (int j = i + 2; j <= i + p + 1; ++j) {
u(j) += double(j - i - 1) * t_inc;
if (j <= 5 && j >= 1) {
// cout << "vel j: " << j << endl;
}
}
for (int j = i + p + 2; j < u.rows(); ++j) {
u(j) += delta_t;
}
}
}
/* acc feasibility */
double max_acc = -1.0;
for (int i = der_num_a; i < P.rows() - 2 - der_num_a; ++i) {
Eigen::Vector3d acc =
p * (p - 1) *
((P.row(i + 2) - P.row(i + 1)) / (u(i + p + 2) - u(i + 2)) -
(P.row(i + 1) - P.row(i)) / (u(i + p + 1) - u(i + 1))) /
(u(i + p + 1) - u(i + 2));
if (fabs(acc(0)) > limit_acc + 1e-4 || fabs(acc(1)) > limit_acc + 1e-4 ||
fabs(acc(2)) > limit_acc + 1e-4) {
fea = false;
/* insert mid point */
max_acc = -1.0;
max_acc = max(max_acc, fabs(acc(0)));
max_acc = max(max_acc, fabs(acc(1)));
max_acc = max(max_acc, fabs(acc(2)));
if (show)
SETR << "[Realloc]: Infeasible acc " << i << " :" << acc.transpose()
<< REC;
double ratio = sqrt(max_acc / limit_acc) + 1e-4;
if (ratio > limit_ratio) ratio = limit_ratio;
// cout << "ratio: " << ratio << endl;
double time_ori = u(i + p + 1) - u(i + 2);
double time_new = ratio * time_ori;
double delta_t = time_new - time_ori;
double t_inc = delta_t / double(p - 1);
if (i == 1 || i == 2) {
// cout << "acc i: " << i << endl;
for (int j = 2; j <= 5; ++j) {
u(j) += double(j - 1) * t_inc;
}
for (int j = 6; j < u.rows(); ++j) {
u(j) += 4.0 * t_inc;
}
} else {
for (int j = i + 3; j <= i + p + 1; ++j) {
u(j) += double(j - i - 2) * t_inc;
if (j <= 5 && j >= 1) {
// cout << "acc j: " << j << endl;
}
}
for (int j = i + p + 2; j < u.rows(); ++j) {
u(j) += delta_t;
}
}
}
}
// cout << "new knots:\n" << u.transpose() << endl;
// getRegion(tm, tmp);
// double tn = tmp - tm;
// cout << "new duration: " << tn << endl;
// SETY << "realloc ratio: " << tn / to << REC;
return fea;
}
void NonUniformBspline::adjustTime() {
int num1 = 5;
int num2 = getKnot().rows() - 1 - 5;
adjustKnot(num1, num2, 1.05);
}
void NonUniformBspline::adjustKnot(int num1, int num2, double ratio) {
double delta_t = (ratio - 1.0) * (u(num2) - u(num1));
double t_inc = delta_t / double(num2 - num1);
for (int i = num1 + 1; i <= num2; ++i) u(i) += double(i - num1) * t_inc;
for (int i = num2 + 1; i < u.rows(); ++i) u(i) += delta_t;
}
void NonUniformBspline::recomputeInit() {
double t1 = u(1), t2 = u(2), t3 = u(3), t4 = u(4), t5 = u(5);
/* write the A matrix */
Eigen::Matrix3d A;
/* position */
A(0, 0) =
((t2 - t5) * (t3 - t4) * (t3 - t4)) / ((t1 - t4) * (t2 - t4) * (t2 - t5));
A(0, 1) =
((t2 - t5) * (t3 - t4) * (t1 - t3)) /
((t1 - t4) * (t2 - t4) * (t2 - t5)) +
((t1 - t4) * (t2 - t3) * (t3 - t5)) / ((t1 - t4) * (t2 - t4) * (t2 - t5));
A(0, 2) =
((t1 - t4) * (t2 - t3) * (t2 - t3)) / ((t1 - t4) * (t2 - t4) * (t2 - t5));
/* velocity */
A(1, 0) = 3.0 * ((t2 - t5) * (t3 - t4)) / ((t1 - t4) * (t2 - t4) * (t2 - t5));
A(1, 1) = 3.0 * ((t1 - t4) * (t2 - t3) - (t2 - t5) * (t3 - t4)) /
((t1 - t4) * (t2 - t4) * (t2 - t5));
A(1, 2) =
-3.0 * ((t1 - t4) * (t2 - t3)) / ((t1 - t4) * (t2 - t4) * (t2 - t5));
/* acceleration */
A(2, 0) = 6.0 * (t2 - t5) / ((t1 - t4) * (t2 - t4) * (t2 - t5));
A(2, 1) =
-6.0 * ((t1 - t4) + (t2 - t5)) / ((t1 - t4) * (t2 - t4) * (t2 - t5));
A(2, 2) = 6.0 * (t1 - t4) / ((t1 - t4) * (t2 - t4) * (t2 - t5));
/* write B = (bx, by, bz) */
Eigen::Matrix3d B;
Eigen::Vector3d bx, by, bz;
B.row(0) = x0;
B.row(1) = v0;
B.row(2) = a0;
// cout << "B:\n" << B << endl;
bx = B.col(0);
by = B.col(1);
bz = B.col(2);
/* solve */
Eigen::Vector3d px = A.colPivHouseholderQr().solve(bx);
Eigen::Vector3d py = A.colPivHouseholderQr().solve(by);
Eigen::Vector3d pz = A.colPivHouseholderQr().solve(bz);
Eigen::Matrix3d P;
P.col(0) = px;
P.col(1) = py;
P.col(2) = pz;
control_points.row(0) = P.row(0);
control_points.row(1) = P.row(1);
control_points.row(2) = P.row(2);
B = A * P;
// cout << "B:\n" << B << endl;
}
// input :
// sample : 3 x (K+2) (for 3 order) for x, y, z sample
// ts
// output:
// control_pts (K+6)x3
void NonUniformBspline::getControlPointEqu3(Eigen::MatrixXd samples, double ts,
Eigen::MatrixXd &control_pts) {
int K = samples.cols() - 4 - 1;
// write A
Eigen::VectorXd prow(3), vrow(3), arow(3);
prow << 1, 4, 1;
vrow << -1, 0, 1;
arow << 1, -2, 1;
Eigen::MatrixXd A = Eigen::MatrixXd::Zero(K + 5, K + 4);
for (int i = 0; i < K + 2; ++i) A.block(i, i, 1, 3) = prow.transpose();
A.block(K + 2, 0, 1, 3) = A.block(K + 3, K + 1, 1, 3) = vrow.transpose();
A.block(K + 4, 0, 1, 3) = arow.transpose();
// cout << "A:\n" << A << endl;
A.block(0, 0, K + 2, K + 4) = (1 / 6.0) * A.block(0, 0, K + 2, K + 4);
A.block(K + 2, 0, 2, K + 4) = (1 / 2.0 / ts) * A.block(K + 2, 0, 2, K + 4);
A.row(K + 4) = (1 / ts / ts) * A.row(K + 4);
// write b
Eigen::VectorXd bx(K + 5), by(K + 5), bz(K + 5);
for (int i = 0; i < K + 5; ++i) {
bx(i) = samples(0, i);
by(i) = samples(1, i);
bz(i) = samples(2, i);
}
// solve Ax = b
Eigen::VectorXd px = A.colPivHouseholderQr().solve(bx);
Eigen::VectorXd py = A.colPivHouseholderQr().solve(by);
Eigen::VectorXd pz = A.colPivHouseholderQr().solve(bz);
// convert to control pts
control_pts.resize(K + 4, 3);
control_pts.col(0) = px;
control_pts.col(1) = py;
control_pts.col(2) = pz;
}
void NonUniformBspline::BsplineParameterize(
const double &ts, const vector<Eigen::Vector3d> &point_set,
const vector<Eigen::Vector3d> &start_end_derivative,
Eigen::MatrixXd &ctrl_pts) {
if (ts <= 0) {
cout << "[B-spline]:time step error." << endl;
return;
}
if (point_set.size() <= 3) {
cout << "[B-spline]:point set have only " << point_set.size() << " points."
<< endl;
return;
}
if (start_end_derivative.size() != 4) {
cout << "[B-spline]:derivatives error." << endl;
}
int K = point_set.size() - 2;
// write A
Eigen::VectorXd prow(3), vrow(3), arow(3);
prow << 1, 4, 1;
vrow << -1, 0, 1;
arow << 1, -2, 1;
Eigen::MatrixXd A = Eigen::MatrixXd::Zero(K + 6, K + 4);
for (int i = 0; i < K + 2; ++i) A.block(i, i, 1, 3) = prow.transpose();
A.block(K + 2, 0, 1, 3) = A.block(K + 3, K + 1, 1, 3) = vrow.transpose();
A.block(K + 4, 0, 1, 3) = A.block(K + 5, K + 1, 1, 3) = arow.transpose();
// cout << "A:\n" << A << endl;
A.block(0, 0, K + 2, K + 4) = (1 / 6.0) * A.block(0, 0, K + 2, K + 4);
A.block(K + 2, 0, 2, K + 4) = (1 / 2.0 / ts) * A.block(K + 2, 0, 2, K + 4);
A.row(K + 4) = (1 / ts / ts) * A.row(K + 4);
A.row(K + 5) = (1 / ts / ts) * A.row(K + 5);
// write b
Eigen::VectorXd bx(K + 6), by(K + 6), bz(K + 6);
for (int i = 0; i < K + 2; ++i) {
bx(i) = point_set[i](0), by(i) = point_set[i](1), bz(i) = point_set[i](2);
}
for (int i = 0; i < 4; ++i) {
bx(K + 2 + i) = start_end_derivative[i](0);
by(K + 2 + i) = start_end_derivative[i](1);
bz(K + 2 + i) = start_end_derivative[i](2);
}
// solve Ax = b
Eigen::VectorXd px = A.colPivHouseholderQr().solve(bx);
Eigen::VectorXd py = A.colPivHouseholderQr().solve(by);
Eigen::VectorXd pz = A.colPivHouseholderQr().solve(bz);
// convert to control pts
ctrl_pts.resize(K + 4, 3);
ctrl_pts.col(0) = px;
ctrl_pts.col(1) = py;
ctrl_pts.col(2) = pz;
//cout << "[B-spline]: parameterization ok." << endl;
}
double NonUniformBspline::getTimeSum() {
double tm, tmp;
getRegion(tm, tmp);
return tmp - tm;
}
double NonUniformBspline::getLength() {
double length = 0.0;
double tm, tmp;
getRegion(tm, tmp);
Eigen::Vector3d p_l = evaluateDeBoor(tm), p_n;
for (double t = tm + 0.01; t <= tmp; t += 0.01) {
p_n = evaluateDeBoor(t);
length += (p_n - p_l).norm();
p_n = p_l;
}
return length;
}
double NonUniformBspline::getJerk() {
NonUniformBspline jerk_traj = getDerivative().getDerivative().getDerivative();
Eigen::VectorXd times = jerk_traj.getKnot();
Eigen::MatrixXd ctrl_pts = jerk_traj.getControlPoint();
cout << "num knot:" << times.rows() << endl;
cout << "num ctrl pts:" << ctrl_pts.rows() << endl;
double jerk = 0.0;
for (int i = 0; i < ctrl_pts.rows(); ++i) {
jerk += (times(i + 1) - times(i)) * ctrl_pts(i, 0) * ctrl_pts(i, 0);
jerk += (times(i + 1) - times(i)) * ctrl_pts(i, 1) * ctrl_pts(i, 1);
jerk += (times(i + 1) - times(i)) * ctrl_pts(i, 2) * ctrl_pts(i, 2);
}
return jerk;
}
void NonUniformBspline::getMeanAndMaxVel(double &mean_v, double &max_v) {
NonUniformBspline vel = getDerivative();
double tm, tmp;
vel.getRegion(tm, tmp);
double max_vel = -1.0, mean_vel = 0.0;
int num = 0;
for (double t = tm; t <= tmp; t += 0.01) {
Eigen::Vector3d v3d = vel.evaluateDeBoor(t);
double vn = v3d.norm();
mean_vel += vn;
++num;
if (vn > max_vel) {
max_vel = vn;
}
}
mean_vel = mean_vel / double(num);
mean_v = mean_vel;
max_v = max_vel;
}
void NonUniformBspline::getMeanAndMaxAcc(double &mean_a, double &max_a) {
NonUniformBspline acc = getDerivative().getDerivative();
double tm, tmp;
acc.getRegion(tm, tmp);
double max_acc = -1.0, mean_acc = 0.0;
int num = 0;
for (double t = tm; t <= tmp; t += 0.01) {
Eigen::Vector3d a3d = acc.evaluateDeBoor(t);
double an = a3d.norm();
mean_acc += an;
++num;
if (an > max_acc) {
max_acc = an;
}
}
mean_acc = mean_acc / double(num);
mean_a = mean_acc;
max_a = max_acc;
}