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