@@ -130,6 +130,11 @@ void AStarAlgorithm<NodeT>::setSearchBounds(const geometry_msgs::Pose& search_bo
130
130
_expander->setSearchBounds (search_bounds, start_point, allow_goal_overshoot);
131
131
}
132
132
133
+ template <typename NodeT>
134
+ void AStarAlgorithm<NodeT>::setSearchStraightPathFlag(const bool search_straight_path) {
135
+ _search_info.search_straight_path = search_straight_path;
136
+ }
137
+
133
138
134
139
template <typename NodeT>
135
140
typename AStarAlgorithm<NodeT>::NodePtr AStarAlgorithm<NodeT>::addToGraph(
@@ -292,6 +297,98 @@ bool AStarAlgorithm<NodeT>::areInputsValid()
292
297
return true ;
293
298
}
294
299
300
+ template <typename NodeT>
301
+ uint32_t AStarAlgorithm<NodeT>::getStraightPath(
302
+ CoordinateVector & path,
303
+ std::function<bool ()> cancel_checker)
304
+ {
305
+ if (!areInputsValid ()) {
306
+ return mbf_msgs::GetPathResult::INTERNAL_ERROR;
307
+ }
308
+
309
+ // Start coordinates differ for Node2D
310
+ typename NodeT::Coordinates start_coords;
311
+ if constexpr (std::is_same<NodeT, Node2D>::value) {
312
+ start_coords = _start->getCoords (_start->getIndex ());
313
+ } else {
314
+ start_coords = _start->pose ;
315
+ }
316
+
317
+ typename NodeT::Coordinates goal_coords = _goal_coordinates;
318
+
319
+ std::vector<Coordinates> path_coordinates;
320
+
321
+ // Add goal
322
+ path_coordinates.push_back (goal_coords);
323
+
324
+ // Simple interpolation between start and goal
325
+ const float dx = goal_coords.x - start_coords.x ;
326
+ const float dy = goal_coords.y - start_coords.y ;
327
+ const float steps = std::max (std::abs (dx), std::abs (dy));
328
+
329
+ // Add intermediate points along the straight line
330
+ for (int i = 1 ; i < steps; ++i) {
331
+ if (cancel_checker ()) {
332
+ return mbf_msgs::GetPathResult::CANCELED;
333
+ }
334
+ const float ratio = static_cast <float >(i) / steps;
335
+ typename NodeT::Coordinates intermediate;
336
+ intermediate.x = start_coords.x + dx * ratio;
337
+ intermediate.y = start_coords.y + dy * ratio;
338
+
339
+ if constexpr (!std::is_same<NodeT, Node2D>::value) {
340
+ // Only non-Node2D has theta
341
+ intermediate.theta = start_coords.theta ;
342
+ }
343
+
344
+ unsigned int index;
345
+ if constexpr (std::is_same<NodeT, Node2D>::value) {
346
+ index = Node2D::getIndex (
347
+ static_cast <unsigned int >(intermediate.x ),
348
+ static_cast <unsigned int >(intermediate.y ),
349
+ _costmap->getSizeInCellsX ());
350
+ } else {
351
+ index = NodeT::getIndex (
352
+ static_cast <unsigned int >(intermediate.x ),
353
+ static_cast <unsigned int >(intermediate.y ),
354
+ static_cast <unsigned int >(intermediate.theta ));
355
+ }
356
+
357
+ NodePtr node = addToGraph (index);
358
+
359
+ if constexpr (!std::is_same<NodeT, Node2D>::value) {
360
+ node->setPose (intermediate);
361
+ }
362
+
363
+ // Check if this node is valid
364
+ if (!node->isNodeValid (_traverse_unknown, _collision_checker)) {
365
+ double map_x, map_y;
366
+ _costmap->mapToWorld (
367
+ static_cast <unsigned int >(intermediate.x ),
368
+ static_cast <unsigned int >(intermediate.y ),
369
+ map_x, map_y);
370
+ ROS_ERROR_NAMED (" smac_planner" ,
371
+ " Fail to plan straight path because point at (%.2f, %.2f) is in collision" , map_x, map_y);
372
+ return mbf_msgs::GetPathResult::NO_PATH_FOUND;
373
+ }
374
+
375
+ path_coordinates.push_back (intermediate);
376
+ }
377
+
378
+ // Add start
379
+ path_coordinates.push_back (start_coords);
380
+
381
+ if constexpr (!std::is_same<NodeT, Node2D>::value) {
382
+ for (auto & coord : path_coordinates) {
383
+ coord.theta = NodeT::motion_table.getAngleFromBin (coord.theta );
384
+ }
385
+ }
386
+
387
+ path = std::move (path_coordinates);
388
+ return mbf_msgs::GetPathResult::SUCCESS;
389
+ }
390
+
391
+
295
392
template <typename NodeT>
296
393
uint32_t AStarAlgorithm<NodeT>::createPath(
297
394
CoordinateVector & path, int & iterations,
@@ -308,6 +405,12 @@ uint32_t AStarAlgorithm<NodeT>::createPath(
308
405
return mbf_msgs::GetPathResult::INTERNAL_ERROR;
309
406
}
310
407
408
+ // Check if we should use straight path
409
+ if (_search_info.search_straight_path ) {
410
+ return getStraightPath (path, cancel_checker);
411
+ }
412
+
413
+
311
414
// 0) Add starting point to the open set
312
415
addNode (0.0 , getStart ());
313
416
getStart ()->setAccumulatedCost (0.0 );
0 commit comments