Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
server_handler.hpp
1 // Copyright (c) 2020 Vinny Ruia
2 // Copyright (c) 2020 Sarthak Mittal
3 // Copyright (c) 2018 Intel Corporation
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License. Reserved.
16 
17 #ifndef BEHAVIOR_TREE__SERVER_HANDLER_HPP_
18 #define BEHAVIOR_TREE__SERVER_HANDLER_HPP_
19 
20 #include <memory>
21 #include <string>
22 #include <thread>
23 #include <utility>
24 #include <vector>
25 
26 #include "nav2_msgs/srv/clear_entire_costmap.hpp"
27 #include "nav2_msgs/srv/clear_costmap_around_robot.hpp"
28 #include "nav2_msgs/srv/clear_costmap_except_region.hpp"
29 #include "nav2_msgs/srv/clear_costmap_around_pose.hpp"
30 #include "nav2_msgs/srv/is_path_valid.hpp"
31 #include "nav2_msgs/action/compute_path_to_pose.hpp"
32 #include "nav2_msgs/action/follow_path.hpp"
33 #include "nav2_msgs/action/spin.hpp"
34 #include "nav2_msgs/action/back_up.hpp"
35 #include "nav2_msgs/action/wait.hpp"
36 #include "nav2_msgs/action/drive_on_heading.hpp"
37 #include "nav2_msgs/action/compute_path_through_poses.hpp"
38 #include "nav2_msgs/action/compute_route.hpp"
39 #include "nav2_msgs/action/smooth_path.hpp"
40 
41 #include "geometry_msgs/msg/point_stamped.hpp"
42 
43 #include "rclcpp/rclcpp.hpp"
44 
45 #include "dummy_action_server.hpp"
46 #include "dummy_service.hpp"
47 
48 namespace nav2_system_tests
49 {
50 
52  : public DummyActionServer<nav2_msgs::action::ComputePathToPose>
53 {
54 public:
55  explicit DummyComputePathToPoseActionServer(const rclcpp::Node::SharedPtr & node)
56  : DummyActionServer(node, "compute_path_to_pose")
57  {
58  geometry_msgs::msg::PoseStamped pose;
59  pose.header.stamp = node->get_clock()->now();
60  pose.header.frame_id = "map";
61  pose.pose.position.x = 0.0;
62  pose.pose.position.y = 0.0;
63  pose.pose.position.z = 0.0;
64  pose.pose.orientation.x = 0.0;
65  pose.pose.orientation.y = 0.0;
66  pose.pose.orientation.z = 0.0;
67  pose.pose.orientation.w = 1.0;
68 
69  result_->path.header.stamp = node->now();
70  result_->path.header.frame_id = pose.header.frame_id;
71  for (int i = 0; i < 6; ++i) {
72  result_->path.poses.push_back(pose);
73  }
74  }
75 
76 protected:
77  void updateResultForFailure(
78  std::shared_ptr<nav2_msgs::action::ComputePathToPose::Result>
79  & result) override
80  {
81  result->error_code = nav2_msgs::action::ComputePathToPose::Result::TIMEOUT;
82  result->error_msg = "Timeout";
83  }
84 };
85 
86 class DummyFollowPathActionServer : public DummyActionServer<nav2_msgs::action::FollowPath>
87 {
88 public:
89  explicit DummyFollowPathActionServer(const rclcpp::Node::SharedPtr & node)
90  : DummyActionServer(node, "follow_path") {}
91 
92 protected:
93  void updateResultForFailure(
94  std::shared_ptr<nav2_msgs::action::FollowPath::Result>
95  & result) override
96  {
97  result->error_code = nav2_msgs::action::FollowPath::Result::NO_VALID_CONTROL;
98  result->error_msg = "No valid control";
99  }
100 };
101 
102 class DummyClearEntireCostmapService : public DummyService<nav2_msgs::srv::ClearEntireCostmap>
103 {
104 public:
106  const rclcpp::Node::SharedPtr & node,
107  std::string service_name)
108  : DummyService(node, service_name) {}
109 
110 protected:
111  void fillResponse(
112  const std::shared_ptr<nav2_msgs::srv::ClearEntireCostmap::Request>/*request*/,
113  const std::shared_ptr<nav2_msgs::srv::ClearEntireCostmap::Response> response) override
114  {
115  response->success = true;
116  }
117 };
118 
120  : public DummyService<nav2_msgs::srv::ClearCostmapAroundRobot>
121 {
122 public:
124  const rclcpp::Node::SharedPtr & node,
125  std::string service_name)
126  : DummyService(node, service_name) {}
127 
128 protected:
129  void fillResponse(
130  const std::shared_ptr<nav2_msgs::srv::ClearCostmapAroundRobot::Request>/*request*/,
131  const std::shared_ptr<nav2_msgs::srv::ClearCostmapAroundRobot::Response> response) override
132  {
133  response->success = true;
134  }
135 };
136 
138  : public DummyService<nav2_msgs::srv::ClearCostmapExceptRegion>
139 {
140 public:
142  const rclcpp::Node::SharedPtr & node,
143  std::string service_name)
144  : DummyService(node, service_name) {}
145 
146 protected:
147  void fillResponse(
148  const std::shared_ptr<nav2_msgs::srv::ClearCostmapExceptRegion::Request>/*request*/,
149  const std::shared_ptr<nav2_msgs::srv::ClearCostmapExceptRegion::Response> response) override
150  {
151  response->success = true;
152  }
153 };
154 
156  : public DummyService<nav2_msgs::srv::ClearCostmapAroundPose>
157 {
158 public:
160  const rclcpp::Node::SharedPtr & node,
161  std::string service_name)
162  : DummyService(node, service_name) {}
163 
164 protected:
165  void fillResponse(
166  const std::shared_ptr<nav2_msgs::srv::ClearCostmapAroundPose::Request>/*request*/,
167  const std::shared_ptr<nav2_msgs::srv::ClearCostmapAroundPose::Response> response) override
168  {
169  response->success = true;
170  }
171 };
172 
174 {
175 public:
176  ServerHandler();
177  ~ServerHandler();
178 
179  void activate();
180 
181  void deactivate();
182 
183  bool isActive() const
184  {
185  return is_active_;
186  }
187 
188  void reset() const;
189 
190 public:
191  std::unique_ptr<DummyClearEntireCostmapService> clear_local_costmap_server;
192  std::unique_ptr<DummyClearEntireCostmapService> clear_global_costmap_server;
193  std::unique_ptr<DummyClearCostmapAroundRobotService> clear_costmap_around_robot_server;
194  std::unique_ptr<DummyClearCostmapExceptRegionService> clear_costmap_except_region_server;
195  std::unique_ptr<DummyClearCostmapAroundPoseService> clear_costmap_around_pose_server;
196  std::unique_ptr<DummyService<nav2_msgs::srv::IsPathValid>> validate_path_server;
197  std::unique_ptr<DummyComputePathToPoseActionServer> compute_path_to_pose_server;
198  std::unique_ptr<DummyFollowPathActionServer> follow_path_server;
199  std::unique_ptr<DummyActionServer<nav2_msgs::action::Spin>> spin_server;
200  std::unique_ptr<DummyActionServer<nav2_msgs::action::Wait>> wait_server;
201  std::unique_ptr<DummyActionServer<nav2_msgs::action::BackUp>> backup_server;
202  std::unique_ptr<DummyActionServer<nav2_msgs::action::ComputeRoute>> compute_route_server;
203  std::unique_ptr<DummyActionServer<nav2_msgs::action::SmoothPath>> smoother_server;
204  std::unique_ptr<DummyActionServer<nav2_msgs::action::DriveOnHeading>> drive_on_heading_server;
205  std::unique_ptr<DummyActionServer<nav2_msgs::action::ComputePathThroughPoses>> ntp_server;
206 
207 private:
208  void spinThread();
209 
210  bool is_active_;
211  rclcpp::Node::SharedPtr node_;
212  std::shared_ptr<std::thread> server_thread_;
213 };
214 
215 } // namespace nav2_system_tests
216 
217 #endif // BEHAVIOR_TREE__SERVER_HANDLER_HPP_