PathPlannerLib
Loading...
Searching...
No Matches
PathPlannerPath.h
1#pragma once
2
3#include "pathplanner/lib/path/RotationTarget.h"
4#include "pathplanner/lib/path/PointTowardsZone.h"
5#include "pathplanner/lib/path/ConstraintsZone.h"
6#include "pathplanner/lib/path/EventMarker.h"
7#include "pathplanner/lib/path/PathConstraints.h"
8#include "pathplanner/lib/path/IdealStartingState.h"
9#include "pathplanner/lib/path/GoalEndState.h"
10#include "pathplanner/lib/path/PathPoint.h"
11#include "pathplanner/lib/path/Waypoint.h"
12#include "pathplanner/lib/trajectory/PathPlannerTrajectory.h"
13#include "pathplanner/lib/config/RobotConfig.h"
14#include <vector>
15#include <optional>
16#include <frc/geometry/Translation2d.h>
17#include <frc/kinematics/ChassisSpeeds.h>
18#include <wpi/json.h>
19#include <string>
20#include <unordered_map>
21#include <units/length.h>
22#include <memory>
23#include <initializer_list>
24
25namespace pathplanner {
26class PathPlannerPath: public std::enable_shared_from_this<PathPlannerPath> {
27public:
28 std::string name;
29 bool preventFlipping = false;
30
45 PathPlannerPath(std::vector<Waypoint> waypoints,
46 std::vector<RotationTarget> rotationTargets,
47 std::vector<PointTowardsZone> pointTowardsZones,
48 std::vector<ConstraintsZone> constraintZones,
49 std::vector<EventMarker> eventMarkers,
50 PathConstraints globalConstraints,
51 std::optional<IdealStartingState> idealStartingState,
52 GoalEndState goalEndState, bool reversed);
53
65 PathPlannerPath(std::vector<Waypoint> waypoints,
66 PathConstraints constraints,
67 std::optional<IdealStartingState> idealStartingState,
68 GoalEndState goalEndState, bool reversed = false) : PathPlannerPath(
69 waypoints, std::vector<RotationTarget>(),
70 std::vector<PointTowardsZone>(), std::vector<ConstraintsZone>(),
71 std::vector<EventMarker>(), constraints, idealStartingState,
72 goalEndState, reversed) {
73 }
74
78 PathPlannerPath(PathConstraints constraints, GoalEndState goalEndState);
79
80 void hotReload(const wpi::json &json);
81
88 static std::vector<Waypoint> waypointsFromPoses(
89 std::vector<frc::Pose2d> poses);
90
97 [[deprecated("Renamed to waypointsFromPoses")]]
98 static inline std::vector<Waypoint> bezierFromPoses(
99 std::vector<frc::Pose2d> poses) {
100 return waypointsFromPoses(poses);
101 }
102
109 static std::shared_ptr<PathPlannerPath> fromPathFile(std::string pathName);
110
118 static std::shared_ptr<PathPlannerPath> fromChoreoTrajectory(
119 std::string trajectoryName);
120
129 static inline std::shared_ptr<PathPlannerPath> fromChoreoTrajectory(
130 std::string trajectoryName, size_t splitIndex) {
131 std::string cacheName = trajectoryName + "."
132 + std::to_string(splitIndex);
133 if (getChoreoPathCache().contains(cacheName)) {
134 return getChoreoPathCache()[cacheName];
135 }
136
137 // Path is not in the cache, load the main trajectory to load all splits
138 loadChoreoTrajectoryIntoCache(trajectoryName);
139 return getChoreoPathCache()[cacheName];
140 }
141
147 frc::Pose2d getStartingDifferentialPose();
148
155 std::optional<frc::Pose2d> getStartingHolonomicPose();
156
161 static std::shared_ptr<PathPlannerPath> fromPathPoints(
162 std::vector<PathPoint> pathPoints,
163 PathConstraints globalConstraints, GoalEndState goalEndState);
164
170 constexpr const std::vector<PathPoint>& getAllPathPoints() const {
171 return m_allPoints;
172 }
173
179 inline size_t numPoints() const {
180 return m_allPoints.size();
181 }
182
189 inline const PathPoint& getPoint(size_t index) const {
190 return m_allPoints[index];
191 }
192
202 std::optional<PathPlannerTrajectory> getIdealTrajectory(
203 RobotConfig robotConfig);
204
210 inline frc::Rotation2d getInitialHeading() const {
211 return (getPoint(1).position - getPoint(0).position).Angle();
212 }
213
218 constexpr std::vector<Waypoint>& getWaypoints() {
219 return m_waypoints;
220 }
221
226 constexpr std::vector<RotationTarget>& getRotationTargets() {
227 return m_rotationTargets;
228 }
229
235 constexpr std::vector<PointTowardsZone>& getPointTowardsZones() {
236 return m_pointTowardsZones;
237 }
238
243 constexpr std::vector<ConstraintsZone>& getConstraintZones() {
244 return m_constraintZones;
245 }
246
252 constexpr const PathConstraints& getGlobalConstraints() const {
253 return m_globalConstraints;
254 }
255
261 constexpr const GoalEndState& getGoalEndState() const {
262 return m_goalEndState;
263 }
264
270 constexpr const std::optional<IdealStartingState>& getIdealStartingState() const {
271 return m_idealStartingState;
272 }
273
279 constexpr std::vector<EventMarker>& getEventMarkers() {
280 return m_eventMarkers;
281 }
282
288 constexpr bool isReversed() const {
289 return m_reversed;
290 }
291
297 constexpr bool isChoreoPath() const {
298 return m_isChoreoPath;
299 }
300
301 inline PathPlannerTrajectory generateTrajectory(
302 frc::ChassisSpeeds startingSpeeds, frc::Rotation2d startingRotation,
303 const RobotConfig &config) {
304 if (m_isChoreoPath) {
305 return m_idealTrajectory.value();
306 } else {
307 return PathPlannerTrajectory(shared_from_this(), startingSpeeds,
308 startingRotation, config);
309 }
310 }
311
317 std::shared_ptr<PathPlannerPath> flipPath();
318
326 std::shared_ptr<PathPlannerPath> mirrorPath();
327
334 inline std::vector<frc::Pose2d> getPathPoses() const {
335 std::vector < frc::Pose2d > poses;
336 for (const PathPoint &point : m_allPoints) {
337 poses.emplace_back(point.position, frc::Rotation2d());
338 }
339 return poses;
340 }
341
343 static inline void clearPathCache() {
344 PathPlannerPath::getPathCache().clear();
345 PathPlannerPath::getChoreoPathCache().clear();
346 }
347
348private:
349 std::vector<PathPoint> createPath();
350
351 static std::shared_ptr<PathPlannerPath> fromJson(const wpi::json &json);
352
353 static inline std::vector<Waypoint> waypointsFromJson(
354 const wpi::json &waypointsJson) {
355 std::vector < Waypoint > waypoints;
356 for (wpi::json::const_reference waypoint : waypointsJson) {
357 waypoints.emplace_back(Waypoint::fromJson(waypoint));
358 }
359 return waypoints;
360 }
361
362 static void loadChoreoTrajectoryIntoCache(std::string trajectoryName);
363
364 void precalcValues();
365
366 static units::meter_t getCurveRadiusAtPoint(size_t index,
367 std::vector<PathPoint> &points);
368
369 inline PathConstraints constraintsForWaypointPos(double pos) const {
370 for (auto z : m_constraintZones) {
371 if (pos >= z.getMinWaypointRelativePos()
372 && pos <= z.getMaxWaypointRelativePos()) {
373 return z.getConstraints();
374 }
375 }
376
377 // Check if constraints should be unlimited
378 if (m_globalConstraints.isUnlimited()) {
380 m_globalConstraints.getNominalVoltage());
381 }
382
383 return m_globalConstraints;
384 }
385
386 inline std::optional<PointTowardsZone> pointZoneForWaypointPos(
387 double pos) const {
388 for (auto z : m_pointTowardsZones) {
389 if (pos >= z.getMinWaypointRelativePos()
390 && pos <= z.getMaxWaypointRelativePos()) {
391 return z;
392 }
393 }
394 return std::nullopt;
395 }
396
397 static frc::Translation2d mirrorTranslation(frc::Translation2d translation);
398
399 frc::Translation2d samplePath(double waypointRelativePos) const;
400
401 static std::unordered_map<std::string, std::shared_ptr<PathPlannerPath>>& getPathCache();
402
403 static std::unordered_map<std::string, std::shared_ptr<PathPlannerPath>>& getChoreoPathCache();
404
405 std::vector<Waypoint> m_waypoints;
406 std::vector<RotationTarget> m_rotationTargets;
407 std::vector<PointTowardsZone> m_pointTowardsZones;
408 std::vector<ConstraintsZone> m_constraintZones;
409 std::vector<EventMarker> m_eventMarkers;
410 PathConstraints m_globalConstraints;
411 std::optional<IdealStartingState> m_idealStartingState;
412 GoalEndState m_goalEndState;
413 std::vector<PathPoint> m_allPoints;
414 bool m_reversed;
415
416 bool m_isChoreoPath;
417 std::optional<PathPlannerTrajectory> m_idealTrajectory = std::nullopt;
418
419 static int m_instances;
420
421 static constexpr double targetIncrement = 0.05;
422 static constexpr units::meter_t targetSpacing = 0.2_m;
423};
424}
Definition: ConstraintsZone.h:7
Definition: EventMarker.h:11
Definition: GoalEndState.h:9
Definition: PathConstraints.h:12
static constexpr PathConstraints unlimitedConstraints(units::volt_t nominalVoltage)
Definition: PathConstraints.h:48
constexpr units::volt_t getNominalVoltage() const
Definition: PathConstraints.h:99
Definition: PathPlannerPath.h:26
static std::shared_ptr< PathPlannerPath > fromPathFile(std::string pathName)
Definition: PathPlannerPath.cpp:107
constexpr std::vector< ConstraintsZone > & getConstraintZones()
Definition: PathPlannerPath.h:243
frc::Pose2d getStartingDifferentialPose()
Definition: PathPlannerPath.cpp:627
std::optional< frc::Pose2d > getStartingHolonomicPose()
Definition: PathPlannerPath.cpp:640
const PathPoint & getPoint(size_t index) const
Definition: PathPlannerPath.h:189
frc::Rotation2d getInitialHeading() const
Definition: PathPlannerPath.h:210
constexpr std::vector< Waypoint > & getWaypoints()
Definition: PathPlannerPath.h:218
static std::shared_ptr< PathPlannerPath > fromChoreoTrajectory(std::string trajectoryName, size_t splitIndex)
Definition: PathPlannerPath.h:129
static std::shared_ptr< PathPlannerPath > fromPathPoints(std::vector< PathPoint > pathPoints, PathConstraints globalConstraints, GoalEndState goalEndState)
Definition: PathPlannerPath.cpp:381
static std::vector< Waypoint > bezierFromPoses(std::vector< frc::Pose2d > poses)
Definition: PathPlannerPath.h:98
constexpr const GoalEndState & getGoalEndState() const
Definition: PathPlannerPath.h:261
static std::vector< Waypoint > waypointsFromPoses(std::vector< frc::Pose2d > poses)
Definition: PathPlannerPath.cpp:76
std::vector< frc::Pose2d > getPathPoses() const
Definition: PathPlannerPath.h:334
constexpr const std::vector< PathPoint > & getAllPathPoints() const
Definition: PathPlannerPath.h:170
constexpr const PathConstraints & getGlobalConstraints() const
Definition: PathPlannerPath.h:252
constexpr bool isReversed() const
Definition: PathPlannerPath.h:288
constexpr std::vector< EventMarker > & getEventMarkers()
Definition: PathPlannerPath.h:279
size_t numPoints() const
Definition: PathPlannerPath.h:179
std::shared_ptr< PathPlannerPath > flipPath()
Definition: PathPlannerPath.cpp:722
static std::shared_ptr< PathPlannerPath > fromChoreoTrajectory(std::string trajectoryName)
Definition: PathPlannerPath.cpp:946
constexpr std::vector< RotationTarget > & getRotationTargets()
Definition: PathPlannerPath.h:226
PathPlannerPath(std::vector< Waypoint > waypoints, PathConstraints constraints, std::optional< IdealStartingState > idealStartingState, GoalEndState goalEndState, bool reversed=false)
Definition: PathPlannerPath.h:65
constexpr const std::optional< IdealStartingState > & getIdealStartingState() const
Definition: PathPlannerPath.h:270
std::shared_ptr< PathPlannerPath > mirrorPath()
Definition: PathPlannerPath.cpp:784
std::optional< PathPlannerTrajectory > getIdealTrajectory(RobotConfig robotConfig)
Definition: PathPlannerPath.cpp:651
static void clearPathCache()
Definition: PathPlannerPath.h:343
constexpr std::vector< PointTowardsZone > & getPointTowardsZones()
Definition: PathPlannerPath.h:235
constexpr bool isChoreoPath() const
Definition: PathPlannerPath.h:297
Definition: PathPlannerTrajectory.h:29
Definition: PathPoint.h:13
Definition: PointTowardsZone.h:11
Definition: RobotConfig.h:17
Definition: RotationTarget.h:7
static Waypoint fromJson(const wpi::json &waypointJson)
Definition: Waypoint.cpp:26