TinyRobotics
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
AStar< T > Class Template Reference

Flexible A* pathfinding algorithm for any map implementing IMap<T> and using Coordinate<T> nodes. More...

#include <AStar.h>

Classes

struct  NodeRecord
 

Public Types

using CostCallback = std::function< float(const Coordinate< T > &, const Coordinate< T > &, void *ref)>
 
using ValidCallback = std::function< bool(const Coordinate< T > &, void *ref)>
 

Public Member Functions

void setCostCallback (CostCallback cb)
 
void setValidCallback (ValidCallback cb)
 provide a callback to determine if a node is valid (e.g., not an obstacle)
 
void setReference (void *reference)
 
Path< Coordinate< T > > findPath (const IMapNeighbors< T > &map, const Coordinate< T > &start, const Coordinate< T > &goal)
 
Coordinate< T > nextStep (const IMapNeighbors< T > &map, const Coordinate< T > &start, const Coordinate< T > &goal)
 

Protected Member Functions

bool aStarSearch (const IMapNeighbors< T > &map, const Coordinate< T > &start, const Coordinate< T > &goal, std::unordered_map< Coordinate< T >, Coordinate< T >, std::hash< Coordinate< T > >, std::equal_to< Coordinate< T > >, AllocatorPSRAM< std::pair< const Coordinate< T >, Coordinate< T > > > > *cameFrom, std::unordered_map< Coordinate< T >, float, std::hash< Coordinate< T > >, std::equal_to< Coordinate< T > >, AllocatorPSRAM< std::pair< const Coordinate< T >, float > > > *outCostSoFar)
 
Path< Coordinate< T > > reconstructPath (const std::unordered_map< Coordinate< T >, Coordinate< T >, std::hash< Coordinate< T > >, std::equal_to< Coordinate< T > >, AllocatorPSRAM< std::pair< const Coordinate< T >, Coordinate< T > > > > &cameFrom, const Coordinate< T > &start, const Coordinate< T > &goal)
 

Static Protected Member Functions

static float defaultCost (const Coordinate< T > &from, const Coordinate< T > &to, void *ref)
 
static bool defaultValid (const Coordinate< T > &node, void *ref)
 

Protected Attributes

CostCallback cost_cb = defaultCost
 
ValidCallback valid_cb = defaultValid
 
void * ref = this
 

Detailed Description

template<typename T = DistanceM>
class tinyrobotics::AStar< T >

Flexible A* pathfinding algorithm for any map implementing IMap<T> and using Coordinate<T> nodes.

The AStar class implements the A* search algorithm for any map or graph that implements the IMap<T> interface. It uses Coordinate<T> as the node type and supports user-defined cost and validity callbacks for custom metrics, obstacle handling, and heuristics.

Features:

Usage:

  1. Create an AStar instance (optionally providing cost and validity callbacks).
  2. Call findPath(map, start, goal) to get the optimal path as a Path<Coordinate<T>> object.
  3. Optionally, use nextStep(map, start, goal) to get only the next move.
  4. Customize cost and heuristic logic via the cost callback (e.g., Euclidean, Manhattan, or domain-specific).

Example:

AStar astar;
astar.setCostCallback([](const Coordinate<DistanceM>& from, const Coordinate<DistanceM>& to, void*) {
// Custom cost (e.g., Euclidean distance)
return from.distance(to);
});
auto path = astar.findPath(map, start, goal);
if (!path.empty()) {
// Use the path
}
Flexible A* pathfinding algorithm for any map implementing IMap<T> and using Coordinate<T> nodes.
Definition: AStar.h:52
Path< Coordinate< T > > findPath(const IMapNeighbors< T > &map, const Coordinate< T > &start, const Coordinate< T > &goal)
Definition: AStar.h:70
void setCostCallback(CostCallback cb)
Definition: AStar.h:61
A generic 3D coordinate class for robotics, navigation, and spatial calculations.
Definition: Coordinate.h:57
DistanceM distance(const Coordinate &other, DistanceUnit unit=DistanceUnit::M) const
Definition: Coordinate.h:79

This class is suitable for embedded and desktop robotics, navigation, and any application requiring flexible, efficient pathfinding.

Member Function Documentation

◆ findPath()

Path< Coordinate< T > > findPath ( const IMapNeighbors< T > &  map,
const Coordinate< T > &  start,
const Coordinate< T > &  goal 
)
inline

Finds the optimal path from start to goal. Returns an empty path if no path

◆ nextStep()

Coordinate< T > nextStep ( const IMapNeighbors< T > &  map,
const Coordinate< T > &  start,
const Coordinate< T > &  goal 
)
inline

Returns the next node on the optimal path from start to goal. If no path is found, returns start.

◆ setCostCallback()

void setCostCallback ( CostCallback  cb)
inline

provide a callback to determine the cost of moving from one node to another

◆ setReference()

void setReference ( void *  reference)
inline

provide reference for callbacks (e.g., to access map data or other context)


The documentation for this class was generated from the following file: