Valkyrie 2026
Loading...
Searching...
No Matches
PIDController.h
1#pragma once
2#include <frc/controller/PIDController.h>
3#include <valkyrie/Robot.h>
4#include <valkyrie/controllers/PIDF.h>
5
6namespace valor {
7
11template <typename INPUT, typename OUTPUT>
12class PIDController : frc::PIDController {
13 public:
14 using ErrorDerivative = units::unit_t<units::compound_unit<INPUT, units::inverse<units::second>>>;
15
23 constexpr PIDController(const PIDF<INPUT, OUTPUT>& pidf, units::second_t period = 20_ms)
24 : frc::PIDController{pidf.P.value(), pidf.I.value(), pidf.D.value(), period}, pidf{pidf} {
26 }
27
33 constexpr void SetPID(const PIDF<INPUT, OUTPUT>& pidf) {
34 this->pidf = pidf;
35 frc::PIDController::SetPID(pidf.P.value(), pidf.I.value(), pidf.D.value());
37 }
38
50 constexpr void SetIZone(PIDF<INPUT, OUTPUT>::Output_t iZone) { frc::PIDController::SetIZone(iZone.value()); }
51
57 constexpr PIDF<INPUT, OUTPUT>::Output_t GetIZone() const {
58 return typename PIDF<INPUT, OUTPUT>::Output_t{frc::PIDController::GetIZone()};
59 }
60
66 constexpr PIDF<INPUT, OUTPUT>::Input_t GetErrorTolerance() const {
67 return typename PIDF<INPUT, OUTPUT>::Input_t{frc::PIDController::GetErrorTolerance()};
68 }
69
75 constexpr ErrorDerivative GetErrorDerivativeTolerance() const {
76 return ErrorDerivative{frc::PIDController::GetErrorDerivativeTolerance()};
77 }
78
85 constexpr PIDF<INPUT, OUTPUT>::Input_t GetAccumulatedError() const {
86 return typename PIDF<INPUT, OUTPUT>::Input_t{frc::PIDController::GetAccumulatedError()};
87 }
88
94 constexpr void SetSetpoint(PIDF<INPUT, OUTPUT>::Input_t setpoint) { frc::PIDController::SetSetpoint(setpoint.value()); }
95
101 constexpr PIDF<INPUT, OUTPUT>::Input_t GetSetpoint() const {
102 return typename PIDF<INPUT, OUTPUT>::Input_t{frc::PIDController::GetSetpoint()};
103 }
104
115 constexpr void EnableContinuousInput(PIDF<INPUT, OUTPUT>::Input_t minimumInput, PIDF<INPUT, OUTPUT>::Input_t maximumInput) {
116 frc::PIDController::EnableContinuousInput(minimumInput.value(), maximumInput.value());
117 }
118
129 constexpr void SetIntegratorRange(PIDF<INPUT, OUTPUT>::Output_t minimumIntegral, PIDF<INPUT, OUTPUT>::Output_t maximumIntegral) {
130 frc::PIDController::SetIntegratorRange(minimumIntegral.value(), maximumIntegral.value());
131 }
132
139 constexpr void SetTolerance(PIDF<INPUT, OUTPUT>::Input_t errorTolerance,
140 ErrorDerivative errorDerivativeTolerance = ErrorDerivative{std::numeric_limits<double>::infinity()}) {
141 pidf.error = errorTolerance;
142 frc::PIDController::SetTolerance(errorTolerance.value(), errorDerivativeTolerance.value());
143 }
144
148 constexpr PIDF<INPUT, OUTPUT>::Input_t GetError() const {
149 return typename PIDF<INPUT, OUTPUT>::Input_t{frc::PIDController::GetError()};
150 }
151
155 constexpr ErrorDerivative GetErrorDerivative() const { return ErrorDerivative{frc::PIDController::GetErrorDerivative()}; }
156
162 constexpr const valor::PIDF<INPUT, OUTPUT>& GetPID() const { return pidf; }
163
169 constexpr PIDF<INPUT, OUTPUT>::Output_t Calculate(PIDF<INPUT, OUTPUT>::Input_t measurement) {
170 typename PIDF<INPUT, OUTPUT>::Output_t output{frc::PIDController::Calculate(measurement.value())};
171 if constexpr (std::is_same_v<decltype(measurement), typename PIDF<INPUT, OUTPUT>::Velocity_t>)
172 if (pidf.kV)
173 output += *pidf.kV * GetSetpoint();
174 output += pidf.kS;
175 if (pidf.GType == GravityFFType::LINEAR)
176 output += pidf.kG;
177 else if constexpr (std::is_convertible_v<decltype(measurement), units::turn_t>)
178 if (pidf.GType == GravityFFType::CIRCULAR)
179 output += pidf.kG * units::math::cos(measurement + pidf.GTarget);
180 return output;
181 }
182
183 using frc::PIDController::AtSetpoint;
184 using frc::PIDController::DisableContinuousInput;
185 using frc::PIDController::GetPeriod;
186 using frc::PIDController::IsContinuousInputEnabled;
187 using frc::PIDController::Reset;
188
189 private:
191};
192
193} // namespace valor
constexpr PIDF< INPUT, OUTPUT >::Input_t GetErrorTolerance() const
Definition PIDController.h:66
constexpr void SetIZone(PIDF< INPUT, OUTPUT >::Output_t iZone)
Definition PIDController.h:50
constexpr PIDController(const PIDF< INPUT, OUTPUT > &pidf, units::second_t period=20_ms)
Definition PIDController.h:23
constexpr void SetIntegratorRange(PIDF< INPUT, OUTPUT >::Output_t minimumIntegral, PIDF< INPUT, OUTPUT >::Output_t maximumIntegral)
Definition PIDController.h:129
constexpr void EnableContinuousInput(PIDF< INPUT, OUTPUT >::Input_t minimumInput, PIDF< INPUT, OUTPUT >::Input_t maximumInput)
Definition PIDController.h:115
constexpr PIDF< INPUT, OUTPUT >::Output_t Calculate(PIDF< INPUT, OUTPUT >::Input_t measurement)
Definition PIDController.h:169
constexpr PIDF< INPUT, OUTPUT >::Input_t GetAccumulatedError() const
Definition PIDController.h:85
constexpr PIDF< INPUT, OUTPUT >::Input_t GetError() const
Definition PIDController.h:148
constexpr PIDF< INPUT, OUTPUT >::Output_t GetIZone() const
Definition PIDController.h:57
constexpr ErrorDerivative GetErrorDerivativeTolerance() const
Definition PIDController.h:75
constexpr void SetTolerance(PIDF< INPUT, OUTPUT >::Input_t errorTolerance, ErrorDerivative errorDerivativeTolerance=ErrorDerivative{std::numeric_limits< double >::infinity()})
Definition PIDController.h:139
constexpr ErrorDerivative GetErrorDerivative() const
Definition PIDController.h:155
constexpr void SetSetpoint(PIDF< INPUT, OUTPUT >::Input_t setpoint)
Definition PIDController.h:94
constexpr PIDF< INPUT, OUTPUT >::Input_t GetSetpoint() const
Definition PIDController.h:101
constexpr const valor::PIDF< INPUT, OUTPUT > & GetPID() const
Definition PIDController.h:162
constexpr void SetPID(const PIDF< INPUT, OUTPUT > &pidf)
Definition PIDController.h:33
Container to hold PID and feed forward values for the motor controller.
Definition PIDF.h:26