2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2008-2018 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
|
|
|
|
2017-11-13 22:28:55 -08:00
|
|
|
#include <algorithm>
|
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"
|
2017-12-04 23:28:33 -08:00
|
|
|
#include "SmartDashboard/SendableBuilder.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2017-11-13 22:28:55 -08:00
|
|
|
template <class T>
|
|
|
|
|
constexpr const T& clamp(const T& value, const T& low, const T& high) {
|
|
|
|
|
return std::max(low, std::min(value, high));
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
2017-12-04 23:28:33 -08:00
|
|
|
double period)
|
|
|
|
|
: SendableBase(false) {
|
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
|
|
|
|
2017-11-19 15:58:30 -08:00
|
|
|
// Save original source
|
|
|
|
|
m_origSource = std::shared_ptr<PIDSource>(source, NullDeleter<PIDSource>());
|
|
|
|
|
|
|
|
|
|
// Create LinearDigitalFilter with original source as its source argument
|
|
|
|
|
m_filter = LinearDigitalFilter::MovingAverage(m_origSource, 1);
|
|
|
|
|
m_pidInput = &m_filter;
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
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);
|
2017-12-04 23:28:33 -08:00
|
|
|
SetName("PIDController", instances);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2017-11-19 19:06:00 -08: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
|
|
|
|
|
* @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. This particularly
|
|
|
|
|
* effects calculations of the integral and differental terms.
|
|
|
|
|
* The default is 50ms.
|
|
|
|
|
*/
|
|
|
|
|
PIDController::PIDController(double Kp, double Ki, double Kd, PIDSource& source,
|
|
|
|
|
PIDOutput& output, double period)
|
|
|
|
|
: PIDController(Kp, Ki, Kd, 0.0, &source, &output, period) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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. This particularly
|
|
|
|
|
* effects calculations of the integral and differental terms.
|
|
|
|
|
* The default is 50ms.
|
|
|
|
|
*/
|
|
|
|
|
PIDController::PIDController(double Kp, double Ki, double Kd, double Kf,
|
|
|
|
|
PIDSource& source, PIDOutput& output,
|
|
|
|
|
double period)
|
|
|
|
|
: PIDController(Kp, Ki, Kd, Kf, &source, &output, period) {}
|
|
|
|
|
|
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();
|
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() {
|
Reduced scope of PIDController's critical sections
m_pidInput and m_pidOutput are considered constant after their construction.
Setting the input range, output range, tolerance, or continuous mode locks the
controller. m_error and m_result are held in temp variables and set at the end
of the calculation.
Getters of P, I, D, F, m_error, m_setpoint, m_result, and m_enabled lock the
critical section. P, I, D, F, and m_setpoint are retrieved at the beginning of
Calculate().
Fixes #40.
2017-11-23 01:16:17 -08:00
|
|
|
if (m_origSource == nullptr || m_pidOutput == nullptr) return;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
Reduced scope of PIDController's critical sections
m_pidInput and m_pidOutput are considered constant after their construction.
Setting the input range, output range, tolerance, or continuous mode locks the
controller. m_error and m_result are held in temp variables and set at the end
of the calculation.
Getters of P, I, D, F, m_error, m_setpoint, m_result, and m_enabled lock the
critical section. P, I, D, F, and m_setpoint are retrieved at the beginning of
Calculate().
Fixes #40.
2017-11-23 01:16:17 -08:00
|
|
|
bool enabled;
|
2015-06-25 01:54:20 -07:00
|
|
|
{
|
2017-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
enabled = m_enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (enabled) {
|
Reduced scope of PIDController's critical sections
m_pidInput and m_pidOutput are considered constant after their construction.
Setting the input range, output range, tolerance, or continuous mode locks the
controller. m_error and m_result are held in temp variables and set at the end
of the calculation.
Getters of P, I, D, F, m_error, m_setpoint, m_result, and m_enabled lock the
critical section. P, I, D, F, and m_setpoint are retrieved at the beginning of
Calculate().
Fixes #40.
2017-11-23 01:16:17 -08:00
|
|
|
double input;
|
|
|
|
|
|
|
|
|
|
// Storage for function inputs
|
|
|
|
|
PIDSourceType pidSourceType;
|
|
|
|
|
double P;
|
|
|
|
|
double I;
|
|
|
|
|
double D;
|
2017-09-28 23:32:35 -07:00
|
|
|
double feedForward = CalculateFeedForward();
|
Reduced scope of PIDController's critical sections
m_pidInput and m_pidOutput are considered constant after their construction.
Setting the input range, output range, tolerance, or continuous mode locks the
controller. m_error and m_result are held in temp variables and set at the end
of the calculation.
Getters of P, I, D, F, m_error, m_setpoint, m_result, and m_enabled lock the
critical section. P, I, D, F, and m_setpoint are retrieved at the beginning of
Calculate().
Fixes #40.
2017-11-23 01:16:17 -08:00
|
|
|
double minimumOutput;
|
|
|
|
|
double maximumOutput;
|
2017-09-28 23:32:35 -07:00
|
|
|
|
Reduced scope of PIDController's critical sections
m_pidInput and m_pidOutput are considered constant after their construction.
Setting the input range, output range, tolerance, or continuous mode locks the
controller. m_error and m_result are held in temp variables and set at the end
of the calculation.
Getters of P, I, D, F, m_error, m_setpoint, m_result, and m_enabled lock the
critical section. P, I, D, F, and m_setpoint are retrieved at the beginning of
Calculate().
Fixes #40.
2017-11-23 01:16:17 -08:00
|
|
|
// Storage for function input-outputs
|
|
|
|
|
double prevError;
|
|
|
|
|
double error;
|
|
|
|
|
double totalError;
|
|
|
|
|
|
|
|
|
|
{
|
2017-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
Reduced scope of PIDController's critical sections
m_pidInput and m_pidOutput are considered constant after their construction.
Setting the input range, output range, tolerance, or continuous mode locks the
controller. m_error and m_result are held in temp variables and set at the end
of the calculation.
Getters of P, I, D, F, m_error, m_setpoint, m_result, and m_enabled lock the
critical section. P, I, D, F, and m_setpoint are retrieved at the beginning of
Calculate().
Fixes #40.
2017-11-23 01:16:17 -08:00
|
|
|
|
|
|
|
|
input = m_pidInput->PIDGet();
|
2015-09-01 16:47:57 -07:00
|
|
|
|
Reduced scope of PIDController's critical sections
m_pidInput and m_pidOutput are considered constant after their construction.
Setting the input range, output range, tolerance, or continuous mode locks the
controller. m_error and m_result are held in temp variables and set at the end
of the calculation.
Getters of P, I, D, F, m_error, m_setpoint, m_result, and m_enabled lock the
critical section. P, I, D, F, and m_setpoint are retrieved at the beginning of
Calculate().
Fixes #40.
2017-11-23 01:16:17 -08:00
|
|
|
pidSourceType = m_pidInput->GetPIDSourceType();
|
|
|
|
|
P = m_P;
|
|
|
|
|
I = m_I;
|
|
|
|
|
D = m_D;
|
|
|
|
|
minimumOutput = m_minimumOutput;
|
|
|
|
|
maximumOutput = m_maximumOutput;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
Reduced scope of PIDController's critical sections
m_pidInput and m_pidOutput are considered constant after their construction.
Setting the input range, output range, tolerance, or continuous mode locks the
controller. m_error and m_result are held in temp variables and set at the end
of the calculation.
Getters of P, I, D, F, m_error, m_setpoint, m_result, and m_enabled lock the
critical section. P, I, D, F, and m_setpoint are retrieved at the beginning of
Calculate().
Fixes #40.
2017-11-23 01:16:17 -08:00
|
|
|
prevError = m_prevError;
|
|
|
|
|
error = GetContinuousError(m_setpoint - input);
|
|
|
|
|
totalError = m_totalError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Storage for function outputs
|
|
|
|
|
double result;
|
|
|
|
|
|
|
|
|
|
if (pidSourceType == PIDSourceType::kRate) {
|
|
|
|
|
if (P != 0) {
|
|
|
|
|
totalError =
|
|
|
|
|
clamp(totalError + error, minimumOutput / P, maximumOutput / P);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
Reduced scope of PIDController's critical sections
m_pidInput and m_pidOutput are considered constant after their construction.
Setting the input range, output range, tolerance, or continuous mode locks the
controller. m_error and m_result are held in temp variables and set at the end
of the calculation.
Getters of P, I, D, F, m_error, m_setpoint, m_result, and m_enabled lock the
critical section. P, I, D, F, and m_setpoint are retrieved at the beginning of
Calculate().
Fixes #40.
2017-11-23 01:16:17 -08:00
|
|
|
result = D * error + P * totalError + feedForward;
|
2016-05-20 17:30:37 -07:00
|
|
|
} else {
|
Reduced scope of PIDController's critical sections
m_pidInput and m_pidOutput are considered constant after their construction.
Setting the input range, output range, tolerance, or continuous mode locks the
controller. m_error and m_result are held in temp variables and set at the end
of the calculation.
Getters of P, I, D, F, m_error, m_setpoint, m_result, and m_enabled lock the
critical section. P, I, D, F, and m_setpoint are retrieved at the beginning of
Calculate().
Fixes #40.
2017-11-23 01:16:17 -08:00
|
|
|
if (I != 0) {
|
|
|
|
|
totalError =
|
|
|
|
|
clamp(totalError + error, minimumOutput / I, maximumOutput / I);
|
2015-07-14 20:37:52 -07:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
Reduced scope of PIDController's critical sections
m_pidInput and m_pidOutput are considered constant after their construction.
Setting the input range, output range, tolerance, or continuous mode locks the
controller. m_error and m_result are held in temp variables and set at the end
of the calculation.
Getters of P, I, D, F, m_error, m_setpoint, m_result, and m_enabled lock the
critical section. P, I, D, F, and m_setpoint are retrieved at the beginning of
Calculate().
Fixes #40.
2017-11-23 01:16:17 -08:00
|
|
|
result =
|
|
|
|
|
P * error + I * totalError + D * (error - prevError) + feedForward;
|
2015-09-01 16:47:57 -07:00
|
|
|
}
|
|
|
|
|
|
Reduced scope of PIDController's critical sections
m_pidInput and m_pidOutput are considered constant after their construction.
Setting the input range, output range, tolerance, or continuous mode locks the
controller. m_error and m_result are held in temp variables and set at the end
of the calculation.
Getters of P, I, D, F, m_error, m_setpoint, m_result, and m_enabled lock the
critical section. P, I, D, F, and m_setpoint are retrieved at the beginning of
Calculate().
Fixes #40.
2017-11-23 01:16:17 -08:00
|
|
|
result = clamp(result, minimumOutput, maximumOutput);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2017-11-24 00:55:35 -08:00
|
|
|
{
|
|
|
|
|
// Ensures m_enabled check and PIDWrite() call occur atomically
|
|
|
|
|
std::lock_guard<wpi::mutex> pidWriteLock(m_pidWriteMutex);
|
|
|
|
|
std::unique_lock<wpi::mutex> mainLock(m_thisMutex);
|
|
|
|
|
if (m_enabled) {
|
|
|
|
|
// Don't block other PIDController operations on PIDWrite()
|
|
|
|
|
mainLock.unlock();
|
|
|
|
|
|
|
|
|
|
m_pidOutput->PIDWrite(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-08-05 11:48:47 -04:00
|
|
|
|
2017-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
Reduced scope of PIDController's critical sections
m_pidInput and m_pidOutput are considered constant after their construction.
Setting the input range, output range, tolerance, or continuous mode locks the
controller. m_error and m_result are held in temp variables and set at the end
of the calculation.
Getters of P, I, D, F, m_error, m_setpoint, m_result, and m_enabled lock the
critical section. P, I, D, F, and m_setpoint are retrieved at the beginning of
Calculate().
Fixes #40.
2017-11-23 01:16:17 -08:00
|
|
|
m_prevError = m_error;
|
|
|
|
|
m_error = error;
|
|
|
|
|
m_totalError = totalError;
|
|
|
|
|
m_result = result;
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_P = p;
|
|
|
|
|
m_I = i;
|
|
|
|
|
m_D = 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) {
|
2017-12-04 23:28:33 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
|
|
|
|
m_P = p;
|
|
|
|
|
m_I = i;
|
|
|
|
|
m_D = d;
|
|
|
|
|
m_F = f;
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
/**
|
|
|
|
|
* Set the Proportional coefficient of the PID controller gain.
|
|
|
|
|
*
|
|
|
|
|
* @param p proportional coefficient
|
|
|
|
|
*/
|
|
|
|
|
void PIDController::SetP(double p) {
|
|
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
|
|
|
|
m_P = p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the Integral coefficient of the PID controller gain.
|
|
|
|
|
*
|
|
|
|
|
* @param i integral coefficient
|
|
|
|
|
*/
|
|
|
|
|
void PIDController::SetI(double i) {
|
|
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
|
|
|
|
m_I = i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the Differential coefficient of the PID controller gain.
|
|
|
|
|
*
|
|
|
|
|
* @param d differential coefficient
|
|
|
|
|
*/
|
|
|
|
|
void PIDController::SetD(double d) {
|
|
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
|
|
|
|
m_D = d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the Feed forward coefficient of the PID controller gain.
|
|
|
|
|
*
|
|
|
|
|
* @param f Feed forward coefficient
|
|
|
|
|
*/
|
|
|
|
|
void PIDController::SetF(double f) {
|
|
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
|
|
|
|
m_F = 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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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,
|
|
|
|
|
*
|
2018-01-11 21:06:25 -08:00
|
|
|
* Rather then using the max and min input range as constraints, it considers
|
|
|
|
|
* them to be the same point and automatically calculates the shortest route to
|
2016-05-20 17:30:37 -07:00
|
|
|
* 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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_minimumInput = minimumInput;
|
|
|
|
|
m_maximumInput = maximumInput;
|
2017-11-22 21:56:35 -05:00
|
|
|
m_inputRange = maximumInput - minimumInput;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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.
|
|
|
|
|
*
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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
|
|
|
}
|
|
|
|
|
|
2017-11-19 18:23:48 -08:00
|
|
|
/**
|
|
|
|
|
* Returns the current average of the error over the past few iterations.
|
|
|
|
|
*
|
|
|
|
|
* You can specify the number of iterations to average with SetToleranceBuffer()
|
|
|
|
|
* (defaults to 1). This is the same value that is used for OnTarget().
|
|
|
|
|
*
|
|
|
|
|
* @return the average error
|
|
|
|
|
*/
|
|
|
|
|
double PIDController::GetAvgError() const { return GetError(); }
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
2015-07-16 14:45:07 -04:00
|
|
|
|
2017-11-19 15:58:30 -08:00
|
|
|
// Create LinearDigitalFilter with original source as its source argument
|
|
|
|
|
m_filter = LinearDigitalFilter::MovingAverage(m_origSource, bufLength);
|
|
|
|
|
m_pidInput = &m_filter;
|
2015-07-16 14:45:07 -04:00
|
|
|
}
|
|
|
|
|
|
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-11-19 15:58:30 -08:00
|
|
|
double error = GetError();
|
2017-09-28 23:32:35 -07:00
|
|
|
|
2017-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
2015-06-25 01:54:20 -07:00
|
|
|
switch (m_toleranceType) {
|
|
|
|
|
case kPercentTolerance:
|
2017-11-22 21:56:35 -05:00
|
|
|
return std::fabs(error) < m_tolerance / 100 * m_inputRange;
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
2015-06-25 01:54:20 -07:00
|
|
|
m_enabled = 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-11-24 00:55:35 -08:00
|
|
|
// Ensures m_enabled modification and PIDWrite() call occur atomically
|
|
|
|
|
std::lock_guard<wpi::mutex> pidWriteLock(m_pidWriteMutex);
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<wpi::mutex> mainLock(m_thisMutex);
|
|
|
|
|
m_enabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
m_pidOutput->PIDWrite(0);
|
|
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
/**
|
|
|
|
|
* Set the enabled state of the PIDController.
|
|
|
|
|
*/
|
|
|
|
|
void PIDController::SetEnabled(bool enable) {
|
|
|
|
|
if (enable) {
|
|
|
|
|
Enable();
|
|
|
|
|
} else {
|
|
|
|
|
Disable();
|
|
|
|
|
}
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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-11-24 00:55:35 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
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
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void PIDController::InitSendable(SendableBuilder& builder) {
|
|
|
|
|
builder.SetSmartDashboardType("PIDController");
|
|
|
|
|
builder.SetSafeState([=]() { Reset(); });
|
|
|
|
|
builder.AddDoubleProperty("p", [=]() { return GetP(); },
|
2017-12-10 20:58:29 -08:00
|
|
|
[=](double value) { SetP(value); });
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.AddDoubleProperty("i", [=]() { return GetI(); },
|
2017-12-10 20:58:29 -08:00
|
|
|
[=](double value) { SetI(value); });
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.AddDoubleProperty("d", [=]() { return GetD(); },
|
2017-12-10 20:58:29 -08:00
|
|
|
[=](double value) { SetD(value); });
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.AddDoubleProperty("f", [=]() { return GetF(); },
|
2017-12-10 20:58:29 -08:00
|
|
|
[=](double value) { SetF(value); });
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.AddDoubleProperty("setpoint", [=]() { return GetSetpoint(); },
|
2017-12-10 20:58:29 -08:00
|
|
|
[=](double value) { SetSetpoint(value); });
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.AddBooleanProperty("enabled", [=]() { return IsEnabled(); },
|
|
|
|
|
[=](bool value) { SetEnabled(value); });
|
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 {
|
2018-01-11 21:06:25 -08:00
|
|
|
if (m_continuous && m_inputRange != 0) {
|
2017-11-26 00:15:33 -05:00
|
|
|
error = std::fmod(error, m_inputRange);
|
|
|
|
|
if (std::fabs(error) > m_inputRange / 2) {
|
|
|
|
|
if (error > 0) {
|
|
|
|
|
return error - m_inputRange;
|
|
|
|
|
} else {
|
|
|
|
|
return error + m_inputRange;
|
|
|
|
|
}
|
2016-07-15 13:44:04 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return error;
|
|
|
|
|
}
|