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