WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
ProfiledPIDCommand.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
7#include <functional>
8#include <utility>
9
11#include <units/time.h>
12
16
17namespace frc2 {
18/**
19 * A command that controls an output with a ProfiledPIDController. Runs forever
20 * by default - to add exit conditions and/or other behavior, subclass this
21 * class. The controller calculation and output are performed synchronously in
22 * the command's execute() method.
23 *
24 * This class is provided by the NewCommands VendorDep
25 *
26 * @see ProfiledPIDController<Distance>
27 */
28template <class Distance>
30 : public CommandHelper<Command, ProfiledPIDCommand<Distance>> {
32 using Velocity =
35 using State = typename frc::TrapezoidProfile<Distance>::State;
36
37 public:
38 /**
39 * Creates a new PIDCommand, which controls the given output with a
40 * ProfiledPIDController.
41 *
42 * @param controller the controller that controls the output.
43 * @param measurementSource the measurement of the process variable
44 * @param goalSource the controller's goal
45 * @param useOutput the controller's output
46 * @param requirements the subsystems required by this command
47 * @deprecated Use a ProfiledPIDController instead
48 */
49 [[deprecated("Use a ProfiledPIDController instead")]]
51 std::function<Distance_t()> measurementSource,
52 std::function<State()> goalSource,
53 std::function<void(double, State)> useOutput,
54 Requirements requirements = {})
55 : m_controller{controller},
56 m_measurement{std::move(measurementSource)},
57 m_goal{std::move(goalSource)},
58 m_useOutput{std::move(useOutput)} {
59 this->AddRequirements(requirements);
60 }
61
62 /**
63 * Creates a new PIDCommand, which controls the given output with a
64 * ProfiledPIDController.
65 *
66 * @param controller the controller that controls the output.
67 * @param measurementSource the measurement of the process variable
68 * @param goalSource the controller's goal
69 * @param useOutput the controller's output
70 * @param requirements the subsystems required by this command
71 * @deprecated Use a ProfiledPIDController instead
72 */
73 [[deprecated("Use a ProfiledPIDController instead")]]
75 std::function<Distance_t()> measurementSource,
76 std::function<Distance_t()> goalSource,
77 std::function<void(double, State)> useOutput,
78 Requirements requirements = {})
80 controller, measurementSource,
81 [goalSource = std::move(goalSource)]() {
82 return State{goalSource(), Velocity_t{0}};
83 },
84 useOutput, requirements) {}
85
86 /**
87 * Creates a new PIDCommand, which controls the given output with a
88 * ProfiledPIDController with a constant goal.
89 *
90 * @param controller the controller that controls the output.
91 * @param measurementSource the measurement of the process variable
92 * @param goal the controller's goal
93 * @param useOutput the controller's output
94 * @param requirements the subsystems required by this command
95 * @deprecated Use a ProfiledPIDController instead
96 */
97
98 [[deprecated("Use a ProfiledPIDController instead")]]
100 std::function<Distance_t()> measurementSource, State goal,
101 std::function<void(double, State)> useOutput,
102 Requirements requirements = {})
104 controller, measurementSource, [goal] { return goal; }, useOutput,
105 requirements) {}
106
107 /**
108 * Creates a new PIDCommand, which controls the given output with a
109 * ProfiledPIDController with a constant goal.
110 *
111 * @param controller the controller that controls the output.
112 * @param measurementSource the measurement of the process variable
113 * @param goal the controller's goal
114 * @param useOutput the controller's output
115 * @param requirements the subsystems required by this command
116 * @deprecated Use a ProfiledPIDController instead
117 */
118 [[deprecated("Use a ProfiledPIDController instead")]]
120 std::function<Distance_t()> measurementSource,
121 Distance_t goal,
122 std::function<void(double, State)> useOutput,
123 Requirements requirements = {})
125 controller, measurementSource, [goal] { return goal; }, useOutput,
126 requirements) {}
127
129
130 ProfiledPIDCommand(const ProfiledPIDCommand& other) = default;
131
132 void Initialize() override { m_controller.Reset(m_measurement()); }
133
134 void Execute() override {
136 m_controller.GetSetpoint());
137 }
138
139 void End(bool interrupted) override {
140 m_useOutput(0, State{Distance_t(0), Velocity_t(0)});
141 }
142
143 /**
144 * Returns the ProfiledPIDController used by the command.
145 *
146 * @return The ProfiledPIDController
147 */
149
150 protected:
151 /// Profiled PID controller.
153
154 /// Measurement getter.
155 std::function<Distance_t()> m_measurement;
156
157 /// Goal getter.
158 std::function<State()> m_goal;
159
160 /// Profiled PID controller output consumer.
161 std::function<void(double, State)> m_useOutput;
162};
163} // namespace frc2
CRTP implementation to allow polymorphic decorator functions in Command.
Definition CommandHelper.h:25
A command that controls an output with a ProfiledPIDController.
Definition ProfiledPIDCommand.h:30
ProfiledPIDCommand(ProfiledPIDCommand &&other)=default
ProfiledPIDCommand(frc::ProfiledPIDController< Distance > controller, std::function< Distance_t()> measurementSource, State goal, std::function< void(double, State)> useOutput, Requirements requirements={})
Creates a new PIDCommand, which controls the given output with a ProfiledPIDController with a constan...
Definition ProfiledPIDCommand.h:99
ProfiledPIDCommand(frc::ProfiledPIDController< Distance > controller, std::function< Distance_t()> measurementSource, Distance_t goal, std::function< void(double, State)> useOutput, Requirements requirements={})
Creates a new PIDCommand, which controls the given output with a ProfiledPIDController with a constan...
Definition ProfiledPIDCommand.h:119
std::function< Distance_t()> m_measurement
Measurement getter.
Definition ProfiledPIDCommand.h:155
ProfiledPIDCommand(frc::ProfiledPIDController< Distance > controller, std::function< Distance_t()> measurementSource, std::function< State()> goalSource, std::function< void(double, State)> useOutput, Requirements requirements={})
Creates a new PIDCommand, which controls the given output with a ProfiledPIDController.
Definition ProfiledPIDCommand.h:50
std::function< void(double, State)> m_useOutput
Profiled PID controller output consumer.
Definition ProfiledPIDCommand.h:161
void Initialize() override
Definition ProfiledPIDCommand.h:132
std::function< State()> m_goal
Goal getter.
Definition ProfiledPIDCommand.h:158
void Execute() override
Definition ProfiledPIDCommand.h:134
void End(bool interrupted) override
Definition ProfiledPIDCommand.h:139
frc::ProfiledPIDController< Distance > m_controller
Profiled PID controller.
Definition ProfiledPIDCommand.h:152
frc::ProfiledPIDController< Distance > & GetController()
Returns the ProfiledPIDController used by the command.
Definition ProfiledPIDCommand.h:148
ProfiledPIDCommand(frc::ProfiledPIDController< Distance > controller, std::function< Distance_t()> measurementSource, std::function< Distance_t()> goalSource, std::function< void(double, State)> useOutput, Requirements requirements={})
Creates a new PIDCommand, which controls the given output with a ProfiledPIDController.
Definition ProfiledPIDCommand.h:74
ProfiledPIDCommand(const ProfiledPIDCommand &other)=default
Represents requirements for a command, which is a set of (pointers to) subsystems.
Definition Requirements.h:20
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
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280