WPILibC++ 2023.4.3
PIDCommand.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 <initializer_list>
9#include <span>
10
12
15
16namespace frc2 {
17/**
18 * A command that controls an output with a PIDController. Runs forever by
19 * default - to add exit conditions and/or other behavior, subclass this class.
20 * The controller calculation and output are performed synchronously in the
21 * command's execute() method.
22 *
23 * This class is provided by the NewCommands VendorDep
24 *
25 * @see PIDController
26 */
27class PIDCommand : public CommandHelper<CommandBase, PIDCommand> {
28 public:
29 /**
30 * Creates a new PIDCommand, which controls the given output with a
31 * PIDController.
32 *
33 * @param controller the controller that controls the output.
34 * @param measurementSource the measurement of the process variable
35 * @param setpointSource the controller's reference (aka setpoint)
36 * @param useOutput the controller's output
37 * @param requirements the subsystems required by this command
38 */
40 std::function<double()> measurementSource,
41 std::function<double()> setpointSource,
42 std::function<void(double)> useOutput,
43 std::initializer_list<Subsystem*> requirements);
44
45 /**
46 * Creates a new PIDCommand, which controls the given output with a
47 * PIDController.
48 *
49 * @param controller the controller that controls the output.
50 * @param measurementSource the measurement of the process variable
51 * @param setpointSource the controller's reference (aka setpoint)
52 * @param useOutput the controller's output
53 * @param requirements the subsystems required by this command
54 */
56 std::function<double()> measurementSource,
57 std::function<double()> setpointSource,
58 std::function<void(double)> useOutput,
59 std::span<Subsystem* const> requirements = {});
60
61 /**
62 * Creates a new PIDCommand, which controls the given output with a
63 * PIDController with a constant setpoint.
64 *
65 * @param controller the controller that controls the output.
66 * @param measurementSource the measurement of the process variable
67 * @param setpoint the controller's setpoint (aka setpoint)
68 * @param useOutput the controller's output
69 * @param requirements the subsystems required by this command
70 */
72 std::function<double()> measurementSource, double setpoint,
73 std::function<void(double)> useOutput,
74 std::initializer_list<Subsystem*> requirements);
75
76 /**
77 * Creates a new PIDCommand, which controls the given output with a
78 * PIDController with a constant setpoint.
79 *
80 * @param controller the controller that controls the output.
81 * @param measurementSource the measurement of the process variable
82 * @param setpoint the controller's setpoint (aka setpoint)
83 * @param useOutput the controller's output
84 * @param requirements the subsystems required by this command
85 */
87 std::function<double()> measurementSource, double setpoint,
88 std::function<void(double)> useOutput,
89 std::span<Subsystem* const> requirements = {});
90
91 PIDCommand(PIDCommand&& other) = default;
92
93 PIDCommand(const PIDCommand& other) = default;
94
95 void Initialize() override;
96
97 void Execute() override;
98
99 void End(bool interrupted) override;
100
101 /**
102 * Returns the PIDController used by the command.
103 *
104 * @return The PIDController
105 */
107
108 protected:
110 std::function<double()> m_measurement;
111 std::function<double()> m_setpoint;
112 std::function<void(double)> m_useOutput;
113};
114} // namespace frc2
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
A command that controls an output with a PIDController.
Definition: PIDCommand.h:27
PIDCommand(PIDController controller, std::function< double()> measurementSource, double setpoint, std::function< void(double)> useOutput, std::span< Subsystem *const > requirements={})
Creates a new PIDCommand, which controls the given output with a PIDController with a constant setpoi...
PIDCommand(PIDController controller, std::function< double()> measurementSource, std::function< double()> setpointSource, std::function< void(double)> useOutput, std::span< Subsystem *const > requirements={})
Creates a new PIDCommand, which controls the given output with a PIDController.
PIDCommand(PIDController controller, std::function< double()> measurementSource, std::function< double()> setpointSource, std::function< void(double)> useOutput, std::initializer_list< Subsystem * > requirements)
Creates a new PIDCommand, which controls the given output with a PIDController.
PIDCommand(PIDCommand &&other)=default
std::function< double()> m_measurement
Definition: PIDCommand.h:110
void Initialize() override
The initial subroutine of a command.
std::function< void(double)> m_useOutput
Definition: PIDCommand.h:112
PIDCommand(PIDController controller, std::function< double()> measurementSource, double setpoint, std::function< void(double)> useOutput, std::initializer_list< Subsystem * > requirements)
Creates a new PIDCommand, which controls the given output with a PIDController with a constant setpoi...
void End(bool interrupted) override
The action to take when the command ends.
void Execute() override
The main body of a command.
PIDController & GetController()
Returns the PIDController used by the command.
PIDCommand(const PIDCommand &other)=default
PIDController m_controller
Definition: PIDCommand.h:109
std::function< double()> m_setpoint
Definition: PIDCommand.h:111
Implements a PID control loop.
Definition: PIDController.h:23
Definition: InstantCommand.h:14