base_obstacle.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
37 #include <nav_core2/exceptions.h>
39 
41 
42 namespace dwb_critics
43 {
44 
45 void BaseObstacleCritic::onInit()
46 {
47  critic_nh_.param("sum_scores", sum_scores_, false);
48 }
49 
50 double BaseObstacleCritic::scoreTrajectory(const dwb_msgs::Trajectory2D& traj)
51 {
52  const nav_core2::Costmap& costmap = *costmap_;
53  double score = 0.0;
54  for (unsigned int i = 0; i < traj.poses.size(); ++i)
55  {
56  double pose_score = scorePose(costmap, traj.poses[i]);
57  // Optimized/branchless version of if (sum_scores_) score += pose_score, else score = pose_score;
58  score = static_cast<double>(sum_scores_) * score + pose_score;
59  }
60  return score;
61 }
62 
63 double BaseObstacleCritic::scorePose(const nav_core2::Costmap& costmap, const geometry_msgs::Pose2D& pose)
64 {
65  unsigned int cell_x, cell_y;
66  if (!worldToGridBounded(costmap.getInfo(), pose.x, pose.y, cell_x, cell_y))
67  throw nav_core2::IllegalTrajectoryException(name_, "Trajectory Goes Off Grid.");
68  unsigned char cost = costmap(cell_x, cell_y);
69  if (!isValidCost(cost))
70  throw nav_core2::IllegalTrajectoryException(name_, "Trajectory Hits Obstacle.");
71  return cost;
72 }
73 
74 bool BaseObstacleCritic::isValidCost(const unsigned char cost)
75 {
76  return cost != costmap_->LETHAL_OBSTACLE &&
77  cost != costmap_->INSCRIBED_INFLATED_OBSTACLE &&
78  cost != costmap_->NO_INFORMATION;
79 }
80 
81 void BaseObstacleCritic::addCriticVisualization(sensor_msgs::PointCloud& pc)
82 {
83  sensor_msgs::ChannelFloat32 grid_scores;
84  grid_scores.name = name_;
85 
86  const nav_core2::Costmap& costmap = *costmap_;
87  unsigned int size_x = costmap.getWidth();
88  unsigned int size_y = costmap.getHeight();
89  grid_scores.values.resize(size_x * size_y);
90  unsigned int i = 0;
91  for (unsigned int cy = 0; cy < size_y; cy++)
92  {
93  for (unsigned int cx = 0; cx < size_x; cx++)
94  {
95  grid_scores.values[i] = costmap(cx, cy);
96  i++;
97  }
98  }
99  pc.channels.push_back(grid_scores);
100 }
101 
102 } // namespace dwb_critics
nav_core2::BasicCostmap costmap
Uses costmap to assign negative costs if a circular robot would collide at any point of the trajector...
Definition: base_obstacle.h:53
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
bool worldToGridBounded(const NavGridInfo &info, double wx, double wy, unsigned int &mx, unsigned int &my)


dwb_critics
Author(s): David V. Lu!!
autogenerated on Sun Jan 10 2021 04:08:44