TinyRobotics
Loading...
Searching...
No Matches
Path.h
1#pragma once
2
3#include <algorithm>
4
5#include "TinyRobotics/coordinates/Coordinate.h"
6#include "TinyRobotics/utils/AllocatorPSRAM.h"
7#include "TinyRobotics/utils/Common.h"
8#include "TinyRobotics/utils/Optional.h"
9
10namespace tinyrobotics {
11
12/**
13 * @class ScheduledWayPoint
14 * @ingroup planning
15 * @brief Represents a waypoint with an associated timestamp.
16 *
17 * Used for time-based path following or trajectory execution, where each waypoint
18 * should be reached at a specific time.
19 *
20 * @tparam T Coordinate type (e.g., Coordinate, GPSCoordinate).
21 */
22template <typename CoordinateT>
24 public:
25 CoordinateT coordinate;
26 unsigned long timestamp; // Time at which this waypoint should be reached
27};
28
29/**
30 * @class WayPointAndSpeed
31 * @ingroup planning
32 * @brief Represents a waypoint with an associated speed.
33 *
34 * Used for path following or trajectory execution, where each waypoint specifies
35 * the desired speed at that point.
36 *
37 * @tparam T Coordinate type (e.g., Coordinate, GPSCoordinate).
38 */
39template <typename CoordinateT>
40class WayPointAndSpeed {
41 public:
42 CoordinateT coordinate;
43 float speed;
44};
45
46/**
47 * @class Path
48 * @ingroup planning
49 * @brief A simple path class that holds a sequence of waypoints. Each waypoint
50 * is e.g. a coordinate (e.g., x,y,z) that the robot should follow. The path can
51 * be used for navigation, motion planning, or trajectory execution. It provides
52 * methods to add waypoints.
53 *
54 * @tparam T Coordinate e.g. GPSCoordinate, Coordinate, ScheduledWayPoint,
55 * WayPointAndSpeed etc.
56 */
57
58template <typename CoordinateT = Coordinate<DistanceM>, typename AllocatorT = AllocatorPSRAM<CoordinateT>>
59class Path {
60 public:
61
62 /// Default constructor
63 Path() = default;
64
65 /// Construct a path from a variable number of waypoints
66 template <typename... Args>
67 Path(const Args&... args) {
68 addWaypoints(args...);
69 }
70
71 /// Construct a path from a vector of waypoints
72 Path(const std::vector<CoordinateT>& waypoints) : waypoints(waypoints) {}
73
74 /// Add a single waypoint to the path
75 void addWaypoint(const CoordinateT& waypoint) { waypoints.push_back(waypoint); }
76
77 /// Add multiple waypoints from a vector
78 void addWaypoints(const std::vector<CoordinateT>& new_waypoints) {
79 waypoints.insert(waypoints.end(), new_waypoints.begin(), new_waypoints.end());
80 }
81
82 /// Helper for variadic addWaypoints (base case)
83 void addWaypoints() {}
84
85 /// Helper for variadic addWaypoints (recursive case)
86 template <typename First, typename... Rest>
87 void addWaypoints(const First& first, const Rest&... rest) {
88 addWaypoint(first);
89 addWaypoints(rest...);
90 }
91
92 /// Replace all waypoints with a new vector
93 void setWaypoints(const std::vector<CoordinateT>& new_waypoints) {
94 waypoints = new_waypoints;
95 }
96
97 /// Set a waypoint at a specific position
98 bool setWaypoint(int pos, const CoordinateT& waypoint) {
99 if (pos >= 0 && pos < waypoints.size()) {
100 waypoints[pos] = waypoint;
101 return true;
102 }
103 return false;
104 }
105
106 /// Get all waypoints as a vector
107 std::vector<CoordinateT, AllocatorT> getWaypoints() const { return waypoints; }
108
109 /// Get a waypoint by index (returns std::nullopt if out of bounds)
110 std::optional<CoordinateT> getWaypoint(size_t index) const {
111 if (index < waypoints.size()) {
112 return waypoints[index];
113 }
114 return std::nullopt; // Out of bounds
115 }
116
117 /// Get the last waypoint (returns std::nullopt if empty)
118 std::optional<CoordinateT> getLastWaypoint() const {
119 if (!waypoints.empty()) {
120 return waypoints.back();
121 }
122 return std::nullopt; // No waypoints
123 }
124
125 /// Remove the first waypoint in the path (e.g., after reaching it). Returns true if a waypoint was removed, false if the path was already empty.
126 bool removeHead() {
127 if (!waypoints.empty()) {
128 waypoints.erase(waypoints.begin());
129 return true;
130 }
131 return false; // No waypoints to remove
132 }
133
134 /// Non-const index operator
135 CoordinateT& operator[](size_t index) { return waypoints[index]; }
136
137 /// Const index operator
138 const CoordinateT& operator[](size_t index) const { return waypoints[index]; }
139
140 /// Get the number of waypoints
141 size_t size() const { return waypoints.size(); }
142
143 /// Check if the path is empty
144 bool isEmpty() const { return waypoints.empty(); }
145
146 /// Reverse the order of waypoints
147 void reverse() { std::reverse(waypoints.begin(), waypoints.end()); }
148
149 /// Remove all waypoints
150 void clear() { waypoints.clear(); }
151
152 /// Calculate the total distance
153 float distance() const {
154 float total_distance = 0.0f;
155 for (size_t i = 1; i < waypoints.size(); ++i) {
156 total_distance += waypoints[i - 1].distance(waypoints[i]);
157 }
158 return total_distance;
159 }
160
161 protected:
162 std::vector<CoordinateT, AllocatorT> waypoints;
163};
164
165} // namespace tinyrobotics
Custom allocator that uses ESP32's PSRAM for memory allocation.
Definition: AllocatorPSRAM.h:21
A generic 3D coordinate class for robotics, navigation, and spatial calculations.
Definition: Coordinate.h:57
A simple path class that holds a sequence of waypoints. Each waypoint is e.g. a coordinate (e....
Definition: Path.h:59
std::optional< CoordinateT > getLastWaypoint() const
Get the last waypoint (returns std::nullopt if empty)
Definition: Path.h:118
void addWaypoints()
Helper for variadic addWaypoints (base case)
Definition: Path.h:83
Path(const Args &... args)
Construct a path from a variable number of waypoints.
Definition: Path.h:67
void addWaypoint(const CoordinateT &waypoint)
Add a single waypoint to the path.
Definition: Path.h:75
void addWaypoints(const std::vector< CoordinateT > &new_waypoints)
Add multiple waypoints from a vector.
Definition: Path.h:78
size_t size() const
Get the number of waypoints.
Definition: Path.h:141
void reverse()
Reverse the order of waypoints.
Definition: Path.h:147
bool removeHead()
Remove the first waypoint in the path (e.g., after reaching it). Returns true if a waypoint was remov...
Definition: Path.h:126
std::optional< CoordinateT > getWaypoint(size_t index) const
Get a waypoint by index (returns std::nullopt if out of bounds)
Definition: Path.h:110
void setWaypoints(const std::vector< CoordinateT > &new_waypoints)
Replace all waypoints with a new vector.
Definition: Path.h:93
float distance() const
Calculate the total distance.
Definition: Path.h:153
void addWaypoints(const First &first, const Rest &... rest)
Helper for variadic addWaypoints (recursive case)
Definition: Path.h:87
Path()=default
Default constructor.
CoordinateT & operator[](size_t index)
Non-const index operator.
Definition: Path.h:135
Path(const std::vector< CoordinateT > &waypoints)
Construct a path from a vector of waypoints.
Definition: Path.h:72
bool setWaypoint(int pos, const CoordinateT &waypoint)
Set a waypoint at a specific position.
Definition: Path.h:98
void clear()
Remove all waypoints.
Definition: Path.h:150
bool isEmpty() const
Check if the path is empty.
Definition: Path.h:144
std::vector< CoordinateT, AllocatorT > getWaypoints() const
Get all waypoints as a vector.
Definition: Path.h:107
const CoordinateT & operator[](size_t index) const
Const index operator.
Definition: Path.h:138
Represents a waypoint with an associated timestamp.
Definition: Path.h:23
Represents a waypoint with an associated speed.
Definition: Path.h:40