WPILibC++ 2024.3.2
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 */
39 std::function<double()> measurementSource,
40 std::function<double()> setpointSource,
41 std::function<void(double)> useOutput,
42 Requirements requirements = {});
43
44 /**
45 * Creates a new PIDCommand, which controls the given output with a
46 * PIDController with a constant setpoint.
47 *
48 * @param controller the controller that controls the output.
49 * @param measurementSource the measurement of the process variable
50 * @param setpoint the controller's setpoint (aka setpoint)
51 * @param useOutput the controller's output
52 * @param requirements the subsystems required by this command
53 */
55 std::function<double()> measurementSource, double setpoint,
56 std::function<void(double)> useOutput,
57 Requirements requirements = {});
58
59 PIDCommand(PIDCommand&& other) = default;
60
61 PIDCommand(const PIDCommand& other) = default;
62
63 void Initialize() override;
64
65 void Execute() override;
66
67 void End(bool interrupted) override;
68
69 /**
70 * Returns the PIDController used by the command.
71 *
72 * @return The PIDController
73 */
75
76 protected:
77 /// PID controller.
79
80 /// Measurement getter.
81 std::function<double()> m_measurement;
82
83 /// Setpoint getter.
84 std::function<double()> m_setpoint;
85
86 /// PID controller output consumer.
87 std::function<void(double)> m_useOutput;
88};
89} // namespace frc2
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:27
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:81
void Initialize() override
std::function< void(double)> m_useOutput
PID controller output consumer.
Definition: PIDCommand.h:87
void End(bool interrupted) override
void Execute() override
frc::PIDController m_controller
PID controller.
Definition: PIDCommand.h:78
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:84
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:23
Definition: TrapezoidProfileSubsystem.h:12