2#include <frc/controller/ProfiledPIDController.h>
3#include <valkyrie/controllers/PIDController.h>
11template <
typename INPUT,
typename OUTPUT>
14 using ErrorDerivative = units::unit_t<units::compound_unit<INPUT, units::inverse<units::second>>>;
27 : controller{pidf}, profile{{pidf.maxVelocity, pidf.maxAcceleration}} {}
37 controller.SetPID(pidf);
38 profile = frc::TrapezoidProfile<INPUT>({pidf.maxVelocity, pidf.maxAcceleration});
52 constexpr void SetIZone(PIDF<INPUT, OUTPUT>::Output_t iZone) { controller.SetIZone(iZone); }
66 constexpr PIDF<INPUT, OUTPUT>::Output_t
GetIZone()
const {
return controller.GetIZone(); }
73 constexpr units::second_t
GetPeriod()
const {
return controller.GetPeriod(); }
80 constexpr PIDF<INPUT, OUTPUT>::Input_t
GetPositionTolerance()
const {
return controller.GetErrorTolerance(); }
87 constexpr ErrorDerivative
GetVelocityTolerance()
const {
return controller.GetErrorDerivativeTolerance(); }
95 constexpr PIDF<INPUT, OUTPUT>::Input_t
GetAccumulatedError()
const {
return controller.GetAccumulatedError(); }
102 constexpr void SetGoal(frc::TrapezoidProfile<INPUT>::State goal) { this->goal = goal; }
107 constexpr frc::TrapezoidProfile<INPUT>::State
GetGoal()
const {
return goal; }
121 constexpr frc::TrapezoidProfile<INPUT>::State
GetSetpoint()
const {
return setpoint; }
132 constexpr bool AtSetpoint()
const {
return controller.AtSetpoint(); }
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;
165 constexpr void SetIntegratorRange(PIDF<INPUT, OUTPUT>::Output_t minimumIntegral, PIDF<INPUT, OUTPUT>::Output_t maximumIntegral) {
166 controller.SetIntegratorRange(minimumIntegral, maximumIntegral);
176 constexpr void SetTolerance(PIDF<INPUT, OUTPUT>::Input_t positionTolerance,
177 ErrorDerivative velocityTolerance = ErrorDerivative{std::numeric_limits<double>::infinity()}) {
178 controller.SetTolerance(positionTolerance, velocityTolerance);
186 constexpr PIDF<INPUT, OUTPUT>::Input_t
GetPositionError()
const {
return controller.GetError(); }
191 constexpr ErrorDerivative
GetVelocityError()
const {
return controller.GetErrorDerivative(); }
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;
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;
221 constexpr void Reset(
const frc::TrapezoidProfile<INPUT>::State& measurement) {
223 setpoint = measurement;
228 frc::TrapezoidProfile<INPUT>::State goal;
229 frc::TrapezoidProfile<INPUT>::State setpoint;
230 frc::TrapezoidProfile<INPUT> profile;
232 PIDF<INPUT, OUTPUT>::Input_t minimumInput;
233 PIDF<INPUT, OUTPUT>::Input_t maximumInput;
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