15 #ifndef BEHAVIOR_TREE__DUMMY_SERVERS_HPP_
16 #define BEHAVIOR_TREE__DUMMY_SERVERS_HPP_
24 #include "rclcpp_action/rclcpp_action.hpp"
25 #include "rclcpp/rclcpp.hpp"
27 using namespace std::chrono_literals;
28 using namespace std::chrono;
29 using namespace std::placeholders;
31 template<
class ServiceT>
36 const rclcpp::Node::SharedPtr & node,
37 std::string service_name)
39 service_name_(service_name),
43 server_ = node->create_service<ServiceT>(
45 std::bind(&DummyService::handle_service,
this, _1, _2, _3));
57 server_ = node_->create_service<ServiceT>(
59 std::bind(&DummyService::handle_service,
this, _1, _2, _3));
70 int getRequestCount()
const
72 return request_count_;
76 virtual void fillResponse(
77 const std::shared_ptr<typename ServiceT::Request>,
78 const std::shared_ptr<typename ServiceT::Response>) {}
81 const std::shared_ptr<rmw_request_id_t>,
82 const std::shared_ptr<typename ServiceT::Request> request,
83 const std::shared_ptr<typename ServiceT::Response> response)
86 fillResponse(request, response);
90 rclcpp::Node::SharedPtr node_;
91 typename rclcpp::Service<ServiceT>::SharedPtr server_;
92 std::string service_name_;
97 template<
class ActionT>
102 const rclcpp::Node::SharedPtr & node,
103 std::string action_name)
104 : action_name_(action_name),
107 this->action_server_ = rclcpp_action::create_server<ActionT>(
108 node->get_node_base_interface(),
109 node->get_node_clock_interface(),
110 node->get_node_logging_interface(),
111 node->get_node_waitables_interface(),
113 std::bind(&DummyActionServer::handle_goal,
this, _1, _2),
114 std::bind(&DummyActionServer::handle_cancel,
this, _1),
115 std::bind(&DummyActionServer::handle_accepted,
this, _1));
118 void setFailureRanges(
const std::vector<std::pair<int, int>> & failureRanges)
120 failure_ranges_ = failureRanges;
123 void setRunningRanges(
const std::vector<std::pair<int, int>> & runningRanges)
125 running_ranges_ = runningRanges;
130 failure_ranges_.clear();
131 running_ranges_.clear();
135 int getGoalCount()
const
141 virtual std::shared_ptr<typename ActionT::Result> fillResult()
143 return std::make_shared<typename ActionT::Result>();
146 virtual rclcpp_action::GoalResponse handle_goal(
147 const rclcpp_action::GoalUUID &,
148 std::shared_ptr<const typename ActionT::Goal>)
150 return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
153 virtual rclcpp_action::CancelResponse handle_cancel(
154 const typename std::shared_ptr<rclcpp_action::ServerGoalHandle<ActionT>>)
156 return rclcpp_action::CancelResponse::ACCEPT;
160 const typename std::shared_ptr<rclcpp_action::ServerGoalHandle<ActionT>> goal_handle)
163 auto result = fillResult();
167 for (
auto & index : running_ranges_) {
168 if (goal_count_ >= index.first && goal_count_ <= index.second) {
169 std::this_thread::sleep_for(1s);
175 for (
auto & index : failure_ranges_) {
176 if (goal_count_ >= index.first && goal_count_ <= index.second) {
177 goal_handle->abort(result);
183 goal_handle->succeed(result);
186 void handle_accepted(
187 const std::shared_ptr<rclcpp_action::ServerGoalHandle<ActionT>> goal_handle)
189 using namespace std::placeholders;
191 std::thread{std::bind(&DummyActionServer::execute,
this, _1), goal_handle}.detach();
195 typename rclcpp_action::Server<ActionT>::SharedPtr action_server_;
196 std::string action_name_;
201 std::vector<std::pair<int, int>> failure_ranges_;
202 std::vector<std::pair<int, int>> running_ranges_;