Skip to content

Commit 5111f98

Browse files
SteveMacenskiBriceRenaudeauGoesMGoesMfacontidavide
authored
New MPPI Cost Critic (Contrib: Brice Renaudeau) (#4090)
* Share code Signed-off-by: Brice <brice.renaudeau@gmail.com> * Update inflation_cost_critic.hpp - copyright - ifndef Signed-off-by: Brice <brice.renaudeau@gmail.com> * fix lint cpp - extra space Signed-off-by: Brice <brice.renaudeau@gmail.com> * Fix Smac Planner confined collision checker (#4055) * Update collision_checker.cpp Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> * Fix tests Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> * Update test_a_star.cpp Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> --------- Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> Signed-off-by: Brice <brice.renaudeau@gmail.com> * Prevent analytic expansions from shortcutting Smac Planner feasible paths (#3962) * a potential solution to smac shortcutting * costmap reoslution * some fixes * completed prototype * some fixes for collision detection and performance * completing shortcutting fix * updating tests * adding readme --------- Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> Signed-off-by: Brice <brice.renaudeau@gmail.com> * change pointer free order in amcl to avoid use-after-free bug mentioned in #4068 (#4070) Signed-off-by: GoesM <GoesM@buaa.edu.cn> Co-authored-by: GoesM <GoesM@buaa.edu.cn> Signed-off-by: Brice <brice.renaudeau@gmail.com> * [Smac Planner] Massive Improvement of Behavior for SE2 Footprint Checking (ie non-circular robots) In Confined Settings (#4067) * prototype to test SE2 footprint H improvements * some fixes * fixed * invert logic * Working final prototype to be tested * complete unit test conversions * Update inflation_layer.hpp Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> --------- Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> Signed-off-by: Brice <brice.renaudeau@gmail.com> * Adding new Smac paper to readme Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> Signed-off-by: Brice <brice.renaudeau@gmail.com> * Update README.md Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> Signed-off-by: Brice <brice.renaudeau@gmail.com> * [behavior_tree] don't repeat yourself in "blackboard->set" (#4074) * don't repeat yourself: templates in tests Signed-off-by: Davide Faconti <davide.faconti@gmail.com> * misse change Signed-off-by: Davide Faconti <davide.faconti@gmail.com> --------- Signed-off-by: Davide Faconti <davide.faconti@gmail.com> Signed-off-by: Brice <brice.renaudeau@gmail.com> * Allow path end pose deviation revive (#4065) * Support stitching paths in compute path to poses * Update nav2_planner/src/planner_server.cpp Co-authored-by: Steve Macenski <stevenmacenski@gmail.com> * Rename parameter to allow_path_through_poses_goal_deviation * Fix description * restore nav2_params * missing whitespace * lint fix * removed parameter Signed-off-by: gg <josho.wallace@gmail.com> * Update planner_server.hpp * Update planner_server.cpp --------- Signed-off-by: gg <josho.wallace@gmail.com> Co-authored-by: pepisg <pedro.gonzalez@eia.edu.co> Co-authored-by: Pedro Alejandro González <71234974+pepisg@users.noreply.github.com> Co-authored-by: Steve Macenski <stevenmacenski@gmail.com> Signed-off-by: Brice <brice.renaudeau@gmail.com> * Updated code to use getInflationLayer() method (#4076) * updated code to use getInflationLayer method Signed-off-by: Jose Faria <20467009+jncfa@users.noreply.github.com> * Fix linting Signed-off-by: Jose Faria <20467009+jncfa@users.noreply.github.com> --------- Signed-off-by: Jose Faria <20467009+jncfa@users.noreply.github.com> Signed-off-by: Brice <brice.renaudeau@gmail.com> * 1594 twist stamped publisher (#4077) * Add TwistStamped to controller_server via TwistPublisher util * Add a new util class for publishing either Twist or TwistStamped * Add a new parameter for selecting to stamp the twist data * Consume TwistPublisher in nav2_controller Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Fix small issues * Unused variable * Incorrect doxygen Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Remove stored node and assert Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Add tests for node * Facing timeout even though it does the same thing as velocity smoother test Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Add missing spin call to solve timeout Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Fix copyright (me instead of intel) Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Add full test coverage with subscriber Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Remove unused rclcpp fixture * Can't use it due to needing to join the pub thread after rclcpp shuts down Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Use TwistStamped in nav2_behaviors Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Use TwistStamped in collision monitor node Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Add TwistStamped readme updates to velocity smoother Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Add TwistSubscriber implementation Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Fix syntax errors Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Use TwistSubscriber in test_velocity_smoother Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Use TwistSubscriber in assisted_teleop Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Use TwistSubscriber in collision monitor node Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Use TwistSubscriber in velocity smoother Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Remove unused code Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * add timestamp and frame_id to TwistStamped message * Add missing utility include Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Document TwistPublisher and TwistSubscriber usage Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Use pass-by-reference * Instead of std::move(std::unique_ptr<TwistStamped>) Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Finish twist subscriber tests Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Add other constructor and docs Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Fix linter issues Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Manually fix paren alignment Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Remove GSoC reference Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Document twist bool param in README Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Handle twistPublisher in collision monitor * Implement behavior in the stamped callback * Unstamped callback calls the stamped callback * Switch to unique pointer for publisher Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Convert to using TwistStamped interally * Use incoming twistStamped timestamp if available * Convert all internal representations to use TwistStamped Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Remove nav2_util usage instructions Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Remove unused Twist only subscriber Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * More linter fixes Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Prefer working with unique_ptr for cmd_vel * This makes it easier to switch to std::move instead of dereference on publish Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> * Completing twist stamped migration * shared to unique ptr Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> * twist add stamps and properly propogated * nav2_util: fix for compiling with clang - Resolve error: moving a temporary object prevents copy elision [-Werror,-Wpessimizing-move] Signed-off-by: Rhys Mainwaring <rhys.mainwaring@me.com> --------- Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> Signed-off-by: Rhys Mainwaring <rhys.mainwaring@me.com> Co-authored-by: pedro-fuoco <pedrofuoco6@gmail.com> Co-authored-by: Steve Macenski <stevenmacenski@gmail.com> Co-authored-by: Rhys Mainwaring <rhys.mainwaring@me.com> Signed-off-by: Brice <brice.renaudeau@gmail.com> * Change costmap_queue to shared library (#4072) Signed-off-by: cybaol <cybao292261@163.com> Signed-off-by: Brice <brice.renaudeau@gmail.com> * fix include of hpp Signed-off-by: Brice Renaudeau <brice.renaudeau@gmail.com> * inflation cost optmiizations and cleanu * rename, add defaults, and docs * smoke test addition * lintg * normalize weight * update readme * increment cache * Update cost_critic.hpp Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> * Update cost_critic.cpp Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> --------- Signed-off-by: Brice <brice.renaudeau@gmail.com> Signed-off-by: Steve Macenski <stevenmacenski@gmail.com> Signed-off-by: GoesM <GoesM@buaa.edu.cn> Signed-off-by: Davide Faconti <davide.faconti@gmail.com> Signed-off-by: gg <josho.wallace@gmail.com> Signed-off-by: Jose Faria <20467009+jncfa@users.noreply.github.com> Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> Signed-off-by: Rhys Mainwaring <rhys.mainwaring@me.com> Signed-off-by: cybaol <cybao292261@163.com> Signed-off-by: Brice Renaudeau <brice.renaudeau@gmail.com> Co-authored-by: BriceRenaudeau <48433002+BriceRenaudeau@users.noreply.github.com> Co-authored-by: GoesM <130988564+GoesM@users.noreply.github.com> Co-authored-by: GoesM <GoesM@buaa.edu.cn> Co-authored-by: Davide Faconti <davide.faconti@gmail.com> Co-authored-by: Joshua Wallace <josho.wallace@gmail.com> Co-authored-by: pepisg <pedro.gonzalez@eia.edu.co> Co-authored-by: Pedro Alejandro González <71234974+pepisg@users.noreply.github.com> Co-authored-by: jncfa <20467009+jncfa@users.noreply.github.com> Co-authored-by: Ryan <ryanfriedman5410+github@gmail.com> Co-authored-by: pedro-fuoco <pedrofuoco6@gmail.com> Co-authored-by: Rhys Mainwaring <rhys.mainwaring@me.com> Co-authored-by: Kino <cybao292261@163.com>
1 parent 70dc1f9 commit 5111f98

File tree

7 files changed

+331
-1
lines changed

7 files changed

+331
-1
lines changed

nav2_mppi_controller/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ add_library(mppi_controller SHARED
7979

8080
add_library(mppi_critics SHARED
8181
src/critics/obstacles_critic.cpp
82+
src/critics/cost_critic.cpp
8283
src/critics/goal_critic.cpp
8384
src/critics/goal_angle_critic.cpp
8485
src/critics/path_align_critic.cpp

nav2_mppi_controller/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ This process is then repeated a number of times and returns a converged solution
9898

9999

100100
#### Obstacles Critic
101+
102+
Uses estimated distances from obstacles using cost and inflation parameters to avoid obstacles
103+
101104
| Parameter | Type | Definition |
102105
| --------------- | ------ | ----------------------------------------------------------------------------------------------------------- |
103106
| consider_footprint | bool | Default: False. Whether to use point cost (if robot is circular or low compute power) or compute SE2 footprint cost. |
@@ -109,6 +112,20 @@ This process is then repeated a number of times and returns a converged solution
109112
| near_goal_distance | double | Default 0.5. Distance near goal to stop applying preferential obstacle term to allow robot to smoothly converge to goal pose in close proximity to obstacles.
110113
| inflation_layer_name | string | Default "". Name of the inflation layer. If empty, it uses the last inflation layer in the costmap. If you have multiple inflation layers, you may want to specify the name of the layer to use. |
111114

115+
#### Cost Critic
116+
117+
Uses inflated costmap cost directly to avoid obstacles
118+
119+
| Parameter | Type | Definition |
120+
| --------------- | ------ | ----------------------------------------------------------------------------------------------------------- |
121+
| consider_footprint | bool | Default: False. Whether to use point cost (if robot is circular or low compute power) or compute SE2 footprint cost. |
122+
| cost_weight | double | Default 3.81. Wight to apply to critic to avoid obstacles. |
123+
| cost_power | int | Default 1. Power order to apply to term. |
124+
| collision_cost | double | Default 1000000.0. Cost to apply to a true collision in a trajectory. |
125+
| critical_cost | double | Default 300.0. Cost to apply to a pose with any point in in inflated space to prefer distance from obstacles. |
126+
| near_goal_distance | double | Default 0.5. Distance near goal to stop applying preferential obstacle term to allow robot to smoothly converge to goal pose in close proximity to obstacles.
127+
| inflation_layer_name | string | Default "". Name of the inflation layer. If empty, it uses the last inflation layer in the costmap. If you have multiple inflation layers, you may want to specify the name of the layer to use. |
128+
112129
#### Path Align Critic
113130
| Parameter | Type | Definition |
114131
| --------------- | ------ | ----------------------------------------------------------------------------------------------------------- |
@@ -213,6 +230,15 @@ controller_server:
213230
collision_cost: 10000.0
214231
collision_margin_distance: 0.1
215232
near_goal_distance: 0.5
233+
# Option to replace Obstacles and use Cost instead
234+
# CostCritic:
235+
# enabled: true
236+
# cost_power: 1
237+
# cost_weight: 3.81
238+
# critical_cost: 300.0
239+
# consider_footprint: true
240+
# collision_cost: 1000000.0
241+
# near_goal_distance: 1.0
216242
PathAlignCritic:
217243
enabled: true
218244
cost_power: 1

nav2_mppi_controller/critics.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<description>mppi critic for obstacle avoidance</description>
66
</class>
77

8+
<class type="mppi::critics::CostCritic" base_class_type="mppi::critics::CriticFunction">
9+
<description>mppi critic for obstacle avoidance using costmap score</description>
10+
</class>
11+
812
<class type="mppi::critics::GoalCritic" base_class_type="mppi::critics::CriticFunction">
913
<description>mppi critic for driving towards the goal</description>
1014
</class>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright (c) 2023 Robocc Brice Renaudeau
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef NAV2_MPPI_CONTROLLER__CRITICS__COST_CRITIC_HPP_
16+
#define NAV2_MPPI_CONTROLLER__CRITICS__COST_CRITIC_HPP_
17+
18+
#include <memory>
19+
#include <string>
20+
21+
#include "nav2_costmap_2d/footprint_collision_checker.hpp"
22+
#include "nav2_costmap_2d/inflation_layer.hpp"
23+
24+
#include "nav2_mppi_controller/critic_function.hpp"
25+
#include "nav2_mppi_controller/models/state.hpp"
26+
#include "nav2_mppi_controller/tools/utils.hpp"
27+
28+
namespace mppi::critics
29+
{
30+
31+
/**
32+
* @class mppi::critics::CostCritic
33+
* @brief Critic objective function for avoiding obstacles using costmap's inflated cost
34+
*/
35+
class CostCritic : public CriticFunction
36+
{
37+
public:
38+
/**
39+
* @brief Initialize critic
40+
*/
41+
void initialize() override;
42+
43+
/**
44+
* @brief Evaluate cost related to obstacle avoidance
45+
*
46+
* @param costs [out] add obstacle cost values to this tensor
47+
*/
48+
void score(CriticData & data) override;
49+
50+
protected:
51+
/**
52+
* @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
57+
* @return bool if in collision
58+
*/
59+
bool inCollision(float cost, float x, float y, float theta);
60+
61+
/**
62+
* @brief cost at a robot pose
63+
* @param x X of pose
64+
* @param y Y of pose
65+
* @return Collision information at pose
66+
*/
67+
float costAtPose(float x, float y);
68+
69+
/**
70+
* @brief Find the min cost of the inflation decay function for which the robot MAY be
71+
* in collision in any orientation
72+
* @param costmap Costmap2DROS to get minimum inscribed cost (e.g. 128 in inflation layer documentation)
73+
* @return double circumscribed cost, any higher than this and need to do full footprint collision checking
74+
* since some element of the robot could be in collision
75+
*/
76+
float findCircumscribedCost(std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap);
77+
78+
protected:
79+
nav2_costmap_2d::FootprintCollisionChecker<nav2_costmap_2d::Costmap2D *>
80+
collision_checker_{nullptr};
81+
float possibly_inscribed_cost_;
82+
83+
bool consider_footprint_{true};
84+
float circumscribed_radius_{0};
85+
float circumscribed_cost_{0};
86+
float collision_cost_{0};
87+
float critical_cost_{0};
88+
float weight_{0};
89+
90+
float near_goal_distance_;
91+
std::string inflation_layer_name_;
92+
93+
unsigned int power_{0};
94+
};
95+
96+
} // namespace mppi::critics
97+
98+
#endif // NAV2_MPPI_CONTROLLER__CRITICS__COST_CRITIC_HPP_
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
// Copyright (c) 2022 Samsung Research America, @artofnothingness Alexey Budyakov
2+
// Copyright (c) 2023 Open Navigation LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#include <cmath>
17+
#include "nav2_mppi_controller/critics/cost_critic.hpp"
18+
19+
namespace mppi::critics
20+
{
21+
22+
void CostCritic::initialize()
23+
{
24+
auto getParam = parameters_handler_->getParamGetter(name_);
25+
getParam(consider_footprint_, "consider_footprint", false);
26+
getParam(power_, "cost_power", 1);
27+
getParam(weight_, "cost_weight", 3.81);
28+
getParam(critical_cost_, "critical_cost", 300.0);
29+
getParam(collision_cost_, "collision_cost", 1000000.0);
30+
getParam(near_goal_distance_, "near_goal_distance", 0.5);
31+
getParam(inflation_layer_name_, "inflation_layer_name", std::string(""));
32+
33+
// Normalized by cost value to put in same regime as other weights
34+
weight_ /= 254.0f;
35+
36+
collision_checker_.setCostmap(costmap_);
37+
possibly_inscribed_cost_ = findCircumscribedCost(costmap_ros_);
38+
39+
if (possibly_inscribed_cost_ < 1.0f) {
40+
RCLCPP_ERROR(
41+
logger_,
42+
"Inflation layer either not found or inflation is not set sufficiently for "
43+
"optimized non-circular collision checking capabilities. It is HIGHLY recommended to set"
44+
" the inflation radius to be at MINIMUM half of the robot's largest cross-section. See "
45+
"github.com/ros-planning/navigation2/tree/main/nav2_smac_planner#potential-fields"
46+
" for full instructions. This will substantially impact run-time performance.");
47+
}
48+
49+
RCLCPP_INFO(
50+
logger_,
51+
"InflationCostCritic instantiated with %d power and %f / %f weights. "
52+
"Critic will collision check based on %s cost.",
53+
power_, critical_cost_, weight_, consider_footprint_ ?
54+
"footprint" : "circular");
55+
}
56+
57+
float CostCritic::findCircumscribedCost(
58+
std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap)
59+
{
60+
double result = -1.0;
61+
const double circum_radius = costmap->getLayeredCostmap()->getCircumscribedRadius();
62+
if (static_cast<float>(circum_radius) == circumscribed_radius_) {
63+
// early return if footprint size is unchanged
64+
return circumscribed_cost_;
65+
}
66+
67+
// check if the costmap has an inflation layer
68+
const auto inflation_layer = nav2_costmap_2d::InflationLayer::getInflationLayer(
69+
costmap,
70+
inflation_layer_name_);
71+
if (inflation_layer != nullptr) {
72+
const double resolution = costmap->getCostmap()->getResolution();
73+
result = inflation_layer->computeCost(circum_radius / resolution);
74+
} else {
75+
RCLCPP_WARN(
76+
logger_,
77+
"No inflation layer found in costmap configuration. "
78+
"If this is an SE2-collision checking plugin, it cannot use costmap potential "
79+
"field to speed up collision checking by only checking the full footprint "
80+
"when robot is within possibly-inscribed radius of an obstacle. This may "
81+
"significantly slow down planning times and not avoid anything but absolute collisions!");
82+
}
83+
84+
circumscribed_radius_ = static_cast<float>(circum_radius);
85+
circumscribed_cost_ = static_cast<float>(result);
86+
87+
return circumscribed_cost_;
88+
}
89+
90+
void CostCritic::score(CriticData & data)
91+
{
92+
using xt::evaluation_strategy::immediate;
93+
if (!enabled_) {
94+
return;
95+
}
96+
97+
if (consider_footprint_) {
98+
// footprint may have changed since initialization if user has dynamic footprints
99+
possibly_inscribed_cost_ = findCircumscribedCost(costmap_ros_);
100+
}
101+
102+
// If near the goal, don't apply the preferential term since the goal is near obstacles
103+
bool near_goal = false;
104+
if (utils::withinPositionGoalTolerance(near_goal_distance_, data.state.pose.pose, data.path)) {
105+
near_goal = true;
106+
}
107+
108+
auto && repulsive_cost = xt::xtensor<float, 1>::from_shape({data.costs.shape(0)});
109+
repulsive_cost.fill(0.0);
110+
111+
const size_t traj_len = data.trajectories.x.shape(1);
112+
bool all_trajectories_collide = true;
113+
for (size_t i = 0; i < data.trajectories.x.shape(0); ++i) {
114+
bool trajectory_collide = false;
115+
const auto & traj = data.trajectories;
116+
float pose_cost;
117+
118+
for (size_t j = 0; j < traj_len; j++) {
119+
// The costAtPose doesn't use orientation
120+
// The footprintCostAtPose will always return "INSCRIBED" if footprint is over it
121+
// So the center point has more information than the footprint
122+
pose_cost = costAtPose(traj.x(i, j), traj.y(i, j));
123+
if (pose_cost < 1.0f) {continue;} // In free space
124+
125+
if (inCollision(pose_cost, traj.x(i, j), traj.y(i, j), traj.yaws(i, j))) {
126+
trajectory_collide = true;
127+
break;
128+
}
129+
130+
// Let near-collision trajectory points be punished severely
131+
// Note that we collision check based on the footprint actual,
132+
// but score based on the center-point cost regardless
133+
using namespace nav2_costmap_2d; // NOLINT
134+
if (pose_cost >= INSCRIBED_INFLATED_OBSTACLE) {
135+
repulsive_cost[i] += critical_cost_;
136+
} else if (!near_goal) { // Generally prefer trajectories further from obstacles
137+
repulsive_cost[i] += pose_cost;
138+
}
139+
}
140+
141+
if (!trajectory_collide) {
142+
all_trajectories_collide = false;
143+
} else {
144+
repulsive_cost[i] = collision_cost_;
145+
}
146+
}
147+
148+
data.costs += xt::pow((weight_ * repulsive_cost / traj_len), power_);
149+
data.fail_flag = all_trajectories_collide;
150+
}
151+
152+
/**
153+
* @brief Checks if cost represents a collision
154+
* @param cost Costmap cost
155+
* @return bool if in collision
156+
*/
157+
bool CostCritic::inCollision(float cost, float x, float y, float theta)
158+
{
159+
bool is_tracking_unknown =
160+
costmap_ros_->getLayeredCostmap()->isTrackingUnknown();
161+
162+
// If consider_footprint_ check footprint scort for collision
163+
if (consider_footprint_ &&
164+
(cost >= possibly_inscribed_cost_ || possibly_inscribed_cost_ < 1.0f))
165+
{
166+
cost = static_cast<float>(collision_checker_.footprintCostAtPose(
167+
x, y, theta, costmap_ros_->getRobotFootprint()));
168+
}
169+
170+
switch (static_cast<unsigned char>(cost)) {
171+
using namespace nav2_costmap_2d; // NOLINT
172+
case (LETHAL_OBSTACLE):
173+
return true;
174+
case (INSCRIBED_INFLATED_OBSTACLE):
175+
return consider_footprint_ ? false : true;
176+
case (NO_INFORMATION):
177+
return is_tracking_unknown ? false : true;
178+
}
179+
180+
return false;
181+
}
182+
183+
float CostCritic::costAtPose(float x, float y)
184+
{
185+
using namespace nav2_costmap_2d; // NOLINT
186+
unsigned int x_i, y_i;
187+
if (!collision_checker_.worldToMap(x, y, x_i, y_i)) {
188+
return nav2_costmap_2d::NO_INFORMATION;
189+
}
190+
191+
return collision_checker_.pointCost(x_i, y_i);
192+
}
193+
194+
} // namespace mppi::critics
195+
196+
#include <pluginlib/class_list_macros.hpp>
197+
198+
PLUGINLIB_EXPORT_CLASS(
199+
mppi::critics::CostCritic,
200+
mppi::critics::CriticFunction)

nav2_mppi_controller/test/critics_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "nav2_mppi_controller/critics/goal_angle_critic.hpp"
2525
#include "nav2_mppi_controller/critics/goal_critic.hpp"
2626
#include "nav2_mppi_controller/critics/obstacles_critic.hpp"
27+
#include "nav2_mppi_controller/critics/cost_critic.hpp"
2728
#include "nav2_mppi_controller/critics/path_align_critic.hpp"
2829
#include "nav2_mppi_controller/critics/path_align_legacy_critic.hpp"
2930
#include "nav2_mppi_controller/critics/path_angle_critic.hpp"

nav2_mppi_controller/test/optimizer_smoke_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ INSTANTIATE_TEST_SUITE_P(
103103
std::make_tuple(
104104
"DiffDrive",
105105
std::vector<std::string>(
106-
{{"GoalCritic"}, {"GoalAngleCritic"}, {"ObstaclesCritic"},
106+
{{"GoalCritic"}, {"GoalAngleCritic"}, {"CostCritic"},
107107
{"PathAngleCritic"}, {"PathFollowCritic"}, {"PreferForwardCritic"}}),
108108
true),
109109
std::make_tuple(

0 commit comments

Comments
 (0)