Nav2 Navigation Stack - lyrical  lyrical
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::LifecycleNode("costmap_filter_info_server", "", options)
29 {
30 }
31 
33 {
34 }
35 
36 nav2::CallbackReturn
37 CostmapFilterInfoServer::on_configure(const rclcpp_lifecycle::State & /*state*/)
38 {
39  RCLCPP_INFO(get_logger(), "Configuring");
40  auto node = shared_from_this();
41 
42  std::string filter_info_topic = node->declare_or_get_parameter(
43  "filter_info_topic", std::string("costmap_filter_info"));
44 
45  publisher_ = this->create_publisher<nav2_msgs::msg::CostmapFilterInfo>(
46  filter_info_topic, nav2::qos::LatchedPublisherQoS());
47 
48  msg_ = nav2_msgs::msg::CostmapFilterInfo();
49  msg_.header.frame_id = "";
50  msg_.header.stamp = now();
51  msg_.type = node->declare_or_get_parameter("type", 0);
52  msg_.filter_mask_topic = node->declare_or_get_parameter(
53  "mask_topic", std::string("filter_mask"));
54  msg_.base = static_cast<float>(
55  node->declare_or_get_parameter("base", 0.0));
56  msg_.multiplier = static_cast<float>(
57  node->declare_or_get_parameter("multiplier", 1.0));
58 
59  return nav2::CallbackReturn::SUCCESS;
60 }
61 
62 nav2::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::CallbackReturn::SUCCESS;
75 }
76 
77 nav2::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::CallbackReturn::SUCCESS;
88 }
89 
90 nav2::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::CallbackReturn::SUCCESS;
98 }
99 
100 nav2::CallbackReturn
101 CostmapFilterInfoServer::on_shutdown(const rclcpp_lifecycle::State & /*state*/)
102 {
103  RCLCPP_INFO(get_logger(), "Shutting down");
104 
105  return nav2::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)
void destroyBond()
Destroy bond connection to lifecycle manager.
nav2::LifecycleNode::SharedPtr shared_from_this()
Get a shared pointer of this.
void createBond()
Create bond connection to lifecycle manager.
A QoS profile for latched, reliable topics with a history of 1 messages.
nav2::CallbackReturn on_activate(const rclcpp_lifecycle::State &state) override
Publishes a CostmapFilterInfo message.
nav2::CallbackReturn on_shutdown(const rclcpp_lifecycle::State &state) override
Called when in Shutdown state.
~CostmapFilterInfoServer()
Destructor for the nav2_map_server::CostmapFilterInfoServer.
nav2::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::CallbackReturn on_deactivate(const rclcpp_lifecycle::State &state) override
Deactivates publisher.
nav2::CallbackReturn on_configure(const rclcpp_lifecycle::State &state) override
Creates CostmapFilterInfo publisher and forms published message from ROS parameters.