2018-05-16 19:51:37 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2020-06-27 20:39:00 -07:00
|
|
|
/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */
|
2018-05-16 19:51:37 -07:00
|
|
|
/* 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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/PIDBase.h"
|
2018-05-16 19:51:37 -07:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
2018-05-16 19:51:37 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/PIDOutput.h"
|
|
|
|
|
#include "frc/smartdashboard/SendableBuilder.h"
|
2019-09-14 15:22:54 -05:00
|
|
|
#include "frc/smartdashboard/SendableRegistry.h"
|
2018-05-16 19:51:37 -07:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
|
constexpr const T& clamp(const T& value, const T& low, const T& high) {
|
|
|
|
|
return std::max(low, std::min(value, high));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PIDBase::PIDBase(double Kp, double Ki, double Kd, PIDSource& source,
|
|
|
|
|
PIDOutput& output)
|
|
|
|
|
: PIDBase(Kp, Ki, Kd, 0.0, source, output) {}
|
|
|
|
|
|
|
|
|
|
PIDBase::PIDBase(double Kp, double Ki, double Kd, double Kf, PIDSource& source,
|
2019-09-14 15:22:54 -05:00
|
|
|
PIDOutput& output) {
|
2018-05-16 19:51:37 -07:00
|
|
|
m_P = Kp;
|
|
|
|
|
m_I = Ki;
|
|
|
|
|
m_D = Kd;
|
|
|
|
|
m_F = Kf;
|
|
|
|
|
|
2019-06-28 13:35:57 -07:00
|
|
|
m_pidInput = &source;
|
2019-12-01 02:12:02 -05:00
|
|
|
m_filter = LinearFilter<double>::MovingAverage(1);
|
2018-05-16 19:51:37 -07:00
|
|
|
|
|
|
|
|
m_pidOutput = &output;
|
|
|
|
|
|
|
|
|
|
m_setpointTimer.Start();
|
|
|
|
|
|
|
|
|
|
static int instances = 0;
|
|
|
|
|
instances++;
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_PIDController, instances);
|
2019-09-14 15:22:54 -05:00
|
|
|
SendableRegistry::GetInstance().Add(this, "PIDController", instances);
|
2018-05-16 19:51:37 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
double PIDBase::Get() const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-31 20:47:15 -07:00
|
|
|
return m_result;
|
|
|
|
|
}
|
2018-05-16 19:51:37 -07:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void PIDBase::SetContinuous(bool continuous) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-31 20:47:15 -07:00
|
|
|
m_continuous = continuous;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIDBase::SetInputRange(double minimumInput, double maximumInput) {
|
2018-05-16 19:51:37 -07:00
|
|
|
{
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-31 20:47:15 -07:00
|
|
|
m_minimumInput = minimumInput;
|
|
|
|
|
m_maximumInput = maximumInput;
|
|
|
|
|
m_inputRange = maximumInput - minimumInput;
|
2018-05-16 19:51:37 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
SetSetpoint(m_setpoint);
|
2018-05-16 19:51:37 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void PIDBase::SetOutputRange(double minimumOutput, double maximumOutput) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-31 20:47:15 -07:00
|
|
|
m_minimumOutput = minimumOutput;
|
|
|
|
|
m_maximumOutput = maximumOutput;
|
2018-05-16 19:51:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIDBase::SetPID(double p, double i, double d) {
|
|
|
|
|
{
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
m_P = p;
|
|
|
|
|
m_I = i;
|
|
|
|
|
m_D = d;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIDBase::SetPID(double p, double i, double d, double f) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
m_P = p;
|
|
|
|
|
m_I = i;
|
|
|
|
|
m_D = d;
|
|
|
|
|
m_F = f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIDBase::SetP(double p) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
m_P = p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIDBase::SetI(double i) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
m_I = i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIDBase::SetD(double d) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
m_D = d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIDBase::SetF(double f) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
m_F = f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double PIDBase::GetP() const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
return m_P;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double PIDBase::GetI() const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
return m_I;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double PIDBase::GetD() const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
return m_D;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double PIDBase::GetF() const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
return m_F;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIDBase::SetSetpoint(double setpoint) {
|
|
|
|
|
{
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07: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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double PIDBase::GetSetpoint() const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
return m_setpoint;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double PIDBase::GetDeltaSetpoint() const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
return (m_setpoint - m_prevSetpoint) / m_setpointTimer.Get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double PIDBase::GetError() const {
|
|
|
|
|
double setpoint = GetSetpoint();
|
|
|
|
|
{
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
return GetContinuousError(setpoint - m_pidInput->PIDGet());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double PIDBase::GetAvgError() const { return GetError(); }
|
|
|
|
|
|
|
|
|
|
void PIDBase::SetPIDSourceType(PIDSourceType pidSource) {
|
|
|
|
|
m_pidInput->SetPIDSourceType(pidSource);
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2018-05-16 19:51:37 -07:00
|
|
|
PIDSourceType PIDBase::GetPIDSourceType() const {
|
|
|
|
|
return m_pidInput->GetPIDSourceType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIDBase::SetTolerance(double percent) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
m_toleranceType = kPercentTolerance;
|
|
|
|
|
m_tolerance = percent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIDBase::SetAbsoluteTolerance(double absTolerance) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
m_toleranceType = kAbsoluteTolerance;
|
|
|
|
|
m_tolerance = absTolerance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIDBase::SetPercentTolerance(double percent) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
m_toleranceType = kPercentTolerance;
|
|
|
|
|
m_tolerance = percent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIDBase::SetToleranceBuffer(int bufLength) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2019-12-01 02:12:02 -05:00
|
|
|
m_filter = LinearFilter<double>::MovingAverage(bufLength);
|
2018-05-16 19:51:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PIDBase::OnTarget() const {
|
|
|
|
|
double error = GetError();
|
|
|
|
|
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
switch (m_toleranceType) {
|
|
|
|
|
case kPercentTolerance:
|
|
|
|
|
return std::fabs(error) < m_tolerance / 100 * m_inputRange;
|
|
|
|
|
break;
|
|
|
|
|
case kAbsoluteTolerance:
|
|
|
|
|
return std::fabs(error) < m_tolerance;
|
|
|
|
|
break;
|
|
|
|
|
case kNoTolerance:
|
|
|
|
|
// TODO: this case needs an error
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIDBase::Reset() {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-16 19:51:37 -07:00
|
|
|
m_prevError = 0;
|
|
|
|
|
m_totalError = 0;
|
|
|
|
|
m_result = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-19 01:32:43 -07:00
|
|
|
void PIDBase::PIDWrite(double output) { SetSetpoint(output); }
|
|
|
|
|
|
2018-05-16 19:51:37 -07:00
|
|
|
void PIDBase::InitSendable(SendableBuilder& builder) {
|
2018-12-07 22:40:31 -05:00
|
|
|
builder.SetSmartDashboardType("PIDController");
|
2018-05-16 19:51:37 -07:00
|
|
|
builder.SetSafeState([=]() { Reset(); });
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddDoubleProperty(
|
|
|
|
|
"p", [=]() { return GetP(); }, [=](double value) { SetP(value); });
|
|
|
|
|
builder.AddDoubleProperty(
|
|
|
|
|
"i", [=]() { return GetI(); }, [=](double value) { SetI(value); });
|
|
|
|
|
builder.AddDoubleProperty(
|
|
|
|
|
"d", [=]() { return GetD(); }, [=](double value) { SetD(value); });
|
|
|
|
|
builder.AddDoubleProperty(
|
|
|
|
|
"f", [=]() { return GetF(); }, [=](double value) { SetF(value); });
|
|
|
|
|
builder.AddDoubleProperty(
|
|
|
|
|
"setpoint", [=]() { return GetSetpoint(); },
|
|
|
|
|
[=](double value) { SetSetpoint(value); });
|
2018-05-16 19:51:37 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void PIDBase::Calculate() {
|
2019-06-28 13:35:57 -07:00
|
|
|
if (m_pidInput == nullptr || m_pidOutput == nullptr) return;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
bool enabled;
|
|
|
|
|
{
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-31 20:47:15 -07:00
|
|
|
enabled = m_enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
|
double input;
|
|
|
|
|
|
|
|
|
|
// Storage for function inputs
|
|
|
|
|
PIDSourceType pidSourceType;
|
|
|
|
|
double P;
|
|
|
|
|
double I;
|
|
|
|
|
double D;
|
|
|
|
|
double feedForward = CalculateFeedForward();
|
|
|
|
|
double minimumOutput;
|
|
|
|
|
double maximumOutput;
|
|
|
|
|
|
|
|
|
|
// Storage for function input-outputs
|
|
|
|
|
double prevError;
|
|
|
|
|
double error;
|
|
|
|
|
double totalError;
|
|
|
|
|
|
|
|
|
|
{
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2019-06-28 13:35:57 -07:00
|
|
|
input = m_filter.Calculate(m_pidInput->PIDGet());
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
pidSourceType = m_pidInput->GetPIDSourceType();
|
|
|
|
|
P = m_P;
|
|
|
|
|
I = m_I;
|
|
|
|
|
D = m_D;
|
|
|
|
|
minimumOutput = m_minimumOutput;
|
|
|
|
|
maximumOutput = m_maximumOutput;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = D * error + P * totalError + feedForward;
|
|
|
|
|
} else {
|
|
|
|
|
if (I != 0) {
|
|
|
|
|
totalError =
|
|
|
|
|
clamp(totalError + error, minimumOutput / I, maximumOutput / I);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result =
|
|
|
|
|
P * error + I * totalError + D * (error - prevError) + feedForward;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = clamp(result, minimumOutput, maximumOutput);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// Ensures m_enabled check and PIDWrite() call occur atomically
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock pidWriteLock(m_pidWriteMutex);
|
2019-07-07 19:17:14 -07:00
|
|
|
std::unique_lock mainLock(m_thisMutex);
|
2018-05-31 20:47:15 -07:00
|
|
|
if (m_enabled) {
|
|
|
|
|
// Don't block other PIDBase operations on PIDWrite()
|
|
|
|
|
mainLock.unlock();
|
|
|
|
|
|
|
|
|
|
m_pidOutput->PIDWrite(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-05-31 20:47:15 -07:00
|
|
|
m_prevError = m_error;
|
|
|
|
|
m_error = error;
|
|
|
|
|
m_totalError = totalError;
|
|
|
|
|
m_result = result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double PIDBase::CalculateFeedForward() {
|
|
|
|
|
if (m_pidInput->GetPIDSourceType() == PIDSourceType::kRate) {
|
|
|
|
|
return m_F * GetSetpoint();
|
|
|
|
|
} else {
|
|
|
|
|
double temp = m_F * GetDeltaSetpoint();
|
|
|
|
|
m_prevSetpoint = m_setpoint;
|
|
|
|
|
m_setpointTimer.Reset();
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-16 19:51:37 -07:00
|
|
|
double PIDBase::GetContinuousError(double error) const {
|
|
|
|
|
if (m_continuous && m_inputRange != 0) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return error;
|
|
|
|
|
}
|