SMACC2
Loading...
Searching...
No Matches
cb_undo_path_backwards.cpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI 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
25
26namespace cl_nav2z
27{
28using ::cl_nav2z::odom_tracker::CpOdomTracker;
29using ::cl_nav2z::odom_tracker::WorkingMode;
30
31using namespace std::chrono_literals;
32
33CbUndoPathBackwards::CbUndoPathBackwards(std::optional<CbUndoPathBackwardsOptions> options)
34{
35 options_ = options;
36}
37
39{
40 listener = std::make_shared<tf2_ros::Buffer>(this->getNode()->get_clock());
42
44
45 auto plannerSwitcher = nav2zClient_->getComponent<CpPlannerSwitcher>();
46
47 nav_msgs::msg::Path forwardpath = odomTracker->getPath();
48 // RCLCPP_INFO_STREAM(getLogger(),"[UndoPathBackward] Current path backwards: " << forwardpath);
49
50 odomTracker->setWorkingMode(WorkingMode::CLEAR_PATH);
51
52 ClNav2Z::Goal goal;
53
54 auto goalCheckerSwitcher = nav2zClient_->getComponent<CpGoalCheckerSwitcher>();
55
56 // this line is used to flush/reset backward planner in the case it were already there
57 // plannerSwitcher->setDefaultPlanners();
58 if (forwardpath.poses.size() > 0)
59 {
60 goal.pose = forwardpath.poses.front();
61 //goal.pose.header.stamp = getNode()->now();
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::sleep_for(5s);
88 RCLCPP_ERROR_STREAM(getLogger(), "[" << getName() << "] activating undo navigation planner");
89
90 if (options_ && options_->goalCheckerId_)
91 {
92 goalCheckerSwitcher->setGoalCheckerId(*options_->goalCheckerId_);
93 }
94 else
95 {
96 goalCheckerSwitcher->setGoalCheckerId("undo_path_backwards_goal_checker");
97 }
98
99 this->sendGoal(goal);
100 }
101}
102
104{
105 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] Exiting: undo navigation ");
106
107 if (this->navigationResult_ == rclcpp_action::ResultCode::SUCCEEDED)
108 {
109 RCLCPP_INFO_STREAM(
110 getLogger(), getName() << " - [CbUndoPathBackwards] Exiting: undo navigation successful, "
111 "popping odom tracker path");
114
116 }
117 else
118 {
119 RCLCPP_INFO_STREAM(
120 getLogger(), getName() << " - [CbUndoPathBackwards] Exiting: undo navigation abort, avoiding "
121 "popping current path");
122
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
std::optional< CbUndoPathBackwardsOptions > options_
std::shared_ptr< tf2_ros::Buffer > listener
CbUndoPathBackwards(std::optional< CbUndoPathBackwardsOptions > options=std::nullopt)
void popPath(int pathCount=1, bool keepPreviousPath=false)
void setWorkingMode(WorkingMode workingMode)
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const