planner_node.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2018, 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 #include <ros/ros.h>
37 #include <nav_core2/exceptions.h>
39 #include <geometry_msgs/PoseStamped.h>
40 #include <geometry_msgs/PoseWithCovarianceStamped.h>
42 #include <visualization_msgs/Marker.h>
43 #include <memory>
44 #include <string>
45 
46 namespace dlux_global_planner
47 {
63 {
64 public:
65  PlannerNode() : costmap_loader_("nav_core2", "nav_core2::Costmap"), has_start_(false), has_goal_(false)
66  {
67  ros::NodeHandle nh("~");
68  marker_pub_ = nh.advertise<visualization_msgs::Marker>("/visualization_marker", 10);
69 
70  tf_ = std::make_shared<tf2_ros::Buffer>();
71  tf2_ = std::make_shared<tf2_ros::TransformListener>(*tf_);
72  std::string costmap_class;
73  nh.param("global_costmap_class", costmap_class, std::string("nav_core_adapter::CostmapAdapter"));
74  costmap_ = costmap_loader_.createUniqueInstance(costmap_class);
75  costmap_->initialize(nh, std::string("global_costmap"), tf_);
76 
77  gp_.initialize(nh, "planner", tf_, costmap_);
78  goal_sub_ = nh.subscribe<geometry_msgs::PoseStamped>("/move_base_simple/goal", 1,
79  boost::bind(&PlannerNode::goalCB, this, _1));
80  pose_sub_ = nh.subscribe<geometry_msgs::PoseWithCovarianceStamped>("/initialpose", 1,
81  boost::bind(&PlannerNode::poseCB, this, _1));
82  nh.param("red", red_, 1.0);
83  nh.param("green", green_, 1.0);
84  nh.param("blue", blue_, 1.0);
85  nh.param("marker_ns", marker_ns_, std::string(""));
86  }
87 
89  {
90  costmap_.reset();
91  }
92 private:
93  void goalCB(const geometry_msgs::PoseStamped::ConstPtr& goal)
94  {
95  has_goal_ = true;
97  publishPointMarker(goal_, false);
98  plan();
99  }
100 
101  void poseCB(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr& goal)
102  {
103  has_start_ = true;
104  start_.header = goal->header;
105  start_.pose.x = goal->pose.pose.position.x;
106  start_.pose.y = goal->pose.pose.position.y;
107  publishPointMarker(start_, true);
108  plan();
109  }
110 
111  void plan()
112  {
113  if (!has_goal_ || !has_start_) return;
114  nav_2d_msgs::Path2D plan;
115  try
116  {
117  plan = gp_.makePlan(start_, goal_);
118  }
119  catch (nav_core2::PlannerException& e)
120  {
121  ROS_ERROR("%s", e.what());
122  }
123 
124  // publish plan as markers
125  nav_msgs::Path path = nav_2d_utils::pathToPath(plan);
126  double resolution = costmap_->getResolution();
127 
128  visualization_msgs::Marker m;
129  m.header.stamp = ros::Time::now();
130  m.header.frame_id = plan.header.frame_id;
131  m.ns = marker_ns_ + "path";
132  m.type = visualization_msgs::Marker::LINE_STRIP;
133  m.scale.x = resolution / 4;
134  m.color.r = red_;
135  m.color.g = green_;
136  m.color.b = blue_;
137  m.color.a = 0.7;
138  for (unsigned int i = 0; i < plan.poses.size(); i++)
139  {
140  m.points.push_back(path.poses[i].pose.position);
141  }
142  marker_pub_.publish(m);
143 
144  m.type = visualization_msgs::Marker::SPHERE_LIST;
145  m.id += 1;
146  m.color.r = red_ / 2;
147  m.color.g = green_ / 2;
148  m.color.b = blue_ / 2;
149  m.scale.x = resolution / 2;
150  m.scale.y = resolution / 2;
151  m.scale.z = resolution / 2;
152  marker_pub_.publish(m);
153  }
154 
155  void publishPointMarker(nav_2d_msgs::Pose2DStamped pose, bool start)
156  {
157  visualization_msgs::Marker m;
158  m.header = pose.header;
159  m.header.stamp = ros::Time::now();
160  m.ns = (start ? "start" : "goal");
161  m.type = visualization_msgs::Marker::CYLINDER;
162  m.pose.position.x = pose.pose.x;
163  m.pose.position.y = pose.pose.y;
164 
165  double resolution = costmap_->getResolution();
166  m.scale.x = 4 * resolution;
167  m.scale.y = 4 * resolution;
168  m.scale.z = 0.1;
169  m.color.r = start ? 0.0 : 1.0;
170  m.color.g = start ? 1.0 : 0.0;
171  m.color.a = 0.5;
172  marker_pub_.publish(m);
173  }
174 
177 
179  std::shared_ptr<tf2_ros::TransformListener> tf2_;
183 
184  nav_2d_msgs::Pose2DStamped start_, goal_;
186 
187  double red_, green_, blue_;
188  std::string marker_ns_;
189 };
190 } // namespace dlux_global_planner
191 
192 int main(int argc, char** argv)
193 {
194  ros::init(argc, argv, "global_planner");
196  ros::spin();
197  return 0;
198 }
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
std::shared_ptr< tf2_ros::TransformListener > tf2_
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
Demonstration/debug tool that creates paths between arbitrary points.
nav_2d_msgs::Path2D pathToPath(const nav_msgs::Path &path)
void poseCB(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr &goal)
nav_2d_msgs::Pose2DStamped goal_
nav_2d_msgs::Pose2DStamped poseStampedToPose2D(const geometry_msgs::PoseStamped &pose)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void publish(const boost::shared_ptr< M > &message) const
pluginlib::ClassLoader< nav_core2::Costmap > costmap_loader_
int main(int argc, char **argv)
nav_2d_msgs::Pose2DStamped start_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ROSCPP_DECL void spin()
Plugin-based global wavefront planner that conforms to the nav_core2 GlobalPlanner interface...
void publishPointMarker(nav_2d_msgs::Pose2DStamped pose, bool start)
nav_2d_msgs::Path2D makePlan(const nav_2d_msgs::Pose2DStamped &start, const nav_2d_msgs::Pose2DStamped &goal) override
std::shared_ptr< tf2_ros::Buffer > TFListenerPtr
nav_core2::Costmap::Ptr costmap_
static Time now()
void initialize(const ros::NodeHandle &parent, const std::string &name, TFListenerPtr tf, nav_core2::Costmap::Ptr costmap) override
std::shared_ptr< Costmap > Ptr
dlux_global_planner::DluxGlobalPlanner gp_
#define ROS_ERROR(...)
void goalCB(const geometry_msgs::PoseStamped::ConstPtr &goal)


dlux_global_planner
Author(s):
autogenerated on Mon Feb 28 2022 23:33:21