WPILibC++ 2023.4.3
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(PIDController controller, double initialPosition = 0);
29
30 void Periodic() override;
31
32 /**
33 * Sets the setpoint for the subsystem.
34 *
35 * @param setpoint the setpoint for the subsystem
36 */
37 void SetSetpoint(double setpoint);
38
39 /**
40 * Gets the setpoint for the subsystem.
41 *
42 * @return the setpoint for the subsystem
43 */
44 double GetSetpoint() const;
45
46 /**
47 * Enables the PID control. Resets the controller.
48 */
49 virtual void Enable();
50
51 /**
52 * Disables the PID control. Sets output to zero.
53 */
54 virtual void Disable();
55
56 /**
57 * Returns whether the controller is enabled.
58 *
59 * @return Whether the controller is enabled.
60 */
61 bool IsEnabled();
62
63 /**
64 * Returns the PIDController.
65 *
66 * @return The controller.
67 */
69
70 protected:
72 bool m_enabled{false};
73
74 /**
75 * Returns the measurement of the process variable used by the PIDController.
76 *
77 * @return the measurement of the process variable
78 */
79 virtual double GetMeasurement() = 0;
80
81 /**
82 * Uses the output from the PIDController.
83 *
84 * @param output the output of the PIDController
85 * @param setpoint the setpoint of the PIDController (for feedforward)
86 */
87 virtual void UseOutput(double output, double setpoint) = 0;
88};
89} // namespace frc2
Implements a PID control loop.
Definition: PIDController.h:23
A subsystem that uses a PIDController to control an output.
Definition: PIDSubsystem.h:20
bool IsEnabled()
Returns whether the controller is enabled.
PIDController m_controller
Definition: PIDSubsystem.h:71
virtual void Enable()
Enables the PID control.
virtual void UseOutput(double output, double setpoint)=0
Uses the output from the PIDController.
double GetSetpoint() const
Gets the setpoint for the subsystem.
PIDSubsystem(PIDController controller, double initialPosition=0)
Creates a new PIDSubsystem.
bool m_enabled
Definition: PIDSubsystem.h:72
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.
PIDController & GetController()
Returns the PIDController.
void SetSetpoint(double setpoint)
Sets the setpoint for the subsystem.
A base for subsystems that handles registration in the constructor, and provides a more intuitive met...
Definition: SubsystemBase.h:24
Definition: InstantCommand.h:14