Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
costmap_subscriber.hpp
1 // Copyright (c) 2019 Intel Corporation
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 #ifndef NAV2_COSTMAP_2D__COSTMAP_SUBSCRIBER_HPP_
16 #define NAV2_COSTMAP_2D__COSTMAP_SUBSCRIBER_HPP_
17 
18 #include <atomic>
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 
23 
24 #include "rclcpp/rclcpp.hpp"
25 #include "nav2_costmap_2d/costmap_2d.hpp"
26 #include "nav2_msgs/msg/costmap.hpp"
27 #include "nav2_msgs/msg/costmap_update.hpp"
28 #include "nav2_ros_common/lifecycle_node.hpp"
29 
30 namespace nav2_costmap_2d
31 {
37 {
38 public:
43  const nav2::LifecycleNode::WeakPtr & parent,
44  const std::string & topic_name);
45 
46  template<typename NodeT>
48  const NodeT & parent,
49  const std::string & topic_name,
50  const rclcpp::CallbackGroup::SharedPtr & callback_group = nullptr)
51  : topic_name_(topic_name)
52  {
53  logger_ = parent->get_logger();
54 
55  // Could be using a user rclcpp::Node, so need to use the Nav2 factory to create the
56  // subscription to convert nav2::LifecycleNode, rclcpp::Node or rclcpp_lifecycle::LifecycleNode
57  costmap_sub_ = nav2::interfaces::create_subscription<nav2_msgs::msg::Costmap>(
58  parent, topic_name_,
59  std::bind(&CostmapSubscriber::costmapCallback, this, std::placeholders::_1),
60  nav2::qos::LatchedSubscriptionQoS(3), callback_group);
61 
62  costmap_update_sub_ = nav2::interfaces::create_subscription<nav2_msgs::msg::CostmapUpdate>(
63  parent, topic_name_ + "_updates",
64  std::bind(&CostmapSubscriber::costmapUpdateCallback, this, std::placeholders::_1),
65  nav2::qos::LatchedSubscriptionQoS(), callback_group);
66  }
67 
72 
76  std::shared_ptr<Costmap2D> getCostmap();
80  void costmapCallback(const nav2_msgs::msg::Costmap::ConstSharedPtr & msg);
84  void costmapUpdateCallback(const nav2_msgs::msg::CostmapUpdate::ConstSharedPtr & update_msg);
85 
86  std::string getFrameID() const
87  {
88  return frame_id_;
89  }
90 
91 protected:
92  bool isCostmapReceived()
93  {
94  std::lock_guard<std::mutex> guard(costmap_msg_mutex_);
95  return costmap_ != nullptr;
96  }
97  void processCurrentCostmapMsg();
98 
99  bool haveCostmapParametersChanged();
100  bool hasCostmapSizeChanged();
101  bool hasCostmapResolutionChanged();
102  bool hasCostmapOriginPositionChanged();
103 
104  nav2::Subscription<nav2_msgs::msg::Costmap>::SharedPtr costmap_sub_;
105  nav2::Subscription<nav2_msgs::msg::CostmapUpdate>::SharedPtr costmap_update_sub_;
106 
107  std::shared_ptr<Costmap2D> costmap_;
108  nav2_msgs::msg::Costmap::ConstSharedPtr costmap_msg_;
109 
110  std::string topic_name_;
111  std::string frame_id_;
112  std::mutex costmap_msg_mutex_;
113  rclcpp::Logger logger_{rclcpp::get_logger("nav2_costmap_2d")};
114 };
115 
116 } // namespace nav2_costmap_2d
117 
118 #endif // NAV2_COSTMAP_2D__COSTMAP_SUBSCRIBER_HPP_
A QoS profile for latched, reliable topics with a history of 10 messages.
Subscribes to the costmap via a ros topic.
CostmapSubscriber(const nav2::LifecycleNode::WeakPtr &parent, const std::string &topic_name)
A constructor.
std::shared_ptr< Costmap2D > getCostmap()
Get current costmap.
void costmapCallback(const nav2_msgs::msg::Costmap::ConstSharedPtr &msg)
Callback for the costmap topic.
void costmapUpdateCallback(const nav2_msgs::msg::CostmapUpdate::ConstSharedPtr &update_msg)
Callback for the costmap's update topic.