SubZero Common
Common library components for an FRC CommandRobot
Loading...
Searching...
No Matches
PidMotorControllerPair.h
Go to the documentation of this file.
1#pragma once
2
3#include <frc/smartdashboard/SmartDashboard.h>
4#include <rev/CANSparkBase.h>
5#include <units/angle.h>
6#include <units/angular_velocity.h>
7
8#include <string>
9
12
13namespace subzero {
14
23template <typename TMotor, typename TController, typename TRelativeEncoder,
24 typename TAbsoluteEncoder>
26public:
35 std::string prefix,
36 PidMotorController<TMotor, TController, TRelativeEncoder,
37 TAbsoluteEncoder> &first,
38 PidMotorController<TMotor, TController, TRelativeEncoder,
39 TAbsoluteEncoder> &second)
40 : m_shuffleboardPrefix{prefix}, m_controllerFirst{first},
41 m_controllerSecond{second} {}
42
49 void RunWithVelocity(units::revolutions_per_minute_t rpmFirst,
50 units::revolutions_per_minute_t rpmSecond) {
51 m_controllerFirst.RunWithVelocity(rpmFirst);
52 m_controllerSecond.RunWithVelocity(rpmSecond);
53 }
54
61 void RunWithVelocity(double percentageFirst, double percentageSecond) {
62 m_controllerFirst.RunWithVelocity(percentageFirst);
63 m_controllerSecond.RunWithVelocity(percentageSecond);
64 }
65
70 void Stop() {
71 m_controllerFirst.Stop();
72 m_controllerSecond.Stop();
73 }
74
75 const PidSettings &GetPidSettings() const { return m_pidSettings; }
76
78 m_pidSettings = settings;
79
80 m_controllerFirst.UpdatePidSettings(settings);
81 m_controllerSecond.UpdatePidSettings(settings);
82 }
83
84 const std::string m_shuffleboardPrefix;
85
86private:
88 &m_controllerFirst;
90 &m_controllerSecond;
91 PidSettings m_pidSettings;
92};
93
102template <typename TMotor, typename TController, typename TRelativeEncoder,
103 typename TAbsoluteEncoder>
105public:
107 PidMotorControllerPair<TMotor, TController, TRelativeEncoder,
108 TAbsoluteEncoder> &controllerPair)
109 : m_controllerPair{controllerPair} {
110 frc::SmartDashboard::PutNumber(m_controllerPair.m_shuffleboardPrefix +
111 " P Gain",
112 m_controllerPair.GetPidSettings().p);
113 frc::SmartDashboard::PutNumber(m_controllerPair.m_shuffleboardPrefix +
114 " I Gain",
115 m_controllerPair.GetPidSettings().i);
116 frc::SmartDashboard::PutNumber(m_controllerPair.m_shuffleboardPrefix +
117 " D Gain",
118 m_controllerPair.GetPidSettings().d);
119 frc::SmartDashboard::PutNumber(m_controllerPair.m_shuffleboardPrefix +
120 " IZone",
121 m_controllerPair.GetPidSettings().iZone);
122 frc::SmartDashboard::PutNumber(m_controllerPair.m_shuffleboardPrefix +
123 " Feed Forward",
124 m_controllerPair.GetPidSettings().ff);
125 }
126
129 double tP = frc::SmartDashboard::GetNumber(
130 m_controllerPair.m_shuffleboardPrefix + " P Gain",
131 m_controllerPair.GetPidSettings().p);
132 double tI = frc::SmartDashboard::GetNumber(
133 m_controllerPair.m_shuffleboardPrefix + " I Gain",
134 m_controllerPair.GetPidSettings().i);
135 double tD = frc::SmartDashboard::GetNumber(
136 m_controllerPair.m_shuffleboardPrefix + " D Gain",
137 m_controllerPair.GetPidSettings().d);
138 double tIZone = frc::SmartDashboard::GetNumber(
139 m_controllerPair.m_shuffleboardPrefix + " IZone",
140 m_controllerPair.GetPidSettings().iZone);
141 double tFeedForward = frc::SmartDashboard::GetNumber(
142 m_controllerPair.m_shuffleboardPrefix + " Feed Forward",
143 m_controllerPair.GetPidSettings().ff);
144
145 m_controllerPair.UpdatePidSettings(
146 {.p = tP, .i = tI, .d = tD, .iZone = tIZone, .ff = tFeedForward});
147 }
148
149private:
150 PidMotorControllerPair<TMotor, TController, TRelativeEncoder,
151 TAbsoluteEncoder> &m_controllerPair;
152};
153} // namespace subzero
Tunes a pair of motors concurrently through SmartDashboard.
void UpdateFromShuffleboard()
Call this within the Periodic method of the encapsulating subsystem.
PidMotorControllerPairTuner(PidMotorControllerPair< TMotor, TController, TRelativeEncoder, TAbsoluteEncoder > &controllerPair)
Encapsulates a pair of motors that should be treated as a single unit.
void UpdatePidSettings(PidSettings settings)
void RunWithVelocity(double percentageFirst, double percentageSecond)
Run motors at the given percentage of max RPM.
void RunWithVelocity(units::revolutions_per_minute_t rpmFirst, units::revolutions_per_minute_t rpmSecond)
Run motors at the given RPM.
const PidSettings & GetPidSettings() const
PidMotorControllerPair(std::string prefix, PidMotorController< TMotor, TController, TRelativeEncoder, TAbsoluteEncoder > &first, PidMotorController< TMotor, TController, TRelativeEncoder, TAbsoluteEncoder > &second)
Construct a new Pi MotorControllerPair.
Combines a motor, motor drvier, relative encoder, and absolute encoder into a single wrapper; helpful...