Skip to content

Commit 4465836

Browse files
committed
Add goal pose to CriticData
1 parent 507b365 commit 4465836

21 files changed

+145
-59
lines changed

nav2_mppi_controller/include/nav2_mppi_controller/critic_data.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ namespace mppi
3232

3333
/**
3434
* @struct mppi::CriticData
35-
* @brief Data to pass to critics for scoring, including state, trajectories, path, costs, and
36-
* important parameters to share
35+
* @brief Data to pass to critics for scoring, including state, trajectories,
36+
* pruned path, global goal, costs, and important parameters to share
3737
*/
3838
struct CriticData
3939
{
4040
const models::State & state;
4141
const models::Trajectories & trajectories;
4242
const models::Path & path;
43+
const geometry_msgs::msg::Pose & goal;
4344

4445
xt::xtensor<float, 1> & costs;
4546
float & model_dt;

nav2_mppi_controller/include/nav2_mppi_controller/optimizer.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class Optimizer
9090
geometry_msgs::msg::TwistStamped evalControl(
9191
const geometry_msgs::msg::PoseStamped & robot_pose,
9292
const geometry_msgs::msg::Twist & robot_speed, const nav_msgs::msg::Path & plan,
93-
nav2_core::GoalChecker * goal_checker);
93+
const geometry_msgs::msg::Pose & goal, nav2_core::GoalChecker * goal_checker);
9494

9595
/**
9696
* @brief Get the trajectories generated in a cycle for visualization
@@ -132,7 +132,8 @@ class Optimizer
132132
void prepare(
133133
const geometry_msgs::msg::PoseStamped & robot_pose,
134134
const geometry_msgs::msg::Twist & robot_speed,
135-
const nav_msgs::msg::Path & plan, nav2_core::GoalChecker * goal_checker);
135+
const nav_msgs::msg::Path & plan,
136+
const geometry_msgs::msg::Pose & goal, nav2_core::GoalChecker * goal_checker);
136137

137138
/**
138139
* @brief Obtain the main controller's parameters
@@ -250,10 +251,12 @@ class Optimizer
250251
std::array<mppi::models::Control, 4> control_history_;
251252
models::Trajectories generated_trajectories_;
252253
models::Path path_;
254+
geometry_msgs::msg::Pose goal_;
253255
xt::xtensor<float, 1> costs_;
254256

255-
CriticData critics_data_ =
256-
{state_, generated_trajectories_, path_, costs_, settings_.model_dt, false, nullptr, nullptr,
257+
CriticData critics_data_ = {
258+
state_, generated_trajectories_, path_, goal_,
259+
costs_, settings_.model_dt, false, nullptr, nullptr,
257260
std::nullopt, std::nullopt}; /// Caution, keep references
258261

259262
rclcpp::Logger logger_{rclcpp::get_logger("MPPIController")};

nav2_mppi_controller/include/nav2_mppi_controller/tools/path_handler.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ class PathHandler
8989
*/
9090
nav_msgs::msg::Path transformPath(const geometry_msgs::msg::PoseStamped & robot_pose);
9191

