WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
ProfiledPIDSubsystem.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#include <units/time.h>
9
11
12namespace frc2 {
13/**
14 * A subsystem that uses a ProfiledPIDController to control an output. The
15 * controller is run synchronously from the subsystem's periodic() method.
16 *
17 * This class is provided by the NewCommands VendorDep
18 *
19 * @see ProfiledPIDController
20 * @deprecated Use a ProfiledPIDController instead
21 */
22template <class Distance>
23class [[deprecated("Use a ProfiledPIDController instead")]] ProfiledPIDSubsystem
24 : public SubsystemBase {
26 using Velocity =
29 using State = typename frc::TrapezoidProfile<Distance>::State;
30
31 public:
32 /**
33 * Creates a new ProfiledPIDSubsystem.
34 *
35 * @param controller the ProfiledPIDController to use
36 * @param initialPosition the initial goal position of the subsystem
37 */
39 Distance_t initialPosition = Distance_t{0})
40 : m_controller{controller} {
41 SetGoal(initialPosition);
42 }
43
44 void Periodic() override {
45 if (m_enabled) {
46 UseOutput(m_controller.Calculate(GetMeasurement()),
47 m_controller.GetSetpoint());
48 }
49 }
50
51 /**
52 * Sets the goal state for the subsystem.
53 *
54 * @param goal The goal state for the subsystem's motion profile.
55 */
56 void SetGoal(State goal) { m_controller.SetGoal(goal); }
57
58 /**
59 * Sets the goal state for the subsystem. Goal velocity assumed to be zero.
60 *
61 * @param goal The goal position for the subsystem's motion profile.
62 */
63 void SetGoal(Distance_t goal) { SetGoal(State{goal, Velocity_t(0)}); }
64
65 /**
66 * Enables the PID control. Resets the controller.
67 */
68 virtual void Enable() {
69 m_controller.Reset(GetMeasurement());
70 m_enabled = true;
71 }
72
73 /**
74 * Disables the PID control. Sets output to zero.
75 */
76 virtual void Disable() {
77 UseOutput(0, State{Distance_t(0), Velocity_t(0)});
78 m_enabled = false;
79 }
80
81 /**
82 * Returns whether the controller is enabled.
83 *
84 * @return Whether the controller is enabled.
85 */
86 bool IsEnabled() { return m_enabled; }
87
88 /**
89 * Returns the ProfiledPIDController.
90 *
91 * @return The controller.
92 */
94
95 protected:
96 /// Profiled PID controller.
98
99 /// Whether the profiled PID controller output is enabled.
100 bool m_enabled{false};
101
102 /**
103 * Returns the measurement of the process variable used by the
104 * ProfiledPIDController.
105 *
106 * @return the measurement of the process variable
107 */
109
110 /**
111 * Uses the output from the ProfiledPIDController.
112 *
113 * @param output the output of the ProfiledPIDController
114 * @param setpoint the setpoint state of the ProfiledPIDController, for
115 * feedforward
116 */
117 virtual void UseOutput(double output, State setpoint) = 0;
118};
119} // namespace frc2
A subsystem that uses a ProfiledPIDController to control an output.
Definition ProfiledPIDSubsystem.h:24
virtual Distance_t GetMeasurement()=0
Returns the measurement of the process variable used by the ProfiledPIDController.
ProfiledPIDSubsystem(frc::ProfiledPIDController< Distance > controller, Distance_t initialPosition=Distance_t{0})
Creates a new ProfiledPIDSubsystem.
Definition ProfiledPIDSubsystem.h:38
bool IsEnabled()
Returns whether the controller is enabled.
Definition ProfiledPIDSubsystem.h:86
frc::ProfiledPIDController< Distance > & GetController()
Returns the ProfiledPIDController.
Definition ProfiledPIDSubsystem.h:93
void Periodic() override
This method is called periodically by the CommandScheduler.
Definition ProfiledPIDSubsystem.h:44
virtual void UseOutput(double output, State setpoint)=0
Uses the output from the ProfiledPIDController.
void SetGoal(Distance_t goal)
Sets the goal state for the subsystem.
Definition ProfiledPIDSubsystem.h:63
frc::ProfiledPIDController< Distance > m_controller
Profiled PID controller.
Definition ProfiledPIDSubsystem.h:97
virtual void Disable()
Disables the PID control.
Definition ProfiledPIDSubsystem.h:76
virtual void Enable()
Enables the PID control.
Definition ProfiledPIDSubsystem.h:68
void SetGoal(State goal)
Sets the goal state for the subsystem.
Definition ProfiledPIDSubsystem.h:56
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 whose setpoint is constrained by a trapezoid profile.
Definition ProfiledPIDController.h:34
Profile state.
Definition TrapezoidProfile.h:96
typename units::detail::compound_impl< U, Us... >::type compound_unit
Represents a unit type made up from other units.
Definition base.h:1438
Definition FunctionalCommand.h:13