2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2016-05-20 12:07:40 -04:00
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
package edu.wpi.first.wpilibj;
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
|
2017-07-18 20:32:08 -07:00
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Class implements a PID Control Loop.
|
|
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* <p>Creates a separate thread which reads the given PIDSource and takes care of the integral
|
2016-06-20 23:25:23 -07:00
|
|
|
* calculations, as well as writing the given PIDOutput.
|
|
|
|
|
*
|
|
|
|
|
* <p>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.
|
Add replacement PIDController class (#1300)
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.
2019-07-07 15:37:13 -07:00
|
|
|
*
|
|
|
|
|
* @deprecated Use {@link edu.wpi.first.wpilibj.controller.PIDController} instead.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
Add replacement PIDController class (#1300)
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.
2019-07-07 15:37:13 -07:00
|
|
|
@Deprecated(since = "2020", forRemoval = true)
|
2021-02-12 22:22:11 -08:00
|
|
|
public class PIDController extends PIDBase implements Controller {
|
2018-05-16 00:02:21 -07:00
|
|
|
Notifier m_controlLoop = new Notifier(this::calculate);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/**
|
2017-07-28 22:24:05 -07:00
|
|
|
* Allocate a PID object with the given constants for P, I, D, and F.
|
2016-05-20 12:07:40 -04:00
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param Kp the proportional coefficient
|
|
|
|
|
* @param Ki the integral coefficient
|
|
|
|
|
* @param Kd the derivative coefficient
|
|
|
|
|
* @param Kf the feed forward term
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param source The PIDSource object that is used to get values
|
|
|
|
|
* @param output The PIDOutput object that is set to the output percentage
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param period the loop time for doing calculations in seconds. This particularly affects
|
|
|
|
|
* calculations of the integral and differential terms. The default is 0.05 (50ms).
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
2016-05-20 12:07:40 -04:00
|
|
|
@SuppressWarnings("ParameterName")
|
2020-12-29 22:45:16 -08:00
|
|
|
public PIDController(
|
|
|
|
|
double Kp,
|
|
|
|
|
double Ki,
|
|
|
|
|
double Kd,
|
|
|
|
|
double Kf,
|
|
|
|
|
PIDSource source,
|
|
|
|
|
PIDOutput output,
|
|
|
|
|
double period) {
|
2018-05-16 19:51:37 -07:00
|
|
|
super(Kp, Ki, Kd, Kf, source, output);
|
|
|
|
|
m_controlLoop.startPeriodic(period);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-07-28 22:24:05 -07:00
|
|
|
* Allocate a PID object with the given constants for P, I, D and period.
|
2016-05-20 12:07:40 -04:00
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param Kp the proportional coefficient
|
|
|
|
|
* @param Ki the integral coefficient
|
|
|
|
|
* @param Kd the derivative coefficient
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param source the PIDSource object that is used to get values
|
|
|
|
|
* @param output the PIDOutput object that is set to the output percentage
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param period the loop time for doing calculations in seconds. This particularly affects
|
|
|
|
|
* calculations of the integral and differential terms. The default is 0.05 (50ms).
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
2016-05-20 12:07:40 -04:00
|
|
|
@SuppressWarnings("ParameterName")
|
2020-12-29 22:45:16 -08:00
|
|
|
public PIDController(
|
|
|
|
|
double Kp, double Ki, double Kd, PIDSource source, PIDOutput output, double period) {
|
2015-06-25 15:07:55 -04:00
|
|
|
this(Kp, Ki, Kd, 0.0, source, output, period);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Allocate a PID object with the given constants for P, I, D, using a 50ms period.
|
|
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param Kp the proportional coefficient
|
|
|
|
|
* @param Ki the integral coefficient
|
|
|
|
|
* @param Kd the derivative coefficient
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param source The PIDSource object that is used to get values
|
|
|
|
|
* @param output The PIDOutput object that is set to the output percentage
|
|
|
|
|
*/
|
2016-05-20 12:07:40 -04:00
|
|
|
@SuppressWarnings("ParameterName")
|
2015-06-25 15:07:55 -04:00
|
|
|
public PIDController(double Kp, double Ki, double Kd, PIDSource source, PIDOutput output) {
|
|
|
|
|
this(Kp, Ki, Kd, source, output, kDefaultPeriod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Allocate a PID object with the given constants for P, I, D, using a 50ms period.
|
|
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param Kp the proportional coefficient
|
|
|
|
|
* @param Ki the integral coefficient
|
|
|
|
|
* @param Kd the derivative coefficient
|
|
|
|
|
* @param Kf the feed forward term
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param source The PIDSource object that is used to get values
|
|
|
|
|
* @param output The PIDOutput object that is set to the output percentage
|
|
|
|
|
*/
|
2016-05-20 12:07:40 -04:00
|
|
|
@SuppressWarnings("ParameterName")
|
2020-12-29 22:45:16 -08:00
|
|
|
public PIDController(
|
|
|
|
|
double Kp, double Ki, double Kd, double Kf, PIDSource source, PIDOutput output) {
|
2015-06-25 15:07:55 -04:00
|
|
|
this(Kp, Ki, Kd, Kf, source, output, kDefaultPeriod);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
@Override
|
2018-05-22 23:33:17 -07:00
|
|
|
public void close() {
|
2018-05-24 16:56:29 -07:00
|
|
|
m_controlLoop.close();
|
2017-11-24 00:55:35 -08:00
|
|
|
m_thisMutex.lock();
|
|
|
|
|
try {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_pidOutput = null;
|
|
|
|
|
m_pidInput = null;
|
|
|
|
|
m_controlLoop = null;
|
2017-11-24 00:55:35 -08:00
|
|
|
} finally {
|
|
|
|
|
m_thisMutex.unlock();
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Begin running the PIDController. */
|
2015-06-25 15:07:55 -04:00
|
|
|
@Override
|
2017-11-24 00:55:35 -08:00
|
|
|
public void enable() {
|
|
|
|
|
m_thisMutex.lock();
|
|
|
|
|
try {
|
|
|
|
|
m_enabled = true;
|
|
|
|
|
} finally {
|
|
|
|
|
m_thisMutex.unlock();
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Stop running the PIDController, this sets the output to zero before stopping. */
|
2015-06-25 15:07:55 -04:00
|
|
|
@Override
|
2017-11-24 00:55:35 -08:00
|
|
|
public void disable() {
|
|
|
|
|
// Ensures m_enabled check and pidWrite() call occur atomically
|
|
|
|
|
m_pidWriteMutex.lock();
|
|
|
|
|
try {
|
|
|
|
|
m_thisMutex.lock();
|
|
|
|
|
try {
|
|
|
|
|
m_enabled = false;
|
|
|
|
|
} finally {
|
|
|
|
|
m_thisMutex.unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_pidOutput.pidWrite(0);
|
|
|
|
|
} finally {
|
|
|
|
|
m_pidWriteMutex.unlock();
|
|
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2021-06-10 20:46:47 -07:00
|
|
|
/**
|
|
|
|
|
* Set the enabled state of the PIDController.
|
|
|
|
|
*
|
|
|
|
|
* @param enable True to enable the PIDController.
|
|
|
|
|
*/
|
2017-12-04 23:28:33 -08:00
|
|
|
public void setEnabled(boolean enable) {
|
|
|
|
|
if (enable) {
|
|
|
|
|
enable();
|
|
|
|
|
} else {
|
|
|
|
|
disable();
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 20:46:47 -07:00
|
|
|
/**
|
|
|
|
|
* Return true if PIDController is enabled.
|
|
|
|
|
*
|
|
|
|
|
* @return True if PIDController is enabled.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
public boolean isEnabled() {
|
2017-11-24 00:55:35 -08:00
|
|
|
m_thisMutex.lock();
|
|
|
|
|
try {
|
|
|
|
|
return m_enabled;
|
|
|
|
|
} finally {
|
|
|
|
|
m_thisMutex.unlock();
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Reset the previous error, the integral term, and disable the controller. */
|
2016-05-20 12:07:40 -04:00
|
|
|
@Override
|
2017-12-04 23:28:33 -08:00
|
|
|
public void reset() {
|
2015-06-25 15:07:55 -04:00
|
|
|
disable();
|
2017-11-24 00:55:35 -08:00
|
|
|
|
2018-05-16 19:51:37 -07:00
|
|
|
super.reset();
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2017-12-04 23:28:33 -08:00
|
|
|
public void initSendable(SendableBuilder builder) {
|
2018-05-16 19:51:37 -07:00
|
|
|
super.initSendable(builder);
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.addBooleanProperty("enabled", this::isEnabled, this::setEnabled);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|