PathPlannerLib
Loading...
Searching...
No Matches
PathPlannerAuto.h
1#pragma once
2
3#include <frc2/command/CommandHelper.h>
4#include <frc2/command/button/Trigger.h>
5#include <frc/geometry/Pose2d.h>
6#include <wpi/json.h>
7#include <string>
8#include <memory>
9#include <vector>
10#include <frc/event/EventLoop.h>
11#include <functional>
12#include <frc/Timer.h>
13#include "pathplanner/lib/path/PathPlannerPath.h"
14#include "pathplanner/lib/events/EventTrigger.h"
15#include "pathplanner/lib/events/PointTowardsZoneTrigger.h"
16#include "pathplanner/lib/auto/AutoBuilder.h"
17
18namespace pathplanner {
22class PathPlannerAuto: public frc2::CommandHelper<frc2::Command, PathPlannerAuto> {
23public:
24 static std::string currentPathName;
25
31 PathPlannerAuto(std::string autoName);
32
41 PathPlannerAuto(std::string autoName, bool mirror);
42
49 PathPlannerAuto(frc2::CommandPtr &&autoCommand, frc::Pose2d startingPose =
50 frc::Pose2d());
51
58 static std::vector<std::shared_ptr<PathPlannerPath>> getPathGroupFromAutoFile(
59 std::string autoName);
60
67 constexpr frc::Pose2d getStartingPose() const {
68 return m_startingPose;
69 }
70
78 inline frc2::Trigger condition(std::function<bool()> condition) {
79 return frc2::Trigger(m_autoLoop.get(), condition);
80 }
81
87 inline frc2::Trigger isRunning() {
88 return condition([this]() {
89 return m_isRunning;
90 });
91 }
92
99 inline frc2::Trigger timeElapsed(units::second_t time) {
100 return condition([this, time]() {
101 return m_timer.HasElapsed(time);
102 });
103 }
104
112 inline frc2::Trigger timeRange(units::second_t startTime,
113 units::second_t endTime) {
114 return condition([this, startTime, endTime]() {
115 return m_timer.Get() >= startTime && m_timer.Get() <= endTime;
116 });
117 }
118
126 inline EventTrigger event(std::string eventName) {
127 return EventTrigger(m_autoLoop.get(), eventName);
128 }
129
137 inline PointTowardsZoneTrigger pointTowardsZone(std::string zoneName) {
138 return PointTowardsZoneTrigger(m_autoLoop.get(), zoneName);
139 }
140
147 inline frc2::Trigger activePath(std::string pathName) {
148 return condition([pathName]() {
149 return pathName == PathPlannerAuto::currentPathName;
150 });
151 }
152
162 inline frc2::Trigger nearFieldPosition(frc::Translation2d fieldPosition,
163 units::meter_t tolerance) {
164 return condition(
165 [fieldPosition, tolerance]() {
166 return AutoBuilder::getCurrentPose().Translation().Distance(
167 fieldPosition) <= tolerance;
168 });
169 }
170
180 frc2::Trigger nearFieldPositionAutoFlipped(
181 frc::Translation2d blueFieldPosition, units::meter_t tolerance);
182
193 frc2::Trigger inFieldArea(frc::Translation2d boundingBoxMin,
194 frc::Translation2d boundingBoxMax);
195
208 frc2::Trigger inFieldAreaAutoFlipped(frc::Translation2d blueBoundingBoxMin,
209 frc::Translation2d blueBoundingBoxMax);
210
211 void Initialize() override;
212
213 void Execute() override;
214
215 bool IsFinished() override;
216
217 void End(bool interrupted) override;
218
219private:
220 std::unique_ptr<frc2::Command> m_autoCommand;
221 frc::Pose2d m_startingPose;
222
223 // Use a unique_ptr to avoid delted copy constructor shenanigans
224 std::unique_ptr<frc::EventLoop> m_autoLoop;
225 frc::Timer m_timer;
226 bool m_isRunning;
227
228 static std::vector<std::shared_ptr<PathPlannerPath>> pathsFromCommandJson(
229 const wpi::json &json, bool choreoPaths);
230
231 void initFromJson(const wpi::json &json, bool mirror);
232
233 static int m_instances;
234};
235}
static frc::Pose2d getCurrentPose()
Definition: AutoBuilder.h:126
Definition: EventTrigger.h:9
Definition: PathPlannerAuto.h:22
EventTrigger event(std::string eventName)
Definition: PathPlannerAuto.h:126
frc2::Trigger condition(std::function< bool()> condition)
Definition: PathPlannerAuto.h:78
static std::vector< std::shared_ptr< PathPlannerPath > > getPathGroupFromAutoFile(std::string autoName)
Definition: PathPlannerAuto.cpp:138
frc2::Trigger nearFieldPosition(frc::Translation2d fieldPosition, units::meter_t tolerance)
Definition: PathPlannerAuto.h:162
frc2::Trigger activePath(std::string pathName)
Definition: PathPlannerAuto.h:147
frc2::Trigger timeRange(units::second_t startTime, units::second_t endTime)
Definition: PathPlannerAuto.h:112
constexpr frc::Pose2d getStartingPose() const
Definition: PathPlannerAuto.h:67
frc2::Trigger isRunning()
Definition: PathPlannerAuto.h:87
frc2::Trigger inFieldAreaAutoFlipped(frc::Translation2d blueBoundingBoxMin, frc::Translation2d blueBoundingBoxMax)
Definition: PathPlannerAuto.cpp:106
PointTowardsZoneTrigger pointTowardsZone(std::string zoneName)
Definition: PathPlannerAuto.h:137
frc2::Trigger nearFieldPositionAutoFlipped(frc::Translation2d blueFieldPosition, units::meter_t tolerance)
Definition: PathPlannerAuto.cpp:71
frc2::Trigger timeElapsed(units::second_t time)
Definition: PathPlannerAuto.h:99
frc2::Trigger inFieldArea(frc::Translation2d boundingBoxMin, frc::Translation2d boundingBoxMax)
Definition: PathPlannerAuto.cpp:88
Definition: PointTowardsZoneTrigger.h:9