Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
GeoPlanner.h
Go to the documentation of this file.
1
10#ifndef GEOPLANNER_H
11#define GEOPLANNER_H
12
13#include "../../handlers/LiDARHandler.h"
14#include "../../util/GeospatialOperations.hpp"
15
17#include <RoveComm/RoveComm.h>
18#include <RoveComm/RoveCommManifest.h>
19#include <algorithm>
20#include <cmath>
21#include <mutex>
22#include <queue>
23#include <unordered_map>
24#include <unordered_set>
25#include <vector>
26
28
29
37namespace pathplanners
38{
39
49 {
50 public:
52 // Declare class methods.
54
55 GeoPlanner(double dTileSize = 50.0,
56 double dGridResolution = 0.5,
57 double dHeuristicWeight = 1.5,
58 double dBetaBias = 1.0,
59 double dMinTravScore = 0.0,
60 int nDilationPasses = 2,
61 double dSafeTravScoreThreshold = 0.5,
62 int nMaxSpiralSearchRadius = 20,
63 size_t siMaxPlotPointsPerTile = 10,
64 double dPenaltyScalingFactor = 10.0,
65 double dPenaltyPower = 2.0,
66 double dPathWaypointTolerance = 0.5);
68
69 std::vector<geoops::Waypoint> PlanPath(LiDARHandler* pLiDARHandler,
70 const geoops::UTMCoordinate& stStart,
71 const geoops::UTMCoordinate& stEnd,
72 double dSearchRadius = 3.0,
73 double dMaxSearchTimeSeconds = 120.0,
74 double dCorridorPadding = 100.0);
75
76 void ClearGeoCache();
77 void UnloadLiDARTiles(double minX, double maxX, double minY, double maxY);
78
80 // Setters.
82
83 void SetTileSize(double dTileSize);
84 void SetMinTravScore(double dMinTravScore);
85 void SetBetaBias(double dBetaBias);
86
88 // Getters.
90
91 double GetTileSize() const;
92 double GetMinTravScore() const;
93 double GetBetaBias() const;
94
95 private:
97 // Declare private class structs.
99
100
108 struct GridCell
109 {
110 public:
111 double dTravScore = -1.0; // -1.0 indicates an empty/unknown void cell.
112 double dAltitude = 0.0; // Altitude of the terrain at this cell mapping.
113 int nClosestPointID = -1; // The raw LiDAR point ID that defined this cell.
114 int nZone = 0; // UTM Zone for geographic boundary.
115 bool bInNorthernHemisphere = true; // Hemisphere marker for accurate UTM conversion.
116 };
117
118
127 {
128 public:
129 int nGridIndex = -1; // The unique identifier/index in the 1D costmap vector.
130 double dGCost = std::numeric_limits<double>::infinity(); // The cost from the start node to this node.
131 double dHCost = 0.0; // The heuristic cost from this node to the end node.
132 };
133
134
142 {
143 public:
144 bool operator()(const PlannerState& stLeftHandSide, const PlannerState& stRightHandSide) const
145 {
146 // Tiebreaker: Compare based on overall F-Cost, lower cost means higher priority in the Min-Heap.
147 return (stLeftHandSide.dGCost + stLeftHandSide.dHCost) > (stRightHandSide.dGCost + stRightHandSide.dHCost);
148 }
149 };
150
151
158 struct TileKey
159 {
160 public:
161 int nX; // X coordinate of the tile block.
162 int nY; // Y coordinate of the tile block.
163
164
174 bool operator==(const TileKey& stOther) const { return nX == stOther.nX && nY == stOther.nY; }
175 };
176
177
184 {
185 public:
186 size_t operator()(const TileKey& stKey) const noexcept
187 {
188 // Combine the X and Y integers into one size_t hash using bit shifting.
189 return (std::hash<int>()(stKey.nX) << 16) ^ std::hash<int>()(stKey.nY);
190 }
191 };
192
193
201 {
202 public:
203 bool operator()(const TileKey& stLeftHandSide, const TileKey& stRightHandSide) const noexcept
204 {
205 // Check if both X and Y coordinates are perfectly equal.
206 return (stLeftHandSide.nX == stRightHandSide.nX) && (stLeftHandSide.nY == stRightHandSide.nY);
207 }
208 };
209
211 // Declare private methods.
213
215 void SearchAStar();
216 std::vector<geoops::Waypoint> ReconstructPath() const;
217 void CheckAndLoadTile(int nTileX, int nTileY);
218 void FillGridHoles();
219 int FindNearestValidCell(int nStartIndex) const;
220 double EuclideanDistance(double dEasting1, double dNorthing1, double dEasting2, double dNorthing2) const;
221
222
232 inline int GetGridIndex(int nX, int nY) const { return nY * m_nGridWidth + nX; }
233
234
244 inline void GetGridCoords(int nIndex, int& nX, int& nY) const
245 {
246 nY = nIndex / m_nGridWidth;
247 nX = nIndex % m_nGridWidth;
248 }
249
250
256 const std::function<void(const rovecomm::RoveCommPacket<float>&, const sockaddr_in&)> fnMinTravScoreCallback =
257 [this](const rovecomm::RoveCommPacket<float>& stPacket, const sockaddr_in& stdAddr)
258 {
259 (void) stdAddr;
260
261 // Extract minimum travel score from incoming packet.
262 if (stPacket.vData.size() > 0)
263 {
264 m_dMinTravScore = static_cast<double>(stPacket.vData[0]);
265 this->ClearGeoCache();
266
267 LOG_NOTICE(logging::g_qSharedLogger,
268 "Incoming Packet: Setting GeoPlanner minimum travel score to {}. The tile cache has also been cleared.",
269 this->m_dMinTravScore);
270 }
271 };
272
273
279 const std::function<void(const rovecomm::RoveCommPacket<float>&, const sockaddr_in&)> fnBetaBiasCallback =
280 [this](const rovecomm::RoveCommPacket<float>& stPacket, const sockaddr_in& stdAddr)
281 {
282 (void) stdAddr;
283
284 // Extract beta bias from incoming packet.
285 if (stPacket.vData.size() > 0)
286 {
287 m_dBeta = static_cast<double>(stPacket.vData[0]);
288
289 LOG_NOTICE(logging::g_qSharedLogger, "Incoming Packet: Setting GeoPlanner beta bias to {}", this->m_dBeta);
290 }
291 };
292
294 // Private member variables.
296
297 // Extracted algorithmic magic numbers / Configurable parameters
298 double m_dTileSize; // Size of the database grid tiles in meters.
299 double m_dGridResolution; // Metric size of each cell in the discrete costmap (e.g., 0.5m).
300 double m_dHeuristicWeight; // Multiplier for Weighted A* to rapidly accelerate goal-seeking behavior.
301 double m_dBeta; // Bias factor exponent for traversal score penalties.
302 double m_dMinTravScore; // Minimum traversal score threshold required for a cell to be considered traversable.
303 int m_nDilationPasses; // Number of morphological passes used to bridge sparse LiDAR voids.
304 double m_dSafeTravScoreThreshold; // Minimum score permitted to establish a structurally "safe" origin for snapping.
305 int m_nMaxSpiralSearchRadius; // Maximum rings to expand outward to snap origin to a valid terrain location.
306 size_t m_siMaxPlotPointsPerTile; // Subsampling limit threshold to prevent plot renderer overload.
307 double m_dPenaltyScalingFactor; // Multiplier determining severity of avoidance behavior during A* traversal score checks.
308 double m_dPenaltyPower; // Exponential power applied to scores to make dangerous zones drastically more costly.
309 double m_dPathWaypointTolerance; // Tolerance radius encoded into resultant path waypoint structures.
310
311 // Request-specific variables configured during PlanPath
312 double m_dSearchRadius; // Contextual radius for KDTree legacy logic/bounding box padding base.
313 double m_dMaxSearchTimeSeconds; // The absolute maximum CPU time to spend searching for a path in seconds.
314 double m_dCorridorPadding; // Corridor padding radius used to expand the bounding box logic.
315
316 // Resource pointers and synchronization
317 LiDARHandler* m_pLiDARHandler; // Pointer to the LiDARHandler instance for fetching geospatial data point clouds.
318 std::mutex m_muPathGenMutex; // Mutex to protect concurrent path planning operations.
319
320 // Costmap Grid positional state
321 double m_dGridOriginEasting; // Bottom-left UTM easting bound of the generated grid.
322 double m_dGridOriginNorthing; // Bottom-left UTM northing bound of the generated grid.
323 int m_nGridWidth; // Total width of the grid in cell count.
324 int m_nGridHeight; // Total height of the grid in cell count.
325 int m_nStartIndex; // Flat 1D index of the validated start cell position.
326 int m_nEndIndex; // Flat 1D index of the validated end cell position.
327
328 std::vector<GridCell> m_vCostmap; // Flat 1D contiguous vector representing the 2.5D environment for highly optimized caching.
329
330 // Fast A* State Trackers (Using flat contiguous vectors instead of heavily fragmented Hash Maps)
331 std::priority_queue<PlannerState, std::vector<PlannerState>, PlannerStateCompare> m_pqOpenSetNextBest; // The min-heap of active frontier nodes.
332 std::vector<int> m_vPredecessors; // Tracks the index of the parent node to reconstruct the final path.
333 std::vector<bool> m_vbClosedSet; // Tracks which grid cells have already been fully evaluated by the algorithm.
334 std::vector<double> m_vdGCosts; // Stores the best known G-Cost to reach each specific cell index.
335
336 // Implicit graph representation database caches.
337 std::unordered_map<TileKey, std::vector<LiDARHandler::PointRow>, TileKeyHash, TileKeyEqual>
338 m_umTileMapCache; // Maps tile keys to arrays of raw LiDAR points.
339 };
340} // namespace pathplanners
341
342#endif // GEOPLANNER_H
The LiDARHandler class manages runtime queries against a LiDAR point cloud database for autonomy navi...
Definition LiDARHandler.h:40
This class implements a geospatial path planner that uses a discrete 2.5D Costmap and a fast Weighted...
Definition GeoPlanner.h:49
std::vector< geoops::Waypoint > ReconstructPath() const
Transforms the abstract 1D computational grid configurations logically backward to rebuild a continuo...
Definition GeoPlanner.cpp:589
double GetMinTravScore() const
Retrieves the actively configured minimum travel score parameter limit.
Definition GeoPlanner.cpp:255
void SetTileSize(double dTileSize)
Sets the dimensional size of database tiles mapped during planning.
Definition GeoPlanner.cpp:203
void SetMinTravScore(double dMinTravScore)
Sets the absolute minimum travel score required for a valid cell.
Definition GeoPlanner.cpp:216
const std::function< void(const rovecomm::RoveCommPacket< float > &, const sockaddr_in &)> fnMinTravScoreCallback
Callback function used to set the minimum travel score for path planning.
Definition GeoPlanner.h:256
double GetBetaBias() const
Retrieves the beta heuristic bias factor configured dynamically.
Definition GeoPlanner.cpp:268
~GeoPlanner()
Destroy the Geo Planner:: Geo Planner object.
Definition GeoPlanner.cpp:96
const std::function< void(const rovecomm::RoveCommPacket< float > &, const sockaddr_in &)> fnBetaBiasCallback
Callback function used to set the beta bias for travel scores in path planning.
Definition GeoPlanner.h:279
double EuclideanDistance(double dEasting1, double dNorthing1, double dEasting2, double dNorthing2) const
Calculates the standard 2D Euclidean distance between two geographic coordinates. This establishes ma...
Definition GeoPlanner.cpp:861
void UnloadLiDARTiles(double minX, double maxX, double minY, double maxY)
Unload tile LiDAR data.
Definition GeoPlanner.cpp:742
int FindNearestValidCell(int nStartIndex) const
Expands outward concentrically from a target grid cell to locate the nearest neighboring cell that co...
Definition GeoPlanner.cpp:776
int GetGridIndex(int nX, int nY) const
Inline helper to convert 2D grid coordinates to a 1D vector index.
Definition GeoPlanner.h:232
void CheckAndLoadTile(int nTileX, int nTileY)
Checks if a specific tile is loaded in the cache, and if not, fetches the relevant LiDAR point cloud ...
Definition GeoPlanner.cpp:645
bool PreloadCorridorAndBuildGrid(const geoops::UTMCoordinate &stStart, const geoops::UTMCoordinate &stEnd)
Calculates a bounding box based on Start and End coordinates, preloads LiDAR chunks,...
Definition GeoPlanner.cpp:285
void SearchAStar()
Actively runs a highly optimized Weighted A* pathfinding search upon the 1D Costmap grid utilizing ki...
Definition GeoPlanner.cpp:454
void FillGridHoles()
Performs morphological dilation on the costmap to fill in structural voids or gaps caused by sparse L...
Definition GeoPlanner.cpp:679
void SetBetaBias(double dBetaBias)
Sets the algorithm's penalty sensitivity bias multiplier.
Definition GeoPlanner.cpp:229
std::vector< geoops::Waypoint > PlanPath(LiDARHandler *pLiDARHandler, const geoops::UTMCoordinate &stStart, const geoops::UTMCoordinate &stEnd, double dSearchRadius=3.0, double dMaxSearchTimeSeconds=120.0, double dCorridorPadding=100.0)
Plan an optimal trajectory path from the start UTM to the end UTM coordinate utilizing a 2....
Definition GeoPlanner.cpp:116
double GetTileSize() const
Retrieves the currently configured database tile size constraint.
Definition GeoPlanner.cpp:242
void ClearGeoCache()
Explicitly zeroes and reclaims internal spatial database mapping arrays.
Definition GeoPlanner.cpp:188
void GetGridCoords(int nIndex, int &nX, int &nY) const
Inline helper to convert a 1D vector index back into 2D grid coordinates.
Definition GeoPlanner.h:244
This namespace stores classes, functions, and structs that are used to implement different path plann...
Definition AStar.cpp:30
This struct stores/contains information about a UTM coordinate.
Definition GeospatialOperations.hpp:211
Represents a single cell in our 2.5D discretized Costmap grid. This is used to aggregate sparse point...
Definition GeoPlanner.h:109
This struct is used to compare two PlannerState objects based on their cost to properly sort the prio...
Definition GeoPlanner.h:142
This struct represents the state of a node in the A* algorithm. Optimized to work with a flat 1D vect...
Definition GeoPlanner.h:127
This struct is used to compare two TileKey objects for equality after a hash collision is triggered.
Definition GeoPlanner.h:201
This struct is used to hash TileKey objects for use in unordered maps.
Definition GeoPlanner.h:184
This struct represents a tile key in the implicit graph representation. We divide the plan into a reg...
Definition GeoPlanner.h:159
bool operator==(const TileKey &stOther) const
Overridden operator equals for TileKey struct.
Definition GeoPlanner.h:174