2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-23 22:06:13 -07:00
|
|
|
/* Copyright (c) 2008-2017 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "PIDController.h"
|
2016-07-14 13:51:32 -07:00
|
|
|
|
|
|
|
|
#include <cmath>
|
2014-10-24 15:30:54 -04:00
|
|
|
#include <vector>
|
2016-07-14 13:51:32 -07:00
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <HAL/HAL.h>
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "Notifier.h"
|
|
|
|
|
#include "PIDOutput.h"
|
|
|
|
|
#include "PIDSource.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2015-06-30 15:01:20 -04:00
|
|
|
static const std::string kP = "p";
|
|
|
|
|
static const std::string kI = "i";
|
|
|
|
|
static const std::string kD = "d";
|
|
|
|
|
static const std::string kF = "f";
|
|
|
|
|
static const std::string kSetpoint = "setpoint";
|
|
|
|
|
static const std::string kEnabled = "enabled";
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* 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
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param source The PIDSource object that is used to get values
|
|
|
|
|
* @param output The PIDOutput object that is set to the output value
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param period the loop time for doing calculations. This particularly
|
|
|
|
|
* effects calculations of the integral and differental terms.
|
|
|
|
|
* The default is 50ms.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
PIDController::PIDController(double Kp, double Ki, double Kd, PIDSource* source,
|
|
|
|
|
PIDOutput* output, double period)
|
|
|
|
|
: PIDController(Kp, Ki, Kd, 0.0, source, output, period) {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* 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
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param source The PIDSource object that is used to get values
|
|
|
|
|
* @param output The PIDOutput object that is set to the output value
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param period the loop time for doing calculations. This particularly
|
|
|
|
|
* effects calculations of the integral and differental terms.
|
|
|
|
|
* The default is 50ms.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
PIDController::PIDController(double Kp, double Ki, double Kd, double Kf,
|
2016-05-20 17:30:37 -07:00
|
|
|
PIDSource* source, PIDOutput* output,
|
2016-11-20 07:25:03 -08:00
|
|
|
double period) {
|
2015-12-29 10:58:11 -08:00
|
|
|
m_controlLoop = std::make_unique<Notifier>(&PIDController::Calculate, this);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
m_P = Kp;
|
|
|
|
|
m_I = Ki;
|
|
|
|
|
m_D = Kd;
|
|
|
|
|
m_F = Kf;
|
2014-08-05 11:48:47 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
m_pidInput = source;
|
|
|
|
|
m_pidOutput = output;
|
|
|
|
|
m_period = period;
|
2014-08-05 11:48:47 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
m_controlLoop->StartPeriodic(m_period);
|
2015-08-26 03:19:35 -07:00
|
|
|
m_setpointTimer.Start();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
static int instances = 0;
|
2015-06-25 15:07:55 -04:00
|
|
|
instances++;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_PIDController, instances);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-08-13 23:17:19 -07:00
|
|
|
PIDController::~PIDController() {
|
2016-05-24 22:16:21 -07:00
|
|
|
// forcefully stopping the notifier so the callback can successfully run.
|
2016-05-22 23:24:10 -07:00
|
|
|
m_controlLoop->Stop();
|
2017-09-02 00:17:43 -07:00
|
|
|
RemoveListeners();
|
2015-08-13 23:17:19 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/**
|
|
|
|
|
* Read the input, calculate the output accordingly, and write to the output.
|
2015-12-29 10:58:11 -08:00
|
|
|
* This should only be called by the Notifier.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
void PIDController::Calculate() {
|
|
|
|
|
bool enabled;
|
2016-05-20 17:30:37 -07:00
|
|
|
PIDSource* pidInput;
|
|
|
|
|
PIDOutput* pidOutput;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2015-06-25 01:54:20 -07:00
|
|
|
{
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
pidInput = m_pidInput;
|
|
|
|
|
pidOutput = m_pidOutput;
|
|
|
|
|
enabled = m_enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-23 04:49:51 -07:00
|
|
|
if (pidInput == nullptr) return;
|
|
|
|
|
if (pidOutput == nullptr) return;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
if (enabled) {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2016-11-20 07:25:03 -08:00
|
|
|
double input = pidInput->PIDGet();
|
|
|
|
|
double result;
|
2016-05-20 17:30:37 -07:00
|
|
|
PIDOutput* pidOutput;
|
2015-09-01 16:47:57 -07:00
|
|
|
|
2016-07-15 13:44:04 -07:00
|
|
|
m_error = GetContinuousError(m_setpoint - input);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2015-09-01 16:47:57 -07:00
|
|
|
if (m_pidInput->GetPIDSourceType() == PIDSourceType::kRate) {
|
|
|
|
|
if (m_P != 0) {
|
|
|
|
|
double potentialPGain = (m_totalError + m_error) * m_P;
|
|
|
|
|
if (potentialPGain < m_maximumOutput) {
|
|
|
|
|
if (potentialPGain > m_minimumOutput)
|
|
|
|
|
m_totalError += m_error;
|
|
|
|
|
else
|
|
|
|
|
m_totalError = m_minimumOutput / m_P;
|
|
|
|
|
} else {
|
|
|
|
|
m_totalError = m_maximumOutput / m_P;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 03:19:35 -07:00
|
|
|
m_result = m_D * m_error + m_P * m_totalError + CalculateFeedForward();
|
2016-05-20 17:30:37 -07:00
|
|
|
} else {
|
2015-09-01 16:47:57 -07:00
|
|
|
if (m_I != 0) {
|
|
|
|
|
double potentialIGain = (m_totalError + m_error) * m_I;
|
|
|
|
|
if (potentialIGain < m_maximumOutput) {
|
|
|
|
|
if (potentialIGain > m_minimumOutput)
|
|
|
|
|
m_totalError += m_error;
|
|
|
|
|
else
|
|
|
|
|
m_totalError = m_minimumOutput / m_I;
|
|
|
|
|
} else {
|
|
|
|
|
m_totalError = m_maximumOutput / m_I;
|
|
|
|
|
}
|
2015-07-14 20:37:52 -07:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2015-09-01 16:47:57 -07:00
|
|
|
m_result = m_P * m_error + m_I * m_totalError +
|
2015-08-26 03:19:35 -07:00
|
|
|
m_D * (m_error - m_prevError) + CalculateFeedForward();
|
2015-09-01 16:47:57 -07:00
|
|
|
}
|
2015-08-26 03:19:35 -07:00
|
|
|
m_prevError = m_error;
|
2015-09-01 16:47:57 -07:00
|
|
|
|
|
|
|
|
if (m_result > m_maximumOutput)
|
|
|
|
|
m_result = m_maximumOutput;
|
|
|
|
|
else if (m_result < m_minimumOutput)
|
|
|
|
|
m_result = m_minimumOutput;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2015-09-01 16:47:57 -07:00
|
|
|
pidOutput = m_pidOutput;
|
|
|
|
|
result = m_result;
|
2014-08-05 11:48:47 -04:00
|
|
|
|
2015-09-01 16:47:57 -07:00
|
|
|
pidOutput->PIDWrite(result);
|
2015-07-16 14:45:07 -04:00
|
|
|
|
2015-09-01 16:47:57 -07:00
|
|
|
// Update the buffer.
|
2016-08-13 23:49:31 -07:00
|
|
|
m_buf.push(m_error);
|
|
|
|
|
m_bufTotal += m_error;
|
2015-09-01 16:47:57 -07:00
|
|
|
// Remove old elements when buffer is full.
|
|
|
|
|
if (m_buf.size() > m_bufLength) {
|
|
|
|
|
m_bufTotal -= m_buf.front();
|
|
|
|
|
m_buf.pop();
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-08-26 03:19:35 -07:00
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Calculate the feed forward term.
|
2015-08-26 03:19:35 -07:00
|
|
|
*
|
|
|
|
|
* Both of the provided feed forward calculations are velocity feed forwards.
|
|
|
|
|
* If a different feed forward calculation is desired, the user can override
|
2016-05-20 17:30:37 -07:00
|
|
|
* this function and provide his or her own. This function does no
|
2015-08-26 03:19:35 -07:00
|
|
|
* synchronization because the PIDController class only calls it in synchronized
|
|
|
|
|
* code, so be careful if calling it oneself.
|
|
|
|
|
*
|
|
|
|
|
* If a velocity PID controller is being used, the F term should be set to 1
|
|
|
|
|
* over the maximum setpoint for the output. If a position PID controller is
|
|
|
|
|
* being used, the F term should be set to 1 over the maximum speed for the
|
|
|
|
|
* output measured in setpoint units per this controller's update period (see
|
|
|
|
|
* the default period in this class's constructor).
|
|
|
|
|
*/
|
|
|
|
|
double PIDController::CalculateFeedForward() {
|
|
|
|
|
if (m_pidInput->GetPIDSourceType() == PIDSourceType::kRate) {
|
|
|
|
|
return m_F * GetSetpoint();
|
2016-05-20 17:30:37 -07:00
|
|
|
} else {
|
2015-08-26 03:19:35 -07:00
|
|
|
double temp = m_F * GetDeltaSetpoint();
|
|
|
|
|
m_prevSetpoint = m_setpoint;
|
|
|
|
|
m_setpointTimer.Reset();
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Set the PID Controller gain parameters.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* Set the proportional, integral, and differential coefficients.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param p Proportional coefficient
|
|
|
|
|
* @param i Integral coefficient
|
|
|
|
|
* @param d Differential coefficient
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void PIDController::SetPID(double p, double i, double d) {
|
2015-06-25 01:54:20 -07:00
|
|
|
{
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_P = p;
|
|
|
|
|
m_I = i;
|
|
|
|
|
m_D = d;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_pEntry) m_pEntry.SetDouble(m_P);
|
|
|
|
|
if (m_iEntry) m_iEntry.SetDouble(m_I);
|
|
|
|
|
if (m_dEntry) m_dEntry.SetDouble(m_D);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the PID Controller gain parameters.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* Set the proportional, integral, and differential coefficients.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param p Proportional coefficient
|
|
|
|
|
* @param i Integral coefficient
|
|
|
|
|
* @param d Differential coefficient
|
|
|
|
|
* @param f Feed forward coefficient
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void PIDController::SetPID(double p, double i, double d, double f) {
|
2015-06-25 01:54:20 -07:00
|
|
|
{
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_P = p;
|
|
|
|
|
m_I = i;
|
|
|
|
|
m_D = d;
|
|
|
|
|
m_F = f;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_pEntry) m_pEntry.SetDouble(m_P);
|
|
|
|
|
if (m_iEntry) m_iEntry.SetDouble(m_I);
|
|
|
|
|
if (m_dEntry) m_dEntry.SetDouble(m_D);
|
|
|
|
|
if (m_fEntry) m_fEntry.SetDouble(m_F);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Get the Proportional coefficient.
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return proportional coefficient
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double PIDController::GetP() const {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-06-25 01:54:20 -07:00
|
|
|
return m_P;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Get the Integral coefficient.
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return integral coefficient
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double PIDController::GetI() const {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-06-25 01:54:20 -07:00
|
|
|
return m_I;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Get the Differential coefficient.
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return differential coefficient
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double PIDController::GetD() const {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-06-25 01:54:20 -07:00
|
|
|
return m_D;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Get the Feed forward coefficient.
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return Feed forward coefficient
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double PIDController::GetF() const {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-06-25 01:54:20 -07:00
|
|
|
return m_F;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Return the current PID result.
|
|
|
|
|
*
|
|
|
|
|
* This is always centered on zero and constrained the the max and min outs.
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return the latest calculated output
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double PIDController::Get() const {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-06-25 01:54:20 -07:00
|
|
|
return m_result;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Set the PID controller to consider the input to be continuous,
|
|
|
|
|
*
|
|
|
|
|
* Rather then using the max and min in as constraints, it considers them to
|
|
|
|
|
* be the same point and automatically calculates the shortest route to
|
|
|
|
|
* the setpoint.
|
|
|
|
|
*
|
|
|
|
|
* @param continuous true turns on continuous, false turns off continuous
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void PIDController::SetContinuous(bool continuous) {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-06-25 01:54:20 -07:00
|
|
|
m_continuous = continuous;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the maximum and minimum values expected from the input.
|
2014-08-05 11:48:47 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param minimumInput the minimum value expected from the input
|
|
|
|
|
* @param maximumInput the maximum value expected from the output
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
void PIDController::SetInputRange(double minimumInput, double maximumInput) {
|
2015-06-25 01:54:20 -07:00
|
|
|
{
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_minimumInput = minimumInput;
|
|
|
|
|
m_maximumInput = maximumInput;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetSetpoint(m_setpoint);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the minimum and maximum values to write.
|
2014-08-05 11:48:47 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param minimumOutput the minimum value to write to the output
|
|
|
|
|
* @param maximumOutput the maximum value to write to the output
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
void PIDController::SetOutputRange(double minimumOutput, double maximumOutput) {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2016-05-26 12:59:34 -07:00
|
|
|
m_minimumOutput = minimumOutput;
|
|
|
|
|
m_maximumOutput = maximumOutput;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Set the setpoint for the PIDController.
|
|
|
|
|
*
|
2016-08-13 23:49:31 -07:00
|
|
|
* Clears the queue for GetAvgError().
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param setpoint the desired setpoint
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
void PIDController::SetSetpoint(double setpoint) {
|
2015-06-25 01:54:20 -07:00
|
|
|
{
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-08-26 03:19:35 -07:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
if (m_maximumInput > m_minimumInput) {
|
|
|
|
|
if (setpoint > m_maximumInput)
|
|
|
|
|
m_setpoint = m_maximumInput;
|
|
|
|
|
else if (setpoint < m_minimumInput)
|
|
|
|
|
m_setpoint = m_minimumInput;
|
|
|
|
|
else
|
|
|
|
|
m_setpoint = setpoint;
|
|
|
|
|
} else {
|
|
|
|
|
m_setpoint = setpoint;
|
|
|
|
|
}
|
2016-08-13 23:49:31 -07:00
|
|
|
|
|
|
|
|
// Clear m_buf.
|
|
|
|
|
m_buf = std::queue<double>();
|
|
|
|
|
m_bufTotal = 0;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_setpointEntry) m_setpointEntry.SetDouble(m_setpoint);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Returns the current setpoint of the PIDController.
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return the current setpoint
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double PIDController::GetSetpoint() const {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-06-25 01:54:20 -07:00
|
|
|
return m_setpoint;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-08-26 03:19:35 -07:00
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Returns the change in setpoint over time of the PIDController.
|
|
|
|
|
*
|
2015-08-26 03:19:35 -07:00
|
|
|
* @return the change in setpoint over time
|
|
|
|
|
*/
|
|
|
|
|
double PIDController::GetDeltaSetpoint() const {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-08-26 03:19:35 -07:00
|
|
|
return (m_setpoint - m_prevSetpoint) / m_setpointTimer.Get();
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Returns the current difference of the input from the setpoint.
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return the current error
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double PIDController::GetError() const {
|
2016-07-15 13:44:04 -07:00
|
|
|
double setpoint = GetSetpoint();
|
2015-06-25 01:54:20 -07:00
|
|
|
{
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2016-07-15 13:44:04 -07:00
|
|
|
return GetContinuousError(setpoint - m_pidInput->PIDGet());
|
2015-06-25 01:54:20 -07:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-07-14 20:37:52 -07:00
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Sets what type of input the PID controller will use.
|
2015-07-14 20:37:52 -07:00
|
|
|
*/
|
|
|
|
|
void PIDController::SetPIDSourceType(PIDSourceType pidSource) {
|
|
|
|
|
m_pidInput->SetPIDSourceType(pidSource);
|
|
|
|
|
}
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Returns the type of input the PID controller is using.
|
|
|
|
|
*
|
2015-07-14 20:37:52 -07:00
|
|
|
* @return the PID controller input type
|
|
|
|
|
*/
|
|
|
|
|
PIDSourceType PIDController::GetPIDSourceType() const {
|
|
|
|
|
return m_pidInput->GetPIDSourceType();
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-16 14:45:07 -04:00
|
|
|
/**
|
|
|
|
|
* Returns the current average of the error over the past few iterations.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2015-07-16 14:45:07 -04:00
|
|
|
* You can specify the number of iterations to average with SetToleranceBuffer()
|
|
|
|
|
* (defaults to 1). This is the same value that is used for OnTarget().
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2015-07-16 14:45:07 -04:00
|
|
|
* @return the average error
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double PIDController::GetAvgError() const {
|
|
|
|
|
double avgError = 0;
|
2015-07-16 14:45:07 -04:00
|
|
|
{
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-07-16 14:45:07 -04:00
|
|
|
// Don't divide by zero.
|
2016-08-13 23:49:31 -07:00
|
|
|
if (m_buf.size()) avgError = m_bufTotal / m_buf.size();
|
2015-07-16 14:45:07 -04:00
|
|
|
}
|
|
|
|
|
return avgError;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/*
|
|
|
|
|
* Set the percentage error which is considered tolerable for use with
|
|
|
|
|
* OnTarget.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param percentage error which is tolerable
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
void PIDController::SetTolerance(double percent) {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2016-05-26 12:59:34 -07:00
|
|
|
m_toleranceType = kPercentTolerance;
|
|
|
|
|
m_tolerance = percent;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2016-07-14 13:51:32 -07:00
|
|
|
* Set the absolute error which is considered tolerable for use with
|
2013-12-15 18:30:16 -05:00
|
|
|
* OnTarget.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param percentage error which is tolerable
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
void PIDController::SetAbsoluteTolerance(double absTolerance) {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2016-07-14 13:51:32 -07:00
|
|
|
m_toleranceType = kAbsoluteTolerance;
|
|
|
|
|
m_tolerance = absTolerance;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2016-07-14 13:51:32 -07:00
|
|
|
* Set the percentage error which is considered tolerable for use with
|
2013-12-15 18:30:16 -05:00
|
|
|
* OnTarget.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param percentage error which is tolerable
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
void PIDController::SetPercentTolerance(double percent) {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2016-07-14 13:51:32 -07:00
|
|
|
m_toleranceType = kPercentTolerance;
|
|
|
|
|
m_tolerance = percent;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-07-16 14:45:07 -04:00
|
|
|
/*
|
|
|
|
|
* Set the number of previous error samples to average for tolerancing. When
|
|
|
|
|
* determining whether a mechanism is on target, the user may want to use a
|
|
|
|
|
* rolling average of previous measurements instead of a precise position or
|
|
|
|
|
* velocity. This is useful for noisy sensors which return a few erroneous
|
|
|
|
|
* measurements when the mechanism is on target. However, the mechanism will
|
|
|
|
|
* not register as on target for at least the specified bufLength cycles.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2015-07-16 14:45:07 -04:00
|
|
|
* @param bufLength Number of previous cycles to average. Defaults to 1.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
void PIDController::SetToleranceBuffer(int bufLength) {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-07-16 14:45:07 -04:00
|
|
|
m_bufLength = bufLength;
|
|
|
|
|
|
|
|
|
|
// Cut the buffer down to size if needed.
|
2016-09-06 00:01:45 -07:00
|
|
|
while (m_buf.size() > static_cast<uint32_t>(bufLength)) {
|
2015-07-16 14:45:07 -04:00
|
|
|
m_bufTotal -= m_buf.front();
|
|
|
|
|
m_buf.pop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/*
|
|
|
|
|
* Return true if the error is within the percentage of the total input range,
|
|
|
|
|
* determined by SetTolerance. This asssumes that the maximum and minimum input
|
|
|
|
|
* were set using SetInput.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* Currently this just reports on target as the actual value passes through the
|
2016-05-20 17:30:37 -07:00
|
|
|
* setpoint. Ideally it should be based on being within the tolerance for some
|
|
|
|
|
* period of time.
|
|
|
|
|
*
|
2016-01-07 10:19:14 -05:00
|
|
|
* This will return false until at least one input value has been computed.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool PIDController::OnTarget() const {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2016-01-07 10:19:14 -05:00
|
|
|
if (m_buf.size() == 0) return false;
|
2015-07-16 14:45:07 -04:00
|
|
|
double error = GetAvgError();
|
2015-06-25 01:54:20 -07:00
|
|
|
switch (m_toleranceType) {
|
|
|
|
|
case kPercentTolerance:
|
2016-07-14 13:51:32 -07:00
|
|
|
return std::fabs(error) <
|
2016-05-20 17:30:37 -07:00
|
|
|
m_tolerance / 100 * (m_maximumInput - m_minimumInput);
|
2015-06-25 01:54:20 -07:00
|
|
|
break;
|
|
|
|
|
case kAbsoluteTolerance:
|
2016-07-14 13:51:32 -07:00
|
|
|
return std::fabs(error) < m_tolerance;
|
2015-06-25 01:54:20 -07:00
|
|
|
break;
|
|
|
|
|
case kNoTolerance:
|
2015-06-25 15:07:55 -04:00
|
|
|
// TODO: this case needs an error
|
2015-06-25 01:54:20 -07:00
|
|
|
return false;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2015-06-25 01:54:20 -07:00
|
|
|
return false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Begin running the PIDController.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void PIDController::Enable() {
|
2015-06-25 01:54:20 -07:00
|
|
|
{
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-06-25 01:54:20 -07:00
|
|
|
m_enabled = true;
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_enabledEntry) m_enabledEntry.SetBoolean(true);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stop running the PIDController, this sets the output to zero before stopping.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void PIDController::Disable() {
|
2015-06-25 01:54:20 -07:00
|
|
|
{
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_pidOutput->PIDWrite(0);
|
|
|
|
|
m_enabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_enabledEntry) m_enabledEntry.SetBoolean(false);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return true if PIDController is enabled.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool PIDController::IsEnabled() const {
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-06-25 01:54:20 -07:00
|
|
|
return m_enabled;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Reset the previous error, the integral term, and disable the controller.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void PIDController::Reset() {
|
|
|
|
|
Disable();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
2015-08-26 03:19:35 -07:00
|
|
|
m_prevError = 0;
|
2015-06-25 01:54:20 -07:00
|
|
|
m_totalError = 0;
|
|
|
|
|
m_result = 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
std::string PIDController::GetSmartDashboardType() const {
|
|
|
|
|
return "PIDController";
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
void PIDController::InitTable(std::shared_ptr<nt::NetworkTable> subtable) {
|
|
|
|
|
RemoveListeners();
|
2016-07-10 17:47:44 -07:00
|
|
|
m_table = subtable;
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_table) {
|
|
|
|
|
m_pEntry = m_table->GetEntry(kP);
|
|
|
|
|
m_pEntry.SetDouble(GetP());
|
|
|
|
|
m_iEntry = m_table->GetEntry(kI);
|
|
|
|
|
m_iEntry.SetDouble(GetI());
|
|
|
|
|
m_dEntry = m_table->GetEntry(kD);
|
|
|
|
|
m_dEntry.SetDouble(GetD());
|
|
|
|
|
m_fEntry = m_table->GetEntry(kF);
|
|
|
|
|
m_fEntry.SetDouble(GetF());
|
|
|
|
|
m_setpointEntry = m_table->GetEntry(kSetpoint);
|
|
|
|
|
m_setpointEntry.SetDouble(GetSetpoint());
|
|
|
|
|
m_enabledEntry = m_table->GetEntry(kEnabled);
|
|
|
|
|
m_enabledEntry.SetBoolean(IsEnabled());
|
|
|
|
|
|
|
|
|
|
m_pListener = m_pEntry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsDouble()) return;
|
|
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
|
|
|
|
m_P = event.value->GetDouble();
|
|
|
|
|
},
|
|
|
|
|
NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
|
|
|
|
|
m_iListener = m_iEntry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsDouble()) return;
|
|
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
|
|
|
|
m_I = event.value->GetDouble();
|
|
|
|
|
},
|
|
|
|
|
NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
|
|
|
|
|
m_dListener = m_dEntry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsDouble()) return;
|
|
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
|
|
|
|
m_D = event.value->GetDouble();
|
|
|
|
|
},
|
|
|
|
|
NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
|
|
|
|
|
m_fListener = m_fEntry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsDouble()) return;
|
|
|
|
|
std::lock_guard<hal::priority_recursive_mutex> sync(m_mutex);
|
|
|
|
|
m_F = event.value->GetDouble();
|
|
|
|
|
},
|
|
|
|
|
NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
|
|
|
|
|
m_setpointListener = m_setpointEntry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsDouble()) return;
|
|
|
|
|
SetSetpoint(event.value->GetDouble());
|
|
|
|
|
},
|
|
|
|
|
NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
|
|
|
|
|
m_enabledListener = m_enabledEntry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsBoolean()) return;
|
|
|
|
|
if (event.value->GetBoolean()) {
|
|
|
|
|
Enable();
|
|
|
|
|
} else {
|
|
|
|
|
Disable();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-15 13:44:04 -07:00
|
|
|
/**
|
|
|
|
|
* Wraps error around for continuous inputs. The original error is returned if
|
|
|
|
|
* continuous mode is disabled. This is an unsynchronized function.
|
|
|
|
|
*
|
|
|
|
|
* @param error The current error of the PID controller.
|
|
|
|
|
* @return Error for continuous inputs.
|
|
|
|
|
*/
|
|
|
|
|
double PIDController::GetContinuousError(double error) const {
|
2017-07-10 23:31:20 -04:00
|
|
|
if (m_continuous &&
|
|
|
|
|
std::fabs(error) > (m_maximumInput - m_minimumInput) / 2) {
|
|
|
|
|
if (error > 0) {
|
|
|
|
|
return error - (m_maximumInput - m_minimumInput);
|
|
|
|
|
} else {
|
|
|
|
|
return error + (m_maximumInput - m_minimumInput);
|
2016-07-15 13:44:04 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
std::shared_ptr<nt::NetworkTable> PIDController::GetTable() const {
|
|
|
|
|
return m_table;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIDController::UpdateTable() {}
|
|
|
|
|
|
|
|
|
|
void PIDController::StartLiveWindowMode() { Disable(); }
|
|
|
|
|
|
|
|
|
|
void PIDController::StopLiveWindowMode() {}
|
2017-09-02 00:17:43 -07:00
|
|
|
|
|
|
|
|
void PIDController::RemoveListeners() {
|
|
|
|
|
if (m_pListener != 0) {
|
|
|
|
|
m_pEntry.RemoveListener(m_pListener);
|
|
|
|
|
m_pListener = 0;
|
|
|
|
|
}
|
|
|
|
|
if (m_iListener != 0) {
|
|
|
|
|
m_iEntry.RemoveListener(m_iListener);
|
|
|
|
|
m_iListener = 0;
|
|
|
|
|
}
|
|
|
|
|
if (m_dListener != 0) {
|
|
|
|
|
m_dEntry.RemoveListener(m_dListener);
|
|
|
|
|
m_dListener = 0;
|
|
|
|
|
}
|
|
|
|
|
if (m_fListener != 0) {
|
|
|
|
|
m_fEntry.RemoveListener(m_fListener);
|
|
|
|
|
m_fListener = 0;
|
|
|
|
|
}
|
|
|
|
|
if (m_setpointListener != 0) {
|
|
|
|
|
m_setpointEntry.RemoveListener(m_setpointListener);
|
|
|
|
|
m_setpointListener = 0;
|
|
|
|
|
}
|
|
|
|
|
if (m_enabledListener != 0) {
|
|
|
|
|
m_enabledEntry.RemoveListener(m_enabledListener);
|
|
|
|
|
m_enabledListener = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|