Valkyrie 2026
Loading...
Searching...
No Matches
PIDController.h
1#pragma once
2#include <frc/controller/PIDController.h>
3
4#include "valkyrie/Robot.h"
5#include "valkyrie/controllers/PIDF.h"
6
7namespace valor {
8
12template <typename INPUT, typename OUTPUT>
13class PIDController : public frc::PIDController {
14 public:
15 using ErrorDerivative = units::unit_t<units::compound_unit<INPUT, units::inverse<units::second>>>;
16
24 constexpr PIDController(const PIDF<INPUT, OUTPUT>& pidf, units::second_t period = Robot::kDefaultPeriod)
25 : frc::PIDController{pidf.P.value(), pidf.I.value(), pidf.D.value(), period}, pidf{pidf} {
27 }
28
34 constexpr void SetPID(const PIDF<INPUT, OUTPUT>& pidf) {
35 this->pidf = pidf;
36 frc::PIDController::SetPID(pidf.P.value(), pidf.I.value(), pidf.D.value());
38 }
39
45 constexpr void SetP(PIDF<INPUT, OUTPUT>::ProportionalGain_t Kp) {
46 pidf.P = Kp;
47 frc::PIDController::SetP(Kp.value());
48 }
49
55 constexpr void SetI(PIDF<INPUT, OUTPUT>::IntegralGain_t Ki) {
56 pidf.I = Ki;
57 frc::PIDController::SetI(Ki.value());
58 }
59
65 constexpr void SetD(PIDF<INPUT, OUTPUT>::DerivativeGain_t Kd) {
66 pidf.D = Kd;
67 frc::PIDController::SetD(Kd.value());
68 }
69
81 constexpr void SetIZone(PIDF<INPUT, OUTPUT>::Output_t iZone) { frc::PIDController::SetIZone(iZone.value()); }
82
88 constexpr PIDF<INPUT, OUTPUT>::ProportionalGain_t GetP() const {
89 return typename PIDF<INPUT, OUTPUT>::ProportionalGain_t{frc::PIDController::GetP()};
90 }
91
97 constexpr PIDF<INPUT, OUTPUT>::IntegralGain_t GetI() const {
98 return typename PIDF<INPUT, OUTPUT>::IntegralGain_t{frc::PIDController::GetI()};
99 }
100
106 constexpr PIDF<INPUT, OUTPUT>::DerivativeGain_t GetD() const {
107 return typename PIDF<INPUT, OUTPUT>::DerivativeGain_t{frc::PIDController::GetD()};
108 }
109
115 constexpr PIDF<INPUT, OUTPUT>::Output_t GetIZone() const {
116 return typename PIDF<INPUT, OUTPUT>::Output_t{frc::PIDController::GetIZone()};
117 }
118
124 constexpr PIDF<INPUT, OUTPUT>::Input_t GetErrorTolerance() const {
125 return typename PIDF<INPUT, OUTPUT>::Input_t{frc::PIDController::GetErrorTolerance()};
126 }
127
133 constexpr ErrorDerivative GetErrorDerivativeTolerance() const {
134 return ErrorDerivative{frc::PIDController::GetErrorDerivativeTolerance()};
135 }
136
143 constexpr PIDF<INPUT, OUTPUT>::Input_t GetAccumulatedError() const {
144 return typename PIDF<INPUT, OUTPUT>::Input_t{frc::PIDController::GetAccumulatedError()};
145 }
146
152 constexpr void SetSetpoint(PIDF<INPUT, OUTPUT>::Input_t setpoint) { frc::PIDController::SetSetpoint(setpoint.value()); }
153
159 constexpr PIDF<INPUT, OUTPUT>::Input_t GetSetpoint() const {
160 return typename PIDF<INPUT, OUTPUT>::Input_t{frc::PIDController::GetSetpoint()};
161 }
162
173 constexpr void EnableContinuousInput(PIDF<INPUT, OUTPUT>::Input_t minimumInput, PIDF<INPUT, OUTPUT>::Input_t maximumInput) {
174 frc::PIDController::EnableContinuousInput(minimumInput.value(), maximumInput.value());
175 }
176
187 constexpr void SetIntegratorRange(PIDF<INPUT, OUTPUT>::Output_t minimumIntegral, PIDF<INPUT, OUTPUT>::Output_t maximumIntegral) {
188 frc::PIDController::SetIntegratorRange(minimumIntegral.value(), maximumIntegral.value());
189 }
190
197 constexpr void SetTolerance(PIDF<INPUT, OUTPUT>::Input_t errorTolerance,
198 ErrorDerivative errorDerivativeTolerance = ErrorDerivative{std::numeric_limits<double>::infinity()}) {
199 pidf.error = errorTolerance;
200 frc::PIDController::SetTolerance(errorTolerance.value(), errorDerivativeTolerance.value());
201 }
202
206 constexpr PIDF<INPUT, OUTPUT>::Input_t GetError() const {
207 return typename PIDF<INPUT, OUTPUT>::Input_t{frc::PIDController::GetError()};
208 }
209
213 constexpr ErrorDerivative GetErrorDerivative() const { return ErrorDerivative{frc::PIDController::GetErrorDerivative()}; }
214
220 constexpr const valor::PIDF<INPUT, OUTPUT>& GetPID() const { return pidf; }
221
227 constexpr PIDF<INPUT, OUTPUT>::Output_t Calculate(PIDF<INPUT, OUTPUT>::Input_t measurement) {
228 typename PIDF<INPUT, OUTPUT>::Output_t output{frc::PIDController::Calculate(measurement.value())};
229 if constexpr (std::is_same_v<decltype(measurement), typename PIDF<INPUT, OUTPUT>::Velocity_t>)
230 if (pidf.kV)
231 output += *pidf.kV * measurement;
232 output += pidf.kS;
233 if (pidf.GType == GravityFFType::LINEAR)
234 output += pidf.kG;
235 else if constexpr (std::is_convertible_v<decltype(measurement), units::turn_t>)
236 if (pidf.GType == GravityFFType::CIRCULAR)
237 output += pidf.kG * units::math::cos(measurement + pidf.GTarget);
238 return output;
239 }
240
247 constexpr PIDF<INPUT, OUTPUT>::Output_t Calculate(PIDF<INPUT, OUTPUT>::Input_t measurement, PIDF<INPUT, OUTPUT>::Input_t setpoint) {
248 SetSetpoint(setpoint);
249 return Calculate(measurement);
250 }
251
252 private:
254};
255
256} // namespace valor
constexpr PIDF< INPUT, OUTPUT >::Input_t GetErrorTolerance() const
Definition PIDController.h:124
constexpr void SetD(PIDF< INPUT, OUTPUT >::DerivativeGain_t Kd)
Definition PIDController.h:65
constexpr void SetP(PIDF< INPUT, OUTPUT >::ProportionalGain_t Kp)
Definition PIDController.h:45
constexpr PIDF< INPUT, OUTPUT >::IntegralGain_t GetI() const
Definition PIDController.h:97
constexpr void SetIZone(PIDF< INPUT, OUTPUT >::Output_t iZone)
Definition PIDController.h:81
constexpr PIDF< INPUT, OUTPUT >::ProportionalGain_t GetP() const
Definition PIDController.h:88
constexpr void SetIntegratorRange(PIDF< INPUT, OUTPUT >::Output_t minimumIntegral, PIDF< INPUT, OUTPUT >::Output_t maximumIntegral)
Definition PIDController.h:187
constexpr void EnableContinuousInput(PIDF< INPUT, OUTPUT >::Input_t minimumInput, PIDF< INPUT, OUTPUT >::Input_t maximumInput)
Definition PIDController.h:173
constexpr PIDF< INPUT, OUTPUT >::Output_t Calculate(PIDF< INPUT, OUTPUT >::Input_t measurement)
Definition PIDController.h:227
constexpr PIDF< INPUT, OUTPUT >::Input_t GetAccumulatedError() const
Definition PIDController.h:143
constexpr PIDF< INPUT, OUTPUT >::Input_t GetError() const
Definition PIDController.h:206
constexpr PIDF< INPUT, OUTPUT >::Output_t GetIZone() const
Definition PIDController.h:115
constexpr ErrorDerivative GetErrorDerivativeTolerance() const
Definition PIDController.h:133
constexpr void SetTolerance(PIDF< INPUT, OUTPUT >::Input_t errorTolerance, ErrorDerivative errorDerivativeTolerance=ErrorDerivative{std::numeric_limits< double >::infinity()})
Definition PIDController.h:197
constexpr ErrorDerivative GetErrorDerivative() const
Definition PIDController.h:213
constexpr PIDF< INPUT, OUTPUT >::DerivativeGain_t GetD() const
Definition PIDController.h:106
constexpr void SetI(PIDF< INPUT, OUTPUT >::IntegralGain_t Ki)
Definition PIDController.h:55
constexpr void SetSetpoint(PIDF< INPUT, OUTPUT >::Input_t setpoint)
Definition PIDController.h:152
constexpr PIDF< INPUT, OUTPUT >::Input_t GetSetpoint() const
Definition PIDController.h:159
constexpr PIDF< INPUT, OUTPUT >::Output_t Calculate(PIDF< INPUT, OUTPUT >::Input_t measurement, PIDF< INPUT, OUTPUT >::Input_t setpoint)
Definition PIDController.h:247
constexpr PIDController(const PIDF< INPUT, OUTPUT > &pidf, units::second_t period=Robot::kDefaultPeriod)
Definition PIDController.h:24
constexpr const valor::PIDF< INPUT, OUTPUT > & GetPID() const
Definition PIDController.h:220
constexpr void SetPID(const PIDF< INPUT, OUTPUT > &pidf)
Definition PIDController.h:34
Container to hold PID and feed forward values for the motor controller.
Definition PIDF.h:26