GY-63_MS5611/libraries/PID_RT/PID_RT.cpp

150 lines
2.4 KiB
C++
Raw Normal View History

2021-01-29 06:31:58 -05:00
//
2022-11-22 05:48:02 -05:00
// FILE: PID_RT.cpp
2021-01-29 06:31:58 -05:00
// AUTHOR: Rob Tillaart
2022-11-22 05:48:02 -05:00
// VERSION: 0.1.5
2021-01-29 06:31:58 -05:00
// PURPOSE: PID library for Arduino
// URL: https://github.com/RobTillaart/PID
2021-12-23 09:44:14 -05:00
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;
2022-11-22 05:48:02 -05:00
_POI = true; // Proportional On Input - Error
2021-01-29 06:31:58 -05:00
}
bool PID_RT::setK(float Kp, float Ki, float Kd)
{
2022-11-22 05:48:02 -05:00
// prevent short-cut evaluation.
2021-11-12 07:52:09 -05:00
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;
2022-11-22 05:48:02 -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;
2022-11-22 05:48:02 -05:00
// P - proportional on input or proportional on error
2021-01-29 06:31:58 -05:00
if (_POI == false)
{
_output = __Kp * _error;
}
else
{
_output = 0;
_errorSum += __Kp * dI;
}
2022-11-22 05:48:02 -05:00
// I
2021-01-29 06:31:58 -05:00
_errorSum += __Ki * _error;
2021-11-12 07:52:09 -05:00
if (_errorSum > _rangeMax) _errorSum = _rangeMax;
else if (_errorSum < _rangeMin) _errorSum = _rangeMin;
2022-11-22 05:48:02 -05:00
// D
2021-01-29 06:31:58 -05:00
_output += _errorSum + __Kd * dI;
2022-11-22 05:48:02 -05:00
// 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;
2022-11-22 05:48:02 -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
2022-11-22 05:48:02 -05:00
// -- END OF FILE --
2021-12-23 09:44:14 -05:00