Valkyrie 2026
Loading...
Searching...
No Matches
ProfiledPIDController.h
1#pragma once
2#include <frc/controller/ProfiledPIDController.h>
3#include <valkyrie/controllers/PIDController.h>
4
5namespace valor {
6
11template <typename INPUT, typename OUTPUT>
13 public:
14 using ErrorDerivative = units::unit_t<units::compound_unit<INPUT, units::inverse<units::second>>>;
15
26 constexpr ProfiledPIDController(const PIDF<INPUT, OUTPUT>& pidf, units::second_t period = 20_ms)
27 : controller{pidf}, profile{{pidf.maxVelocity, pidf.maxAcceleration}} {}
28
36 constexpr void SetPID(const PIDF<INPUT, OUTPUT>& pidf) {
37 controller.SetPID(pidf);
38 profile = frc::TrapezoidProfile<INPUT>({pidf.maxVelocity, pidf.maxAcceleration});
39 }
40
52 constexpr void SetIZone(PIDF<INPUT, OUTPUT>::Output_t iZone) { controller.SetIZone(iZone); }
53
59 PIDF<INPUT, OUTPUT> GetPID() const { return controller.GetPID(); }
60
66 constexpr PIDF<INPUT, OUTPUT>::Output_t GetIZone() const { return controller.GetIZone(); }
67
73 constexpr units::second_t GetPeriod() const { return controller.GetPeriod(); }
74
80 constexpr PIDF<INPUT, OUTPUT>::Input_t GetPositionTolerance() const { return controller.GetErrorTolerance(); }
81
87 constexpr ErrorDerivative GetVelocityTolerance() const { return controller.GetErrorDerivativeTolerance(); }
88
95 constexpr PIDF<INPUT, OUTPUT>::Input_t GetAccumulatedError() const { return controller.GetAccumulatedError(); }
96
102 constexpr void SetGoal(frc::TrapezoidProfile<INPUT>::State goal) { this->goal = goal; }
103
107 constexpr frc::TrapezoidProfile<INPUT>::State GetGoal() const { return goal; }
108
114 constexpr bool AtGoal() const { return AtSetpoint() && goal == setpoint; }
115
121 constexpr frc::TrapezoidProfile<INPUT>::State GetSetpoint() const { return setpoint; }
122
132 constexpr bool AtSetpoint() const { return controller.AtSetpoint(); }
133
144 constexpr void EnableContinuousInput(PIDF<INPUT, OUTPUT>::Input_t minimumInput, PIDF<INPUT, OUTPUT>::Input_t maximumInput) {
145 controller.EnableContinuousInput(minimumInput, maximumInput);
146 this->minimumInput = minimumInput;
147 this->maximumInput = maximumInput;
148 }
149
153 constexpr void DisableContinuousInput() { controller.DisableContinuousInput(); }
154
165 constexpr void SetIntegratorRange(PIDF<INPUT, OUTPUT>::Output_t minimumIntegral, PIDF<INPUT, OUTPUT>::Output_t maximumIntegral) {
166 controller.SetIntegratorRange(minimumIntegral, maximumIntegral);
167 }
168
176 constexpr void SetTolerance(PIDF<INPUT, OUTPUT>::Input_t positionTolerance,
177 ErrorDerivative velocityTolerance = ErrorDerivative{std::numeric_limits<double>::infinity()}) {
178 controller.SetTolerance(positionTolerance, velocityTolerance);
179 }
180
186 constexpr PIDF<INPUT, OUTPUT>::Input_t GetPositionError() const { return controller.GetError(); }
187
191 constexpr ErrorDerivative GetVelocityError() const { return controller.GetErrorDerivative(); }
192
198 constexpr PIDF<INPUT, OUTPUT>::Output_t Calculate(PIDF<INPUT, OUTPUT>::Input_t measurement) {
199 if (controller.IsContinuousInputEnabled()) {
200 auto errorBound = (maximumInput - minimumInput) / 2.0;
201 auto goalMinDistance = frc::InputModulus(goal.position - measurement, -errorBound, errorBound);
202 auto setpointMinDistance = frc::InputModulus(setpoint.position - measurement, -errorBound, errorBound);
203 goal.position = goalMinDistance + measurement;
204 setpoint.position = setpointMinDistance + measurement;
205 }
206
207 setpoint = profile.Calculate(GetPeriod(), setpoint, goal);
208 controller.SetSetpoint(setpoint.position);
209 auto output = controller.Calculate(measurement);
210 if constexpr (std::is_same_v<decltype(setpoint.velocity), typename PIDF<INPUT, OUTPUT>::Velocity_t>)
211 if (controller.GetPID().kV)
212 output += *controller.GetPID().kV * setpoint.velocity;
213 return output;
214 }
215
221 constexpr void Reset(const frc::TrapezoidProfile<INPUT>::State& measurement) {
222 controller.Reset();
223 setpoint = measurement;
224 }
225
226 private:
228 frc::TrapezoidProfile<INPUT>::State goal;
229 frc::TrapezoidProfile<INPUT>::State setpoint;
230 frc::TrapezoidProfile<INPUT> profile;
231
232 PIDF<INPUT, OUTPUT>::Input_t minimumInput;
233 PIDF<INPUT, OUTPUT>::Input_t maximumInput;
234};
235
236} // namespace valor
Definition PIDController.h:12
constexpr frc::TrapezoidProfile< INPUT >::State GetSetpoint() const
Definition ProfiledPIDController.h:121
constexpr PIDF< INPUT, OUTPUT >::Output_t Calculate(PIDF< INPUT, OUTPUT >::Input_t measurement)
Definition ProfiledPIDController.h:198
constexpr void SetTolerance(PIDF< INPUT, OUTPUT >::Input_t positionTolerance, ErrorDerivative velocityTolerance=ErrorDerivative{std::numeric_limits< double >::infinity()})
Definition ProfiledPIDController.h:176
constexpr ErrorDerivative GetVelocityTolerance() const
Definition ProfiledPIDController.h:87
constexpr void EnableContinuousInput(PIDF< INPUT, OUTPUT >::Input_t minimumInput, PIDF< INPUT, OUTPUT >::Input_t maximumInput)
Definition ProfiledPIDController.h:144
constexpr void DisableContinuousInput()
Definition ProfiledPIDController.h:153
constexpr units::second_t GetPeriod() const
Definition ProfiledPIDController.h:73
constexpr frc::TrapezoidProfile< INPUT >::State GetGoal() const
Definition ProfiledPIDController.h:107
constexpr PIDF< INPUT, OUTPUT >::Output_t GetIZone() const
Definition ProfiledPIDController.h:66
PIDF< INPUT, OUTPUT > GetPID() const
Definition ProfiledPIDController.h:59
constexpr void SetPID(const PIDF< INPUT, OUTPUT > &pidf)
Definition ProfiledPIDController.h:36
constexpr bool AtGoal() const
Definition ProfiledPIDController.h:114
constexpr PIDF< INPUT, OUTPUT >::Input_t GetPositionError() const
Definition ProfiledPIDController.h:186
constexpr ErrorDerivative GetVelocityError() const
Definition ProfiledPIDController.h:191
constexpr ProfiledPIDController(const PIDF< INPUT, OUTPUT > &pidf, units::second_t period=20_ms)
Definition ProfiledPIDController.h:26
constexpr PIDF< INPUT, OUTPUT >::Input_t GetAccumulatedError() const
Definition ProfiledPIDController.h:95
constexpr void SetIntegratorRange(PIDF< INPUT, OUTPUT >::Output_t minimumIntegral, PIDF< INPUT, OUTPUT >::Output_t maximumIntegral)
Definition ProfiledPIDController.h:165
constexpr void SetGoal(frc::TrapezoidProfile< INPUT >::State goal)
Definition ProfiledPIDController.h:102
constexpr PIDF< INPUT, OUTPUT >::Input_t GetPositionTolerance() const
Definition ProfiledPIDController.h:80
constexpr void Reset(const frc::TrapezoidProfile< INPUT >::State &measurement)
Definition ProfiledPIDController.h:221
constexpr void SetIZone(PIDF< INPUT, OUTPUT >::Output_t iZone)
Definition ProfiledPIDController.h:52
constexpr bool AtSetpoint() const
Definition ProfiledPIDController.h:132
Container to hold PID and feed forward values for the motor controller.
Definition PIDF.h:26