Skip to content

Commit ad4f1ec

Browse files
committed
Fix cost critic dead code
Signed-off-by: mini-1235 <mauricepurnawan@gmail.com>
1 parent 9f0af3b commit ad4f1ec

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

nav2_mppi_controller/include/nav2_mppi_controller/critics/cost_critic.hpp

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,35 +50,10 @@ class CostCritic : public CriticFunction
5050
protected:
5151
/**
5252
* @brief Checks if cost represents a collision
53-
* @param cost Point cost at pose center
54-
* @param x X of pose
55-
* @param y Y of pose
56-
* @param theta theta of pose
53+
* @param cost Footprint cost if considering footprint, otherwise point cost
5754
* @return bool if in collision
5855
*/
59-
inline bool inCollision(float cost, float x, float y, float theta)
60-
{
61-
// If consider_footprint_ check footprint scort for collision
62-
float score_cost = cost;
63-
if (consider_footprint_ &&
64-
(cost >= possible_collision_cost_ || possible_collision_cost_ < 1.0f))
65-
{
66-
score_cost = static_cast<float>(collision_checker_.footprintCostAtPose(
67-
static_cast<double>(x), static_cast<double>(y), static_cast<double>(theta),
68-
costmap_ros_->getRobotFootprint()));
69-
}
70-
71-
switch (static_cast<unsigned char>(score_cost)) {
72-
case (nav2_costmap_2d::LETHAL_OBSTACLE):
73-
return true;
74-
case (nav2_costmap_2d::INSCRIBED_INFLATED_OBSTACLE):
75-
return consider_footprint_ ? false : true;
76-
case (nav2_costmap_2d::NO_INFORMATION):
77-
return is_tracking_unknown_ ? false : true;
78-
}
79-
80-
return false;
81-
}
56+
inline bool inCollision(float cost) const;
8257

8358
/**
8459
* @brief Find the min cost of the inflation decay function for which the robot MAY be

nav2_mppi_controller/src/critics/cost_critic.cpp

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@ float CostCritic::findCircumscribedCost(
9595
return circumscribed_cost_;
9696
}
9797

98+
bool CostCritic::inCollision(float cost) const
99+
{
100+
bool is_tracking_unknown =
101+
costmap_ros_->getLayeredCostmap()->isTrackingUnknown();
102+
103+
using namespace nav2_costmap_2d; // NOLINT
104+
switch (static_cast<unsigned char>(cost)) {
105+
case (LETHAL_OBSTACLE):
106+
return true;
107+
case (INSCRIBED_INFLATED_OBSTACLE):
108+
return consider_footprint_ ? false : true;
109+
case (NO_INFORMATION):
110+
return is_tracking_unknown ? false : true;
111+
}
112+
113+
return false;
114+
}
115+
98116
void CostCritic::score(CriticData & data)
99117
{
100118
if (!enabled_) {
@@ -153,27 +171,36 @@ void CostCritic::score(CriticData & data)
153171
// The footprintCostAtPose will always return "INSCRIBED" if footprint is over it
154172
// So the center point has more information than the footprint
155173
if (!worldToMapFloat(Tx, Ty, x_i, y_i)) {
156-
if (!is_tracking_unknown_) {
157-
traj_cost = collision_cost_;
158-
trajectory_collide = true;
159-
break;
160-
}
161-
pose_cost = 255.0f; // NO_INFORMATION in float
174+
pose_cost = nav2_costmap_2d::NO_INFORMATION;
162175
} else {
176+
// Get center cost
163177
pose_cost = static_cast<float>(costmap->getCost(getIndex(x_i, y_i)));
164-
if (pose_cost < 1.0f) {
165-
continue; // In free space
166-
}
167-
if (inCollision(pose_cost, Tx, Ty, traj_yaw(i, j))) {
168-
traj_cost = collision_cost_;
178+
// Skip footprint cost if point cost has already exceeded inscribed obstacle cost
179+
if (pose_cost >= nav2_costmap_2d::INSCRIBED_INFLATED_OBSTACLE) {
169180
trajectory_collide = true;
170181
break;
171182
}
183+
// Get footprint cost
184+
if (consider_footprint_ &&
185+
(pose_cost >= possible_collision_cost_ || possible_collision_cost_ < 1.0f))
186+
{
187+
pose_cost = static_cast<float>(collision_checker_.footprintCostAtPose(
188+
static_cast<double>(Tx), static_cast<double>(Ty),
189+
static_cast<double>(traj_yaw(i, j)),
190+
costmap_ros_->getRobotFootprint()));
191+
}
192+
}
193+
if (pose_cost < 1.0f) {
194+
continue; // In free space
195+
}
196+
// Check if the pose is in collision
197+
if(inCollision(pose_cost)) {
198+
trajectory_collide = true;
199+
break;
172200
}
173-
174201
// Let near-collision trajectory points be punished severely
175-
// Note that we collision check based on the footprint actual,
176-
// but score based on the center-point cost regardless
202+
// This will apply when footprint steps in the inscribed space
203+
// and the center point is not in collision
177204
if (pose_cost >= 253.0f /*INSCRIBED_INFLATED_OBSTACLE in float*/) {
178205
traj_cost += critical_cost_;
179206
} else if (!near_goal) { // Generally prefer trajectories further from obstacles
@@ -183,6 +210,8 @@ void CostCritic::score(CriticData & data)
183210

184211
if (!trajectory_collide) {
185212
all_trajectories_collide = false;
213+
} else {
214+
traj_cost = collision_cost_;
186215
}
187216
}
188217

0 commit comments

Comments
 (0)