2#include <frc/controller/PIDController.h>
4#include "valkyrie/Robot.h"
5#include "valkyrie/controllers/PIDF.h"
12template <
typename INPUT,
typename OUTPUT>
15 using ErrorDerivative = units::unit_t<units::compound_unit<INPUT, units::inverse<units::second>>>;
25 : frc::
PIDController{pidf.P.value(), pidf.I.value(), pidf.D.value(), period}, pidf{pidf} {
36 frc::PIDController::SetPID(pidf.P.value(), pidf.I.value(), pidf.D.value());
45 constexpr void SetP(PIDF<INPUT, OUTPUT>::ProportionalGain_t Kp) {
47 frc::PIDController::SetP(Kp.value());
55 constexpr void SetI(PIDF<INPUT, OUTPUT>::IntegralGain_t Ki) {
57 frc::PIDController::SetI(Ki.value());
65 constexpr void SetD(PIDF<INPUT, OUTPUT>::DerivativeGain_t Kd) {
67 frc::PIDController::SetD(Kd.value());
81 constexpr void SetIZone(PIDF<INPUT, OUTPUT>::Output_t iZone) { frc::PIDController::SetIZone(iZone.value()); }
88 constexpr PIDF<INPUT, OUTPUT>::ProportionalGain_t
GetP()
const {
89 return typename PIDF<INPUT, OUTPUT>::ProportionalGain_t{frc::PIDController::GetP()};
97 constexpr PIDF<INPUT, OUTPUT>::IntegralGain_t
GetI()
const {
98 return typename PIDF<INPUT, OUTPUT>::IntegralGain_t{frc::PIDController::GetI()};
106 constexpr PIDF<INPUT, OUTPUT>::DerivativeGain_t
GetD()
const {
107 return typename PIDF<INPUT, OUTPUT>::DerivativeGain_t{frc::PIDController::GetD()};
115 constexpr PIDF<INPUT, OUTPUT>::Output_t
GetIZone()
const {
116 return typename PIDF<INPUT, OUTPUT>::Output_t{frc::PIDController::GetIZone()};
125 return typename PIDF<INPUT, OUTPUT>::Input_t{frc::PIDController::GetErrorTolerance()};
134 return ErrorDerivative{frc::PIDController::GetErrorDerivativeTolerance()};
144 return typename PIDF<INPUT, OUTPUT>::Input_t{frc::PIDController::GetAccumulatedError()};
152 constexpr void SetSetpoint(PIDF<INPUT, OUTPUT>::Input_t setpoint) { frc::PIDController::SetSetpoint(setpoint.value()); }
160 return typename PIDF<INPUT, OUTPUT>::Input_t{frc::PIDController::GetSetpoint()};
173 constexpr void EnableContinuousInput(PIDF<INPUT, OUTPUT>::Input_t minimumInput, PIDF<INPUT, OUTPUT>::Input_t maximumInput) {
174 frc::PIDController::EnableContinuousInput(minimumInput.value(), maximumInput.value());
187 constexpr void SetIntegratorRange(PIDF<INPUT, OUTPUT>::Output_t minimumIntegral, PIDF<INPUT, OUTPUT>::Output_t maximumIntegral) {
188 frc::PIDController::SetIntegratorRange(minimumIntegral.value(), maximumIntegral.value());
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());
206 constexpr PIDF<INPUT, OUTPUT>::Input_t
GetError()
const {
207 return typename PIDF<INPUT, OUTPUT>::Input_t{frc::PIDController::GetError()};
213 constexpr ErrorDerivative
GetErrorDerivative()
const {
return ErrorDerivative{frc::PIDController::GetErrorDerivative()}; }
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>)
231 output += *pidf.kV * measurement;
233 if (pidf.GType == GravityFFType::LINEAR)
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);
247 constexpr PIDF<INPUT, OUTPUT>::Output_t
Calculate(PIDF<INPUT, OUTPUT>::Input_t measurement, PIDF<INPUT, OUTPUT>::Input_t setpoint) {
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