Valkyrie 2026
Loading...
Searching...
No Matches
WorldAlign.h
1#pragma once
2#include <utility>
3
4#include <frc/geometry/Pose2d.h>
5#include <frc/geometry/Transform2d.h>
6#include <frc/kinematics/ChassisSpeeds.h>
7#include <frc/trajectory/TrapezoidProfile.h>
8#include <networktables/NetworkTableInstance.h>
9#include <networktables/StructTopic.h>
10#include <units/angle.h>
11#include <units/angular_velocity.h>
12#include <units/base.h>
13#include <units/length.h>
14#include <units/time.h>
15#include <units/velocity.h>
16#include <valkyrie/Loggable.h>
17#include <valkyrie/controllers/PIDF.h>
18#include <valkyrie/controllers/ProfiledPIDController.h>
19#include <valkyrie/drivetrain/swerve/Swerve.h>
20
21namespace valor {
22
41 public:
44
55
57
64 void SetPID(TranslationalPIDF_t x, TranslationalPIDF_t y, RotationalPIDF_t rot);
65
70 inline TranslationalPIDF_t GetXPID() const { return xController.GetPID(); }
71
76 inline TranslationalPIDF_t GetYPID() const { return yController.GetPID(); }
77
82 inline RotationalPIDF_t GetRotationalPID() const { return rotationalController.GetPID(); }
83
90 void ResetControllers(frc::Pose2d, frc::ChassisSpeeds);
91
97 void SetGoal(frc::Pose2d goal, frc::ChassisSpeeds state);
98
99 void SetPositionTolerance(units::meter_t xTol, units::meter_t yTol, units::radian_t rotTol);
100
101 void SetVelocityTolerance(units::meters_per_second_t xTol, units::meters_per_second_t yTol, units::radians_per_second_t rotTol);
102
107 std::pair<frc::Pose2d, frc::ChassisSpeeds> GetGoal() const;
108
109 std::pair<frc::Pose2d, frc::ChassisSpeeds> GetSetpoint() const;
110
118 frc::ChassisSpeeds Calculate(frc::Pose2d pose, frc::ChassisSpeeds currentSpeeds);
119
120 constexpr bool AtGoal() const { return xController.AtGoal() && yController.AtGoal() && rotationalController.AtGoal(); }
121
122 constexpr bool AtSetpoint() const { return xController.AtSetpoint() && yController.AtSetpoint() && rotationalController.AtSetpoint(); }
123
124 inline ProfiledPIDController<units::meter, units::meters_per_second>& GetXController() { return xController; }
125
126 inline ProfiledPIDController<units::meter, units::meters_per_second>& GetYController() { return yController; }
127
128 inline ProfiledPIDController<units::radian, units::radians_per_second>& GetRotationalController() { return rotationalController; }
129
130 void LoggablePeriodic() override;
131
132 private:
134 ProfiledPIDController<units::radian, units::radians_per_second> rotationalController{{}};
135
136 frc::Pose2d currPosition;
137 frc::ChassisSpeeds currSpeed;
138};
139
140class WorldAlignCommand : public WorldAlign, public frc2::CommandHelper<frc2::Command, WorldAlignCommand> {
141 public:
142 WorldAlignCommand(swerve::Swerve& swerve);
143
144 void Initialize() override;
145
146 void Execute() override;
147
148 bool IsFinished() override;
149
150 constexpr void EnableRotation(bool e) { rotationEnabled = e; }
151
152 private:
153 swerve::Swerve& swerve;
154 bool rotationEnabled = true;
155};
156
157} // namespace valor
Base helper for publishing and subscribing values to NetworkTables.
Definition Loggable.h:53
Definition ProfiledPIDController.h:12
constexpr bool AtSetpoint() const
Definition ProfiledPIDController.h:132
void SetGoal(frc::Pose2d goal, frc::ChassisSpeeds state)
Sets the desired goal state for the alignment controllers.
TranslationalPIDF_t GetXPID() const
Gets the PID gains for the X-axis controller.
Definition WorldAlign.h:70
TranslationalPIDF_t GetYPID() const
Gets the PID gains for the Y-axis controller.
Definition WorldAlign.h:76
void ResetControllers(frc::Pose2d, frc::ChassisSpeeds)
Resets all controllers with the current robot state. This synchronizes the controller's internal stat...
void LoggablePeriodic() override
Periodic callback for logging updates.
std::pair< frc::Pose2d, frc::ChassisSpeeds > GetGoal() const
Gets the current goal.
frc::ChassisSpeeds Calculate(frc::Pose2d pose, frc::ChassisSpeeds currentSpeeds)
Calculates the required chassis speeds to reach the last set goal. This is an overload that uses the ...
RotationalPIDF_t GetRotationalPID() const
Gets the PID gains for the rotation controller.
Definition WorldAlign.h:82
WorldAlign()
Constructs a WorldAlign object.
void SetPID(TranslationalPIDF_t x, TranslationalPIDF_t y, RotationalPIDF_t rot)
A convenience method to set the PID gains for all three controllers at once.
Definition Swerve.h:45
Container to hold PID and feed forward values for the motor controller.
Definition PIDF.h:26