SMACC2
Loading...
Searching...
No Matches
cb_undo_path_backwards.cpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft Inc.
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/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
22#include <cl_nav2z/common.hpp>
25
26namespace cl_nav2z
27{
28using ::cl_nav2z::odom_tracker::CpOdomTracker;
29using ::cl_nav2z::odom_tracker::WorkingMode;
30
31using namespace std::chrono_literals;
32using namespace smacc2;
33
34CbUndoPathBackwards::CbUndoPathBackwards(std::optional<CbUndoPathBackwardsOptions> options)
35{
36 options_ = options;
37}
38
40{
41 listener = std::make_shared<tf2_ros::Buffer>(this->getNode()->get_clock());
42 requiresComponent(odomTracker, ComponentRequirement::HARD);
43
45
46 CpPlannerSwitcher * plannerSwitcher;
47 requiresComponent(plannerSwitcher, ComponentRequirement::HARD);
48
49 nav_msgs::msg::Path forwardpath = odomTracker->getPath();
50 // RCLCPP_INFO_STREAM(getLogger(),"[UndoPathBackward] Current path backwards: " << forwardpath);
51
52 odomTracker->setWorkingMode(WorkingMode::CLEAR_PATH);
53
54 nav2_msgs::action::NavigateToPose::Goal goal;
55
56 CpGoalCheckerSwitcher * goalCheckerSwitcher;
57 requiresComponent(goalCheckerSwitcher, ComponentRequirement::HARD);
58
59 if (forwardpath.poses.size() > 0)
60 {
61 goal.pose = forwardpath.poses.front();
62 goal.pose.header.stamp = rclcpp::Time(0);
63
64 if (options_ && options_->undoControllerName_)
65 {
66 plannerSwitcher->setUndoPathBackwardPlanner(false);
67 RCLCPP_INFO_STREAM(
68 getLogger(),
69 "[" << getName() << "] Undoing path with controller: " << *options_->undoControllerName_);
70 plannerSwitcher->setDesiredController(*options_->undoControllerName_);
71 plannerSwitcher->commitPublish();
72 }
73 else
74 {
75 plannerSwitcher->setUndoPathBackwardPlanner();
76 }
77
78 // WARNING: There might be some race condition with the remote undo global planner/controller were the global path was not
79 // received yet, thee goal switcher
80 // TODO: waiting notification from global planner that it is loaded
81
82 RCLCPP_ERROR_STREAM(
83 getLogger(), "[" << getName()
84 << "] Waiting for 5 seconds to get planer/controller and wait goal checker "
85 "ready");
86
87 RCLCPP_ERROR_STREAM(getLogger(), "[" << getName() << "] activating undo navigation planner");
88
89 if (options_ && options_->goalCheckerId_)
90 {
91 goalCheckerSwitcher->setGoalCheckerId(*options_->goalCheckerId_);
92 }
93 else
94 {
95 goalCheckerSwitcher->setGoalCheckerId("undo_path_backwards_goal_checker");
96 }
97
98 this->sendGoal(goal);
99 }
100}
101
103{
104 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] Exiting: undo navigation ");
105
106 if (this->navigationResult_ == rclcpp_action::ResultCode::SUCCEEDED)
107 {
108 RCLCPP_INFO_STREAM(
109 getLogger(), getName() << " - [CbUndoPathBackwards] Exiting: undo navigation successful, "
110 "popping odom tracker path");
111
112 requiresComponent(odomTracker, ComponentRequirement::HARD);
114
116 }
117 else
118 {
119 RCLCPP_INFO_STREAM(
120 getLogger(), getName() << " - [CbUndoPathBackwards] Exiting: undo navigation abort, avoiding "
121 "popping current path");
122
123 requiresComponent(odomTracker, ComponentRequirement::HARD);
125 // navigation interrupted or aborted. The path may be not totally undone.
126 // We keep the odom tracker in its current state, probably in the middle of the undoing process.
127 // Could you try to repeat the behavior?
128 }
129}
130
131} // namespace cl_nav2z
void sendGoal(nav2_msgs::action::NavigateToPose::Goal &goal)
std::optional< CbUndoPathBackwardsOptions > options_
std::shared_ptr< tf2_ros::Buffer > listener
CbUndoPathBackwards(std::optional< CbUndoPathBackwardsOptions > options=std::nullopt)
void setGoalCheckerId(std::string goal_checker_id)
void setUndoPathBackwardPlanner(bool commit=true)
void popPath(int pathCount=1, bool keepPreviousPath=false)
void setWorkingMode(WorkingMode workingMode)
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)