1
- // Copyright (c) 2019 Intel Corporation
1
+ // Copyright (c) 2025 Intel Corporation
2
2
//
3
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
4
// you may not use this file except in compliance with the License.
18
18
// Other includes
19
19
#include < string>
20
20
#include < memory>
21
- #include < mutex >
21
+ #include < map >
22
22
23
23
// ROS includes
24
24
#include " rclcpp/rclcpp.hpp"
25
+ #include " rclcpp/callback_group.hpp"
26
+ #include " rclcpp/executors/single_threaded_executor.hpp"
25
27
#include " behaviortree_cpp/control_node.h"
28
+ #include " nav2_util/service_server.hpp"
26
29
27
30
// Interface definitions
28
31
#include " std_srvs/srv/trigger.hpp"
@@ -34,25 +37,49 @@ namespace nav2_behavior_tree
34
37
using Trigger = std_srvs::srv::Trigger;
35
38
36
39
enum state_t {UNPAUSED, PAUSED, PAUSE_REQUESTED, ON_PAUSE, RESUME_REQUESTED, ON_RESUME};
40
+ const std::map<state_t , std::string> state_names = {
41
+ {UNPAUSED, " UNPAUSED" },
42
+ {PAUSED, " PAUSED" },
43
+ {PAUSE_REQUESTED, " PAUSE_REQUESTED" },
44
+ {ON_PAUSE, " ON_PAUSE" },
45
+ {RESUME_REQUESTED, " RESUME_REQUESTED" },
46
+ {ON_RESUME, " ON_RESUME" }
47
+ };
48
+ const std::map<state_t , uint> child_indices = {
49
+ {UNPAUSED, 0 },
50
+ {PAUSED, 1 },
51
+ {ON_PAUSE, 2 },
52
+ {ON_RESUME, 3 }
53
+ };
37
54
38
55
/* @brief Controlled through service calls to pause and resume the execution of the tree
39
56
* It has one mandatory child for the UNPAUSED, and three optional for the PAUSED state,
40
57
* the ON_PAUSE event and the ON_RESUME event.
41
58
* It has two input ports:
42
59
* - pause_service_name: name of the service to pause
43
60
* - resume_service_name: name of the service to resume
61
+ *
62
+ * Usage:
63
+ * <Pause pause_service_name="/pause" resume_service_name="/resume">
64
+ * <!-- UNPAUSED branch -->
65
+ *
66
+ * <!-- PAUSED branch (optional) -->
67
+ *
68
+ * <!-- ON_PAUSE branch (optional) -->
69
+ *
70
+ * <!-- ON_RESUME branch (optional) -->
71
+ * </Pause>
44
72
*/
45
- class Pause : public BT ::ControlNode
73
+
74
+
75
+ class PauseResumeController : public BT ::ControlNode
46
76
{
47
77
public:
48
78
// ! @brief Constructor
49
- Pause (
79
+ PauseResumeController (
50
80
const std::string & xml_tag_name,
51
81
const BT::NodeConfiguration & conf);
52
82
53
- // ! @brief Destructor
54
- ~Pause ();
55
-
56
83
// ! @brief Reset state and go to Idle
57
84
void halt () override ;
58
85
@@ -74,23 +101,27 @@ class Pause : public BT::ControlNode
74
101
75
102
private:
76
103
// ! @brief Service callback to pause
77
- void pause_service_callback (
104
+ void pauseServiceCallback (
105
+ const std::shared_ptr<rmw_request_id_t >/* request_header*/ ,
78
106
const std::shared_ptr<Trigger::Request> request,
79
107
std::shared_ptr<Trigger::Response> response);
80
108
81
109
// ! @brief Service callback to resume
82
- void resume_service_callback (
110
+ void resumeServiceCallback (
111
+ const std::shared_ptr<rmw_request_id_t >/* request_header*/ ,
83
112
const std::shared_ptr<Trigger::Request> request,
84
113
std::shared_ptr<Trigger::Response> response);
85
114
115
+ BT::NodeStatus tickChildAndTransition ();
116
+
117
+ void switchState (const state_t new_state);
118
+
86
119
rclcpp::Node::SharedPtr node_;
87
120
rclcpp::CallbackGroup::SharedPtr cb_group_;
88
121
rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
89
- std::unique_ptr<std::thread> spinner_thread_;
90
- rclcpp::Service<Trigger>::SharedPtr pause_srv_;
91
- rclcpp::Service<Trigger>::SharedPtr resume_srv_;
122
+ nav2_util::ServiceServer<Trigger>::SharedPtr pause_srv_;
123
+ nav2_util::ServiceServer<Trigger>::SharedPtr resume_srv_;
92
124
state_t state_;
93
- std::mutex state_mutex_;
94
125
};
95
126
96
127
} // namespace nav2_behavior_tree
0 commit comments