2021-01-29 06:31:58 -05:00
|
|
|
//
|
|
|
|
// FILE: PID_RT.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2021-11-12 07:52:09 -05:00
|
|
|
// VERSION: 0.1.3
|
2021-01-29 06:31:58 -05:00
|
|
|
// PURPOSE: PID library for Arduino
|
|
|
|
// URL: https://github.com/RobTillaart/PID
|
|
|
|
//
|
2021-11-12 07:52:09 -05:00
|
|
|
// HISTORY
|
|
|
|
// 2020-12-15 0.1.0 update readme, fix unit test,
|
|
|
|
// fix library.json
|
|
|
|
// add 2nd constructor
|
|
|
|
// 2021-05-27 0.1.1 add json check, add lint check
|
|
|
|
// 2021-05-28 0.1.2 minor edits
|
|
|
|
// 2021-11-12 0.1.3 update build-CI, update readme
|
|
|
|
// renamed variables for readability
|
|
|
|
// added history
|
|
|
|
// fixed bug in setK
|
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
#include "PID_RT.h"
|
|
|
|
|
2021-05-28 07:41:04 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
PID_RT::PID_RT()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PID_RT::PID_RT(float sp, float Kp, float Ki, float Kd)
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
setPoint(sp);
|
|
|
|
setK(Kp, Ki, Kd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PID_RT::reset()
|
|
|
|
{
|
2021-11-12 07:52:09 -05:00
|
|
|
_lastTime = 0;
|
|
|
|
_interval = 250;
|
|
|
|
_errorSum = 0.0;
|
|
|
|
_setPoint = 0.0;
|
|
|
|
_input = 0.0;
|
|
|
|
_lastInput = 0.0;
|
|
|
|
_error = 0.0;
|
|
|
|
_output = 0.0;
|
|
|
|
_rangeMin = 0.0;
|
|
|
|
_rangeMax = 100.0;
|
|
|
|
_Kp = 0.0;
|
|
|
|
_Ki = 0.0;
|
|
|
|
_Kd = 0.0;
|
|
|
|
__Kp = 0.0;
|
|
|
|
__Ki = 0.0;
|
|
|
|
__Kd = 0.0;
|
|
|
|
_reverse = false;
|
|
|
|
_running = false;
|
|
|
|
_POI = true; // Proportional On Input - Error
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PID_RT::setK(float Kp, float Ki, float Kd)
|
|
|
|
{
|
2021-11-12 07:52:09 -05:00
|
|
|
// prevent short-cut evaluation.
|
|
|
|
bool b = setKp(Kp);
|
|
|
|
b = b && setKi(Ki);
|
|
|
|
b = b && setKd(Kd);
|
|
|
|
return b;
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PID_RT::setKp(float Kp)
|
|
|
|
{
|
|
|
|
if (Kp < 0) return false;
|
|
|
|
_Kp = Kp;
|
|
|
|
__Kp = Kp;
|
|
|
|
if (_reverse) __Kp = - __Kp;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool PID_RT::setKi(float Ki)
|
|
|
|
{
|
|
|
|
if (Ki < 0) return false;
|
|
|
|
_Ki = Ki;
|
2021-11-12 07:52:09 -05:00
|
|
|
__Ki = _Ki * _interval * 0.001; // milliseconds.
|
2021-01-29 06:31:58 -05:00
|
|
|
if (_reverse) __Ki = - __Ki;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool PID_RT::setKd(float Kd)
|
|
|
|
{
|
|
|
|
if (Kd < 0) return false;
|
|
|
|
_Kd = Kd;
|
|
|
|
__Kd = Kd / (_interval * 0.001);
|
|
|
|
if (_reverse) __Kd = - __Kd;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool PID_RT::compute(float input)
|
|
|
|
{
|
|
|
|
if (_running == false)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (millis() - _lastTime < _interval)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_lastTime += _interval;
|
|
|
|
|
|
|
|
_input = input;
|
|
|
|
_error = _setPoint - _input;
|
|
|
|
float dI = _lastInput - _input;
|
|
|
|
|
|
|
|
// P - proportional on input or proportional on error
|
|
|
|
if (_POI == false)
|
|
|
|
{
|
|
|
|
_output = __Kp * _error;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_output = 0;
|
|
|
|
_errorSum += __Kp * dI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// I
|
|
|
|
_errorSum += __Ki * _error;
|
2021-11-12 07:52:09 -05:00
|
|
|
if (_errorSum > _rangeMax) _errorSum = _rangeMax;
|
|
|
|
else if (_errorSum < _rangeMin) _errorSum = _rangeMin;
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
// D
|
|
|
|
_output += _errorSum + __Kd * dI;
|
|
|
|
|
|
|
|
// limit output to range
|
2021-11-12 07:52:09 -05:00
|
|
|
if (_output > _rangeMax) _output = _rangeMax;
|
|
|
|
else if (_output < _rangeMin) _output = _rangeMin;
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
_lastInput = _input;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PID_RT::setInterval(uint32_t interval)
|
|
|
|
{
|
|
|
|
if (interval != _interval)
|
|
|
|
{
|
|
|
|
_interval = interval;
|
2021-11-12 07:52:09 -05:00
|
|
|
// recalculate __Ki and __Kd.
|
2021-01-29 06:31:58 -05:00
|
|
|
setKi(_Ki);
|
|
|
|
setKd(_Kd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-28 07:41:04 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
// -- END OF FILE --
|