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
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 // this line is used to flush/reset backward planner in the case it were already there
60 // plannerSwitcher->setDefaultPlanners();
61 if (forwardpath.poses.size() > 0)
62 {
63 goal.pose = forwardpath.poses.front();
64 //goal.pose.header.stamp = getNode()->now();
65 goal.pose.header.stamp = rclcpp::Time(0);
66
67 if (options_ && options_->undoControllerName_)
68 {
69 plannerSwitcher->setUndoPathBackwardPlanner(false);
70 RCLCPP_INFO_STREAM(
71 getLogger(),
72 "[" << getName() << "] Undoing path with controller: " << *options_->undoControllerName_);
73 plannerSwitcher->setDesiredController(*options_->undoControllerName_);
74 plannerSwitcher->commitPublish();
75 }
76 else
77 {
78 plannerSwitcher->setUndoPathBackwardPlanner();
79 }
80
81 // WARNING: There might be some race condition with the remote undo global planner/controller were the global path was not
82 // received yet, thee goal switcher
83 // TODO: waiting notification from global planner that it is loaded
84
85 RCLCPP_ERROR_STREAM(
86 getLogger(), "[" << getName()
87 << "] Waiting for 5 seconds to get planer/controller and wait goal checker "
88 "ready");
89
90 // rclcpp::sleep_for(5s);
91 RCLCPP_ERROR_STREAM(getLogger(), "[" << getName() << "] activating undo navigation planner");
92
93 if (options_ && options_->goalCheckerId_)
94 {
95 goalCheckerSwitcher->setGoalCheckerId(*options_->goalCheckerId_);
96 }
97 else
98 {
99 goalCheckerSwitcher->setGoalCheckerId("undo_path_backwards_goal_checker");
100 }
101
102 this->sendGoal(goal);
103 }
104}
105
107{
108 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] Exiting: undo navigation ");
109
110 if (this->navigationResult_ == rclcpp_action::ResultCode::SUCCEEDED)
111 {
112 RCLCPP_INFO_STREAM(
113 getLogger(), getName() << " - [CbUndoPathBackwards] Exiting: undo navigation successful, "
114 "popping odom tracker path");
115
116 requiresComponent(odomTracker, ComponentRequirement::HARD);
118
120 }
121 else
122 {
123 RCLCPP_INFO_STREAM(
124 getLogger(), getName() << " - [CbUndoPathBackwards] Exiting: undo navigation abort, avoiding "
125 "popping current path");
126
127 requiresComponent(odomTracker, ComponentRequirement::HARD);
129 // navigation interrupted or aborted. The path may be not totally undone.
130 // We keep the odom tracker in its current state, probably in the middle of the undoing process.
131 // Could you try to repeat the behavior?
132 }
133}
134
135} // 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)