Skip to content

Commit c892beb

Browse files
authored
Backport #4996 (#5039)
Signed-off-by: mini-1235 <mauricepurnawan@gmail.com>
1 parent 7ee3d2f commit c892beb

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

nav2_mppi_controller/src/critics/cost_critic.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <cmath>
1717
#include "nav2_mppi_controller/critics/cost_critic.hpp"
18+
#include "nav2_core/exceptions.hpp"
1819

1920
namespace mppi::critics
2021
{
@@ -52,6 +53,18 @@ void CostCritic::initialize()
5253
" for full instructions. This will substantially impact run-time performance.");
5354
}
5455

56+
if (costmap_ros_->getUseRadius() == consider_footprint_) {
57+
RCLCPP_WARN(
58+
logger_,
59+
"Inconsistent configuration in collision checking. Please verify the robot's shape settings "
60+
"in both the costmap and the cost critic.");
61+
if (costmap_ros_->getUseRadius()) {
62+
throw nav2_core::PlannerException(
63+
"Considering footprint in collision checking but no robot footprint provided in the "
64+
"costmap.");
65+
}
66+
}
67+
5568
RCLCPP_INFO(
5669
logger_,
5770
"InflationCostCritic instantiated with %d power and %f / %f weights. "

nav2_mppi_controller/src/critics/obstacles_critic.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <cmath>
1616
#include "nav2_mppi_controller/critics/obstacles_critic.hpp"
17+
#include "nav2_core/exceptions.hpp"
1718

1819
namespace mppi::critics
1920
{
@@ -42,6 +43,18 @@ void ObstaclesCritic::initialize()
4243
" for full instructions. This will substantially impact run-time performance.");
4344
}
4445

46+
if (costmap_ros_->getUseRadius() == consider_footprint_) {
47+
RCLCPP_WARN(
48+
logger_,
49+
"Inconsistent configuration in collision checking. Please verify the robot's shape settings "
50+
"in both the costmap and the obstacle critic.");
51+
if (costmap_ros_->getUseRadius()) {
52+
throw nav2_core::PlannerException(
53+
"Considering footprint in collision checking but no robot footprint provided in the "
54+
"costmap.");
55+
}
56+
}
57+
4558
RCLCPP_INFO(
4659
logger_,
4760
"ObstaclesCritic instantiated with %d power and %f / %f weights. "

nav2_mppi_controller/test/critics_tests.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "nav2_mppi_controller/critics/prefer_forward_critic.hpp"
3232
#include "nav2_mppi_controller/critics/twirling_critic.hpp"
3333
#include "nav2_mppi_controller/critics/velocity_deadband_critic.hpp"
34+
#include "nav2_core/exceptions.hpp"
3435
#include "utils_test.cpp" // NOLINT
3536

3637
// Tests the various critic plugin functions
@@ -115,6 +116,81 @@ TEST(CriticTests, ConstraintsCritic)
115116
EXPECT_NEAR(costs(1), 0.48, 0.01);
116117
}
117118

119+
TEST(CriticTests, ObstacleCriticMisalignedParams) {
120+
auto node = std::make_shared<rclcpp_lifecycle::LifecycleNode>("my_node");
121+
auto costmap_ros = std::make_shared<nav2_costmap_2d::Costmap2DROS>(
122+
"dummy_costmap", "", "dummy_costmap");
123+
ParametersHandler param_handler(node);
124+
auto getParam = param_handler.getParamGetter("critic");
125+
bool consider_footprint;
126+
getParam(consider_footprint, "consider_footprint", true);
127+
128+
rclcpp_lifecycle::State lstate;
129+
costmap_ros->on_configure(lstate);
130+
131+
ObstaclesCritic critic;
132+
// Expect throw when settings mismatched
133+
EXPECT_THROW(
134+
critic.on_configure(node, "mppi", "critic", costmap_ros, &param_handler),
135+
nav2_core::PlannerException
136+
);
137+
}
138+
139+
TEST(CriticTests, ObstacleCriticAlignedParams) {
140+
auto node = std::make_shared<rclcpp_lifecycle::LifecycleNode>("my_node");
141+
auto costmap_ros = std::make_shared<nav2_costmap_2d::Costmap2DROS>(
142+
"dummy_costmap", "", "dummy_costmap");
143+
ParametersHandler param_handler(node);
144+
auto getParam = param_handler.getParamGetter("critic");
145+
bool consider_footprint;
146+
getParam(consider_footprint, "consider_footprint", false);
147+
148+
rclcpp_lifecycle::State lstate;
149+
costmap_ros->on_configure(lstate);
150+
151+
ObstaclesCritic critic;
152+
critic.on_configure(node, "mppi", "critic", costmap_ros, &param_handler);
153+
EXPECT_EQ(critic.getName(), "critic");
154+
}
155+
156+
157+
TEST(CriticTests, CostCriticMisAlignedParams) {
158+
// Standard preamble
159+
auto node = std::make_shared<rclcpp_lifecycle::LifecycleNode>("my_node");
160+
auto costmap_ros = std::make_shared<nav2_costmap_2d::Costmap2DROS>(
161+
"dummy_costmap", "", "dummy_costmap");
162+
ParametersHandler param_handler(node);
163+
rclcpp_lifecycle::State lstate;
164+
auto getParam = param_handler.getParamGetter("critic");
165+
bool consider_footprint;
166+
getParam(consider_footprint, "consider_footprint", true);
167+
costmap_ros->on_configure(lstate);
168+
169+
CostCritic critic;
170+
// Expect throw when settings mismatched
171+
EXPECT_THROW(
172+
critic.on_configure(node, "mppi", "critic", costmap_ros, &param_handler),
173+
nav2_core::PlannerException
174+
);
175+
}
176+
177+
TEST(CriticTests, CostCriticAlignedParams) {
178+
// Standard preamble
179+
auto node = std::make_shared<rclcpp_lifecycle::LifecycleNode>("my_node");
180+
auto costmap_ros = std::make_shared<nav2_costmap_2d::Costmap2DROS>(
181+
"dummy_costmap", "", "dummy_costmap");
182+
ParametersHandler param_handler(node);
183+
rclcpp_lifecycle::State lstate;
184+
auto getParam = param_handler.getParamGetter("critic");
185+
bool consider_footprint;
186+
getParam(consider_footprint, "consider_footprint", false);
187+
costmap_ros->on_configure(lstate);
188+
189+
CostCritic critic;
190+
critic.on_configure(node, "mppi", "critic", costmap_ros, &param_handler);
191+
EXPECT_EQ(critic.getName(), "critic");
192+
}
193+
118194
TEST(CriticTests, GoalAngleCritic)
119195
{
120196
// Standard preamble

0 commit comments

Comments
 (0)