Nav2 Navigation Stack - jazzy  jazzy
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_util/lifecycle_node.hpp"
29 
30 namespace nav2_costmap_2d
31 {
37 {
38 public:
43  const nav2_util::LifecycleNode::WeakPtr & parent,
44  const std::string & topic_name);
45 
50  const rclcpp::Node::WeakPtr & parent,
51  const std::string & topic_name);
52 
57 
61  std::shared_ptr<Costmap2D> getCostmap();
65  void costmapCallback(const nav2_msgs::msg::Costmap::SharedPtr msg);
69  void costmapUpdateCallback(const nav2_msgs::msg::CostmapUpdate::SharedPtr update_msg);
70 
71 protected:
72  bool isCostmapReceived()
73  {
74  std::lock_guard<std::mutex> guard(costmap_msg_mutex_);
75  return costmap_ != nullptr;
76  }
77  void processCurrentCostmapMsg();
78 
79  bool haveCostmapParametersChanged();
80  bool hasCostmapSizeChanged();
81  bool hasCostmapResolutionChanged();
82  bool hasCostmapOriginPositionChanged();
83 
84  rclcpp::Subscription<nav2_msgs::msg::Costmap>::SharedPtr costmap_sub_;
85  rclcpp::Subscription<nav2_msgs::msg::CostmapUpdate>::SharedPtr costmap_update_sub_;
86 
87  std::shared_ptr<Costmap2D> costmap_;
88  nav2_msgs::msg::Costmap::SharedPtr costmap_msg_;
89 
90  std::string topic_name_;
91  std::mutex costmap_msg_mutex_;
92  rclcpp::Logger logger_{rclcpp::get_logger("nav2_costmap_2d")};
93 };
94 
95 } // namespace nav2_costmap_2d
96 
97 #endif // NAV2_COSTMAP_2D__COSTMAP_SUBSCRIBER_HPP_
Subscribes to the costmap via a ros topic.
CostmapSubscriber(const nav2_util::LifecycleNode::WeakPtr &parent, const std::string &topic_name)
A constructor.
void costmapUpdateCallback(const nav2_msgs::msg::CostmapUpdate::SharedPtr update_msg)
Callback for the costmap's update topic.
void costmapCallback(const nav2_msgs::msg::Costmap::SharedPtr msg)
Callback for the costmap topic.
std::shared_ptr< Costmap2D > getCostmap()
Get current costmap.