WPILibC++  2020.3.2-60-g3011ebe
PIDCommand.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <functional>
11 #include <initializer_list>
12 
13 #include <frc/controller/PIDController.h>
14 
15 #include "frc2/command/CommandBase.h"
16 #include "frc2/command/CommandHelper.h"
17 
18 namespace frc2 {
27 class PIDCommand : public CommandHelper<CommandBase, PIDCommand> {
28  public:
39  PIDCommand(PIDController controller,
40  std::function<double()> measurementSource,
41  std::function<double()> setpointSource,
42  std::function<void(double)> useOutput,
43  std::initializer_list<Subsystem*> requirements);
44 
55  PIDCommand(PIDController controller,
56  std::function<double()> measurementSource,
57  std::function<double()> setpointSource,
58  std::function<void(double)> useOutput,
59  wpi::ArrayRef<Subsystem*> requirements = {});
60 
71  PIDCommand(PIDController controller,
72  std::function<double()> measurementSource, double setpoint,
73  std::function<void(double)> useOutput,
74  std::initializer_list<Subsystem*> requirements);
75 
86  PIDCommand(PIDController controller,
87  std::function<double()> measurementSource, double setpoint,
88  std::function<void(double)> useOutput,
89  wpi::ArrayRef<Subsystem*> 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 
107 
108  protected:
109  PIDController m_controller;
110  std::function<double()> m_measurement;
111  std::function<double()> m_setpoint;
112  std::function<void(double)> m_useOutput;
113 };
114 } // namespace frc2
frc2::PIDCommand::PIDCommand
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.
frc2::PIDCommand::Execute
void Execute() override
The main body of a command.
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
frc2::PIDCommand::Initialize
void Initialize() override
The initial subroutine of a command.
frc2::PIDCommand::GetController
PIDController & GetController()
Returns the PIDController used by the command.
frc2::PIDCommand
A command that controls an output with a PIDController.
Definition: PIDCommand.h:27
frc2::CommandHelper
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
frc2::PIDCommand::End
void End(bool interrupted) override
The action to take when the command ends.
frc2::PIDController
Implements a PID control loop.
Definition: PIDController.h:23