WPILibC++ 2024.3.2
PIDSubsystem.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
8
10
11namespace frc2 {
12/**
13 * A subsystem that uses a PIDController to control an output. The controller
14 * is run synchronously from the subsystem's periodic() method.
15 *
16 * This class is provided by the NewCommands VendorDep
17 *
18 * @see PIDController
19 */
21 public:
22 /**
23 * Creates a new PIDSubsystem.
24 *
25 * @param controller the PIDController to use
26 * @param initialPosition the initial setpoint of the subsystem
27 */
28 explicit PIDSubsystem(frc::PIDController controller,
29 double initialPosition = 0);
30
31 void Periodic() override;
32
33 /**
34 * Sets the setpoint for the subsystem.
35 *
36 * @param setpoint the setpoint for the subsystem
37 */
38 void SetSetpoint(double setpoint);
39
40 /**
41 * Gets the setpoint for the subsystem.
42 *
43 * @return the setpoint for the subsystem
44 */
45 double GetSetpoint() const;
46
47 /**
48 * Enables the PID control. Resets the controller.
49 */
50 virtual void Enable();
51
52 /**
53 * Disables the PID control. Sets output to zero.
54 */
55 virtual void Disable();
56
57 /**
58 * Returns whether the controller is enabled.
59 *
60 * @return Whether the controller is enabled.
61 */
62 bool IsEnabled();
63
64 /**
65 * Returns the PIDController.
66 *
67 * @return The controller.
68 */
70
71 protected:
72 /// PID controller.
74
75 /// Whether PID controller output is enabled.
76 bool m_enabled{false};
77
78 /**
79 * Returns the measurement of the process variable used by the PIDController.
80 *
81 * @return the measurement of the process variable
82 */
83 virtual double GetMeasurement() = 0;
84
85 /**
86 * Uses the output from the PIDController.
87 *
88 * @param output the output of the PIDController
89 * @param setpoint the setpoint of the PIDController (for feedforward)
90 */
91 virtual void UseOutput(double output, double setpoint) = 0;
92};
93} // namespace frc2
A subsystem that uses a PIDController to control an output.
Definition: PIDSubsystem.h:20
bool IsEnabled()
Returns whether the controller is enabled.
virtual void Enable()
Enables the PID control.
virtual void UseOutput(double output, double setpoint)=0
Uses the output from the PIDController.
frc::PIDController & GetController()
Returns the PIDController.
double GetSetpoint() const
Gets the setpoint for the subsystem.
PIDSubsystem(frc::PIDController controller, double initialPosition=0)
Creates a new PIDSubsystem.
bool m_enabled
Whether PID controller output is enabled.
Definition: PIDSubsystem.h:76
virtual void Disable()
Disables the PID control.
void Periodic() override
This method is called periodically by the CommandScheduler.
virtual double GetMeasurement()=0
Returns the measurement of the process variable used by the PIDController.
void SetSetpoint(double setpoint)
Sets the setpoint for the subsystem.
frc::PIDController m_controller
PID controller.
Definition: PIDSubsystem.h:73
A base for subsystems that handles registration in the constructor, and provides a more intuitive met...
Definition: SubsystemBase.h:24
Implements a PID control loop.
Definition: PIDController.h:23
Definition: TrapezoidProfileSubsystem.h:12