WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
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
10
14
15namespace frc2 {
16/**
17 * A command that controls an output with a PIDController. Runs forever by
18 * default - to add exit conditions and/or other behavior, subclass this class.
19 * The controller calculation and output are performed synchronously in the
20 * command's execute() method.
21 *
22 * This class is provided by the NewCommands VendorDep
23 *
24 * @see PIDController
25 */
26class PIDCommand : public CommandHelper<Command, PIDCommand> {
27 public:
28 /**
29 * Creates a new PIDCommand, which controls the given output with a
30 * PIDController.
31 *
32 * @param controller the controller that controls the output.
33 * @param measurementSource the measurement of the process variable
34 * @param setpointSource the controller's reference (aka setpoint)
35 * @param useOutput the controller's output
36 * @param requirements the subsystems required by this command
37 * @deprecated Use a PIDController instead
38 */
39 [[deprecated("Use a PIDController instead")]]
41 std::function<double()> measurementSource,
42 std::function<double()> setpointSource,
43 std::function<void(double)> useOutput,
44 Requirements requirements = {});
45
46 /**
47 * Creates a new PIDCommand, which controls the given output with a
48 * PIDController with a constant setpoint.
49 *
50 * @param controller the controller that controls the output.
51 * @param measurementSource the measurement of the process variable
52 * @param setpoint the controller's setpoint (aka setpoint)
53 * @param useOutput the controller's output
54 * @param requirements the subsystems required by this command
55 * @deprecated Use a PIDController instead
56 */
57 [[deprecated("Use a PIDController instead")]]
59 std::function<double()> measurementSource, double setpoint,
60 std::function<void(double)> useOutput,
61 Requirements requirements = {});
62
63 PIDCommand(PIDCommand&& other) = default;
64
65 PIDCommand(const PIDCommand& other) = default;
66
67 void Initialize() override;
68
69 void Execute() override;
70
71 void End(bool interrupted) override;
72
73 /**
74 * Returns the PIDController used by the command.
75 *
76 * @return The PIDController
77 */
79
80 protected:
81 /// PID controller.
83
84 /// Measurement getter.
85 std::function<double()> m_measurement;
86
87 /// Setpoint getter.
88 std::function<double()> m_setpoint;
89
90 /// PID controller output consumer.
91 std::function<void(double)> m_useOutput;
92};
93} // namespace frc2
CRTP implementation to allow polymorphic decorator functions in Command.
Definition CommandHelper.h:25
A command that controls an output with a PIDController.
Definition PIDCommand.h:26
PIDCommand(frc::PIDController controller, std::function< double()> measurementSource, double setpoint, std::function< void(double)> useOutput, Requirements requirements={})
Creates a new PIDCommand, which controls the given output with a PIDController with a constant setpoi...
PIDCommand(PIDCommand &&other)=default
std::function< double()> m_measurement
Measurement getter.
Definition PIDCommand.h:85
void Initialize() override
std::function< void(double)> m_useOutput
PID controller output consumer.
Definition PIDCommand.h:91
void End(bool interrupted) override
void Execute() override
frc::PIDController m_controller
PID controller.
Definition PIDCommand.h:82
frc::PIDController & GetController()
Returns the PIDController used by the command.
PIDCommand(const PIDCommand &other)=default
std::function< double()> m_setpoint
Setpoint getter.
Definition PIDCommand.h:88
PIDCommand(frc::PIDController controller, std::function< double()> measurementSource, std::function< double()> setpointSource, std::function< void(double)> useOutput, Requirements requirements={})
Creates a new PIDCommand, which controls the given output with a PIDController.
Represents requirements for a command, which is a set of (pointers to) subsystems.
Definition Requirements.h:20
Implements a PID control loop.
Definition PIDController.h:29
Definition FunctionalCommand.h:13