Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
server_handler.cpp
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 #include <memory>
18 #include <thread>
19 
20 #include "server_handler.hpp"
21 
22 using namespace std::chrono_literals; // NOLINT
23 using namespace std::chrono; // NOLINT
24 
25 namespace nav2_system_tests
26 {
27 
28 
29 ServerHandler::ServerHandler()
30 : is_active_(false)
31 {
32  node_ = rclcpp::Node::make_shared("behavior_tree_tester");
33 
34  clear_local_costmap_server = std::make_unique<DummyClearEntireCostmapService>(
35  node_, "local_costmap/clear_entirely_local_costmap");
36  clear_global_costmap_server = std::make_unique<DummyClearEntireCostmapService>(
37  node_, "global_costmap/clear_entirely_global_costmap");
38  clear_costmap_around_robot_server = std::make_unique<DummyClearCostmapAroundRobotService>(
39  node_, "local_costmap/clear_around_local_costmap");
40  clear_costmap_except_region_server = std::make_unique<DummyClearCostmapExceptRegionService>(
41  node_, "local_costmap/clear_except_local_costmap");
42  clear_costmap_around_pose_server = std::make_unique<DummyClearCostmapAroundPoseService>(
43  node_, "local_costmap/clear_around_pose_local_costmap");
44  validate_path_server = std::make_unique<DummyService<nav2_msgs::srv::IsPathValid>>(
45  node_, "is_path_valid");
46  compute_path_to_pose_server = std::make_unique<DummyComputePathToPoseActionServer>(node_);
47  follow_path_server = std::make_unique<DummyFollowPathActionServer>(node_);
48  spin_server = std::make_unique<DummyActionServer<nav2_msgs::action::Spin>>(
49  node_, "spin");
50  wait_server = std::make_unique<DummyActionServer<nav2_msgs::action::Wait>>(
51  node_, "wait");
52  backup_server = std::make_unique<DummyActionServer<nav2_msgs::action::BackUp>>(
53  node_, "backup");
54  compute_route_server = std::make_unique<DummyActionServer<nav2_msgs::action::ComputeRoute>>(
55  node_, "compute_route");
56  smoother_server = std::make_unique<DummyActionServer<nav2_msgs::action::SmoothPath>>(
57  node_, "smooth_path");
58  drive_on_heading_server = std::make_unique<DummyActionServer<nav2_msgs::action::DriveOnHeading>>(
59  node_, "drive_on_heading");
60  ntp_server = std::make_unique<DummyActionServer<nav2_msgs::action::ComputePathThroughPoses>>(
61  node_, "compute_path_through_poses");
62 }
63 
64 ServerHandler::~ServerHandler()
65 {
66  if (is_active_) {
67  deactivate();
68  }
69 }
70 
71 void ServerHandler::activate()
72 {
73  if (is_active_) {
74  throw std::runtime_error("Trying to activate while already activated");
75  }
76 
77  is_active_ = true;
78  server_thread_ =
79  std::make_shared<std::thread>(std::bind(&ServerHandler::spinThread, this));
80 
81  std::cout << "Server handler is active!" << std::endl;
82 }
83 
84 void ServerHandler::deactivate()
85 {
86  if (!is_active_) {
87  throw std::runtime_error("Trying to deactivate while already inactive");
88  }
89 
90  is_active_ = false;
91  server_thread_->join();
92 
93  std::cout << "Server handler has been deactivated!" << std::endl;
94 }
95 
96 void ServerHandler::reset() const
97 {
98  clear_global_costmap_server->reset();
99  clear_local_costmap_server->reset();
100  clear_costmap_around_robot_server->reset();
101  clear_costmap_except_region_server->reset();
102  clear_costmap_around_pose_server->reset();
103  compute_path_to_pose_server->reset();
104  follow_path_server->reset();
105  spin_server->reset();
106  wait_server->reset();
107  backup_server->reset();
108  drive_on_heading_server->reset();
109 }
110 
111 void ServerHandler::spinThread()
112 {
113  rclcpp::spin(node_);
114 }
115 
116 } // namespace nav2_system_tests