Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
pause_resume_controller.hpp
1 // Copyright (c) 2025 Enjoy Robotics
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 #ifndef NAV2_BEHAVIOR_TREE__PLUGINS__CONTROL__PAUSE_RESUME_CONTROLLER_HPP_
16 #define NAV2_BEHAVIOR_TREE__PLUGINS__CONTROL__PAUSE_RESUME_CONTROLLER_HPP_
17 
18 // Other includes
19 #include <string>
20 #include <memory>
21 #include <map>
22 
23 // ROS includes
24 #include "rclcpp/rclcpp.hpp"
25 #include "rclcpp/callback_group.hpp"
26 #include "rclcpp/executors/single_threaded_executor.hpp"
27 #include "behaviortree_cpp/control_node.h"
28 #include "nav2_ros_common/service_server.hpp"
29 #include "nav2_ros_common/lifecycle_node.hpp"
30 
31 // Interface definitions
32 #include "std_srvs/srv/trigger.hpp"
33 
34 
35 namespace nav2_behavior_tree
36 {
37 
38 using Trigger = std_srvs::srv::Trigger;
39 
40 enum state_t {RESUMED, PAUSED, PAUSE_REQUESTED, ON_PAUSE, RESUME_REQUESTED, ON_RESUME};
41 const std::map<state_t, std::string> state_names = {
42  {RESUMED, "RESUMED"},
43  {PAUSED, "PAUSED"},
44  {PAUSE_REQUESTED, "PAUSE_REQUESTED"},
45  {ON_PAUSE, "ON_PAUSE"},
46  {RESUME_REQUESTED, "RESUME_REQUESTED"},
47  {ON_RESUME, "ON_RESUME"}
48 };
49 const std::map<state_t, uint16_t> child_indices = {
50  {RESUMED, 0},
51  {PAUSED, 1},
52  {ON_PAUSE, 2},
53  {ON_RESUME, 3}
54 };
55 
106 class PauseResumeController : public BT::ControlNode
107 {
108 public:
111  const std::string & xml_tag_name,
112  const BT::NodeConfiguration & conf);
113 
115  void halt() override;
116 
118  BT::NodeStatus tick() override;
119 
121  static BT::PortsList providedPorts()
122  {
123  return {
124  BT::InputPort<std::string>(
125  "pause_service_name",
126  "Name of the service to pause"),
127  BT::InputPort<std::string>(
128  "resume_service_name",
129  "Name of the service to resume"),
130  };
131  }
132 
133  [[nodiscard]] inline state_t getState() const
134  {
135  return state_;
136  }
137 
138 private:
140  void pauseServiceCallback(
141  const std::shared_ptr<rmw_request_id_t>/*request_header*/,
142  const std::shared_ptr<Trigger::Request> request,
143  std::shared_ptr<Trigger::Response> response);
144 
146  void resumeServiceCallback(
147  const std::shared_ptr<rmw_request_id_t>/*request_header*/,
148  const std::shared_ptr<Trigger::Request> request,
149  std::shared_ptr<Trigger::Response> response);
150 
161  void switchToNextState();
162 
163  rclcpp::Logger logger_{rclcpp::get_logger("PauseResumeController")};
164  rclcpp::CallbackGroup::SharedPtr cb_group_;
165  rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
166  nav2::ServiceServer<Trigger>::SharedPtr pause_srv_;
167  nav2::ServiceServer<Trigger>::SharedPtr resume_srv_;
168  state_t state_;
169 };
170 
171 } // namespace nav2_behavior_tree
172 
173 #endif // NAV2_BEHAVIOR_TREE__PLUGINS__CONTROL__PAUSE_RESUME_CONTROLLER_HPP_
Controlled through service calls to pause and resume the execution of the tree It has one mandatory c...
PauseResumeController(const std::string &xml_tag_name, const BT::NodeConfiguration &conf)
Constructor.
BT::NodeStatus tick() override
Handle transitions if requested and tick child related to the actual state.
static BT::PortsList providedPorts()
Declare ports.
void halt() override
Reset state and go to Idle.