|
| 1 | +// Copyright (c) 2019 Intel Corporation |
| 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_HPP_ |
| 16 | +#define NAV2_BEHAVIOR_TREE__PLUGINS__CONTROL__PAUSE_HPP_ |
| 17 | + |
| 18 | +// Other includes |
| 19 | +#include <string> |
| 20 | +#include <memory> |
| 21 | +#include <mutex> |
| 22 | + |
| 23 | +// ROS includes |
| 24 | +#include "rclcpp/rclcpp.hpp" |
| 25 | +#include "behaviortree_cpp/control_node.h" |
| 26 | + |
| 27 | +// Interface definitions |
| 28 | +#include "std_srvs/srv/trigger.hpp" |
| 29 | + |
| 30 | + |
| 31 | +namespace nav2_behavior_tree |
| 32 | +{ |
| 33 | + |
| 34 | +using Trigger = std_srvs::srv::Trigger; |
| 35 | + |
| 36 | +enum state_t {UNPAUSED, PAUSED, PAUSE_REQUESTED, ON_PAUSE, RESUME_REQUESTED, ON_RESUME}; |
| 37 | + |
| 38 | +/* @brief Controlled through service calls to pause and resume the execution of the tree |
| 39 | + * It has one mandatory child for the UNPAUSED, and three optional for the PAUSED state, |
| 40 | + * the ON_PAUSE event and the ON_RESUME event. |
| 41 | + * It has two input ports: |
| 42 | + * - pause_service_name: name of the service to pause |
| 43 | + * - resume_service_name: name of the service to resume |
| 44 | + */ |
| 45 | +class Pause : public BT::ControlNode |
| 46 | +{ |
| 47 | +public: |
| 48 | + //! @brief Constructor |
| 49 | + Pause( |
| 50 | + const std::string & xml_tag_name, |
| 51 | + const BT::NodeConfiguration & conf); |
| 52 | + |
| 53 | + //! @brief Destructor |
| 54 | + ~Pause(); |
| 55 | + |
| 56 | + //! @brief Reset state and go to Idle |
| 57 | + void halt() override; |
| 58 | + |
| 59 | + //! @brief Return a NodeStatus according to the children's status |
| 60 | + BT::NodeStatus tick() override; |
| 61 | + |
| 62 | + //! @brief Declare ports |
| 63 | + static BT::PortsList providedPorts() |
| 64 | + { |
| 65 | + return { |
| 66 | + BT::InputPort<std::string>( |
| 67 | + "pause_service_name", |
| 68 | + "Name of the service to pause"), |
| 69 | + BT::InputPort<std::string>( |
| 70 | + "resume_service_name", |
| 71 | + "Name of the service to resume"), |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | +private: |
| 76 | + //! @brief Service callback to pause |
| 77 | + void pause_service_callback( |
| 78 | + const std::shared_ptr<Trigger::Request> request, |
| 79 | + std::shared_ptr<Trigger::Response> response); |
| 80 | + |
| 81 | + //! @brief Service callback to resume |
| 82 | + void resume_service_callback( |
| 83 | + const std::shared_ptr<Trigger::Request> request, |
| 84 | + std::shared_ptr<Trigger::Response> response); |
| 85 | + |
| 86 | + rclcpp::Node::SharedPtr node_; |
| 87 | + rclcpp::CallbackGroup::SharedPtr cb_group_; |
| 88 | + 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_; |
| 92 | + state_t state_; |
| 93 | + std::mutex state_mutex_; |
| 94 | +}; |
| 95 | + |
| 96 | +} // namespace nav2_behavior_tree |
| 97 | + |
| 98 | +#endif // NAV2_BEHAVIOR_TREE__PLUGINS__CONTROL__PAUSE_HPP_ |
0 commit comments