-
Notifications
You must be signed in to change notification settings - Fork 140
/
sbpl_lattice_planner.cpp
389 lines (332 loc) · 14.8 KB
/
sbpl_lattice_planner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* Copyright (c) 2008, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the Willow Garage nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Author: Mike Phillips
*********************************************************************/
#include <sbpl_lattice_planner/sbpl_lattice_planner.h>
#include <pluginlib/class_list_macros.h>
#include <nav_msgs/Path.h>
#include <sbpl_lattice_planner/SBPLLatticePlannerStats.h>
using namespace std;
using namespace ros;
PLUGINLIB_DECLARE_CLASS(sbpl_latice_planner, SBPLLatticePlanner, sbpl_lattice_planner::SBPLLatticePlanner, nav_core::BaseGlobalPlanner);
namespace sbpl_lattice_planner{
class LatticeSCQ : public StateChangeQuery{
public:
LatticeSCQ(EnvironmentNAVXYTHETALAT* env, std::vector<nav2dcell_t> const & changedcellsV)
: env_(env), changedcellsV_(changedcellsV) {
}
// lazy init, because we do not always end up calling this method
virtual std::vector<int> const * getPredecessors() const{
if(predsOfChangedCells_.empty() && !changedcellsV_.empty())
env_->GetPredsofChangedEdges(&changedcellsV_, &predsOfChangedCells_);
return &predsOfChangedCells_;
}
// lazy init, because we do not always end up calling this method
virtual std::vector<int> const * getSuccessors() const{
if(succsOfChangedCells_.empty() && !changedcellsV_.empty())
env_->GetSuccsofChangedEdges(&changedcellsV_, &succsOfChangedCells_);
return &succsOfChangedCells_;
}
EnvironmentNAVXYTHETALAT * env_;
std::vector<nav2dcell_t> const & changedcellsV_;
mutable std::vector<int> predsOfChangedCells_;
mutable std::vector<int> succsOfChangedCells_;
};
SBPLLatticePlanner::SBPLLatticePlanner()
: initialized_(false), costmap_ros_(NULL){
}
SBPLLatticePlanner::SBPLLatticePlanner(std::string name, costmap_2d::Costmap2DROS* costmap_ros)
: initialized_(false), costmap_ros_(NULL){
initialize(name, costmap_ros);
}
void SBPLLatticePlanner::initialize(std::string name, costmap_2d::Costmap2DROS* costmap_ros){
if(!initialized_){
ros::NodeHandle private_nh("~/"+name);
ros::NodeHandle nh(name);
ROS_INFO("Name is %s", name.c_str());
private_nh.param("planner_type", planner_type_, string("ARAPlanner"));
private_nh.param("allocated_time", allocated_time_, 10.0);
private_nh.param("initial_epsilon",initial_epsilon_,3.0);
private_nh.param("environment_type", environment_type_, string("XYThetaLattice"));
private_nh.param("forward_search", forward_search_, bool(false));
private_nh.param("primitive_filename",primitive_filename_,string(""));
private_nh.param("force_scratch_limit",force_scratch_limit_,500);
double nominalvel_mpersecs, timetoturn45degsinplace_secs;
private_nh.param("nominalvel_mpersecs", nominalvel_mpersecs, 0.4);
private_nh.param("timetoturn45degsinplace_secs", timetoturn45degsinplace_secs, 0.6);
int lethal_obstacle;
private_nh.param("lethal_obstacle",lethal_obstacle,20);
lethal_obstacle_ = (unsigned char) lethal_obstacle;
inscribed_inflated_obstacle_ = lethal_obstacle_-1;
sbpl_cost_multiplier_ = (unsigned char) (costmap_2d::INSCRIBED_INFLATED_OBSTACLE/inscribed_inflated_obstacle_ + 1);
ROS_DEBUG("SBPL: lethal: %uz, inscribed inflated: %uz, multiplier: %uz",lethal_obstacle,inscribed_inflated_obstacle_,sbpl_cost_multiplier_);
costmap_ros_ = costmap_ros;
costmap_ros_->clearRobotFootprint();
costmap_ros_->getCostmapCopy(cost_map_);
std::vector<geometry_msgs::Point> footprint = costmap_ros_->getRobotFootprint();
if ("XYThetaLattice" == environment_type_){
ROS_DEBUG("Using a 3D costmap for theta lattice\n");
env_ = new EnvironmentNAVXYTHETALAT();
}
else{
ROS_ERROR("XYThetaLattice is currently the only supported environment!\n");
exit(1);
}
if(!env_->SetEnvParameter("cost_inscribed_thresh",costMapCostToSBPLCost(costmap_2d::INSCRIBED_INFLATED_OBSTACLE))){
ROS_ERROR("Failed to set cost_inscribed_thresh parameter");
exit(1);
}
if(!env_->SetEnvParameter("cost_possibly_circumscribed_thresh", costMapCostToSBPLCost(cost_map_.getCircumscribedCost()))){
ROS_ERROR("Failed to set cost_possibly_circumscribed_thresh parameter");
exit(1);
}
int obst_cost_thresh = costMapCostToSBPLCost(costmap_2d::LETHAL_OBSTACLE);
vector<sbpl_2Dpt_t> perimeterptsV;
perimeterptsV.reserve(footprint.size());
for (size_t ii(0); ii < footprint.size(); ++ii) {
sbpl_2Dpt_t pt;
pt.x = footprint[ii].x;
pt.y = footprint[ii].y;
perimeterptsV.push_back(pt);
}
bool ret;
try{
ret = env_->InitializeEnv(costmap_ros_->getSizeInCellsX(), // width
costmap_ros_->getSizeInCellsY(), // height
0, // mapdata
0, 0, 0, // start (x, y, theta, t)
0, 0, 0, // goal (x, y, theta)
0, 0, 0, //goal tolerance
perimeterptsV, costmap_ros_->getResolution(), nominalvel_mpersecs,
timetoturn45degsinplace_secs, obst_cost_thresh,
primitive_filename_.c_str());
}
catch(SBPL_Exception e){
ROS_ERROR("SBPL encountered a fatal exception!");
ret = false;
}
if(!ret){
ROS_ERROR("SBPL initialization failed!");
exit(1);
}
for (ssize_t ix(0); ix < costmap_ros_->getSizeInCellsX(); ++ix)
for (ssize_t iy(0); iy < costmap_ros_->getSizeInCellsY(); ++iy)
env_->UpdateCost(ix, iy, costMapCostToSBPLCost(cost_map_.getCost(ix,iy)));
if ("ARAPlanner" == planner_type_){
ROS_INFO("Planning with ARA*");
planner_ = new ARAPlanner(env_, forward_search_);
}
else if ("ADPlanner" == planner_type_){
ROS_INFO("Planning with AD*");
planner_ = new ADPlanner(env_, forward_search_);
}
else{
ROS_ERROR("ARAPlanner and ADPlanner are currently the only supported planners!\n");
exit(1);
}
ROS_INFO("[sbpl_lattice_planner] Initialized successfully");
plan_pub_ = private_nh.advertise<nav_msgs::Path>("plan", 1);
stats_publisher_ = private_nh.advertise<sbpl_lattice_planner::SBPLLatticePlannerStats>("sbpl_lattice_planner_stats", 1);
initialized_ = true;
}
}
//Taken from Sachin's sbpl_cart_planner
//This rescales the costmap according to a rosparam which sets the obstacle cost
unsigned char SBPLLatticePlanner::costMapCostToSBPLCost(unsigned char newcost){
if(newcost == costmap_2d::LETHAL_OBSTACLE)
return lethal_obstacle_;
else if(newcost == costmap_2d::INSCRIBED_INFLATED_OBSTACLE)
return inscribed_inflated_obstacle_;
else if(newcost == 0 || newcost == costmap_2d::NO_INFORMATION)
return 0;
else
return (unsigned char) (newcost/sbpl_cost_multiplier_ + 0.5);
}
void SBPLLatticePlanner::publishStats(int solution_cost, int solution_size,
const geometry_msgs::PoseStamped& start,
const geometry_msgs::PoseStamped& goal){
// Fill up statistics and publish
sbpl_lattice_planner::SBPLLatticePlannerStats stats;
stats.initial_epsilon = initial_epsilon_;
stats.plan_to_first_solution = false;
stats.final_number_of_expands = planner_->get_n_expands();
stats.allocated_time = allocated_time_;
stats.time_to_first_solution = planner_->get_initial_eps_planning_time();
stats.actual_time = planner_->get_final_eps_planning_time();
stats.number_of_expands_initial_solution = planner_->get_n_expands_init_solution();
stats.final_epsilon = planner_->get_final_epsilon();
stats.solution_cost = solution_cost;
stats.path_size = solution_size;
stats.start = start;
stats.goal = goal;
stats_publisher_.publish(stats);
}
bool SBPLLatticePlanner::makePlan(const geometry_msgs::PoseStamped& start,
const geometry_msgs::PoseStamped& goal,
std::vector<geometry_msgs::PoseStamped>& plan){
if(!initialized_){
ROS_ERROR("Global planner is not initialized");
return false;
}
plan.clear();
ROS_DEBUG("[sbpl_lattice_planner] getting fresh copy of costmap");
costmap_ros_->clearRobotFootprint();
ROS_DEBUG("[sbpl_lattice_planner] robot footprint cleared");
costmap_ros_->getCostmapCopy(cost_map_);
ROS_INFO("[sbpl_lattice_planner] getting start point (%g,%g) goal point (%g,%g)",
start.pose.position.x, start.pose.position.y,goal.pose.position.x, goal.pose.position.y);
double theta_start = 2 * atan2(start.pose.orientation.z, start.pose.orientation.w);
double theta_goal = 2 * atan2(goal.pose.orientation.z, goal.pose.orientation.w);
try{
int ret = env_->SetStart(start.pose.position.x - cost_map_.getOriginX(), start.pose.position.y - cost_map_.getOriginY(), theta_start);
if(ret < 0 || planner_->set_start(ret) == 0){
ROS_ERROR("ERROR: failed to set start state\n");
return false;
}
}
catch(SBPL_Exception e){
ROS_ERROR("SBPL encountered a fatal exception while setting the start state");
return false;
}
try{
int ret = env_->SetGoal(goal.pose.position.x - cost_map_.getOriginX(), goal.pose.position.y - cost_map_.getOriginY(), theta_goal);
if(ret < 0 || planner_->set_goal(ret) == 0){
ROS_ERROR("ERROR: failed to set goal state\n");
return false;
}
}
catch(SBPL_Exception e){
ROS_ERROR("SBPL encountered a fatal exception while setting the goal state");
return false;
}
int offOnCount = 0;
int onOffCount = 0;
int allCount = 0;
vector<nav2dcell_t> changedcellsV;
for(unsigned int ix = 0; ix < cost_map_.getSizeInCellsX(); ix++) {
for(unsigned int iy = 0; iy < cost_map_.getSizeInCellsY(); iy++) {
unsigned char oldCost = env_->GetMapCost(ix,iy);
unsigned char newCost = costMapCostToSBPLCost(cost_map_.getCost(ix,iy));
if(oldCost == newCost) continue;
allCount++;
//first case - off cell goes on
if((oldCost != costMapCostToSBPLCost(costmap_2d::LETHAL_OBSTACLE) && oldCost != costMapCostToSBPLCost(costmap_2d::INSCRIBED_INFLATED_OBSTACLE)) &&
(newCost == costMapCostToSBPLCost(costmap_2d::LETHAL_OBSTACLE) || newCost == costMapCostToSBPLCost(costmap_2d::INSCRIBED_INFLATED_OBSTACLE))) {
offOnCount++;
}
if((oldCost == costMapCostToSBPLCost(costmap_2d::LETHAL_OBSTACLE) || oldCost == costMapCostToSBPLCost(costmap_2d::INSCRIBED_INFLATED_OBSTACLE)) &&
(newCost != costMapCostToSBPLCost(costmap_2d::LETHAL_OBSTACLE) && newCost != costMapCostToSBPLCost(costmap_2d::INSCRIBED_INFLATED_OBSTACLE))) {
onOffCount++;
}
env_->UpdateCost(ix, iy, costMapCostToSBPLCost(cost_map_.getCost(ix,iy)));
nav2dcell_t nav2dcell;
nav2dcell.x = ix;
nav2dcell.y = iy;
changedcellsV.push_back(nav2dcell);
}
}
try{
if(!changedcellsV.empty()){
StateChangeQuery* scq = new LatticeSCQ(env_, changedcellsV);
planner_->costs_changed(*scq);
delete scq;
}
if(allCount > force_scratch_limit_)
planner_->force_planning_from_scratch();
}
catch(SBPL_Exception e){
ROS_ERROR("SBPL failed to update the costmap");
return false;
}
//setting planner parameters
ROS_DEBUG("allocated:%f, init eps:%f\n",allocated_time_,initial_epsilon_);
planner_->set_initialsolution_eps(initial_epsilon_);
planner_->set_search_mode(false);
ROS_DEBUG("[sbpl_lattice_planner] run planner");
vector<int> solution_stateIDs;
int solution_cost;
try{
int ret = planner_->replan(allocated_time_, &solution_stateIDs, &solution_cost);
if(ret)
ROS_DEBUG("Solution is found\n");
else{
ROS_INFO("Solution not found\n");
publishStats(solution_cost, 0, start, goal);
return false;
}
}
catch(SBPL_Exception e){
ROS_ERROR("SBPL encountered a fatal exception while planning");
return false;
}
ROS_DEBUG("size of solution=%d", (int)solution_stateIDs.size());
vector<EnvNAVXYTHETALAT3Dpt_t> sbpl_path;
try{
env_->ConvertStateIDPathintoXYThetaPath(&solution_stateIDs, &sbpl_path);
}
catch(SBPL_Exception e){
ROS_ERROR("SBPL encountered a fatal exception while reconstructing the path");
return false;
}
ROS_DEBUG("Plan has %d points.\n", (int)sbpl_path.size());
ros::Time plan_time = ros::Time::now();
//create a message for the plan
nav_msgs::Path gui_path;
gui_path.poses.resize(sbpl_path.size());
gui_path.header.frame_id = costmap_ros_->getGlobalFrameID();
gui_path.header.stamp = plan_time;
for(unsigned int i=0; i<sbpl_path.size(); i++){
geometry_msgs::PoseStamped pose;
pose.header.stamp = plan_time;
pose.header.frame_id = costmap_ros_->getGlobalFrameID();
pose.pose.position.x = sbpl_path[i].x + cost_map_.getOriginX();
pose.pose.position.y = sbpl_path[i].y + cost_map_.getOriginY();
pose.pose.position.z = start.pose.position.z;
tf::Quaternion temp;
temp.setEulerZYX(sbpl_path[i].theta,0,0);
pose.pose.orientation.x = temp.getX();
pose.pose.orientation.y = temp.getY();
pose.pose.orientation.z = temp.getZ();
pose.pose.orientation.w = temp.getW();
plan.push_back(pose);
gui_path.poses[i].pose.position.x = plan[i].pose.position.x;
gui_path.poses[i].pose.position.y = plan[i].pose.position.y;
gui_path.poses[i].pose.position.z = plan[i].pose.position.z;
}
plan_pub_.publish(gui_path);
publishStats(solution_cost, sbpl_path.size(), start, goal);
return true;
}
};