92+
/**
93+
* @brief Get the global goal pose transformed to the local frame
94+
* @return Transformed goal pose
95+
*/
96+
geometry_msgs::msg::PoseStamped getTransformedGoal();
97+
9298
protected:
9399
/**
94100
* @brief Transform a pose to another frame

nav2_mppi_controller/include/nav2_mppi_controller/tools/utils.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ inline bool withinPositionGoalTolerance(
229229

230230
/**
231231
* @brief Check if the robot pose is within tolerance to the goal
232+
*
233+
* Deprecated!
234+
* This uses the end of pruned path as the goal, if you want to use
235+
* the global goal, use the other overload
236+
*
232237
* @param pose_tolerance Pose tolerance to use
233238
* @param robot Pose of robot
234239
* @param path Path to retreive goal pose from
@@ -257,6 +262,29 @@ inline bool withinPositionGoalTolerance(
257262
return false;
258263
}
259264

265+
/**
266+
* @brief Check if the robot pose is within tolerance to the goal
267+
* @param pose_tolerance Pose tolerance to use
268+
* @param robot Pose of robot
269+
* @param path Path to retreive goal pose from
270+
* @return bool If robot is within tolerance to the goal
271+
*/
272+
inline bool withinPositionGoalTolerance(
273+
float pose_tolerance, const CriticData & data)
274+
{
275+
const double & dist_sq =
276+
std::pow(data.goal.position.x - data.state.pose.pose.position.x, 2) +
277+
std::pow(data.goal.position.y - data.state.pose.pose.position.y, 2);
278+
279+
const auto pose_tolerance_sq = pose_tolerance * pose_tolerance;
280+
281+
if (dist_sq < pose_tolerance_sq) {
282+
return true;
283+
}
284+
285+
return false;
286+
}
287+
260288
/**
261289
* @brief normalize
262290
* Normalizes the angle to be -M_PI circle to +M_PI circle

nav2_mppi_controller/src/controller.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,15 @@ geometry_msgs::msg::TwistStamped MPPIController::computeVelocityCommands(
9292
last_time_called_ = clock_->now();
9393

9494
std::lock_guard<std::mutex> param_lock(*parameters_handler_->getLock());
95+
geometry_msgs::msg::Pose goal = path_handler_.getTransformedGoal().pose;
96+
9597
nav_msgs::msg::Path transformed_plan = path_handler_.transformPath(robot_pose);
9698

9799
nav2_costmap_2d::Costmap2D * costmap = costmap_ros_->getCostmap();
98100
std::unique_lock<nav2_costmap_2d::Costmap2D::mutex_t> costmap_lock(*(costmap->getMutex()));
99101

100102
geometry_msgs::msg::TwistStamped cmd =
101-
optimizer_.evalControl(robot_pose, robot_speed, transformed_plan, goal_checker);
103+
optimizer_.evalControl(robot_pose, robot_speed, transformed_plan, goal, goal_checker);
102104

103105
#ifdef BENCHMARK_TESTING
104106
auto end = std::chrono::system_clock::now();

nav2_mppi_controller/src/critics/cost_critic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void CostCritic::score(CriticData & data)
119119

120120
// If near the goal, don't apply the preferential term since the goal is near obstacles
121121
bool near_goal = false;
122-
if (utils::withinPositionGoalTolerance(near_goal_distance_, data.state.pose.pose, data.path)) {
122+
if (utils::withinPositionGoalTolerance(near_goal_distance_, data)) {
123123
near_goal = true;
124124
}
125125

nav2_mppi_controller/src/critics/goal_angle_critic.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ void GoalAngleCritic::initialize()
3535

3636
void GoalAngleCritic::score(CriticData & data)
3737
{
38-
if (!enabled_ || !utils::withinPositionGoalTolerance(
39-
threshold_to_consider_, data.state.pose.pose, data.path))
38+
if (!enabled_ || !utils::withinPositionGoalTolerance(threshold_to_consider_, data))
4039
{
4140
return;
4241
}

nav2_mppi_controller/src/critics/goal_critic.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,13 @@ void GoalCritic::initialize()
3535

3636
void GoalCritic::score(CriticData & data)
3737
{
38-
if (!enabled_ || !utils::withinPositionGoalTolerance(
39-
threshold_to_consider_, data.state.pose.pose, data.path))
38+
if (!enabled_ || !utils::withinPositionGoalTolerance(threshold_to_consider_, data))
4039
{
4140
return;
4241
}
4342

44-
const auto goal_idx = data.path.x.shape(0) - 1;
45-
46-
const auto goal_x = data.path.x(goal_idx);
47-
const auto goal_y = data.path.y(goal_idx);
43+
const auto & goal_x = data.goal.position.x;
44+
const auto & goal_y = data.goal.position.y;
4845

4946
const auto traj_x = xt::view(data.trajectories.x, xt::all(), xt::all());
5047
const auto traj_y = xt::view(data.trajectories.y, xt::all(), xt::all());

nav2_mppi_controller/src/critics/obstacles_critic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void ObstaclesCritic::score(CriticData & data)
125125

126126
// If near the goal, don't apply the preferential term since the goal is near obstacles
127127
bool near_goal = false;
128-
if (utils::withinPositionGoalTolerance(near_goal_distance_, data.state.pose.pose, data.path)) {
128+
if (utils::withinPositionGoalTolerance(near_goal_distance_, data)) {
129129
near_goal = true;
130130
}
131131

nav2_mppi_controller/src/critics/path_align_critic.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ void PathAlignCritic::initialize()
4646
void PathAlignCritic::score(CriticData & data)
4747
{
4848
// Don't apply close to goal, let the goal critics take over
49-
if (!enabled_ ||
50-
utils::withinPositionGoalTolerance(threshold_to_consider_, data.state.pose.pose, data.path))
49+
if (!enabled_ || utils::withinPositionGoalTolerance(threshold_to_consider_, data))
5150
{
5251
return;
5352
}

0 commit comments

Comments
 (0)