Nav2 Navigation Stack - jazzy  jazzy
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<DummyService<nav2_msgs::srv::ClearEntireCostmap>>(
35  node_, "local_costmap/clear_entirely_local_costmap");
36  clear_global_costmap_server = std::make_unique<DummyService<nav2_msgs::srv::ClearEntireCostmap>>(
37  node_, "global_costmap/clear_entirely_global_costmap");
38  compute_path_to_pose_server = std::make_unique<DummyComputePathToPoseActionServer>(node_);
39  follow_path_server = std::make_unique<DummyFollowPathActionServer>(node_);
40  spin_server = std::make_unique<DummyActionServer<nav2_msgs::action::Spin>>(
41  node_, "spin");
42  wait_server = std::make_unique<DummyActionServer<nav2_msgs::action::Wait>>(
43  node_, "wait");
44  backup_server = std::make_unique<DummyActionServer<nav2_msgs::action::BackUp>>(
45  node_, "backup");
46  drive_on_heading_server = std::make_unique<DummyActionServer<nav2_msgs::action::DriveOnHeading>>(
47  node_, "drive_on_heading");
48  ntp_server = std::make_unique<DummyActionServer<nav2_msgs::action::ComputePathThroughPoses>>(
49  node_, "compute_path_through_poses");
50 }
51 
52 ServerHandler::~ServerHandler()
53 {
54  if (is_active_) {
55  deactivate();
56  }
57 }
58 
59 void ServerHandler::activate()
60 {
61  if (is_active_) {
62  throw std::runtime_error("Trying to activate while already activated");
63  }
64 
65  is_active_ = true;
66  server_thread_ =
67  std::make_shared<std::thread>(std::bind(&ServerHandler::spinThread, this));
68 
69  std::cout << "Server handler is active!" << std::endl;
70 }
71 
72 void ServerHandler::deactivate()
73 {
74  if (!is_active_) {
75  throw std::runtime_error("Trying to deactivate while already inactive");
76  }
77 
78  is_active_ = false;
79  server_thread_->join();
80 
81  std::cout << "Server handler has been deactivated!" << std::endl;
82 }
83 
84 void ServerHandler::reset() const
85 {
86  clear_global_costmap_server->reset();
87  clear_local_costmap_server->reset();
88  compute_path_to_pose_server->reset();
89  follow_path_server->reset();
90  spin_server->reset();
91  wait_server->reset();
92  backup_server->reset();
93  drive_on_heading_server->reset();
94 }
95 
96 void ServerHandler::spinThread()
97 {
98  rclcpp::spin(node_);
99 }
100 
101 } // namespace nav2_system_tests