Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
publisher.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef DWB_CORE__PUBLISHER_HPP_
36 #define DWB_CORE__PUBLISHER_HPP_
37 
38 #include <memory>
39 #include <string>
40 #include <vector>
41 
42 #include "nav2_costmap_2d/costmap_2d_ros.hpp"
43 #include "dwb_core/trajectory_critic.hpp"
44 #include "dwb_msgs/msg/local_plan_evaluation.hpp"
45 #include "nav_msgs/msg/path.hpp"
46 #include "rclcpp/clock.hpp"
47 #include "rclcpp/publisher.hpp"
48 #include "sensor_msgs/msg/point_cloud2.hpp"
49 #include "visualization_msgs/msg/marker_array.hpp"
50 #include "nav2_ros_common/lifecycle_node.hpp"
51 #include "builtin_interfaces/msg/duration.hpp"
52 
53 using nav2::Publisher;
54 
55 namespace dwb_core
56 {
57 
69 {
70 public:
71  explicit DWBPublisher(
72  const nav2::LifecycleNode::WeakPtr & parent,
73  const std::string & plugin_name);
74 
75  nav2::CallbackReturn on_configure();
76  nav2::CallbackReturn on_activate();
77  nav2::CallbackReturn on_deactivate();
78  nav2::CallbackReturn on_cleanup();
79 
84  bool shouldRecordEvaluation() {return publish_evaluation_ || publish_trajectories_;}
85 
89  void publishEvaluation(std::shared_ptr<dwb_msgs::msg::LocalPlanEvaluation> results);
90  void publishLocalPlan(
91  const std_msgs::msg::Header & header,
92  const dwb_msgs::msg::Trajectory2D & traj);
93  void publishCostGrid(
94  const std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros,
95  const std::vector<TrajectoryCritic::Ptr> critics);
96  void publishLocalPlan(const nav_msgs::msg::Path plan);
97 
98 protected:
99  void publishTrajectories(const dwb_msgs::msg::LocalPlanEvaluation & results);
100 
101  // Helper function for publishing other plans
102  void publishGenericPlan(
103  const nav_msgs::msg::Path plan,
104  rclcpp::Publisher<nav_msgs::msg::Path> & pub, bool flag); // nosemgrep
105 
106  // Flags for turning on/off publishing specific components
107  bool publish_evaluation_;
108  bool publish_local_plan_;
109  bool publish_trajectories_;
110  bool publish_cost_grid_pc_;
111  bool publish_input_params_;
112 
113  // Marker Lifetime
114  builtin_interfaces::msg::Duration marker_lifetime_;
115 
116  // Publisher Objects
117  std::shared_ptr<Publisher<dwb_msgs::msg::LocalPlanEvaluation>> eval_pub_;
118  std::shared_ptr<Publisher<nav_msgs::msg::Path>> local_pub_;
119  std::shared_ptr<Publisher<visualization_msgs::msg::MarkerArray>> marker_pub_;
120  std::shared_ptr<Publisher<sensor_msgs::msg::PointCloud2>> cost_grid_pc_pub_;
121 
122  nav2::LifecycleNode::WeakPtr node_;
123  rclcpp::Clock::SharedPtr clock_;
124  std::string plugin_name_;
125 };
126 
127 } // namespace dwb_core
128 
129 #endif // DWB_CORE__PUBLISHER_HPP_
Consolidation of all the publishing logic for the DWB Local Planner.
Definition: publisher.hpp:69
bool shouldRecordEvaluation()
Does the publisher require that the LocalPlanEvaluation be saved.
Definition: publisher.hpp:84
void publishEvaluation(std::shared_ptr< dwb_msgs::msg::LocalPlanEvaluation > results)
If the pointer is not null, publish the evaluation and trajectories as needed.
Definition: publisher.cpp:127