mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +00:00
Originally, PIDController used PIDSource with its "PIDSourceType" to determine whether a class should return position or velocity to the controller. However, the supported languages have changed a lot over 10 years and now support lambdas. Instead of using PIDSource and PIDOutput, users can pass in doubles to the Calculate() function synchronously. This makes the controller much more flexible for team's needs as they no longer have to make a separate PIDSource-inheriting class just to provide a custom input. The built-in feedforward was removed. Since PIDController is synchronous now, they can add their own feedforward on top of what Calculate() returns. To facilitate running the controller asynchronously, there is a PIDControllerRunner class that handles that. By separating the loop from the control law, PIDController can now be composed with others and be used to control a drivetrain (a multiple input, multiple output system that requires summing the results from two controllers) much easier. Also, motion profiling can be used to set the reference over time. All the classes related to the old PIDController are now deprecated. The new classes are in an experimental namespace to avoid name conflicts. While this is a large change, I think it is a necessary one for growth. The old PIDController design was created in a time when languages only supported OOP, and we have more tools at our disposal now to solve problems. This more versatile implementation can be used in more places like as a replacement for Pathfinder's "EncoderFollower" class. There has been hesitation to add lambda support to WPILib for a while now out of concerns for requiring teams to learn more features of C++ or Java. In my opinion, this change makes PIDController easier to use, not harder. The concept of a function is a building block of OOP and should be learned before classes. The ability to store functions as first-class objects and invoke them just like variables is rather natural. Note that PID constants for the new controller will be different from the old one. The original controller didn't take the discretization period into account. To fix this, teams should just have to divide their Ki gain by 0.05 and multiply their Kd gain by 0.05 where 0.05 is the original default period.
141 lines
5.0 KiB
C++
141 lines
5.0 KiB
C++
/*----------------------------------------------------------------------------*/
|
|
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
/* the project. */
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
#pragma once
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
#include <wpi/deprecated.h>
|
|
#include <wpi/mutex.h>
|
|
|
|
#include "frc/Base.h"
|
|
#include "frc/Controller.h"
|
|
#include "frc/Notifier.h"
|
|
#include "frc/PIDBase.h"
|
|
#include "frc/PIDSource.h"
|
|
#include "frc/Timer.h"
|
|
|
|
namespace frc {
|
|
|
|
class PIDOutput;
|
|
|
|
/**
|
|
* Class implements a PID Control Loop.
|
|
*
|
|
* Creates a separate thread which reads the given PIDSource and takes care of
|
|
* the integral calculations, as well as writing the given PIDOutput.
|
|
*
|
|
* This feedback controller runs in discrete time, so time deltas are not used
|
|
* in the integral and derivative calculations. Therefore, the sample rate
|
|
* affects the controller's behavior for a given set of PID constants.
|
|
*/
|
|
class PIDController : public PIDBase, public Controller {
|
|
public:
|
|
/**
|
|
* Allocate a PID object with the given constants for P, I, D.
|
|
*
|
|
* @param Kp the proportional coefficient
|
|
* @param Ki the integral coefficient
|
|
* @param Kd the derivative coefficient
|
|
* @param source The PIDSource object that is used to get values
|
|
* @param output The PIDOutput object that is set to the output value
|
|
* @param period the loop time for doing calculations in seconds. This
|
|
* particularly affects calculations of the integral and
|
|
* differental terms. The default is 0.05 (50ms).
|
|
*/
|
|
WPI_DEPRECATED("Use frc2::PIDController class instead.")
|
|
PIDController(double p, double i, double d, PIDSource* source,
|
|
PIDOutput* output, double period = 0.05);
|
|
|
|
/**
|
|
* Allocate a PID object with the given constants for P, I, D.
|
|
*
|
|
* @param Kp the proportional coefficient
|
|
* @param Ki the integral coefficient
|
|
* @param Kd the derivative coefficient
|
|
* @param source The PIDSource object that is used to get values
|
|
* @param output The PIDOutput object that is set to the output value
|
|
* @param period the loop time for doing calculations in seconds. This
|
|
* particularly affects calculations of the integral and
|
|
* differental terms. The default is 0.05 (50ms).
|
|
*/
|
|
WPI_DEPRECATED("Use frc2::PIDController class instead.")
|
|
PIDController(double p, double i, double d, double f, PIDSource* source,
|
|
PIDOutput* output, double period = 0.05);
|
|
|
|
/**
|
|
* Allocate a PID object with the given constants for P, I, D.
|
|
*
|
|
* @param Kp the proportional coefficient
|
|
* @param Ki the integral coefficient
|
|
* @param Kd the derivative coefficient
|
|
* @param source The PIDSource object that is used to get values
|
|
* @param output The PIDOutput object that is set to the output value
|
|
* @param period the loop time for doing calculations in seconds. This
|
|
* particularly affects calculations of the integral and
|
|
* differental terms. The default is 0.05 (50ms).
|
|
*/
|
|
WPI_DEPRECATED("Use frc2::PIDController class instead.")
|
|
PIDController(double p, double i, double d, PIDSource& source,
|
|
PIDOutput& output, double period = 0.05);
|
|
|
|
/**
|
|
* Allocate a PID object with the given constants for P, I, D.
|
|
*
|
|
* @param Kp the proportional coefficient
|
|
* @param Ki the integral coefficient
|
|
* @param Kd the derivative coefficient
|
|
* @param source The PIDSource object that is used to get values
|
|
* @param output The PIDOutput object that is set to the output value
|
|
* @param period the loop time for doing calculations in seconds. This
|
|
* particularly affects calculations of the integral and
|
|
* differental terms. The default is 0.05 (50ms).
|
|
*/
|
|
WPI_DEPRECATED("Use frc2::PIDController class instead.")
|
|
PIDController(double p, double i, double d, double f, PIDSource& source,
|
|
PIDOutput& output, double period = 0.05);
|
|
|
|
~PIDController() override;
|
|
|
|
PIDController(PIDController&&) = default;
|
|
PIDController& operator=(PIDController&&) = default;
|
|
|
|
/**
|
|
* Begin running the PIDController.
|
|
*/
|
|
void Enable() override;
|
|
|
|
/**
|
|
* Stop running the PIDController, this sets the output to zero before
|
|
* stopping.
|
|
*/
|
|
void Disable() override;
|
|
|
|
/**
|
|
* Set the enabled state of the PIDController.
|
|
*/
|
|
void SetEnabled(bool enable);
|
|
|
|
/**
|
|
* Return true if PIDController is enabled.
|
|
*/
|
|
bool IsEnabled() const;
|
|
|
|
/**
|
|
* Reset the previous error, the integral term, and disable the controller.
|
|
*/
|
|
void Reset() override;
|
|
|
|
void InitSendable(SendableBuilder& builder) override;
|
|
|
|
private:
|
|
std::unique_ptr<Notifier> m_controlLoop;
|
|
};
|
|
|
|
} // namespace frc
|