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/callback_group.hpp"
25 #include "rclcpp/logger.hpp"
26 #include "rclcpp/node.hpp"
27 #include "nav2_costmap_2d/costmap_2d.hpp"
28 #include "nav2_msgs/msg/costmap.hpp"
29 #include "nav2_msgs/msg/costmap_update.hpp"
30 #include "nav2_ros_common/lifecycle_node.hpp"
31 
32 namespace nav2_costmap_2d
33 {
39 {
40 public:
45  const nav2::LifecycleNode::WeakPtr & parent,
46  const std::string & topic_name);
47 
48  template<typename NodeT>
50  const NodeT & parent,
51  const std::string & topic_name,
52  const rclcpp::CallbackGroup::SharedPtr & callback_group = nullptr)
53  : topic_name_(topic_name)
54  {
55  logger_ = parent->get_logger();
56 
57  // Could be using a user rclcpp::Node, so need to use the Nav2 factory to create the
58  // subscription to convert nav2::LifecycleNode, rclcpp::Node or rclcpp_lifecycle::LifecycleNode
59  costmap_sub_ = nav2::interfaces::create_subscription<nav2_msgs::msg::Costmap>(
60  parent, topic_name_,
61  std::bind(&CostmapSubscriber::costmapCallback, this, std::placeholders::_1),
62  nav2::qos::LatchedSubscriptionQoS(3), callback_group);
63 
64  costmap_update_sub_ = nav2::interfaces::create_subscription<nav2_msgs::msg::CostmapUpdate>(
65  parent, topic_name_ + "_updates",
66  std::bind(&CostmapSubscriber::costmapUpdateCallback, this, std::placeholders::_1),
67  nav2::qos::LatchedSubscriptionQoS(), callback_group);
68  }
69 
74 
78  std::shared_ptr<Costmap2D> getCostmap();
82  void costmapCallback(const nav2_msgs::msg::Costmap::ConstSharedPtr & msg);
86  void costmapUpdateCallback(const nav2_msgs::msg::CostmapUpdate::ConstSharedPtr & update_msg);
87 
88  std::string getFrameID() const
89  {
90  return frame_id_;
91  }
92 
93 protected:
94  bool isCostmapReceived()
95  {
96  std::lock_guard<std::recursive_mutex> guard(costmap_msg_mutex_);
97  return costmap_ != nullptr;
98  }
99  void processCurrentCostmapMsg();
100 
101  bool haveCostmapParametersChanged();
102  bool hasCostmapSizeChanged();
103  bool hasCostmapResolutionChanged();
104  bool hasCostmapOriginPositionChanged();
105 
106  nav2::Subscription<nav2_msgs::msg::Costmap>::SharedPtr costmap_sub_;
107  nav2::Subscription<nav2_msgs::msg::CostmapUpdate>::SharedPtr costmap_update_sub_;
108 
109  std::shared_ptr<Costmap2D> costmap_;
110  nav2_msgs::msg::Costmap::ConstSharedPtr costmap_msg_;
111 
112  std::string topic_name_;
113  std::string frame_id_;
114  std::recursive_mutex costmap_msg_mutex_;
115  rclcpp::Logger logger_{rclcpp::get_logger("nav2_costmap_2d")};
116 };
117 
118 } // namespace nav2_costmap_2d
119 
120 #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.