WPILibC++ 2023.4.3-108-ge5452e3
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 */
21template <class Distance>
24 using Velocity =
27 using State = typename frc::TrapezoidProfile<Distance>::State;
28
29 public:
30 /**
31 * Creates a new ProfiledPIDSubsystem.
32 *
33 * @param controller the ProfiledPIDController to use
34 * @param initialPosition the initial goal position of the subsystem
35 */
37 Distance_t initialPosition = Distance_t{0})
38 : m_controller{controller} {
39 SetGoal(initialPosition);
40 }
41
42 void Periodic() override {
43 if (m_enabled) {
45 m_controller.GetSetpoint());
46 }
47 }
48
49 /**
50 * Sets the goal state for the subsystem.
51 *
52 * @param goal The goal state for the subsystem's motion profile.
53 */
54 void SetGoal(State goal) { m_controller.SetGoal(goal); }
55
56 /**
57 * Sets the goal state for the subsystem. Goal velocity assumed to be zero.
58 *
59 * @param goal The goal position for the subsystem's motion profile.
60 */
61 void SetGoal(Distance_t goal) { SetGoal(State{goal, Velocity_t(0)}); }
62
63 /**
64 * Enables the PID control. Resets the controller.
65 */
66 virtual void Enable() {
68 m_enabled = true;
69 }
70
71 /**
72 * Disables the PID control. Sets output to zero.
73 */
74 virtual void Disable() {
75 UseOutput(0, State{Distance_t(0), Velocity_t(0)});
76 m_enabled = false;
77 }
78
79 /**
80 * Returns whether the controller is enabled.
81 *
82 * @return Whether the controller is enabled.
83 */
84 bool IsEnabled() { return m_enabled; }
85
86 /**
87 * Returns the ProfiledPIDController.
88 *
89 * @return The controller.
90 */
92
93 protected:
95 bool m_enabled{false};
96
97 /**
98 * Returns the measurement of the process variable used by the
99 * ProfiledPIDController.
100 *
101 * @return the measurement of the process variable
102 */
104
105 /**
106 * Uses the output from the ProfiledPIDController.
107 *
108 * @param output the output of the ProfiledPIDController
109 * @param setpoint the setpoint state of the ProfiledPIDController, for
110 * feedforward
111 */
112 virtual void UseOutput(double output, State setpoint) = 0;
113};
114} // namespace frc2
A subsystem that uses a ProfiledPIDController to control an output.
Definition: ProfiledPIDSubsystem.h:22
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:36
bool IsEnabled()
Returns whether the controller is enabled.
Definition: ProfiledPIDSubsystem.h:84
frc::ProfiledPIDController< Distance > & GetController()
Returns the ProfiledPIDController.
Definition: ProfiledPIDSubsystem.h:91
void Periodic() override
This method is called periodically by the CommandScheduler.
Definition: ProfiledPIDSubsystem.h:42
virtual void UseOutput(double output, State setpoint)=0
Uses the output from the ProfiledPIDController.
bool m_enabled
Definition: ProfiledPIDSubsystem.h:95
void SetGoal(Distance_t goal)
Sets the goal state for the subsystem.
Definition: ProfiledPIDSubsystem.h:61
frc::ProfiledPIDController< Distance > m_controller
Definition: ProfiledPIDSubsystem.h:94
virtual void Disable()
Disables the PID control.
Definition: ProfiledPIDSubsystem.h:74
virtual void Enable()
Enables the PID control.
Definition: ProfiledPIDSubsystem.h:66
void SetGoal(State goal)
Sets the goal state for the subsystem.
Definition: ProfiledPIDSubsystem.h:54
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:35
Definition: TrapezoidProfile.h:67
typename units::detail::compound_impl< U, Us... >::type compound_unit
Represents a unit type made up from other units.
Definition: base.h:1445
Definition: ProfiledPIDCommand.h:18