Skip to content

Commit 64dc81a

Browse files
committed
Make NavigateThroughPoses navigator report waypoint statuses information
Signed-off-by: zz990099 <771647586@qq.com>
1 parent 1a9e20c commit 64dc81a

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

nav2_bt_navigator/behavior_trees/navigate_through_poses_w_replanning_and_recovery.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<RateController hz="0.333">
1515
<RecoveryNode number_of_retries="1" name="ComputePathThroughPoses">
1616
<ReactiveSequence>
17-
<RemovePassedGoals input_goals="{goals}" output_goals="{goals}" radius="0.7"/>
17+
<RemovePassedGoals input_goals="{goals}" output_goals="{goals}" radius="0.7" input_waypoint_statuses="{waypoint_statuses}" output_waypoint_statuses="{waypoint_statuses}"/>
1818
<ComputePathThroughPoses goals="{goals}" path="{path}" planner_id="{selected_planner}" error_code_id="{compute_path_error_code}" error_msg="{compute_path_error_msg}"/>
1919
</ReactiveSequence>
2020
<Sequence>

nav2_bt_navigator/include/nav2_bt_navigator/navigators/navigate_through_poses.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "geometry_msgs/msg/pose_stamped.hpp"
2424
#include "nav2_core/behavior_tree_navigator.hpp"
2525
#include "nav2_msgs/action/navigate_through_poses.hpp"
26+
#include "nav2_msgs/msg/waypoint_status.hpp"
2627
#include "nav_msgs/msg/path.hpp"
2728
#include "nav2_util/robot_utils.hpp"
2829
#include "nav2_util/geometry_utils.hpp"
@@ -109,6 +110,7 @@ class NavigateThroughPosesNavigator
109110
rclcpp::Time start_time_;
110111
std::string goals_blackboard_id_;
111112
std::string path_blackboard_id_;
113+
std::string waypoint_statuses_blackboard_id_;
112114

113115
// Odometry smoother object
114116
std::shared_ptr<nav2_util::OdomSmoother> odom_smoother_;

nav2_bt_navigator/src/navigators/navigate_through_poses.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ NavigateThroughPosesNavigator::configure(
4242

4343
path_blackboard_id_ = node->get_parameter("path_blackboard_id").as_string();
4444

45+
if (!node->has_parameter("waypoint_statuses_blackboard_id")) {
46+
node->declare_parameter("waypoint_statuses_blackboard_id", std::string("waypoint_statuses"));
47+
}
48+
49+
waypoint_statuses_blackboard_id_ =
50+
node->get_parameter("waypoint_statuses_blackboard_id").as_string();
51+
4552
// Odometry smoother object for getting current speed
4653
odom_smoother_ = odom_smoother;
4754

@@ -86,7 +93,7 @@ NavigateThroughPosesNavigator::goalReceived(ActionT::Goal::ConstSharedPtr goal)
8693
void
8794
NavigateThroughPosesNavigator::goalCompleted(
8895
typename ActionT::Result::SharedPtr result,
89-
const nav2_behavior_tree::BtStatus /*final_bt_status*/)
96+
const nav2_behavior_tree::BtStatus final_bt_status)
9097
{
9198
if (result->error_code == 0) {
9299
if (bt_action_server_->populateInternalError(result)) {
@@ -100,6 +107,22 @@ NavigateThroughPosesNavigator::goalCompleted(
100107
result->error_code,
101108
result->error_msg.c_str());
102109
}
110+
111+
// populate waypoint statuses in result
112+
auto blackboard = bt_action_server_->getBlackboard();
113+
auto waypoint_statuses =
114+
blackboard->get<std::vector<nav2_msgs::msg::WaypointStatus>>(waypoint_statuses_blackboard_id_);
115+
116+
// populate remaining waypoint statuses based on final_bt_status
117+
auto integrate_waypoint_status = final_bt_status == nav2_behavior_tree::BtStatus::SUCCEEDED ?
118+
nav2_msgs::msg::WaypointStatus::COMPLETED : nav2_msgs::msg::WaypointStatus::MISSED;
119+
for (auto & waypoint_status : waypoint_statuses) {
120+
if (waypoint_status.waypoint_status == nav2_msgs::msg::WaypointStatus::PENDING) {
121+
waypoint_status.waypoint_status = integrate_waypoint_status;
122+
}
123+
}
124+
125+
result->waypoint_statuses = std::move(waypoint_statuses);
103126
}
104127

105128
void
@@ -116,6 +139,9 @@ NavigateThroughPosesNavigator::onLoop()
116139
nav_msgs::msg::Goals goal_poses;
117140
[[maybe_unused]] auto res = blackboard->get(goals_blackboard_id_, goal_poses);
118141

142+
feedback_msg->waypoint_statuses =
143+
blackboard->get<std::vector<nav2_msgs::msg::WaypointStatus>>(waypoint_statuses_blackboard_id_);
144+
119145
if (goal_poses.goals.size() == 0) {
120146
bt_action_server_->publishFeedback(feedback_msg);
121147
return;
@@ -262,6 +288,15 @@ NavigateThroughPosesNavigator::initializeGoalPoses(ActionT::Goal::ConstSharedPtr
262288
blackboard->set<nav_msgs::msg::Goals>(goals_blackboard_id_,
263289
std::move(goals_array));
264290

291+
// Reset the waypoint states vector in the blackboard
292+
std::vector<nav2_msgs::msg::WaypointStatus> waypoint_statuses(goals_array.goals.size());
293+
for (size_t waypoint_index = 0 ; waypoint_index < goals_array.goals.size() ; ++waypoint_index) {
294+
waypoint_statuses[waypoint_index].waypoint_index = waypoint_index;
295+
waypoint_statuses[waypoint_index].waypoint_pose = goals_array.goals[waypoint_index];
296+
}
297+
blackboard->set<decltype(waypoint_statuses)>(waypoint_statuses_blackboard_id_,
298+
std::move(waypoint_statuses));
299+
265300
return true;
266301
}
267302

0 commit comments

Comments
 (0)