MRSL Motion Primitive Library  1.2
A motion primitive library for generating trajectory for mobile robots
env_map.h
Go to the documentation of this file.
1 
6 #ifndef MPL_ENV_MAP_H
7 #define MPL_ENV_MAP_H
10 
11 #include <memory>
12 #include <unordered_map>
13 
14 namespace MPL {
18 template <int Dim>
19 class env_map : public env_base<Dim> {
20  public:
22  env_map(std::shared_ptr<MapUtil<Dim>> map_util) : map_util_(map_util) {}
23 
25  bool is_goal(const Waypoint<Dim> &state) const {
26  bool goaled =
27  (state.pos - this->goal_node_.pos).template lpNorm<Eigen::Infinity>() <=
28  this->tol_pos_;
29 
30  if (goaled && this->tol_vel_ >= 0)
31  goaled = (state.vel - this->goal_node_.vel)
32  .template lpNorm<Eigen::Infinity>() <= this->tol_vel_;
33  if (goaled && this->tol_acc_ >= 0)
34  goaled = (state.acc - this->goal_node_.acc)
35  .template lpNorm<Eigen::Infinity>() <= this->tol_acc_;
36  if (goaled && this->tol_yaw_ >= 0)
37  goaled = std::abs(state.yaw - this->goal_node_.yaw) <= this->tol_yaw_;
38  if (goaled) {
39  auto pns = map_util_->rayTrace(state.pos, this->goal_node_.pos);
40  for (const auto &it : pns) {
41  if (map_util_->isOccupied(it)) return false;
42  }
43  }
44  return goaled;
45  }
46 
48  bool is_free(const Vecf<Dim> &pt) const {
49  const auto pn = map_util_->floatToInt(pt);
50  return map_util_->isFree(pn);
51  }
52 
60  bool is_free(const Primitive<Dim> &pr) const {
61  decimal_t max_v = 0;
62  for (int i = 0; i < Dim; i++) {
63  if (pr.max_vel(i) > max_v) max_v = pr.max_vel(i);
64  }
65  int n = std::ceil(max_v * pr.t() / map_util_->getRes());
66  vec_E<Waypoint<Dim>> pts = pr.sample(n);
67  for (const auto &pt : pts) {
68  Veci<Dim> pn = map_util_->floatToInt(pt.pos);
69  if (map_util_->isOccupied(pn) || map_util_->isOutside(pn)) return false;
70  if (!this->search_region_.empty() &&
71  !this->search_region_[map_util_->getIndex(pn)])
72  return false;
73  }
74 
75  return true;
76  }
77 
91  decimal_t max_v = 0;
92  for (int i = 0; i < Dim; i++) {
93  if (pr.max_vel(i) > max_v) max_v = pr.max_vel(i);
94  }
95  int n = std::max(5, (int)std::ceil(max_v * pr.t() / map_util_->getRes()));
96  decimal_t c = 0;
97 
98  decimal_t dt = pr.t() / n;
99  for (decimal_t t = 0; t < pr.t(); t += dt) {
100  const auto pt = pr.evaluate(t);
101  const Veci<Dim> pn = map_util_->floatToInt(pt.pos);
102  const int idx = map_util_->getIndex(pn);
103 
104  if (map_util_->isOutside(pn) ||
105  (!this->search_region_.empty() && !this->search_region_[idx]))
106  return std::numeric_limits<decimal_t>::infinity();
107  /*
108  decimal_t v_value = gradient_map_[idx].dot(pt.vel);
109  if(v_value > 0)
110  v_value = 0;
111  v_value = -v_value;
112  */
113  if (!potential_map_.empty()) {
114  if (potential_map_[idx] < 100 && potential_map_[idx] > 0) {
115  c += dt * (potential_weight_ * potential_map_[idx] +
116  gradient_weight_ * pt.vel.norm());
117  } else if (potential_map_[idx] >= 100)
118  return std::numeric_limits<decimal_t>::infinity();
119  } else if (map_util_->isOccupied(pn))
120  return std::numeric_limits<decimal_t>::infinity();
121  if (this->wyaw_ > 0 && pt.use_yaw) {
122  const auto v = pt.vel.template topRows<2>();
123  if (v.norm() > 1e-5) { // if v is not zero
124  decimal_t v_value =
125  1 - v.normalized().dot(Vec2f(cos(pt.yaw), sin(pt.yaw)));
126  c += this->wyaw_ * v_value * dt;
127  }
128  }
129  }
130 
131  return c;
132  }
133 
147  void get_succ(const Waypoint<Dim> &curr, vec_E<Waypoint<Dim>> &succ,
148  std::vector<decimal_t> &succ_cost,
149  std::vector<int> &action_idx) const {
150  succ.clear();
151  succ_cost.clear();
152  action_idx.clear();
153 
154  this->expanded_nodes_.push_back(curr.pos);
155  for (unsigned int i = 0; i < this->U_.size(); i++) {
156  Primitive<Dim> pr(curr, this->U_[i], this->dt_);
157  Waypoint<Dim> tn = pr.evaluate(this->dt_);
158  if (tn == curr || !validate_primitive(pr, this->v_max_, this->a_max_,
159  this->j_max_, this->yaw_max_))
160  continue;
161  tn.t = curr.t + this->dt_;
162  succ.push_back(tn);
163  decimal_t cost = curr.pos == tn.pos ? 0 : traverse_primitive(pr);
164  if (!std::isinf(cost)) {
165  cost += this->calculate_intrinsic_cost(pr);
166  this->expanded_edges_.push_back(pr);
167  }
168 
169  succ_cost.push_back(cost);
170  action_idx.push_back(i);
171  }
172  }
173 
175  void set_gradient_map(const vec_E<Vecf<Dim>> &map) { gradient_map_ = map; }
176 
179 
181  void set_potential_map(const std::vector<int8_t> &map) {
182  potential_map_ = map;
183  }
184 
187 
190  this->prior_traj_.clear();
191  decimal_t total_time = traj.getTotalTime();
192  const int n = std::ceil(this->v_max_ * total_time / map_util_->getRes());
193  const auto pts = traj.sample(n);
194 
195  std::vector<decimal_t> costs;
196  for (decimal_t t = 0; t < total_time; t += this->dt_) {
197  decimal_t potential_cost = 0;
198  if (!potential_map_.empty()) {
199  int prev_idx = -1;
200  for (const auto &pt : pts) {
201  if (pt.t >= t) break;
202  const Veci<Dim> pn = map_util_->floatToInt(pt.pos);
203  const int idx = map_util_->getIndex(pn);
204  if (prev_idx == idx)
205  continue;
206  else
207  prev_idx = idx;
208  potential_cost += potential_weight_ * potential_map_[idx] +
209  gradient_weight_ * pt.vel.norm();
210  }
211  }
212  costs.push_back(this->w_ * t + potential_cost);
213  printf("t: %.2f, cost: %f\n", t, costs.back());
214  }
215 
216  decimal_t total_cost = traverse_trajectory(traj) + this->w_ * total_time;
217  printf("total cost: %f\n", total_cost);
218 
219  for (decimal_t t = 0; t < total_time; t += this->dt_) {
220  int id = t / this->dt_;
221  this->prior_traj_.push_back(
222  std::make_pair(traj.evaluate(t), total_cost - costs[id]));
223  }
224 
225  this->goal_node_ = traj.evaluate(total_time); // set goal
226  }
227 
230  decimal_t total_time = traj.getTotalTime();
231  int n = std::ceil(this->v_max_ * total_time / map_util_->getRes());
232  decimal_t c = 0;
233  const auto pts = traj.sample(n);
234  int prev_idx = -1;
235  for (const auto &pt : pts) {
236  const Veci<Dim> pn = map_util_->floatToInt(pt.pos);
237  const int idx = map_util_->getIndex(pn);
238  if (prev_idx == idx)
239  continue;
240  else
241  prev_idx = idx;
242  if (map_util_->isOutside(pn))
243  return std::numeric_limits<decimal_t>::infinity();
244  if (!potential_map_.empty()) {
245  if (potential_map_[idx] < 100 && potential_map_[idx] > 0) {
246  c += potential_weight_ * potential_map_[idx] +
247  gradient_weight_ * pt.vel.norm();
248  } else if (potential_map_[idx] >= 100)
249  return std::numeric_limits<decimal_t>::infinity();
250  } else if (map_util_->isOccupied(pn))
251  return std::numeric_limits<decimal_t>::infinity();
252  }
253 
254  return c;
255  }
256 
258  void info() {
259  printf("++++++++++++++++++++ env_map ++++++++++++++++++\n");
260  printf("+ w: %.2f +\n", this->w_);
261  printf("+ wyaw: %.2f +\n", this->wyaw_);
262  printf("+ dt: %.2f +\n", this->dt_);
263  printf("+ t_max: %.2f +\n", this->t_max_);
264  printf("+ v_max: %.2f +\n", this->v_max_);
265  printf("+ a_max: %.2f +\n", this->a_max_);
266  printf("+ j_max: %.2f +\n", this->j_max_);
267  printf("+ yaw_max: %.2f +\n", this->yaw_max_);
268  printf("+ U num: %zu +\n", this->U_.size());
269  printf("+ tol_pos: %.2f +\n", this->tol_pos_);
270  printf("+ tol_vel: %.2f +\n", this->tol_vel_);
271  printf("+ tol_acc: %.2f +\n", this->tol_acc_);
272  printf("+ tol_yaw: %.2f +\n", this->tol_yaw_);
273  printf("+heur_ignore_dynamics: %d +\n",
274  this->heur_ignore_dynamics_);
275  if (!potential_map_.empty())
276  printf("+ potential_weight: %.2f +\n",
278  if (!gradient_map_.empty())
279  printf("+ gradient_weight: %.2f +\n",
281  if (!this->prior_traj_.empty())
282  printf("+ use_prior_traj: true +\n");
283  printf("++++++++++++++++++++ env_map ++++++++++++++++++\n");
284  }
285 
286  protected:
288  std::shared_ptr<MapUtil<Dim>> map_util_;
290  std::vector<int8_t> potential_map_;
297 };
298 } // namespace MPL
299 
300 #endif
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< 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
decimal_t wyaw_
weight of yaw
Definition: env_base.h:372
decimal_t yaw
yaw
Definition: waypoint.h:36
void set_gradient_weight(decimal_t w)
Set gradient weight.
Definition: env_map.h:178
decimal_t traverse_trajectory(const Trajectory< Dim > &traj) const
traverse trajectory
Definition: env_map.h:229
std::vector< int8_t > potential_map_
Potential map, optional.
Definition: env_map.h:290
Waypoint base class.
Definition: waypoint.h:23
decimal_t potential_weight_
Weight of potential value.
Definition: env_map.h:294
decimal_t getTotalTime() const
Get the total duration of Trajectory.
Definition: trajectory.h:300
void set_potential_weight(decimal_t w)
Set potential weight.
Definition: env_map.h:186
environment base class
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
decimal_t tol_yaw_
tolerance of yaw for goal region, 0 means no tolerance
Definition: env_base.h:380
void set_gradient_map(const vec_E< Vecf< Dim >> &map)
Set gradient map.
Definition: env_map.h:175
bool is_goal(const Waypoint< Dim > &state) const
Check if state hit the goal region, use L-1 norm.
Definition: env_map.h:25
vec_E< Vecf< Dim > > gradient_map_
Gradient map, optional.
Definition: env_map.h:292
decimal_t traverse_primitive(const Primitive< Dim > &pr) const
Accumulate the cost along the primitive.
Definition: env_map.h:90
MapUtil classes.
Waypoint< Dim > evaluate(decimal_t t) const
Return Waypoint at time .
Definition: primitive.h:321
decimal_t yaw_max_
max yaw
Definition: env_base.h:388
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
decimal_t v_max_
max velocity
Definition: env_base.h:382
Vecf< Dim > vel
velocity in
Definition: waypoint.h:33
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_map.h:147
decimal_t gradient_weight_
Weight of gradient value.
Definition: env_map.h:296
decimal_t t
time when reaching this waypoint in graph search
Definition: waypoint.h:37
decimal_t t() const
Return duration .
Definition: primitive.h:336
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
void info()
Print out params.
Definition: env_map.h:258
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
void set_prior_trajectory(const Trajectory< Dim > &traj)
Set prior trajectory.
Definition: env_map.h:189
vec_Vecf< Dim > expanded_nodes_
expanded nodes for debug
Definition: env_base.h:402
vec_E< Command< Dim > > sample(int N) const
Sample N+1 Command along trajectory using uniformed time.
Definition: trajectory.h:230
bool is_free(const Primitive< Dim > &pr) const
Check if the primitive is in free space.
Definition: env_map.h:60
Vecf< 2 > Vec2f
Eigen 1D float vector of size 2.
Definition: data_type.h:76
Waypoint< Dim > goal_node_
Goal node.
Definition: env_base.h:396
decimal_t max_vel(int k) const
Return max velocity along one axis.
Definition: primitive.h:353
decimal_t a_max_
max acceleration
Definition: env_base.h:384
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
Base environment class.
Definition: env_base.h:17
std::shared_ptr< MapUtil< Dim > > map_util_
Collision checking util.
Definition: env_map.h:288
decimal_t w_
weight of time cost
Definition: env_base.h:370
bool heur_ignore_dynamics_
if enabled, ignore dynamics when calculate heuristic
Definition: env_base.h:368
vec_E< Waypoint< Dim > > sample(int N) const
Sample N+1 Waypoints using uniformed time.
Definition: primitive.h:415
Voxel map environment.
Definition: env_map.h:19
env_map(std::shared_ptr< MapUtil< Dim >> map_util)
Constructor with map util as input.
Definition: env_map.h:22
Primitive class.
Definition: primitive.h:206
Definition: map_util.h:20
Vecf< Dim > pos
position in
Definition: waypoint.h:32
decimal_t tol_pos_
tolerance of position for goal region, 0.5 is the default
Definition: env_base.h:374
bool is_free(const Vecf< Dim > &pt) const
Check if a point is in free space.
Definition: env_map.h:48
void set_potential_map(const std::vector< int8_t > &map)
Set potential map.
Definition: env_map.h:181