publisher.cpp
Go to the documentation of this file.
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  */
36 #include <visualization_msgs/Marker.h>
37 #include <visualization_msgs/MarkerArray.h>
38 #include <sensor_msgs/PointCloud2.h>
41 #include <memory>
42 #include <vector>
43 
44 namespace dwb_local_planner
45 {
46 
48 {
49  ros::NodeHandle global_nh;
50  // Load Publishers
51  nh.param("publish_evaluation", publish_evaluation_, true);
53  eval_pub_ = nh.advertise<dwb_msgs::LocalPlanEvaluation>("evaluation", 1);
54 
55  nh.param("publish_input_params", publish_input_params_, true);
57  {
58  info_pub_ = nh.advertise<nav_2d_msgs::NavGridInfo>("info", 1);
59  pose_pub_ = nh.advertise<geometry_msgs::Pose2D>("pose", 1);
60  goal_pub_ = nh.advertise<geometry_msgs::Pose2D>("goal", 1);
61  velocity_pub_ = nh.advertise<nav_2d_msgs::Twist2D>("velocity", 1);
62  }
63 
64  nh.param("publish_global_plan", publish_global_plan_, true);
66  global_pub_ = nh.advertise<nav_msgs::Path>("global_plan", 1);
67 
68  nh.param("publish_transformed_plan", publish_transformed_, true);
70  transformed_pub_ = nh.advertise<nav_msgs::Path>("transformed_global_plan", 1);
71 
72  nh.param("publish_local_plan", publish_local_plan_, true);
74  local_pub_ = nh.advertise<nav_msgs::Path>("local_plan", 1);
75 
76  nh.param("publish_trajectories", publish_trajectories_, true);
78  marker_pub_ = global_nh.advertise<visualization_msgs::MarkerArray>("marker", 1);
79  double marker_lifetime;
80  nh.param("marker_lifetime", marker_lifetime, 0.1);
81  marker_lifetime_ = ros::Duration(marker_lifetime);
82 
83  nh.param("publish_cost_grid_pc", publish_cost_grid_pc_, false);
85  cost_grid_pc_pub_ = nh.advertise<sensor_msgs::PointCloud2>("cost_cloud", 1);
86 }
87 
88 void DWBPublisher::publishEvaluation(std::shared_ptr<dwb_msgs::LocalPlanEvaluation> results)
89 {
90  if (results == nullptr) return;
92  {
93  eval_pub_.publish(*results);
94  }
95  publishTrajectories(*results);
96 }
97 
98 void DWBPublisher::publishTrajectories(const dwb_msgs::LocalPlanEvaluation& results)
99 {
100  if (!publish_trajectories_ || marker_pub_.getNumSubscribers() == 0) return;
101  visualization_msgs::MarkerArray ma;
102  visualization_msgs::Marker m;
103 
104  if (results.twists.size() == 0) return;
105 
106  geometry_msgs::Point pt;
107 
108  m.header = results.header;
109  m.type = m.LINE_STRIP;
110  m.pose.orientation.w = 1;
111  m.scale.x = 0.002;
112  m.color.a = 1.0;
113  m.lifetime = marker_lifetime_;
114 
115  double best_cost = results.twists[results.best_index].total,
116  worst_cost = results.twists[results.worst_index].total,
117  denominator = worst_cost - best_cost;
118  if (std::fabs(denominator) < 1e-9)
119  {
120  denominator = 1.0;
121  }
122 
123  for (unsigned int i = 0; i < results.twists.size(); i++)
124  {
125  const dwb_msgs::TrajectoryScore& twist = results.twists[i];
126  if (twist.total >= 0)
127  {
128  m.color.r = 1 - (twist.total - best_cost) / denominator;
129  m.color.g = 1 - (twist.total - best_cost) / denominator;
130  m.color.b = 1;
131  m.ns = "ValidTrajectories";
132  }
133  else
134  {
135  m.color.b = 0;
136  m.ns = "InvalidTrajectories";
137  }
138  m.points.clear();
139  for (unsigned int j = 0; j < twist.traj.poses.size(); ++j)
140  {
141  pt.x = twist.traj.poses[j].x;
142  pt.y = twist.traj.poses[j].y;
143  pt.z = 0;
144  m.points.push_back(pt);
145  }
146  ma.markers.push_back(m);
147  m.id += 1;
148  }
149 
150  marker_pub_.publish(ma);
151 }
152 
153 void DWBPublisher::publishLocalPlan(const std_msgs::Header& header,
154  const dwb_msgs::Trajectory2D& traj)
155 {
156  if (!publish_local_plan_ || local_pub_.getNumSubscribers() == 0) return;
157 
158  nav_msgs::Path path = nav_2d_utils::poses2DToPath(traj.poses, header.frame_id, header.stamp);
159  local_pub_.publish(path);
160 }
161 
163  const std::vector<TrajectoryCritic::Ptr> critics)
164 {
166 
167  const nav_grid::NavGridInfo& info = costmap->getInfo();
168  sensor_msgs::PointCloud cost_grid_pc;
169  cost_grid_pc.header.frame_id = info.frame_id;
170  cost_grid_pc.header.stamp = ros::Time::now();
171 
172  double x_coord, y_coord;
173  unsigned int n = info.width * info.height;
174  cost_grid_pc.points.resize(n);
175  unsigned int i = 0;
176  for (unsigned int cy = 0; cy < info.height; cy++)
177  {
178  for (unsigned int cx = 0; cx < info.width; cx++)
179  {
180  gridToWorld(info, cx, cy, x_coord, y_coord);
181  cost_grid_pc.points[i].x = x_coord;
182  cost_grid_pc.points[i].y = y_coord;
183  i++;
184  }
185  }
186 
187  sensor_msgs::ChannelFloat32 totals;
188  totals.name = "total_cost";
189  totals.values.resize(n);
190 
191  for (TrajectoryCritic::Ptr critic : critics)
192  {
193  unsigned int channel_index = cost_grid_pc.channels.size();
194  critic->addCriticVisualization(cost_grid_pc);
195  if (channel_index == cost_grid_pc.channels.size())
196  {
197  // No channels were added, so skip to next critic
198  continue;
199  }
200  double scale = critic->getScale();
201  for (i = 0; i < n; i++)
202  {
203  totals.values[i] += cost_grid_pc.channels[channel_index].values[i] * scale;
204  }
205  }
206  cost_grid_pc.channels.push_back(totals);
207 
208  sensor_msgs::PointCloud2 cost_grid_pc2;
209  convertPointCloudToPointCloud2(cost_grid_pc, cost_grid_pc2);
210  cost_grid_pc_pub_.publish(cost_grid_pc2);
211 }
212 
213 void DWBPublisher::publishGlobalPlan(const nav_2d_msgs::Path2D plan)
214 {
216 }
217 
218 void DWBPublisher::publishTransformedPlan(const nav_2d_msgs::Path2D plan)
219 {
221 }
222 
223 void DWBPublisher::publishLocalPlan(const nav_2d_msgs::Path2D plan)
224 {
226 }
227 
228 void DWBPublisher::publishGenericPlan(const nav_2d_msgs::Path2D plan, const ros::Publisher pub, bool flag)
229 {
230  if (!flag || pub.getNumSubscribers() == 0) return;
231  nav_msgs::Path path = nav_2d_utils::pathToPath(plan);
232  pub.publish(path);
233 }
234 
235 void DWBPublisher::publishInputParams(const nav_grid::NavGridInfo& info, const geometry_msgs::Pose2D& start_pose,
236  const nav_2d_msgs::Twist2D& velocity, const geometry_msgs::Pose2D& goal_pose)
237 {
238  if (!publish_input_params_) return;
239 
241  pose_pub_.publish(start_pose);
242  goal_pub_.publish(goal_pose);
243  velocity_pub_.publish(velocity);
244 }
245 
246 } // namespace dwb_local_planner
void publishCostGrid(const nav_core2::Costmap::Ptr costmap, const std::vector< TrajectoryCritic::Ptr > critics)
Definition: publisher.cpp:162
void publish(const boost::shared_ptr< M > &message) const
void publishTrajectories(const dwb_msgs::LocalPlanEvaluation &results)
Definition: publisher.cpp:98
std::shared_ptr< dwb_local_planner::TrajectoryCritic > Ptr
void publishGlobalPlan(const nav_2d_msgs::Path2D plan)
Definition: publisher.cpp:213
void publishTransformedPlan(const nav_2d_msgs::Path2D plan)
Definition: publisher.cpp:218
ros::Publisher cost_grid_pc_pub_
Definition: publisher.h:101
nav_2d_msgs::NavGridInfo toMsg(const nav_grid::NavGridInfo &info)
void publishGenericPlan(const nav_2d_msgs::Path2D plan, const ros::Publisher pub, bool flag)
Definition: publisher.cpp:228
nav_2d_msgs::Path2D pathToPath(const nav_msgs::Path &path)
void gridToWorld(const NavGridInfo &info, int mx, int my, double &wx, double &wy)
void publishInputParams(const nav_grid::NavGridInfo &info, const geometry_msgs::Pose2D &start_pose, const nav_2d_msgs::Twist2D &velocity, const geometry_msgs::Pose2D &goal_pose)
Definition: publisher.cpp:235
ros::Duration marker_lifetime_
Definition: publisher.h:98
ros::Publisher transformed_pub_
Definition: publisher.h:101
nav_msgs::Path poses2DToPath(const std::vector< geometry_msgs::Pose2D > &poses, const std::string &frame, const ros::Time &stamp)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void publishEvaluation(std::shared_ptr< dwb_msgs::LocalPlanEvaluation > results)
If the pointer is not null, publish the evaluation and trajectories as needed.
Definition: publisher.cpp:88
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void publishLocalPlan(const std_msgs::Header &header, const dwb_msgs::Trajectory2D &traj)
Definition: publisher.cpp:153
uint32_t getNumSubscribers() const
static Time now()
std::shared_ptr< Costmap > Ptr
void initialize(ros::NodeHandle &nh)
Load the parameters and advertise topics as needed.
Definition: publisher.cpp:47


dwb_local_planner
Author(s): David V. Lu!!
autogenerated on Sun Jan 10 2021 04:08:34