SubZero Common
Common library components for an FRC CommandRobot
Loading...
Searching...
No Matches
PidMotorController.h
Go to the documentation of this file.
1#pragma once
2
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>
9
10#include <string>
11
14
15namespace subzero {
26template <typename TMotor, typename TController, typename TRelativeEncoder,
27 typename TAbsoluteEncoder>
29public:
42 explicit PidMotorController(std::string name, TMotor &motor,
43 TRelativeEncoder &encoder,
44 TController &controller, PidSettings pidSettings,
45 TAbsoluteEncoder *absEncoder,
46 units::revolutions_per_minute_t maxRpm);
47
53 void Set(double percentage) override;
54
60 void Set(units::volt_t volts) override;
61
67 void SetPidTolerance(double tolerance) override;
68
73 void Update() override;
74
80 void RunWithVelocity(units::revolutions_per_minute_t rpm) override;
81
87 void RunWithVelocity(double percentage) override;
88
94 void RunToPosition(double position) override;
95
96 inline virtual void ResetEncoder() override {
97 m_encoder.SetPosition(0);
98 ConsoleWriter.logInfo(m_name + " PID Controller", "Reset encoder%s", "");
99 }
100
101 inline double GetEncoderPosition() override {
102 return m_encoder.GetPosition();
103 }
104
105 std::optional<double> GetAbsoluteEncoderPosition() override;
106
113 inline void SetEncoderConversionFactor(double factor) override {
114 m_encoder.SetPositionConversionFactor(factor);
115 }
116
123 inline void SetAbsoluteEncoderConversionFactor(double factor) override {
124 if (m_absEncoder) {
125 m_absEncoder->SetPositionConversionFactor(factor);
126 }
127 }
128
133 void Stop() override;
134
135 inline const PidSettings &GetPidSettings() override { return m_settings; }
136
137 void UpdatePidSettings(PidSettings settings) override;
138
139protected:
140 TMotor &m_motor;
141 TController &m_controller;
142 TRelativeEncoder &m_encoder;
143 TAbsoluteEncoder *m_absEncoder;
145 frc::PIDController m_pidController;
148 const units::revolutions_per_minute_t m_maxRpm;
149};
150
151// TODO: Move to its own file and make it work with IPidMotorController
161template <typename TMotor, typename TController, typename TRelativeEncoder,
162 typename TAbsoluteEncoder>
164public:
166 PidMotorController<TMotor, TController, TRelativeEncoder,
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);
179 }
180
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);
197
198 m_controller.UpdatePidSettings(
199 {.p = tP, .i = tI, .d = tD, .iZone = tIZone, .ff = tFeedForward});
200 }
201
202private:
204 &m_controller;
205};
206
208 : public PidMotorController<rev::CANSparkMax, rev::SparkPIDController,
209 rev::SparkRelativeEncoder,
210 rev::SparkAbsoluteEncoder> {};
211} // namespace subzero
#define ConsoleWriter
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.
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
void RunWithVelocity(units::revolutions_per_minute_t rpm) override
Set to this velocity.
void Update() override
! Call this every loop in Periodic !
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
void SetPidTolerance(double tolerance) override
Absolute positioning is considered 'Done' when within this zone.
std::optional< double > GetAbsoluteEncoderPosition() override