MRSL Motion Primitive Library  1.2
A motion primitive library for generating trajectory for mobile robots
traj_solver.h
Go to the documentation of this file.
1 
5 #ifndef MPL_TRAJ_SOLVER_H
6 #define MPL_TRAJ_SOLVER_H
7 #include <mpl_basis/trajectory.h>
9 
11 template <int Dim>
12 class TrajSolver {
13  public:
20  Control::Control yaw_control = Control::VEL, bool debug = false)
21  : control_(control), yaw_control_(yaw_control) {
22  if (control == Control::VEL || control == Control::VELxYAW)
23  poly_solver_.reset(new PolySolver<Dim>(0, 1, debug));
24  else if (control == Control::ACC || control == Control::ACCxYAW)
25  poly_solver_.reset(new PolySolver<Dim>(1, 2, debug));
26  else if (control == Control::JRK || control == Control::JRKxYAW)
27  poly_solver_.reset(new PolySolver<Dim>(2, 3, debug));
28  // Due to dimension issue, only workd up to thrid order
29  // if(control == Control::SNP || control == Control::SNPxYAW)
30  // poly_solver_.reset(new PolySolver<Dim>(3, 4));
31  if (yaw_control == Control::VEL)
32  yaw_solver_.reset(new PolySolver<1>(0, 1));
33  else if (yaw_control == Control::ACC)
34  yaw_solver_.reset(new PolySolver<1>(1, 2));
35  else if (yaw_control == Control::JRK)
36  yaw_solver_.reset(new PolySolver<1>(2, 3));
37  }
38 
40  void setWaypoints(const vec_E<Waypoint<Dim>>& ws) {
41  path_.resize(ws.size());
42  for (size_t i = 0; i < ws.size(); i++) path_[i] = ws[i].pos;
43  waypoints_ = ws;
44  }
45 
47  void setV(decimal_t v) { v_ = v; }
48 
51  void setDts(const std::vector<decimal_t>& dts) { dts_ = dts; }
52 
55  void setPath(const vec_Vecf<Dim>& path) {
56  path_ = path;
57 
58  waypoints_.resize(path_.size());
59  for (size_t i = 0; i < waypoints_.size(); i++) {
60  waypoints_[i].pos = path[i];
61  waypoints_[i].vel = Vecf<Dim>::Zero();
62  waypoints_[i].acc = Vecf<Dim>::Zero();
63  waypoints_[i].jrk = Vecf<Dim>::Zero();
64  waypoints_[i].yaw = 0;
65  waypoints_[i].control = Control::VEL;
66  }
67 
68  waypoints_.front().control = control_;
69  waypoints_.back().control = control_;
70  }
71 
73  Trajectory<Dim> solve(bool verbose = false) {
74  if (waypoints_.size() != dts_.size() + 1) dts_ = allocate_time(path_, v_);
75 
76  if (verbose) {
77  for (const auto& it : dts_) std::cout << "dt: " << it << std::endl;
78  for (const auto& it : waypoints_) it.print();
79  }
80 
81  if (poly_solver_ && yaw_solver_) {
82  // solve for pos
83  poly_solver_->solve(waypoints_, dts_);
84  auto traj =
85  Trajectory<Dim>(poly_solver_->getTrajectory()->toPrimitives());
86  // solve for yaw
87  vec_E<Waypoint<1>> yaws;
88  for (const auto& it : waypoints_) {
89  Waypoint<1> yaw(Control::VEL);
90  yaw.pos(0) = it.yaw;
91  yaw.vel(0) = 0;
92  yaw.acc(0) = 0;
93  yaw.jrk(0) = 0;
94  yaws.push_back(yaw);
95  }
96  yaws.front().control = yaw_control_;
97  yaws.back().control = yaw_control_;
98  yaw_solver_->solve(yaws, dts_);
99  auto yaw_prs = yaw_solver_->getTrajectory()->toPrimitives();
100  for (size_t i = 0; i < traj.segs.size(); i++)
101  traj.segs[i].pr_yaw_ = yaw_prs[i].prs_[0];
102  return traj;
103  } else {
104  if (verbose)
105  printf(ANSI_COLOR_RED
106  "TrajSolver is not initialized properlly!\n" ANSI_COLOR_RESET);
107  return Trajectory<Dim>();
108  }
109  }
110 
112  vec_Vecf<Dim> getPath() const { return path_; }
113 
116 
118  std::vector<decimal_t> getDts() const { return dts_; }
119 
120  private:
122  std::vector<decimal_t> allocate_time(const vec_Vecf<Dim>& pts, decimal_t v) {
123  if (pts.size() < 2 || v <= 0) return std::vector<decimal_t>();
124  std::vector<decimal_t> dts(pts.size() - 1);
125  for (unsigned int i = 1; i < pts.size(); i++) {
126  decimal_t d = (pts[i] - pts[i - 1]).template lpNorm<Eigen::Infinity>();
127  dts[i - 1] = d / v;
128  }
129  return dts;
130  }
131 
134 
137 
139  std::vector<decimal_t> dts_;
140 
143 
146 
149 
151  std::unique_ptr<PolySolver<Dim>> poly_solver_;
152 
154  std::unique_ptr<PolySolver<1>> yaw_solver_;
155 };
156 
159 
162 #endif
void setWaypoints(const vec_E< Waypoint< Dim >> &ws)
Set Waypoint array directly, overwrite global vars.
Definition: traj_solver.h:40
std::unique_ptr< PolySolver< 1 > > yaw_solver_
Poly solver for yaw only.
Definition: traj_solver.h:154
vec_E< Waypoint< Dim > > getWaypoints() const
Get the Waypoint array used to solve trajectory.
Definition: traj_solver.h:115
vec_E< Waypoint< Dim > > waypoints_
Intermediate waypoints.
Definition: traj_solver.h:136
Trajectory class.
std::unique_ptr< PolySolver< Dim > > poly_solver_
Poly solver.
Definition: traj_solver.h:151
Vecf< Dim > jrk
jerk in
Definition: waypoint.h:35
#define ANSI_COLOR_RED
Set red font in printf funtion.
Definition: data_type.h:16
Waypoint base class.
Definition: waypoint.h:23
Trajectory class.
Definition: trajectory.h:43
void setPath(const vec_Vecf< Dim > &path)
Definition: traj_solver.h:55
decimal_t v_
Velocity used for internal time allocation.
Definition: traj_solver.h:142
vec_Vecf< Dim > getPath() const
Get the path used for time allocation.
Definition: traj_solver.h:112
Control
Enum for control input.
Definition: control.h:10
std::vector< decimal_t > dts_
Time allocation.
Definition: traj_solver.h:139
void setDts(const std::vector< decimal_t > &dts)
Definition: traj_solver.h:51
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
Control::Control yaw_control_
Control constraints for start and goal yaw.
Definition: traj_solver.h:148
vec_E< Vecf< N > > vec_Vecf
Vector of Eigen 1D float vector.
Definition: data_type.h:70
Vecf< Dim > vel
velocity in
Definition: waypoint.h:33
TrajSolver< 2 > TrajSolver2D
TrajSolver for 2D.
Definition: traj_solver.h:158
Control::Control control_
Control constraints for start and goal.
Definition: traj_solver.h:145
vec_Vecf< Dim > path_
Intermediate pos.
Definition: traj_solver.h:133
Trajectory generator from given waypoints.
double decimal_t
Rename the float type used in lib.
Definition: data_type.h:49
Trajectory< Dim > solve(bool verbose=false)
Solve for trajectory.
Definition: traj_solver.h:73
std::vector< decimal_t > getDts() const
Get the time allocation.
Definition: traj_solver.h:118
#define ANSI_COLOR_RESET
Reset font color in printf funtion.
Definition: data_type.h:40
Eigen::Matrix< decimal_t, N, 1 > Vecf
Eigen 1D float vector of size N.
Definition: data_type.h:56
TrajSolver< 3 > TrajSolver3D
TrajSolver for 3D.
Definition: traj_solver.h:161
TrajSolver(Control::Control control, Control::Control yaw_control=Control::VEL, bool debug=false)
Constructor.
Definition: traj_solver.h:19
Trajectory generator.
Definition: traj_solver.h:12
Vecf< Dim > pos
position in
Definition: waypoint.h:32
std::vector< decimal_t > allocate_time(const vec_Vecf< Dim > &pts, decimal_t v)
Internal time allocation from path and vel using L-inf.
Definition: traj_solver.h:122
Trajectory generator back-end class.
Definition: poly_solver.h:21
void setV(decimal_t v)
Set velocity used for internal time allocation.
Definition: traj_solver.h:47