3#include <frc/controller/PIDController.h>
4#include <frc/smartdashboard/SmartDashboard.h>
5#include <rev/CANSparkBase.h>
6#include <rev/CANSparkMax.h>
7#include <units/angle.h>
8#include <units/angular_velocity.h>
26template <
typename TMotor,
typename TController,
typename TRelativeEncoder,
27 typename TAbsoluteEncoder>
43 TRelativeEncoder &encoder,
45 TAbsoluteEncoder *absEncoder,
46 units::revolutions_per_minute_t maxRpm);
53 void Set(
double percentage)
override;
60 void Set(units::volt_t volts)
override;
114 m_encoder.SetPositionConversionFactor(factor);
161template <
typename TMotor,
typename TController,
typename TRelativeEncoder,
162 typename TAbsoluteEncoder>
167 TAbsoluteEncoder> &controller)
168 : m_controller{controller} {
169 frc::SmartDashboard::PutNumber(m_controller.m_name +
" P Gain",
170 m_controller.GetPidSettings().p);
171 frc::SmartDashboard::PutNumber(m_controller.m_name +
" I Gain",
172 m_controller.GetPidSettings().i);
173 frc::SmartDashboard::PutNumber(m_controller.m_name +
" D Gain",
174 m_controller.GetPidSettings().d);
175 frc::SmartDashboard::PutNumber(m_controller.m_name +
" IZone",
176 m_controller.GetPidSettings().iZone);
177 frc::SmartDashboard::PutNumber(m_controller.m_name +
" Feed Forward",
178 m_controller.GetPidSettings().ff);
186 double tP = frc::SmartDashboard::GetNumber(m_controller.m_name +
" P Gain",
187 m_controller.GetPidSettings().p);
188 double tI = frc::SmartDashboard::GetNumber(m_controller.m_name +
" I Gain",
189 m_controller.GetPidSettings().i);
190 double tD = frc::SmartDashboard::GetNumber(m_controller.m_name +
" D Gain",
191 m_controller.GetPidSettings().d);
192 double tIZone = frc::SmartDashboard::GetNumber(
193 m_controller.m_name +
" IZone", m_controller.GetPidSettings().iZone);
194 double tFeedForward =
195 frc::SmartDashboard::GetNumber(m_controller.m_name +
" Feed Forward",
196 m_controller.GetPidSettings().ff);
198 m_controller.UpdatePidSettings(
199 {.p = tP, .i = tI, .d = tD, .iZone = tIZone, .ff = tFeedForward});
209 rev::SparkRelativeEncoder,
210 rev::SparkAbsoluteEncoder> {};
Intended for use alongside a PidMotorController for simple tuning through SmartDashboard.
void UpdateFromShuffleboard()
Call this within the Periodic method of the encapsulating subsystem.
PidMotorControllerTuner(PidMotorController< TMotor, TController, TRelativeEncoder, TAbsoluteEncoder > &controller)
Combines a motor, motor drvier, relative encoder, and absolute encoder into a single wrapper; helpful...
void Set(units::volt_t volts) override
Set a motor to a voltage.
void Stop() override
Disable absolute positioning and stop the motor.
void SetEncoderConversionFactor(double factor) override
Sets the multiplier for going between encoder ticks and actual distance.
void RunWithVelocity(double percentage) override
Set to a percentage of max RPM.
bool m_absolutePositionEnabled
void Set(double percentage) override
Set the motor to a percentage of max voltage.
PidMotorController(std::string name, TMotor &motor, TRelativeEncoder &encoder, TController &controller, PidSettings pidSettings, TAbsoluteEncoder *absEncoder, units::revolutions_per_minute_t maxRpm)
Construct a new PidMotorController.
const units::revolutions_per_minute_t m_maxRpm
TController & m_controller
TRelativeEncoder & m_encoder
void RunWithVelocity(units::revolutions_per_minute_t rpm) override
Set to this velocity.
TAbsoluteEncoder * m_absEncoder
void Update() override
! Call this every loop in Periodic !
frc::PIDController m_pidController
void RunToPosition(double position) override
Enables absolute positioning and sets the target to the position.
void SetAbsoluteEncoderConversionFactor(double factor) override
Sets the multiplier for going between encoder ticks and actual distance.
virtual void ResetEncoder() override
void UpdatePidSettings(PidSettings settings) override
const PidSettings & GetPidSettings() override
double GetEncoderPosition() override
void SetPidTolerance(double tolerance) override
Absolute positioning is considered 'Done' when within this zone.
std::optional< double > GetAbsoluteEncoderPosition() override