SubZero Common
Common library components for an FRC CommandRobot
Loading...
Searching...
No Matches
SimPidMotorController.h
Go to the documentation of this file.
1#pragma once
2
3#include <frc/controller/PIDController.h>
4#include <units/voltage.h>
5
6#include <optional>
7
9
10namespace subzero {
12public:
13 explicit SimPidMotorController(std::string name, PidSettings pidSettings,
14 units::revolutions_per_minute_t maxRpm);
15
16 inline void Set(double percentage) override {
17 m_currentRpm = m_maxRpm * percentage;
18 }
19
20 inline void Set(units::volt_t volts) override {
21 m_currentRpm = m_maxRpm * (volts / 12.7).value();
22 }
23
24 inline void SetPidTolerance(double tolerance) override {
25 m_pidController.SetTolerance(tolerance);
26 }
27
28 void Update() override;
29
30 inline void RunWithVelocity(units::revolutions_per_minute_t rpm) override {
31 m_currentRpm = rpm;
32 }
33
34 inline void RunWithVelocity(double percentage) override { Set(percentage); }
35
36 void RunToPosition(double position) override;
37
38 inline void ResetEncoder() override { m_currentRelativePosition = 0; }
39
40 inline double GetEncoderPosition() override {
41 return m_currentRelativePosition * m_conversionFactor;
42 }
43
44 inline std::optional<double> GetAbsoluteEncoderPosition() override {
45 return std::nullopt;
46 }
47
48 inline void SetEncoderConversionFactor(double factor) override {
49 m_conversionFactor = factor;
50 }
51
52 inline void SetAbsoluteEncoderConversionFactor(double factor) override {}
53
54 void Stop() override;
55
56 inline const PidSettings &GetPidSettings() override { return m_settings; }
57
58 inline void UpdatePidSettings(PidSettings settings) override {
59 m_settings = settings;
60 }
61
62private:
63 PidSettings m_settings;
64 frc::PIDController m_pidController;
65 double m_currentRelativePosition = 0;
66 bool m_absolutePositionEnabled = false;
67 double m_conversionFactor = 1.0;
68 double m_absoluteTarget = 0;
69 const units::revolutions_per_minute_t m_maxRpm;
70 units::revolutions_per_minute_t m_currentRpm = 0_rpm;
71};
72} // namespace subzero
const PidSettings & GetPidSettings() override
SimPidMotorController(std::string name, PidSettings pidSettings, units::revolutions_per_minute_t maxRpm)
void Set(units::volt_t volts) override
void SetEncoderConversionFactor(double factor) override
void RunWithVelocity(double percentage) override
void RunWithVelocity(units::revolutions_per_minute_t rpm) override
void SetPidTolerance(double tolerance) override
void SetAbsoluteEncoderConversionFactor(double factor) override
void UpdatePidSettings(PidSettings settings) override
void Set(double percentage) override
void RunToPosition(double position) override
std::optional< double > GetAbsoluteEncoderPosition() override