WPILibC++ 2024.3.1
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 */
49 std::function<Distance_t()> measurementSource,
50 std::function<State()> goalSource,
51 std::function<void(double, State)> useOutput,
52 Requirements requirements = {})
53 : m_controller{controller},
54 m_measurement{std::move(measurementSource)},
55 m_goal{std::move(goalSource)},
56 m_useOutput{std::move(useOutput)} {
57 this->AddRequirements(requirements);
58 }
59
60 /**
61 * Creates a new PIDCommand, which controls the given output with a
62 * ProfiledPIDController.
63 *
64 * @param controller the controller that controls the output.
65 * @param measurementSource the measurement of the process variable
66 * @param goalSource the controller's goal
67 * @param useOutput the controller's output
68 * @param requirements the subsystems required by this command
69 */
71 std::function<Distance_t()> measurementSource,
72 std::function<Distance_t()> goalSource,
73 std::function<void(double, State)> useOutput,
74 Requirements requirements = {})
76 controller, measurementSource,
77 [goalSource = std::move(goalSource)]() {
78 return State{goalSource(), Velocity_t{0}};
79 },
80 useOutput, requirements) {}
81
82 /**
83 * Creates a new PIDCommand, which controls the given output with a
84 * ProfiledPIDController with a constant goal.
85 *
86 * @param controller the controller that controls the output.
87 * @param measurementSource the measurement of the process variable
88 * @param goal the controller's goal
89 * @param useOutput the controller's output
90 * @param requirements the subsystems required by this command
91 */
93 std::function<Distance_t()> measurementSource, State goal,
94 std::function<void(double, State)> useOutput,
95 Requirements requirements = {})
97 controller, measurementSource, [goal] { return goal; }, useOutput,
98 requirements) {}
99
100 /**
101 * Creates a new PIDCommand, which controls the given output with a
102 * ProfiledPIDController with a constant goal.
103 *
104 * @param controller the controller that controls the output.
105 * @param measurementSource the measurement of the process variable
106 * @param goal the controller's goal
107 * @param useOutput the controller's output
108 * @param requirements the subsystems required by this command
109 */
111 std::function<Distance_t()> measurementSource,
112 Distance_t goal,
113 std::function<void(double, State)> useOutput,
114 Requirements requirements = {})
116 controller, measurementSource, [goal] { return goal; }, useOutput,
117 requirements) {}
118
120
121 ProfiledPIDCommand(const ProfiledPIDCommand& other) = default;
122
123 void Initialize() override { m_controller.Reset(m_measurement()); }
124
125 void Execute() override {
127 m_controller.GetSetpoint());
128 }
129
130 void End(bool interrupted) override {
131 m_useOutput(0, State{Distance_t(0), Velocity_t(0)});
132 }
133
134 /**
135 * Returns the ProfiledPIDController used by the command.
136 *
137 * @return The ProfiledPIDController
138 */
140
141 protected:
142 /// Profiled PID controller.
144
145 /// Measurement getter.
146 std::function<Distance_t()> m_measurement;
147
148 /// Goal getter.
149 std::function<State()> m_goal;
150
151 /// Profiled PID controller output consumer.
152 std::function<void(double, State)> m_useOutput;
153};
154} // namespace frc2
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:27
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:92
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:110
std::function< Distance_t()> m_measurement
Measurement getter.
Definition: ProfiledPIDCommand.h:146
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:48
std::function< void(double, State)> m_useOutput
Profiled PID controller output consumer.
Definition: ProfiledPIDCommand.h:152
void Initialize() override
Definition: ProfiledPIDCommand.h:123
std::function< State()> m_goal
Goal getter.
Definition: ProfiledPIDCommand.h:149
void Execute() override
Definition: ProfiledPIDCommand.h:125
void End(bool interrupted) override
Definition: ProfiledPIDCommand.h:130
frc::ProfiledPIDController< Distance > m_controller
Profiled PID controller.
Definition: ProfiledPIDCommand.h:143
frc::ProfiledPIDController< Distance > & GetController()
Returns the ProfiledPIDController used by the command.
Definition: ProfiledPIDCommand.h:139
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:70
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:35
Profile state.
Definition: TrapezoidProfile.h:90
typename units::detail::compound_impl< U, Us... >::type compound_unit
Represents a unit type made up from other units.
Definition: base.h:1434
Definition: TrapezoidProfileSubsystem.h:12