Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
costmap_filter_info_server.cpp
1 // Copyright (c) 2020 Samsung Research Russia
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // TODO(AlexeyMerzlyakov): This dummy info publisher should be removed
16 // after Semantic Map Server having the same functionality will be developed.
17 
18 #include "nav2_map_server/costmap_filter_info_server.hpp"
19 
20 #include <string>
21 #include <memory>
22 #include <utility>
23 
24 namespace nav2_map_server
25 {
26 
27 CostmapFilterInfoServer::CostmapFilterInfoServer(const rclcpp::NodeOptions & options)
28 : nav2_util::LifecycleNode("costmap_filter_info_server", "", options)
29 {
30  declare_parameter("filter_info_topic", "costmap_filter_info");
31  declare_parameter("type", 0);
32  declare_parameter("mask_topic", "filter_mask");
33  declare_parameter("base", 0.0);
34  declare_parameter("multiplier", 1.0);
35 }
36 
38 {
39 }
40 
41 nav2_util::CallbackReturn
42 CostmapFilterInfoServer::on_configure(const rclcpp_lifecycle::State & /*state*/)
43 {
44  RCLCPP_INFO(get_logger(), "Configuring");
45 
46  std::string filter_info_topic = get_parameter("filter_info_topic").as_string();
47 
48  publisher_ = this->create_publisher<nav2_msgs::msg::CostmapFilterInfo>(
49  filter_info_topic, rclcpp::QoS(rclcpp::KeepLast(1)).transient_local().reliable());
50 
51  msg_ = nav2_msgs::msg::CostmapFilterInfo();
52  msg_.header.frame_id = "";
53  msg_.header.stamp = now();
54  msg_.type = get_parameter("type").as_int();
55  msg_.filter_mask_topic = get_parameter("mask_topic").as_string();
56  msg_.base = static_cast<float>(get_parameter("base").as_double());
57  msg_.multiplier = static_cast<float>(get_parameter("multiplier").as_double());
58 
59  return nav2_util::CallbackReturn::SUCCESS;
60 }
61 
62 nav2_util::CallbackReturn
63 CostmapFilterInfoServer::on_activate(const rclcpp_lifecycle::State & /*state*/)
64 {
65  RCLCPP_INFO(get_logger(), "Activating");
66 
67  publisher_->on_activate();
68  auto costmap_filter_info = std::make_unique<nav2_msgs::msg::CostmapFilterInfo>(msg_);
69  publisher_->publish(std::move(costmap_filter_info));
70 
71  // create bond connection
72  createBond();
73 
74  return nav2_util::CallbackReturn::SUCCESS;
75 }
76 
77 nav2_util::CallbackReturn
78 CostmapFilterInfoServer::on_deactivate(const rclcpp_lifecycle::State & /*state*/)
79 {
80  RCLCPP_INFO(get_logger(), "Deactivating");
81 
82  publisher_->on_deactivate();
83 
84  // destroy bond connection
85  destroyBond();
86 
87  return nav2_util::CallbackReturn::SUCCESS;
88 }
89 
90 nav2_util::CallbackReturn
91 CostmapFilterInfoServer::on_cleanup(const rclcpp_lifecycle::State & /*state*/)
92 {
93  RCLCPP_INFO(get_logger(), "Cleaning up");
94 
95  publisher_.reset();
96 
97  return nav2_util::CallbackReturn::SUCCESS;
98 }
99 
100 nav2_util::CallbackReturn
101 CostmapFilterInfoServer::on_shutdown(const rclcpp_lifecycle::State & /*state*/)
102 {
103  RCLCPP_INFO(get_logger(), "Shutting down");
104 
105  return nav2_util::CallbackReturn::SUCCESS;
106 }
107 
108 } // namespace nav2_map_server
109 
110 #include "rclcpp_components/register_node_macro.hpp"
111 
112 // Register the component with class_loader.
113 // This acts as a sort of entry point, allowing the component to be discoverable when its library
114 // is being loaded into a running process.
115 RCLCPP_COMPONENTS_REGISTER_NODE(nav2_map_server::CostmapFilterInfoServer)
~CostmapFilterInfoServer()
Destructor for the nav2_map_server::CostmapFilterInfoServer.
nav2_util::CallbackReturn on_configure(const rclcpp_lifecycle::State &state) override
Creates CostmapFilterInfo publisher and forms published message from ROS parameters.
nav2_util::CallbackReturn on_cleanup(const rclcpp_lifecycle::State &state) override
Resets publisher.
CostmapFilterInfoServer(const rclcpp::NodeOptions &options=rclcpp::NodeOptions())
Constructor for the nav2_map_server::CostmapFilterInfoServer.
nav2_util::CallbackReturn on_deactivate(const rclcpp_lifecycle::State &state) override
Deactivates publisher.
nav2_util::CallbackReturn on_shutdown(const rclcpp_lifecycle::State &state) override
Called when in Shutdown state.
nav2_util::CallbackReturn on_activate(const rclcpp_lifecycle::State &state) override
Publishes a CostmapFilterInfo message.
void createBond()
Create bond connection to lifecycle manager.
void destroyBond()
Destroy bond connection to lifecycle manager.