MRSL Motion Primitive Library  1.2
A motion primitive library for generating trajectory for mobile robots
planner_base.h
Go to the documentation of this file.
1 
8 #ifndef MPL_PLANNER_BASE_H
9 #define MPL_PLANNER_BASE_H
10 
13 
14 namespace MPL {
18 template <int Dim, typename Coord>
19 class PlannerBase {
20  public:
22  PlannerBase(bool verbose = false) : planner_verbose_(verbose) {}
23 
25  bool initialized() { return !(ss_ptr_ == nullptr); }
26 
28  Trajectory<Dim> getTraj() const { return traj_; }
32  for (const auto &it : ss_ptr_->hm_) {
33  if (it.second && !it.second->pred_coord.empty()) {
34  for (unsigned int i = 0; i < it.second->pred_coord.size(); i++) {
35  Coord key = it.second->pred_coord[i];
36  // if(!ss_ptr_->hm_[key] ||
37  // std::isinf(it.second->pred_action_cost[i]))
38  if (std::isinf(it.second->pred_action_cost[i])) continue;
39  Primitive<Dim> pr;
40  ENV_->forward_action(ss_ptr_->hm_[key]->coord,
41  it.second->pred_action_id[i], pr);
42  prs.push_back(pr);
43  }
44  }
45  }
46 
47  if (planner_verbose_)
48  printf("number of states in hm: %zu, number of valid prs: %zu\n",
49  ss_ptr_->hm_.size(), prs.size());
50 
51  return prs;
52  }
53 
57  for (const auto &it : ss_ptr_->hm_) {
58  if (it.second && !it.second->pred_coord.empty()) {
59  for (unsigned int i = 0; i < it.second->pred_coord.size(); i++) {
60  Coord key = it.second->pred_coord[i];
61  Primitive<Dim> pr;
62  ENV_->forward_action(key, it.second->pred_action_id[i], pr);
63  prs.push_back(pr);
64  }
65  }
66  }
67 
68  if (planner_verbose_)
69  printf(
70  "getAllPrimitives number of states in hm: %zu, number of prs: %zu\n",
71  ss_ptr_->hm_.size(), prs.size());
72 
73  return prs;
74  }
75 
78  vec_Vecf<Dim> ps;
79  for (const auto &it : ss_ptr_->pq_) ps.push_back(it.second->coord.pos);
80  return ps;
81  }
82 
85  vec_Vecf<Dim> ps;
86  for (const auto &it : ss_ptr_->hm_) {
87  if (it.second && it.second->iterationclosed)
88  ps.push_back(it.second->coord.pos);
89  }
90  return ps;
91  }
92 
95  vec_Vecf<Dim> ps;
96  for (const auto &it : ss_ptr_->hm_) {
97  if (it.second && !it.second->iterationopened)
98  ps.push_back(it.second->coord.pos);
99  }
100  return ps;
101  }
102 
104  vec_Vecf<Dim> getStates(const Coord &state) const {
105  vec_Vecf<Dim> ps;
106  vec_Vecf<Dim> vels;
107  vec_Vecf<Dim> accs;
108  for (const auto &it : ss_ptr_->hm_) {
109  if (it.second) {
110  auto coord = it.second->coord;
111  bool add = true;
112  if (state.use_vel && (state.vel - coord.vel).norm() > 1e-3) add = false;
113  if (state.use_acc && (state.acc - coord.acc).norm() > 1e-3) add = false;
114  if (state.use_jrk && (state.jrk - coord.jrk).norm() > 1e-3) add = false;
115  if (add) {
116  // std::cout << "add pos: " << coord.pos.transpose() <<
117  // " vel: " << coord.vel.transpose() <<
118  // " acc: " << coord.acc.transpose() << std::endl;
119  ps.push_back(coord.pos);
120  bool new_vel = true;
121  for (const auto &it : vels) {
122  if ((it - coord.vel).norm() < 1e-3) {
123  new_vel = false;
124  break;
125  }
126  }
127 
128  if (new_vel) vels.push_back(coord.vel);
129  }
130  }
131  }
132 
133  for (const auto &it : vels)
134  std::cout << "vel: " << it.transpose() << std::endl;
135  std::cout << "=========================" << std::endl;
136  return ps;
137  }
138 
140  vec_Vecf<Dim> getExpandedNodes() const { return ENV_->expanded_nodes_; }
141 
144  return ENV_->expanded_edges_;
145  }
146 
148  int getExpandedNum() const { return ss_ptr_->expand_iteration_; }
149 
155  void getSubStateSpace(int time_step) { ss_ptr_->getSubStateSpace(time_step); }
156 
158  decimal_t getTrajCost() const { return traj_cost_; }
159 
161  void checkValidation() { ss_ptr_->checkValidation(ss_ptr_->hm_); }
162 
164  void reset() {
165  ss_ptr_ = nullptr;
167  }
168 
170  void setLPAstar(bool use_lpastar) {
171  use_lpastar_ = use_lpastar;
172  if (use_lpastar_)
173  printf("[PlannerBase] use Lifelong Planning A*\n");
174  else
175  printf("[PlannerBase] use normal A*\n");
176  }
177 
179  void setVmax(decimal_t v) {
180  ENV_->set_v_max(v);
181  if (planner_verbose_) printf("[PlannerBase] set v_max: %f\n", v);
182  }
183 
185  void setAmax(decimal_t a) {
186  ENV_->set_a_max(a);
187  if (planner_verbose_) printf("[PlannerBase] set a_max: %f\n", a);
188  }
189 
191  void setJmax(decimal_t j) {
192  ENV_->set_j_max(j);
193  if (planner_verbose_) printf("[PlannerBase] set j_max: %f\n", j);
194  }
195 
197  void setYawmax(decimal_t yaw) {
198  ENV_->set_yaw_max(yaw);
199  if (planner_verbose_) printf("[PlannerBase] set yaw_max: %f\n", yaw);
200  }
201 
203  void setTmax(decimal_t t) {
204  ENV_->set_t_max(t);
205  if (planner_verbose_) printf("[PlannerBase] set max time: %f\n", t);
206  }
207 
209  void setDt(decimal_t dt) {
210  ENV_->set_dt(dt);
211  if (planner_verbose_) printf("[PlannerBase] set dt: %f\n", dt);
212  }
213 
215  void setW(decimal_t w) {
216  ENV_->set_w(w);
217  if (planner_verbose_) printf("[PlannerBase] set w: %f\n", w);
218  }
219 
221  void setWyaw(decimal_t w) {
222  ENV_->set_wyaw(w);
223  if (planner_verbose_) printf("[PlannerBase] set wyaw: %f\n", w);
224  }
225 
227  void setEpsilon(decimal_t eps) {
228  epsilon_ = eps;
229  if (planner_verbose_) printf("[PlannerBase] set epsilon: %f\n", epsilon_);
230  }
231 
233  void setHeurIgnoreDynamics(bool ignore) {
234  ENV_->set_heur_ignore_dynamics(ignore);
235  if (planner_verbose_)
236  printf("[PlannerBase] set heur_ignore_dynamics: %d\n", ignore);
237  }
238 
240  void setMaxNum(int num) {
241  max_num_ = num;
242  if (planner_verbose_) printf("[PlannerBase] set max num: %d\n", max_num_);
243  }
244 
246  void setU(const vec_E<VecDf> &U) { ENV_->set_u(U); }
247 
250  ENV_->set_prior_trajectory(traj);
251  if (planner_verbose_) printf("[PlannerBase] set prior trajectory\n");
252  }
253 
255  void setTol(decimal_t tol_pos, decimal_t tol_vel = -1,
256  decimal_t tol_acc = -1) {
257  ENV_->set_tol_pos(tol_pos);
258  ENV_->set_tol_vel(tol_vel);
259  ENV_->set_tol_acc(tol_acc);
260  if (planner_verbose_) {
261  printf("[PlannerBase] set tol_pos: %f\n", tol_pos);
262  printf("[PlannerBase] set tol_vel: %f\n", tol_vel);
263  printf("[PlannerBase] set tol_acc: %f\n", tol_acc);
264  }
265  }
266 
275  bool plan(const Coord &start, const Coord &goal) {
276  if (planner_verbose_) {
277  start.print("Start:");
278  goal.print("Goal:");
279 
280  ENV_->info();
281  }
282 
283  if (!ENV_->is_free(start.pos)) {
284  printf(ANSI_COLOR_RED "[PlannerBase] start is not free!" ANSI_COLOR_RESET
285  "\n");
286  return false;
287  }
288 
289  std::unique_ptr<MPL::GraphSearch<Dim, Coord>> planner_ptr(
291 
292  // If use A*, reset the state space
293  if (!use_lpastar_)
295  else {
296  // If use LPA*, reset the state space only at the initial planning
297  if (!initialized()) {
298  if (planner_verbose_)
299  printf(ANSI_COLOR_CYAN
300  "[PlannerBase] reset planner state space!" ANSI_COLOR_RESET
301  "\n");
303  }
304  }
305 
306  ENV_->set_goal(goal);
307 
308  ENV_->expanded_nodes_.clear();
309  ENV_->expanded_edges_.clear();
310 
311  ss_ptr_->dt_ = ENV_->get_dt();
312  if (use_lpastar_)
313  traj_cost_ = planner_ptr->LPAstar(start, ENV_, ss_ptr_, traj_, max_num_);
314  else
315  traj_cost_ = planner_ptr->Astar(start, ENV_, ss_ptr_, traj_, max_num_);
316 
317  if (std::isinf(traj_cost_)) {
318  if (planner_verbose_)
319  printf(ANSI_COLOR_RED
320  "[PlannerBase] Cannot find a traj!" ANSI_COLOR_RESET "\n");
321  return false;
322  }
323 
324  return true;
325  }
326 
327  protected:
329  std::shared_ptr<MPL::env_base<Dim>> ENV_;
331  std::shared_ptr<MPL::StateSpace<Dim, Coord>> ss_ptr_;
339  int max_num_ = -1;
341  bool use_lpastar_ = false;
344 };
345 
346 } // namespace MPL
347 
348 #endif
Trajectory< Dim > getTraj() const
Get optimal trajectory.
Definition: planner_base.h:28
void setMaxNum(int num)
Set max number of expansion.
Definition: planner_base.h:240
bool planner_verbose_
Enabled to display debug message.
Definition: planner_base.h:343
void setJmax(decimal_t j)
Set max jerk in each axis.
Definition: planner_base.h:191
bool use_lpastar_
Enable LPAstar for planning.
Definition: planner_base.h:341
vec_E< Primitive< Dim > > getExpandedEdges() const
Get expanded edges, for A* it should be the same as the close set.
Definition: planner_base.h:143
Definition: map_util.h:12
void reset()
Reset state space.
Definition: planner_base.h:164
std::shared_ptr< MPL::env_base< Dim > > ENV_
Environment class.
Definition: planner_base.h:329
std::shared_ptr< MPL::StateSpace< Dim, Coord > > ss_ptr_
Planner workspace.
Definition: planner_base.h:331
void getSubStateSpace(int time_step)
Prune state space.
Definition: planner_base.h:155
#define ANSI_COLOR_RED
Set red font in printf funtion.
Definition: data_type.h:16
bool initialized()
Check if the planner has been initialized.
Definition: planner_base.h:25
void setTmax(decimal_t t)
Set max time step to explore.
Definition: planner_base.h:203
vec_Vecf< Dim > getCloseSet() const
Get points in close set.
Definition: planner_base.h:84
backend of graph search, implemetation of A* and Lifelong Planning A*
GraphSearch class.
Definition: graph_search.h:20
environment base class
void setDt(decimal_t dt)
Set dt for each primitive.
Definition: planner_base.h:209
Trajectory class.
Definition: trajectory.h:43
void checkValidation()
Check tree validation.
Definition: planner_base.h:161
void setYawmax(decimal_t yaw)
Set max jerk in each axis.
Definition: planner_base.h:197
vec_Vecf< Dim > getStates(const Coord &state) const
Get points at certain state.
Definition: planner_base.h:104
vec_E< Primitive< Dim > > getValidPrimitives() const
Get expanded collision free primitives.
Definition: planner_base.h:30
bool plan(const Coord &start, const Coord &goal)
Planning thread.
Definition: planner_base.h:275
void setWyaw(decimal_t w)
Set weight for cost in time.
Definition: planner_base.h:221
#define ANSI_COLOR_CYAN
Set cyan font in printf funtion.
Definition: data_type.h:36
int max_num_
Maxmum number of expansion, -1 means no limitation.
Definition: planner_base.h:339
std::vector< T, Eigen::aligned_allocator< T > > vec_E
Pre-allocated std::vector for Eigen using vec_E.
Definition: data_type.h:53
decimal_t getTrajCost() const
Get trajectory total cost.
Definition: planner_base.h:158
vec_E< Vecf< N > > vec_Vecf
Vector of Eigen 1D float vector.
Definition: data_type.h:70
vec_Vecf< Dim > getNullSet() const
Get points neither in open nor close set.
Definition: planner_base.h:94
void setPriorTrajectory(const Trajectory< Dim > &traj)
Set prior trajectory.
Definition: planner_base.h:249
void setTol(decimal_t tol_pos, decimal_t tol_vel=-1, decimal_t tol_acc=-1)
Set tolerance in geometric and dynamic spaces.
Definition: planner_base.h:255
State space.
Definition: state_space.h:83
double decimal_t
Rename the float type used in lib.
Definition: data_type.h:49
void setW(decimal_t w)
Set weight for cost in time.
Definition: planner_base.h:215
decimal_t traj_cost_
Total cost of final trajectory.
Definition: planner_base.h:335
void setHeurIgnoreDynamics(bool ignore)
Calculate heuristic using dynamics.
Definition: planner_base.h:233
vec_Vecf< Dim > getExpandedNodes() const
Get expanded nodes, for A* it should be the same as the close set.
Definition: planner_base.h:140
#define ANSI_COLOR_RESET
Reset font color in printf funtion.
Definition: data_type.h:40
void setU(const vec_E< VecDf > &U)
Set U.
Definition: planner_base.h:246
void setVmax(decimal_t v)
Set max vel in each axis.
Definition: planner_base.h:179
void setLPAstar(bool use_lpastar)
Set max vel in each axis.
Definition: planner_base.h:170
Primitive class.
Definition: primitive.h:206
vec_Vecf< Dim > getOpenSet() const
Get points in open set.
Definition: planner_base.h:77
void setAmax(decimal_t a)
Set max acc in each axis.
Definition: planner_base.h:185
PlannerBase(bool verbose=false)
Simple constructor.
Definition: planner_base.h:22
decimal_t epsilon_
Greedy searching parameter.
Definition: planner_base.h:337
vec_E< Primitive< Dim > > getAllPrimitives() const
Get expanded primitives.
Definition: planner_base.h:55
Trajectory< Dim > traj_
Optimal trajectory.
Definition: planner_base.h:333
int getExpandedNum() const
Get number of expanded nodes.
Definition: planner_base.h:148
Motion planning base util class.
Definition: planner_base.h:19
void setEpsilon(decimal_t eps)
Set greedy searching param.
Definition: planner_base.h:227