6 #ifndef MPL_PRIMITIVE_H 7 #define MPL_PRIMITIVE_H 43 c << 0, 0, u, state(2), state(1), state(0);
49 c << 0, u, state(3), state(2), state(1), state(0);
55 c << 0, 0, 0, 0, (p2 - p1) / t, p1;
63 A << 0, 0, 0, 1, 0, 0, 1, 0,
power(t, 3) / 6, t * t / 2, t, 1, t * t / 2, t,
67 Vec4f cc = A.inverse() * b;
68 c << 0, 0, cc(0), cc(1), cc(2), cc(3);
76 A << 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
78 power(t, 4) / 24,
power(t, 3) / 6, t * t / 2, t, 1, 0,
power(t, 3) / 6,
79 t * t / 2, t, 1, 0, 0;
81 b << p1, v1, a1, p2, v2, a2;
94 if (control == Control::VEL || control == Control::VELxYAW)
95 return c(0) *
c(0) / 5184 *
power(t, 9) +
96 c(0) *
c(1) / 576 *
power(t, 8) +
97 (
c(1) *
c(1) / 252 +
c(0) *
c(2) / 168) *
power(t, 7) +
98 (
c(0) *
c(3) / 72 +
c(1) *
c(2) / 36) *
power(t, 6) +
99 (
c(2) *
c(2) / 20 +
c(0) *
c(4) / 60 +
c(1) *
c(3) / 15) *
101 (
c(2) *
c(3) / 4 +
c(1) *
c(4) / 12) *
power(t, 4) +
102 (
c(3) *
c(3) / 3 +
c(2) *
c(4) / 3) *
power(t, 3) +
103 c(3) *
c(4) * t * t +
c(4) *
c(4) * t;
105 else if (control == Control::ACC || control == Control::ACCxYAW)
106 return c(0) *
c(0) / 252 *
power(t, 7) +
c(0) *
c(1) / 36 *
power(t, 6) +
107 (
c(1) *
c(1) / 20 +
c(0) *
c(2) / 15) *
power(t, 5) +
108 (
c(0) *
c(3) / 12 +
c(1) *
c(2) / 4) *
power(t, 4) +
109 (
c(2) *
c(2) / 3 +
c(1) *
c(3) / 3) *
power(t, 3) +
110 c(2) *
c(3) * t * t +
c(3) *
c(3) * t;
112 else if (control == Control::JRK || control == Control::JRKxYAW)
113 return c(0) *
c(0) / 20 *
power(t, 5) +
c(0) *
c(1) / 4 *
power(t, 4) +
114 (
c(1) *
c(1) +
c(0) *
c(2)) / 3 *
power(t, 3) +
115 c(1) *
c(2) * t * t +
c(2) *
c(2) * t;
117 else if (control == Control::SNP || control == Control::SNPxYAW)
118 return c(0) *
c(0) / 3 *
power(t, 3) +
c(0) *
c(1) * t * t +
129 return c(0) / 120 *
power(t, 5) +
c(1) / 24 *
power(t, 4) +
130 c(2) / 6 *
power(t, 3) +
c(3) / 2 * t * t +
c(4) * t +
c(5);
135 return c(0) / 24 *
power(t, 4) +
c(1) / 6 *
power(t, 3) +
c(2) / 2 * t * t +
141 return c(0) / 6 *
power(t, 3) +
c(1) / 2 * t * t +
c(2) * t +
c(3);
153 std::vector<decimal_t> roots =
solve(0,
c(0) / 6,
c(1) / 2,
c(2),
c(3));
154 std::vector<decimal_t> ts;
155 for (
const auto& it : roots) {
156 if (it > 0 && it < t)
170 std::vector<decimal_t> roots =
solve(0, 0,
c(0) / 2,
c(1),
c(2));
171 std::vector<decimal_t> ts;
172 for (
const auto& it : roots) {
173 if (it > 0 && it < t)
187 std::vector<decimal_t> ts;
190 if (t_sol > 0 && t_sol < t) ts.push_back(t_sol);
221 : t_(t), control_(p.control) {
222 if (control_ == Control::SNP) {
223 for (
int i = 0; i < Dim; i++) {
228 }
else if (control_ == Control::JRK) {
229 for (
int i = 0; i < Dim; i++)
231 }
else if (control_ == Control::ACC) {
232 for (
int i = 0; i < Dim; i++)
234 }
else if (control_ == Control::VEL) {
235 for (
int i = 0; i < Dim; i++) prs_[i] =
Primitive1D(p.
pos(i), u(i));
236 }
else if (control_ == Control::SNPxYAW) {
237 for (
int i = 0; i < Dim; i++) {
243 }
else if (control_ == Control::JRKxYAW) {
244 for (
int i = 0; i < Dim; i++)
247 }
else if (control_ == Control::ACCxYAW) {
248 for (
int i = 0; i < Dim; i++)
251 }
else if (control_ == Control::VELxYAW) {
252 for (
int i = 0; i < Dim; i++) prs_[i] =
Primitive1D(p.
pos(i), u(i));
255 printf(
"Null Primitive, check the control set-up of the Waypoint!\n");
263 : t_(t), control_(p1.control) {
266 for (
int i = 0; i < Dim; i++)
268 p2.
vel(i), p2.
acc(i), t_);
271 else if (p1.
control == Control::ACC && p2.
control == Control::ACC) {
272 for (
int i = 0; i < Dim; i++)
276 else if (p1.
control == Control::VEL && p2.
control == Control::VEL) {
277 for (
int i = 0; i < Dim; i++)
281 else if (p1.
control == Control::JRKxYAW && p2.
control == Control::JRKxYAW) {
282 for (
int i = 0; i < Dim; i++)
284 p2.
vel(i), p2.
acc(i), t_);
288 else if (p1.
control == Control::ACCxYAW && p2.
control == Control::ACCxYAW) {
289 for (
int i = 0; i < Dim; i++)
294 else if (p1.
control == Control::VELxYAW && p2.
control == Control::VELxYAW) {
295 for (
int i = 0; i < Dim; i++)
301 printf(
"Null Primitive, check the control set-up of the Waypoint!\n");
310 : t_(t), control_(control) {
311 for (
int i = 0; i < Dim; i++) prs_[i] =
Primitive1D(cs[i]);
312 if (cs.size() == Dim + 1) pr_yaw_ =
Primitive1D(cs[Dim]);
323 for (
int k = 0; k < Dim; k++) {
324 p.
pos(k) = prs_[k].p(t);
325 p.
vel(k) = prs_[k].v(t);
326 p.
acc(k) = prs_[k].a(t);
327 p.
jrk(k) = prs_[k].j(t);
328 if (p.
use_yaw) p.
yaw = normalize_angle(pr_yaw_.p(t));
354 std::vector<decimal_t> ts = prs_[k].extrema_v(t_);
355 decimal_t max_v = std::max(std::abs(prs_[k].
v(0)), std::abs(prs_[k].
v(t_)));
356 for (
const auto& it : ts) {
357 if (it > 0 && it < t_) {
359 max_v = v > max_v ?
v : max_v;
370 std::vector<decimal_t> ts = prs_[k].extrema_a(t_);
371 decimal_t max_a = std::max(std::abs(prs_[k].
a(0)), std::abs(prs_[k].
a(t_)));
372 for (
const auto& it : ts) {
373 if (it > 0 && it < t_) {
375 max_a = a > max_a ?
a : max_a;
385 std::vector<decimal_t> ts = prs_[k].extrema_j(t_);
386 decimal_t max_j = std::max(std::abs(prs_[k].
j(0)), std::abs(prs_[k].
j(t_)));
387 for (
const auto& it : ts) {
388 if (it > 0 && it < t_) {
390 max_j = j > max_j ?
j : max_j;
405 for (
const auto& pr : prs_) j += pr.J(t_, control);
418 for (
int i = 0; i <= N; i++) ps[i] = evaluate(i * dt);
428 std::array<Primitive1D, Dim>
prs_;
453 if (pr.
control() == Control::ACC)
455 else if (pr.
control() == Control::JRK)
458 else if (pr.
control() == Control::SNP)
462 else if (pr.
control() == Control::VELxYAW)
464 else if (pr.
control() == Control::ACCxYAW)
466 else if (pr.
control() == Control::JRKxYAW)
469 else if (pr.
control() == Control::SNPxYAW)
485 if (max <= 0)
return true;
487 for (
int i = 0; i < Dim; i++) {
488 if (xxx == Control::VEL && pr.
max_vel(i) > max)
490 else if (xxx == Control::ACC && pr.
max_acc(i) > max)
492 else if (xxx == Control::JRK && pr.
max_jrk(i) > max)
506 if (my <= 0)
return true;
511 for (
const auto& w : ws) {
512 const auto v = w.vel.template topRows<2>();
513 if (
v(0) != 0 ||
v(1) != 0) {
521 if (d < cos(my))
return false;
530 std::cout <<
"Primitive: " << std::endl;
531 std::cout <<
"t: " << p.
t() << std::endl;
532 for (
int i = 0; i < Dim; i++)
533 std::cout <<
"dim[" << i <<
"]: " << p.
pr(i).
coeff().transpose()
535 std::cout <<
"yaw: " << p.
pr_yaw().
coeff().transpose() << std::endl;
542 for (
int i = 0; i < Dim; i++) {
547 std::cout <<
"max_vel: " << max_v.transpose() << std::endl;
548 std::cout <<
"max_acc: " << max_a.transpose() << std::endl;
549 std::cout <<
"max_jrk: " << max_j.transpose() << std::endl;
Primitive(const Waypoint< Dim > &p1, const Waypoint< Dim > &p2, decimal_t t)
Construct from an initial state p1 and a goal state p2 for a given duration t.
Definition: primitive.h:262
Primitive1D pr_yaw_
Primitive for yaw.
Definition: primitive.h:430
Primitive< 2 > Primitive2D
Primitive for 2D.
Definition: primitive.h:434
bool validate_yaw(const Primitive< Dim > &pr, decimal_t my)
Check if the successor goes outside of the fov.
Definition: primitive.h:504
Primitive1D(Vec3f state, decimal_t u)
Definition: primitive.h:42
decimal_t j(decimal_t t) const
Return at time .
Definition: primitive.h:145
decimal_t a(decimal_t t) const
Return at time .
Definition: primitive.h:140
decimal_t max_acc(int k) const
Return max accleration along one axis.
Definition: primitive.h:369
Vecf< Dim > jrk
jerk in
Definition: waypoint.h:35
decimal_t J(const Control::Control &control) const
Return total efforts for the given duration.
Definition: primitive.h:403
decimal_t yaw
yaw
Definition: waypoint.h:36
Waypoint base class.
Definition: waypoint.h:23
Primitive1D class.
Definition: primitive.h:21
Primitive1D()
Empty constructor.
Definition: primitive.h:25
Vec6f coeff() const
Return coffecients.
Definition: primitive.h:125
Primitive1D(decimal_t p1, decimal_t p2, decimal_t t)
Definition: primitive.h:54
Eigen::Matrix< decimal_t, Eigen::Dynamic, 1 > VecDf
Eigen 1D float vector of dynamic size.
Definition: data_type.h:61
decimal_t p(decimal_t t) const
Return at time .
Definition: primitive.h:128
Primitive(const Waypoint< Dim > &p, const VecDf &u, decimal_t t)
Construct from an initial state p and an input control u for a given duration t.
Definition: primitive.h:220
Primitive1D(decimal_t p1, decimal_t v1, decimal_t p2, decimal_t v2, decimal_t t)
Definition: primitive.h:60
decimal_t power(decimal_t t, int n)
Return .
Definition: math.h:197
std::vector< decimal_t > extrema_v(decimal_t t) const
Return vector of time for velocity extrema.
Definition: primitive.h:152
std::vector< decimal_t > solve(decimal_t a, decimal_t b, decimal_t c, decimal_t d, decimal_t e)
General solver for .
Definition: math.h:117
Control
Enum for control input.
Definition: control.h:10
Waypoint< Dim > evaluate(decimal_t t) const
Return Waypoint at time .
Definition: primitive.h:321
decimal_t max_jrk(int k) const
Return max jerk along k-th dimension.
Definition: primitive.h:384
Vecf< Dim > acc
acceleration in
Definition: waypoint.h:34
std::vector< T, Eigen::aligned_allocator< T > > vec_E
Pre-allocated std::vector for Eigen using vec_E.
Definition: data_type.h:53
Vecf< 3 > Vec3f
Eigen 1D float vector of size 3.
Definition: data_type.h:80
Control::Control control
Control value.
Definition: waypoint.h:54
Vecf< Dim > vel
velocity in
Definition: waypoint.h:33
decimal_t Jyaw() const
Return total yaw efforts for the given duration.
Definition: primitive.h:410
Matf< 4, 4 > Mat4f
4x4 Matrix in float
Definition: data_type.h:102
Primitive1D(Vec2f state, decimal_t u)
Definition: primitive.h:38
std::vector< decimal_t > extrema_j(decimal_t t) const
Return vector of time for jerk extrema.
Definition: primitive.h:186
decimal_t v(decimal_t t) const
Return at time .
Definition: primitive.h:134
Primitive1D(decimal_t p, decimal_t u)
Construct 1D primitive from an initial state (p) and an input control (u)
Definition: primitive.h:34
decimal_t t() const
Return duration .
Definition: primitive.h:336
bool validate_xxx(const Primitive< Dim > &pr, decimal_t max, Control::Control xxx)
Check if the max velocity magnitude is within the threshold.
Definition: primitive.h:483
double decimal_t
Rename the float type used in lib.
Definition: data_type.h:49
Matf< 6, 6 > Mat6f
6x6 Matrix in float
Definition: data_type.h:104
Primitive(const vec_E< Vec6f > &cs, decimal_t t, Control::Control control)
Construct from given coefficients and duration .
Definition: primitive.h:309
Primitive1D(const Vec6f &coeff)
Construct from known coefficients.
Definition: primitive.h:31
Primitive1D pr_yaw() const
Get the yaw primitive.
Definition: primitive.h:347
Primitive1D(Vec4f state, decimal_t u)
Definition: primitive.h:48
std::vector< decimal_t > extrema_a(decimal_t t) const
Return vector of time for acceleration extrema.
Definition: primitive.h:169
Vec6f c
Coefficients.
Definition: primitive.h:197
Vecf< 2 > Vec2f
Eigen 1D float vector of size 2.
Definition: data_type.h:76
decimal_t max_vel(int k) const
Return max velocity along one axis.
Definition: primitive.h:353
Vecf< 6 > Vec6f
Column vector in float of size 6.
Definition: data_type.h:86
Defines all data types used in this lib.
Primitive()
Empty constructor.
Definition: primitive.h:211
bool use_yaw
If true, yaw will be used in primitive generation.
Definition: waypoint.h:52
Primitive1D(decimal_t p1, decimal_t v1, decimal_t a1, decimal_t p2, decimal_t v2, decimal_t a2, decimal_t t)
Definition: primitive.h:73
Primitive< 3 > Primitive3D
Primitive for 3D.
Definition: primitive.h:437
bool validate_primitive(const Primitive< Dim > &pr, decimal_t mv=0, decimal_t ma=0, decimal_t mj=0, decimal_t myaw=0)
Check if the max velocity magnitude is within the threshold.
Definition: primitive.h:450
Eigen::Matrix< decimal_t, N, 1 > Vecf
Eigen 1D float vector of size N.
Definition: data_type.h:56
Vecf< 4 > Vec4f
Eigen 1D float vector of size 4.
Definition: data_type.h:84
vec_E< Waypoint< Dim > > sample(int N) const
Sample N+1 Waypoints using uniformed time.
Definition: primitive.h:415
Primitive1D pr(int k) const
Get the 1D primitive.
Definition: primitive.h:345
void print_max(const Primitive< Dim > &p)
Print max dynamic infomation in primitive p.
Definition: primitive.h:540
Control::Control control_
Control.
Definition: primitive.h:426
Primitive class.
Definition: primitive.h:206
decimal_t t_
Duration.
Definition: primitive.h:424
Vecf< Dim > pos
position in
Definition: waypoint.h:32
Control::Control control() const
Get the control indicator.
Definition: primitive.h:339
std::array< Primitive1D, Dim > prs_
By default, primitive class contains Dim 1D primitive.
Definition: primitive.h:428
decimal_t J(decimal_t t, const Control::Control &control) const
Return total efforts of 1D primitive for the given duration: .
Definition: primitive.h:92
void print(const Primitive< Dim > &p)
Print all coefficients in primitive p.
Definition: primitive.h:529