MRSL Motion Primitive Library  1.2
A motion primitive library for generating trajectory for mobile robots
env_base.h
Go to the documentation of this file.
1 
6 #ifndef MPL_ENV_BASE_H
7 #define MPL_ENV_BASE_H
8 
9 #include <mpl_basis/trajectory.h>
10 
11 namespace MPL {
12 
16 template <int Dim>
17 class env_base {
18  public:
20  env_base() {}
21 
23  virtual bool is_goal(const Waypoint<Dim>& state) const {
24  if (state.t >= t_max_) return true;
25  bool goaled =
26  (state.pos - goal_node_.pos).template lpNorm<Eigen::Infinity>() <=
27  tol_pos_;
28  if (goaled && tol_vel_ >= 0)
29  goaled =
30  (state.vel - goal_node_.vel).template lpNorm<Eigen::Infinity>() <=
31  tol_vel_;
32  if (goaled && tol_acc_ >= 0)
33  goaled =
34  (state.acc - goal_node_.acc).template lpNorm<Eigen::Infinity>() <=
35  tol_acc_;
36  if (goaled && tol_yaw_ >= 0)
37  goaled = std::abs(state.yaw - goal_node_.yaw) <= tol_yaw_;
38  return goaled;
39  }
40 
46  virtual decimal_t get_heur(const Waypoint<Dim>& state) const {
47  if (goal_node_ == state) return 0;
48  size_t id = state.t / dt_;
49  if (!prior_traj_.empty() && id < prior_traj_.size())
50  return cal_heur(state, prior_traj_[id].first) + prior_traj_[id].second;
51  else
52  return cal_heur(state, goal_node_);
53  }
54 
56  virtual decimal_t cal_heur(const Waypoint<Dim>& state,
57  const Waypoint<Dim>& goal) const {
59  if (v_max_ > 0) {
60  return w_ * (state.pos - goal.pos).template lpNorm<Eigen::Infinity>() /
61  v_max_;
62  } else
63  return w_ * (state.pos - goal.pos).template lpNorm<Eigen::Infinity>();
64  }
65  // return 0;
66  // return w_*(state.pos - goal.pos).norm();
67  if (state.control == Control::JRK && goal.control == Control::JRK) {
68  const Vecf<Dim> dp = goal.pos - state.pos;
69  const Vecf<Dim> v0 = state.vel;
70  const Vecf<Dim> v1 = goal.vel;
71  const Vecf<Dim> a0 = state.acc;
72  const Vecf<Dim> a1 = goal.acc;
73  decimal_t a = w_;
74  decimal_t b = 0;
75  decimal_t c = -9 * a0.dot(a0) + 6 * a0.dot(a1) - 9 * a1.dot(a1);
76  decimal_t d = -144 * a0.dot(v0) - 96 * a0.dot(v1) + 96 * a1.dot(v0) +
77  144 * a1.dot(v1);
78  decimal_t e = 360 * (a0 - a1).dot(dp) - 576 * v0.dot(v0) -
79  1008 * v0.dot(v1) - 576 * v1.dot(v1);
80  decimal_t f = 2880 * dp.dot(v0 + v1);
81  decimal_t g = -3600 * dp.dot(dp);
82 
83  std::vector<decimal_t> ts = solve(a, b, c, d, e, f, g);
84 
85  decimal_t t_bar =
86  (state.pos - goal.pos).template lpNorm<Eigen::Infinity>() / v_max_;
87  ts.push_back(t_bar);
88  decimal_t min_cost = std::numeric_limits<decimal_t>::max();
89  for (auto t : ts) {
90  if (t < t_bar) continue;
91  decimal_t cost = a * t - c / t - d / 2 / t / t - e / 3 / t / t / t -
92  f / 4 / t / t / t / t - g / 5 / t / t / t / t / t;
93  if (cost < min_cost) min_cost = cost;
94  }
95  return min_cost;
96  }
97 
98  else if (state.control == Control::JRK && goal.control == Control::ACC) {
99  const Vecf<Dim> dp = goal.pos - state.pos;
100  const Vecf<Dim> v0 = state.vel;
101  const Vecf<Dim> v1 = goal.vel;
102  const Vecf<Dim> a0 = state.acc;
103 
104  decimal_t a = w_;
105  decimal_t b = 0;
106  decimal_t c = -8 * a0.dot(a0);
107  decimal_t d = -112 * a0.dot(v0) - 48 * a0.dot(v1);
108  decimal_t e = 240 * a0.dot(dp) - 384 * v0.dot(v0) - 432 * v0.dot(v1) -
109  144 * v1.dot(v1);
110  decimal_t f = dp.dot(1600 * v0 + 960 * v1);
111  decimal_t g = -1600 * dp.dot(dp);
112 
113  std::vector<decimal_t> ts = solve(a, b, c, d, e, f, g);
114 
115  decimal_t t_bar =
116  (state.pos - goal.pos).template lpNorm<Eigen::Infinity>() / v_max_;
117  ts.push_back(t_bar);
118  decimal_t min_cost = std::numeric_limits<decimal_t>::max();
119  for (auto t : ts) {
120  if (t < t_bar) continue;
121  decimal_t cost = a * t - c / t - d / 2 / t / t - e / 3 / t / t / t -
122  f / 4 / t / t / t / t - g / 5 / t / t / t / t / t;
123  if (cost < min_cost) min_cost = cost;
124  // printf("t: %f, cost: %f\n",t, cost);
125  }
126  return min_cost;
127  }
128 
129  else if (state.control == Control::JRK && goal.control == Control::VEL) {
130  const Vecf<Dim> dp = goal.pos - state.pos;
131  const Vecf<Dim> v0 = state.vel;
132  const Vecf<Dim> a0 = state.acc;
133 
134  decimal_t a = w_;
135  decimal_t b = 0;
136  decimal_t c = -5 * a0.dot(a0);
137  decimal_t d = -40 * a0.dot(v0);
138  decimal_t e = 60 * a0.dot(dp) - 60 * v0.dot(v0);
139  decimal_t f = 160 * dp.dot(v0);
140  decimal_t g = -100 * dp.dot(dp);
141 
142  std::vector<decimal_t> ts = solve(a, b, c, d, e, f, g);
143 
144  decimal_t t_bar =
145  (state.pos - goal.pos).template lpNorm<Eigen::Infinity>() / v_max_;
146  ts.push_back(t_bar);
147 
148  decimal_t min_cost = std::numeric_limits<decimal_t>::max();
149  for (auto t : ts) {
150  if (t < t_bar) continue;
151  decimal_t cost = a * t - c / t - d / 2 / t / t - e / 3 / t / t / t -
152  f / 4 / t / t / t / t - g / 5 / t / t / t / t / t;
153  if (cost < min_cost) min_cost = cost;
154  }
155  return min_cost;
156  }
157 
158  else if (state.control == Control::ACC && goal.control == Control::ACC) {
159  const Vecf<Dim> dp = goal.pos - state.pos;
160  const Vecf<Dim> v0 = state.vel;
161  const Vecf<Dim> v1 = goal.vel;
162 
163  decimal_t c1 = -36 * dp.dot(dp);
164  decimal_t c2 = 24 * (v0 + v1).dot(dp);
165  decimal_t c3 = -4 * (v0.dot(v0) + v0.dot(v1) + v1.dot(v1));
166  decimal_t c4 = 0;
167  decimal_t c5 = w_;
168 
169  std::vector<decimal_t> ts = quartic(c5, c4, c3, c2, c1);
170  decimal_t t_bar =
171  (state.pos - goal.pos).template lpNorm<Eigen::Infinity>() / v_max_;
172  ts.push_back(t_bar);
173 
174  decimal_t cost = std::numeric_limits<decimal_t>::max();
175  for (auto t : ts) {
176  if (t < t_bar) continue;
177  decimal_t c = -c1 / 3 / t / t / t - c2 / 2 / t / t - c3 / t + w_ * t;
178  if (c < cost) cost = c;
179  }
180 
181  return cost;
182  }
183 
184  else if (state.control == Control::ACC && goal.control == Control::VEL) {
185  const Vecf<Dim> dp = goal.pos - state.pos;
186  const Vecf<Dim> v0 = state.vel;
187 
188  decimal_t c1 = -9 * dp.dot(dp);
189  decimal_t c2 = 12 * v0.dot(dp);
190  decimal_t c3 = -3 * v0.dot(v0);
191  decimal_t c4 = 0;
192  decimal_t c5 = w_;
193 
194  std::vector<decimal_t> ts = quartic(c5, c4, c3, c2, c1);
195  decimal_t t_bar =
196  (state.pos - goal.pos).template lpNorm<Eigen::Infinity>() / v_max_;
197  ts.push_back(t_bar);
198 
199  decimal_t cost = std::numeric_limits<decimal_t>::max();
200  for (auto t : ts) {
201  if (t < t_bar) continue;
202  decimal_t c = -c1 / 3 / t / t / t - c2 / 2 / t / t - c3 / t + w_ * t;
203  if (c < cost) cost = c;
204  }
205 
206  return cost;
207  } else if (state.control == Control::VEL && goal.control == Control::VEL)
208  return (w_ + 1) * (state.pos - goal.pos).norm();
209  else
210  return w_ * (state.pos - goal.pos).norm() / v_max_;
211  }
212 
214  inline Veci<Dim> round(const Vecf<Dim>& vec, decimal_t res) const {
215  Veci<Dim> vecI;
216  for (int i = 0; i < Dim; i++) vecI(i) = std::round(vec(i) / res);
217  return vecI;
218  }
219 
221  std::string to_string(const Veci<Dim>& vec) const {
222  std::string str;
223  for (int i = 0; i < Dim; i++) str += std::to_string(vec(i)) + "-";
224  return str;
225  }
226 
228  void forward_action(const Waypoint<Dim>& curr, int action_id,
229  Primitive<Dim>& pr) const {
230  pr = Primitive<Dim>(curr, U_[action_id], dt_);
231  }
232 
234  void set_u(const vec_E<VecDf>& U) { U_ = U; }
235 
237  void set_v_max(decimal_t v) { v_max_ = v; }
238 
240  void set_a_max(decimal_t a) { a_max_ = a; }
241 
243  void set_j_max(decimal_t j) { j_max_ = j; }
244 
246  void set_yaw_max(decimal_t yaw) { yaw_max_ = yaw; }
247 
249  virtual void set_prior_trajectory(const Trajectory<Dim>& traj) {
250  prior_traj_.clear();
251  decimal_t total_time = traj.getTotalTime();
252  for (decimal_t t = 0; t < total_time; t += dt_) {
253  prior_traj_.push_back(
254  std::make_pair(traj.evaluate(t), w_ * (total_time - t)));
255  }
256  }
257 
259  void set_dt(decimal_t dt) { dt_ = dt; }
260 
262  void set_tol_pos(decimal_t pos) { tol_pos_ = pos; }
263 
265  void set_tol_vel(decimal_t vel) { tol_vel_ = vel; }
266 
268  void set_tol_acc(decimal_t acc) { tol_acc_ = acc; }
269 
271  void set_tol_yaw(decimal_t yaw) { tol_yaw_ = yaw; }
272 
274  void set_w(decimal_t w) { w_ = w; }
275 
277  void set_wyaw(decimal_t wyaw) { wyaw_ = wyaw; }
278 
280  virtual void set_potential_weight(decimal_t w) {}
281 
283  virtual void set_gradient_weight(decimal_t w) {}
284 
286  virtual void set_potential_map(const std::vector<int8_t>& map) {}
287 
289  virtual void set_gradient_map(const vec_E<Vecf<Dim>>& map) {}
290 
292  void set_t_max(int t) { t_max_ = t; }
293 
295  bool set_goal(const Waypoint<Dim>& state) {
296  if (prior_traj_.empty()) goal_node_ = state;
297  return prior_traj_.empty();
298  }
299 
301  void set_search_region(const std::vector<bool>& search_region) {
302  search_region_ = search_region;
303  }
304 
306  void set_heur_ignore_dynamics(bool ignore) { heur_ignore_dynamics_ = ignore; }
307 
309  virtual void info() {
310  printf(ANSI_COLOR_YELLOW "\n");
311  printf("++++++++++++++++++++ env_base ++++++++++++++++++\n");
312  printf("+ w: %.2f +\n", w_);
313  printf("+ wyaw: %.2f +\n", wyaw_);
314  printf("+ dt: %.2f +\n", dt_);
315  printf("+ t_max: %.2f +\n", t_max_);
316  printf("+ v_max: %.2f +\n", v_max_);
317  printf("+ a_max: %.2f +\n", a_max_);
318  printf("+ j_max: %.2f +\n", j_max_);
319  printf("+ yaw_max: %.2f +\n", yaw_max_);
320  printf("+ U num: %zu +\n", U_.size());
321  printf("+ tol_pos: %.2f +\n", tol_pos_);
322  printf("+ tol_vel: %.2f +\n", tol_vel_);
323  printf("+ tol_acc: %.2f +\n", tol_acc_);
324  printf("+ tol_yaw: %.2f +\n", tol_yaw_);
325  printf("+heur_ignore_dynamics: %d +\n",
327  printf("++++++++++++++++++++ env_base ++++++++++++++++++\n");
328  printf(ANSI_COLOR_RESET "\n");
329  }
330 
332  virtual bool is_free(const Vecf<Dim>& pt) const {
333  printf("Used Null is_free() for pt\n");
334  return true;
335  }
336 
338  virtual bool is_free(const Primitive<Dim>& pr) const {
339  printf("Used Null is_free() for pr\n");
340  return true;
341  }
342 
343  virtual decimal_t calculate_intrinsic_cost(const Primitive<Dim>& pr) const {
344  return pr.J(pr.control()) + w_ * dt_;
345  }
346 
348  decimal_t get_dt() const { return dt_; }
349 
358  virtual void get_succ(const Waypoint<Dim>& curr, vec_E<Waypoint<Dim>>& succ,
359  std::vector<decimal_t>& succ_cost,
360  std::vector<int>& action_idx) const {
361  printf("Used Null get_succ()\n");
362  }
363 
365  std::vector<bool> get_search_region() const { return search_region_; }
366 
370  decimal_t w_{10.0};
390  decimal_t t_max_{std::numeric_limits<decimal_t>::infinity()};
400  std::vector<bool> search_region_;
405 };
406 } // namespace MPL
407 
408 #endif
virtual void get_succ(const Waypoint< Dim > &curr, vec_E< Waypoint< Dim >> &succ, std::vector< decimal_t > &succ_cost, std::vector< int > &action_idx) const
Get successor.
Definition: env_base.h:358
virtual decimal_t cal_heur(const Waypoint< Dim > &state, const Waypoint< Dim > &goal) const
calculate the cost from state to goal
Definition: env_base.h:56
vec_E< std::pair< Waypoint< Dim >, decimal_t > > prior_traj_
Prior trajectory.
Definition: env_base.h:398
Waypoint< Dim > evaluate(decimal_t time) const
Evaluate Waypoint at time .
Definition: trajectory.h:67
std::vector< decimal_t > quartic(decimal_t a, decimal_t b, decimal_t c, decimal_t d, decimal_t e)
Quartic equation: .
Definition: math.h:69
std::vector< bool > search_region_
Valid search region (tunnel constraint)
Definition: env_base.h:400
decimal_t tol_vel_
tolerance of velocity for goal region, 0 means no tolerance
Definition: env_base.h:376
vec_E< Primitive< Dim > > expanded_edges_
expanded edges for debug
Definition: env_base.h:404
Definition: map_util.h:12
Trajectory class.
void forward_action(const Waypoint< Dim > &curr, int action_id, Primitive< Dim > &pr) const
Recover trajectory.
Definition: env_base.h:228
decimal_t wyaw_
weight of yaw
Definition: env_base.h:372
virtual void set_gradient_map(const vec_E< Vecf< Dim >> &map)
set weight for cost in time, usually no need to change
Definition: env_base.h:289
decimal_t J(const Control::Control &control) const
Return total efforts for the given duration.
Definition: primitive.h:403
virtual void set_potential_weight(decimal_t w)
set weight for cost in time, usually no need to change
Definition: env_base.h:280
decimal_t yaw
yaw
Definition: waypoint.h:36
void set_tol_vel(decimal_t vel)
Set velocity tolerance for goal region.
Definition: env_base.h:265
void set_yaw_max(decimal_t yaw)
Set max acc in each axis.
Definition: env_base.h:246
void set_search_region(const std::vector< bool > &search_region)
Set valid search region (tunnel constraint)
Definition: env_base.h:301
virtual decimal_t get_heur(const Waypoint< Dim > &state) const
Heuristic function.
Definition: env_base.h:46
Waypoint base class.
Definition: waypoint.h:23
virtual bool is_goal(const Waypoint< Dim > &state) const
Check if state hit the goal region, use L-1 norm.
Definition: env_base.h:23
bool set_goal(const Waypoint< Dim > &state)
Set goal state.
Definition: env_base.h:295
decimal_t getTotalTime() const
Get the total duration of Trajectory.
Definition: trajectory.h:300
vec_E< VecDf > U_
Array of constant control input.
Definition: env_base.h:394
decimal_t tol_acc_
tolerance of acceleration for goal region, 0 means no tolerance
Definition: env_base.h:378
Trajectory class.
Definition: trajectory.h:43
virtual void set_prior_trajectory(const Trajectory< Dim > &traj)
Set prior trajectory.
Definition: env_base.h:249
decimal_t tol_yaw_
tolerance of yaw for goal region, 0 means no tolerance
Definition: env_base.h:380
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
std::string to_string(const Veci< Dim > &vec) const
Convert a vec to a string.
Definition: env_base.h:221
void set_w(decimal_t w)
set weight for cost in time, usually no need to change
Definition: env_base.h:274
decimal_t yaw_max_
max yaw
Definition: env_base.h:388
Vecf< Dim > acc
acceleration in
Definition: waypoint.h:34
decimal_t get_dt() const
Retrieve dt.
Definition: env_base.h:348
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 v_max_
max velocity
Definition: env_base.h:382
Control::Control control
Control value.
Definition: waypoint.h:54
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
void set_a_max(decimal_t a)
Set max acc in each axis.
Definition: env_base.h:240
decimal_t t
time when reaching this waypoint in graph search
Definition: waypoint.h:37
void set_u(const vec_E< VecDf > &U)
Set control input.
Definition: env_base.h:234
decimal_t t_max_
max time
Definition: env_base.h:390
Eigen::Matrix< int, N, 1 > Veci
Eigen 1D int vector of size N.
Definition: data_type.h:59
std::vector< bool > get_search_region() const
Get the valid region.
Definition: env_base.h:365
double decimal_t
Rename the float type used in lib.
Definition: data_type.h:49
decimal_t dt_
duration of primitive
Definition: env_base.h:392
decimal_t j_max_
max jerk
Definition: env_base.h:386
vec_Vecf< Dim > expanded_nodes_
expanded nodes for debug
Definition: env_base.h:402
Veci< Dim > round(const Vecf< Dim > &vec, decimal_t res) const
Replace the original cast function.
Definition: env_base.h:214
Waypoint< Dim > goal_node_
Goal node.
Definition: env_base.h:396
virtual bool is_free(const Primitive< Dim > &pr) const
Check if a primitive is in free space.
Definition: env_base.h:338
virtual void set_potential_map(const std::vector< int8_t > &map)
set weight for cost in time, usually no need to change
Definition: env_base.h:286
#define ANSI_COLOR_RESET
Reset font color in printf funtion.
Definition: data_type.h:40
decimal_t a_max_
max acceleration
Definition: env_base.h:384
Eigen::Matrix< decimal_t, N, 1 > Vecf
Eigen 1D float vector of size N.
Definition: data_type.h:56
Base environment class.
Definition: env_base.h:17
virtual bool is_free(const Vecf< Dim > &pt) const
Check if a point is in free space.
Definition: env_base.h:332
decimal_t w_
weight of time cost
Definition: env_base.h:370
void set_dt(decimal_t dt)
Set dt for primitive.
Definition: env_base.h:259
bool heur_ignore_dynamics_
if enabled, ignore dynamics when calculate heuristic
Definition: env_base.h:368
void set_wyaw(decimal_t wyaw)
set weight for cost in yaw, usually no need to change
Definition: env_base.h:277
void set_tol_yaw(decimal_t yaw)
Set acceleration tolerance for goal region.
Definition: env_base.h:271
virtual void set_gradient_weight(decimal_t w)
set weight for cost in time, usually no need to change
Definition: env_base.h:283
void set_tol_pos(decimal_t pos)
Set distance tolerance for goal region.
Definition: env_base.h:262
void set_t_max(int t)
Set max time.
Definition: env_base.h:292
Primitive class.
Definition: primitive.h:206
env_base()
Simple constructor.
Definition: env_base.h:20
#define ANSI_COLOR_YELLOW
Set yellow font in printf funtion.
Definition: data_type.h:24
void set_heur_ignore_dynamics(bool ignore)
Set heur_ignore_dynamics.
Definition: env_base.h:306
virtual void info()
Print out params.
Definition: env_base.h:309
Vecf< Dim > pos
position in
Definition: waypoint.h:32
Control::Control control() const
Get the control indicator.
Definition: primitive.h:339
void set_j_max(decimal_t j)
Set max acc in each axis.
Definition: env_base.h:243
decimal_t tol_pos_
tolerance of position for goal region, 0.5 is the default
Definition: env_base.h:374
void set_v_max(decimal_t v)
Set max vel in each axis.
Definition: env_base.h:237
void set_tol_acc(decimal_t acc)
Set acceleration tolerance for goal region.
Definition: env_base.h:268