SMACC
Loading...
Searching...
No Matches
backward_global_planner.cpp
Go to the documentation of this file.
1/*****************************************************************************************************************
2 * ReelRobotix Inc. - Software License Agreement Copyright (c) 2018
3 * Authors: Pablo Inigo Blasco, Brett Aldrich
4 *
5 ******************************************************************************************************************/
6#include <boost/assign.hpp>
7#include <boost/range/adaptor/reversed.hpp>
8#include <boost/range/algorithm/copy.hpp>
9#include <pluginlib/class_list_macros.h>
11#include <fstream>
12#include <streambuf>
13#include <nav_msgs/Path.h>
14#include <visualization_msgs/MarkerArray.h>
15#include <tf/tf.h>
16#include <tf/transform_datatypes.h>
17#include <angles/angles.h>
19
20//register this planner as a BaseGlobalPlanner plugin
22
23namespace cl_move_base_z
24{
25namespace backward_global_planner
26{
33{
35}
36
38{
39 //clear
40 nav_msgs::Path planMsg;
41 planMsg.header.stamp = ros::Time::now();
42 planPub_.publish(planMsg);
43}
44
50void BackwardGlobalPlanner::initialize(std::string name, costmap_2d::Costmap2DROS *costmap_ros)
51{
52 //ROS_INFO_NAMED("Backwards", "BackwardGlobalPlanner initialize");
53 costmap_ros_ = costmap_ros;
54 //ROS_WARN_NAMED("Backwards", "initializating global planner, costmap address: %ld", (long)costmap_ros);
55
56 ros::NodeHandle nh;
57 planPub_ = nh.advertise<nav_msgs::Path>("backward_planner/global_plan", 1);
58 markersPub_ = nh.advertise<visualization_msgs::MarkerArray>("backward_planner/markers", 1);
59}
60
66void BackwardGlobalPlanner::publishGoalMarker(const geometry_msgs::Pose &pose, double r, double g, double b)
67{
68 double phi = tf::getYaw(pose.orientation);
69 visualization_msgs::Marker marker;
70 marker.header.frame_id = this->costmap_ros_->getGlobalFrameID();
71 marker.header.stamp = ros::Time::now();
72 marker.ns = "my_namespace2";
73 marker.id = 0;
74 marker.type = visualization_msgs::Marker::ARROW;
75 marker.action = visualization_msgs::Marker::ADD;
76 marker.scale.x = 0.1;
77 marker.scale.y = 0.3;
78 marker.scale.z = 0.1;
79 marker.color.a = 1.0;
80 marker.color.r = r;
81 marker.color.g = g;
82 marker.color.b = b;
83
84 geometry_msgs::Point start, end;
85 start.x = pose.position.x;
86 start.y = pose.position.y;
87
88 end.x = pose.position.x + 0.5 * cos(phi);
89 end.y = pose.position.y + 0.5 * sin(phi);
90
91 marker.points.push_back(start);
92 marker.points.push_back(end);
93
94 visualization_msgs::MarkerArray ma;
95 ma.markers.push_back(marker);
96
97 markersPub_.publish(ma);
98}
99
105bool BackwardGlobalPlanner::createDefaultBackwardPath(const geometry_msgs::PoseStamped &start,
106 const geometry_msgs::PoseStamped &goal, std::vector<geometry_msgs::PoseStamped> &plan)
107{
108 auto q = start.pose.orientation;
109
110 geometry_msgs::PoseStamped pose;
111 pose = start;
112
113 double dx = start.pose.position.x - goal.pose.position.x;
114 double dy = start.pose.position.y - goal.pose.position.y;
115
116 double length = sqrt(dx * dx + dy * dy);
117
118 geometry_msgs::PoseStamped prevState;
120 {
121 // skip initial pure spinning and initial straight motion
122 //ROS_INFO("1 - heading to goal position pure spinning");
123 double heading_direction = atan2(dy, dx);
124 double startyaw = tf::getYaw(q);
125 double offset = angles::shortest_angular_distance(startyaw, heading_direction);
126 heading_direction = startyaw + offset;
127
128 prevState = cl_move_base_z::makePureSpinningSubPlan(start, heading_direction, plan, puresSpinningRadStep_);
129 //ROS_INFO("2 - going forward keep orientation pure straight");
130
131 prevState = cl_move_base_z::makePureStraightSubPlan(prevState, goal.pose.position, length, plan);
132 }
133 else
134 {
135 prevState = start;
136 }
137
138 ROS_WARN_STREAM( "[BackwardGlobalPlanner ] backward global plan size: " <<plan.size());
139 return true;
140}
141
147bool BackwardGlobalPlanner::makePlan(const geometry_msgs::PoseStamped &start,
148 const geometry_msgs::PoseStamped &goal, std::vector<geometry_msgs::PoseStamped> &plan)
149{
150 //ROS_WARN_NAMED("Backwards", "Backwards global planner: Generating global plan ");
151 //ROS_WARN_NAMED("Backwards", "Clearing...");
152
153 plan.clear();
154
155 this->createDefaultBackwardPath(start, goal, plan);
156 //this->createPureSpiningAndStragihtLineBackwardPath(start, goal, plan);
157
158 //ROS_INFO_STREAM(" start - " << start);
159 //ROS_INFO_STREAM(" end - " << goal.pose.position);
160
161 //ROS_INFO("3 - heading to goal orientation");
162 //double goalOrientation = angles::normalize_angle(tf::getYaw(goal.pose.orientation));
163 //cl_move_base_z::makePureSpinningSubPlan(prevState,goalOrientation,plan);
164
165 //ROS_WARN_STREAM( "MAKE PLAN INVOKED, plan size:"<< plan.size());
166 publishGoalMarker(goal.pose, 1.0, 0, 1.0);
167
168 nav_msgs::Path planMsg;
169 planMsg.poses = plan;
170 planMsg.header.frame_id = this->costmap_ros_->getGlobalFrameID();
171
172 // check plan rejection
173 bool acceptedGlobalPlan = true;
174
175 // static const unsigned char NO_INFORMATION = 255;
176 // static const unsigned char LETHAL_OBSTACLE = 254;
177 // static const unsigned char INSCRIBED_INFLATED_OBSTACLE = 253;
178 // static const unsigned char FREE_SPACE = 0;
179
180 costmap_2d::Costmap2D *costmap2d = this->costmap_ros_->getCostmap();
181 for (auto &p : plan)
182 {
183 uint32_t mx, my;
184 costmap2d->worldToMap(p.pose.position.x, p.pose.position.y, mx, my);
185 auto cost = costmap2d->getCost(mx, my);
186
187 if (cost >= costmap_2d::INSCRIBED_INFLATED_OBSTACLE)
188 {
189 acceptedGlobalPlan = false;
190 break;
191 }
192 }
193
194 planPub_.publish(planMsg);
195
196 // this was previously set to size() <= 1, but a plan with a single point is also a valid plan (the goal)
197 return true;
198}
199
205bool BackwardGlobalPlanner::makePlan(const geometry_msgs::PoseStamped &start,
206 const geometry_msgs::PoseStamped &goal, std::vector<geometry_msgs::PoseStamped> &plan,
207 double &cost)
208{
209 cost = 0;
210 makePlan(start, goal, plan);
211 return true;
212}
213
214} // namespace backward_global_planner
215} // namespace cl_move_base_z
PLUGINLIB_EXPORT_CLASS(cl_move_base_z::backward_global_planner::BackwardGlobalPlanner, nav_core::BaseGlobalPlanner)
virtual bool createDefaultBackwardPath(const geometry_msgs::PoseStamped &start, const geometry_msgs::PoseStamped &goal, std::vector< geometry_msgs::PoseStamped > &plan)
void publishGoalMarker(const geometry_msgs::Pose &pose, double r, double g, double b)
bool makePlan(const geometry_msgs::PoseStamped &start, const geometry_msgs::PoseStamped &goal, std::vector< geometry_msgs::PoseStamped > &plan)
virtual void initialize(std::string name, costmap_2d::Costmap2DROS *costmap_ros_) override
geometry_msgs::PoseStamped makePureStraightSubPlan(const geometry_msgs::PoseStamped &startOrientedPose, const geometry_msgs::Point &goal, double length, std::vector< geometry_msgs::PoseStamped > &plan)
Definition: path_tools.cpp:61
geometry_msgs::PoseStamped makePureSpinningSubPlan(const geometry_msgs::PoseStamped &start, double dstRads, std::vector< geometry_msgs::PoseStamped > &plan, double puresSpinningRadStep=1000)
Definition: path_tools.cpp:13