Valkyrie 2026
Loading...
Searching...
No Matches
TrajectoryFollower.h
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <functional>
6#include <optional>
7
8#include <choreo/trajectory/SwerveSample.h>
9#include <choreo/trajectory/Trajectory.h>
10#include <frc/Timer.h>
11#include <frc/apriltag/AprilTagFieldLayout.h>
12#include <frc/controller/ProfiledPIDController.h>
13#include <frc/geometry/Pose2d.h>
14#include <frc/geometry/Rotation2d.h>
15#include <frc/kinematics/ChassisSpeeds.h>
16#include <frc/kinematics/SwerveModuleState.h>
17#include <frc2/command/Command.h>
18#include <frc2/command/CommandHelper.h>
19#include <frc2/command/CommandPtr.h>
20#include <frc2/command/Commands.h>
21#include <frc2/command/FunctionalCommand.h>
22#include <frc2/command/SequentialCommandGroup.h>
23#include <units/acceleration.h>
24#include <units/time.h>
25#include <valkyrie/Loggable.h>
26#include <valkyrie/WorldAlign.h>
27#include <valkyrie/controllers/BaseController.h>
28#include <valkyrie/controllers/PIDController.h>
29#include <valkyrie/controllers/PIDF.h>
30#include <valkyrie/drivetrain/swerve/Swerve.h>
31
32namespace valor {
33
38 units::meters_per_second_squared_t ax;
39 units::meters_per_second_squared_t ay;
40 units::radians_per_second_squared_t alpha;
41};
42
47 units::second_t timestamp;
48 frc::Pose2d pose;
49 frc::ChassisSpeeds speeds;
51};
52
57 public:
64
68 enum class TrajectoryEndCondition { TIME, POSITION };
69
70 enum class FieldSymmetry { MIRROR, ROTATE };
71
83 static void Register(swerve::Swerve& s) { swerve = &s; }
84
95 static frc2::CommandPtr FollowPath(
96 std::optional<choreo::Trajectory<choreo::SwerveSample>> traj, TrajectoryEndCondition end, bool isMirrored, bool resetOdom,
97 units::meter_t tolerance = 0.25_m, units::second_t timeWithoutCorrection = 0_s,
98 std::function<bool()> interruption = [] { return false; });
99
106 static void SetPID(WorldAlign::TranslationalPIDF_t x, WorldAlign::TranslationalPIDF_t y, WorldAlign::RotationalPIDF_t rot);
107
108 private:
109 class FollowCommand : public frc2::CommandHelper<frc2::Command, FollowCommand> {
110 public:
111 FollowCommand(choreo::Trajectory<choreo::SwerveSample> traj, TrajectoryEndCondition end, bool isMirrored, units::meter_t tolerance,
112 units::second_t timeWithoutCorrection, std::function<bool()> interruption)
113 : traj{traj},
114 end{end},
115 isMirrored{isMirrored},
116 tolerance{tolerance},
117 timeWithoutCorrection{timeWithoutCorrection},
118 interruption{interruption} {}
119
120 void Initialize() override;
121
122 void Execute() override;
123
124 bool IsFinished() override;
125
126 private:
127 choreo::Trajectory<choreo::SwerveSample> traj;
129 bool isMirrored;
130 units::meter_t tolerance;
131 units::second_t timeWithoutCorrection;
132 std::function<bool()> interruption;
133
134 frc::Timer timer;
136 PIDController<units::meter, units::meters_per_second> yController{{}};
137 PIDController<units::radian, units::radians_per_second> rotController{{}};
138 };
139
145 static AutoSwerveState ConvertChoreoSampleIntoSwerveState(choreo::SwerveSample sample);
152 static AutoSwerveState Interop(units::second_t time, std::vector<AutoSwerveState> traj);
158 static AutoSwerveState FlipSwerveState(AutoSwerveState state, FieldSymmetry sym);
164 static AutoSwerveState MirrorSwerveState(AutoSwerveState state);
165
166 static bool IsAllianceFlipped();
167
168 static WorldAlign::TranslationalPIDF_t xPIDF, yPIDF;
169 static WorldAlign::RotationalPIDF_t rotPIDF;
170
171 static swerve::Swerve* swerve;
172
173 static Loggable loggable;
174};
175} // namespace valor
Definition PIDController.h:12
TrajectoryFollower()=delete
The default constructor where everything is initialized to zero.
static void Register(swerve::Swerve &s)
Defines the essential functions for the trajectory follower.
Definition TrajectoryFollower.h:83
TrajectoryEndCondition
Enum to represent the how the trajectory determines that it is finished: time or position.
Definition TrajectoryFollower.h:68
static void SetPID(WorldAlign::TranslationalPIDF_t x, WorldAlign::TranslationalPIDF_t y, WorldAlign::RotationalPIDF_t rot)
Sets a new PIDF config.
static frc2::CommandPtr FollowPath(std::optional< choreo::Trajectory< choreo::SwerveSample > > traj, TrajectoryEndCondition end, bool isMirrored, bool resetOdom, units::meter_t tolerance=0.25_m, units::second_t timeWithoutCorrection=0_s, std::function< bool()> interruption=[] { return false;})
Follows a choreo trajectory.
Definition Swerve.h:45
A struct to represent the acceleration swerve state.
Definition TrajectoryFollower.h:37
A struct to represent a swerve state in auto.
Definition TrajectoryFollower.h:46