15 #ifndef BEHAVIOR_TREE__DUMMY_ACTION_SERVER_HPP_
16 #define BEHAVIOR_TREE__DUMMY_ACTION_SERVER_HPP_
24 #include "rclcpp_action/rclcpp_action.hpp"
25 #include "rclcpp/rclcpp.hpp"
27 namespace nav2_system_tests
30 using namespace std::chrono_literals;
31 using namespace std::chrono;
32 using namespace std::placeholders;
34 using Range = std::pair<unsigned int, unsigned int>;
35 using Ranges = std::vector<Range>;
37 template<
class ActionT>
42 const rclcpp::Node::SharedPtr & node,
43 std::string action_name)
44 : action_name_(action_name),
47 action_server_ = rclcpp_action::create_server<ActionT>(
48 node->get_node_base_interface(),
49 node->get_node_clock_interface(),
50 node->get_node_logging_interface(),
51 node->get_node_waitables_interface(),
53 std::bind(&DummyActionServer::handle_goal,
this, _1, _2),
54 std::bind(&DummyActionServer::handle_cancel,
this, _1),
55 std::bind(&DummyActionServer::handle_accepted,
this, _1));
59 void setFailureRanges(
const Ranges & failureRanges)
61 failure_ranges_ = failureRanges;
64 void setRunningRanges(
const Ranges & runningRanges)
66 running_ranges_ = runningRanges;
71 failure_ranges_.clear();
72 running_ranges_.clear();
76 int getGoalCount()
const
82 virtual std::shared_ptr<typename ActionT::Result> fillResult()
84 return std::make_shared<typename ActionT::Result>();
87 virtual void updateResultForFailure(std::shared_ptr<typename ActionT::Result> &)
91 virtual void updateResultForSuccess(std::shared_ptr<typename ActionT::Result> &)
95 virtual rclcpp_action::GoalResponse handle_goal(
96 const rclcpp_action::GoalUUID &,
97 std::shared_ptr<const typename ActionT::Goal>)
99 return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
102 virtual rclcpp_action::CancelResponse handle_cancel(
103 const typename std::shared_ptr<rclcpp_action::ServerGoalHandle<ActionT>>)
105 return rclcpp_action::CancelResponse::ACCEPT;
109 const typename std::shared_ptr<rclcpp_action::ServerGoalHandle<ActionT>> goal_handle)
112 auto result = fillResult();
116 for (
auto & index : running_ranges_) {
117 if (goal_count_ >= index.first && goal_count_ <= index.second) {
118 std::this_thread::sleep_for(1s);
124 for (
auto & index : failure_ranges_) {
125 if (goal_count_ >= index.first && goal_count_ <= index.second) {
126 updateResultForFailure(result);
127 goal_handle->abort(result);
133 updateResultForSuccess(result);
134 goal_handle->succeed(result);
137 void handle_accepted(
138 const std::shared_ptr<rclcpp_action::ServerGoalHandle<ActionT>> goal_handle)
140 using namespace std::placeholders;
142 std::thread{std::bind(&DummyActionServer::execute,
this, _1), goal_handle}.detach();
146 typename rclcpp_action::Server<ActionT>::SharedPtr action_server_;
147 std::string action_name_;
152 Ranges failure_ranges_;
153 Ranges running_ranges_;
155 unsigned int goal_count_;