2024-01-20 13:19:09 +08:00
|
|
|
|
#include "pid.h"
|
|
|
|
|
#include "board.h"
|
|
|
|
|
#include "print.h"
|
|
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
|
PID::PID(float *_input, float *_feedback, float _kp, float _ki, float _kd, unsigned short _max_output)
|
|
|
|
|
: kp(_kp), ki(_ki), kd(_kd), max_output(_max_output * 1.0), input(_input), feedback(_feedback)
|
2024-01-20 13:19:09 +08:00
|
|
|
|
{
|
2024-01-24 22:08:32 +08:00
|
|
|
|
Clear();
|
2024-01-20 13:19:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
|
void PID::Clear()
|
2024-01-20 13:19:09 +08:00
|
|
|
|
{
|
2024-01-24 22:08:32 +08:00
|
|
|
|
err = integra = derivative = preErr = 0;
|
2024-01-20 13:19:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
|
void PID::Update(float _kp, float _ki, float _kd, unsigned short _maxOutput)
|
2024-01-20 13:19:09 +08:00
|
|
|
|
{
|
2024-01-24 22:08:32 +08:00
|
|
|
|
kp = _kp;
|
|
|
|
|
ki = _ki;
|
|
|
|
|
kd = _kd;
|
|
|
|
|
max_output = _maxOutput;
|
2024-01-20 13:19:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
|
short PID::Compute(float interval)
|
2024-01-20 13:19:09 +08:00
|
|
|
|
{
|
2024-01-24 22:08:32 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD>ֵ<EFBFBD>IJ<EFBFBD>ֵ
|
|
|
|
|
err = *input - *feedback;
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
integra = integra + err * interval;
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD>
|
|
|
|
|
derivative = (err - preErr) / interval;
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
preErr = err;
|
2024-01-20 13:19:09 +08:00
|
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
|
if (ki != 0)
|
|
|
|
|
{
|
|
|
|
|
#if PID_DEBUG_OUTPUT
|
|
|
|
|
log("integra=%ld max_output=%ld %ld", long(integra * 1000), long(-(max_output / ki * 1000)), long(max_output / ki * 1000));
|
|
|
|
|
#endif
|
|
|
|
|
if (integra < -(max_output / ki))
|
|
|
|
|
{
|
|
|
|
|
#if PID_DEBUG_OUTPUT
|
|
|
|
|
log("integra clear-");
|
|
|
|
|
#endif
|
|
|
|
|
integra = -(max_output / ki);
|
|
|
|
|
}
|
|
|
|
|
if (integra > max_output / ki)
|
|
|
|
|
{
|
|
|
|
|
#if PID_DEBUG_OUTPUT
|
|
|
|
|
log("integra clear+");
|
|
|
|
|
#endif
|
|
|
|
|
integra = max_output / ki;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-20 13:19:09 +08:00
|
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>PIDֵ
|
|
|
|
|
float val = err * kp + integra * ki + derivative * kd;
|
2024-01-20 13:19:09 +08:00
|
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΧԼ<CEA7><D4BC>
|
|
|
|
|
if (val < -max_output)
|
|
|
|
|
val = -max_output + 1;
|
|
|
|
|
else if (val > max_output)
|
|
|
|
|
val = max_output - 1;
|
2024-01-20 13:19:09 +08:00
|
|
|
|
|
|
|
|
|
#if PID_DEBUG_OUTPUT
|
2024-01-24 22:08:32 +08:00
|
|
|
|
log("error=%ld integra=%ld derivative=%ld val=%ld", long(error * 1000), long(integra * 1000), long(derivative * 1000), long(val * 1000));
|
2024-01-20 13:19:09 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
|
return val;
|
2024-01-20 13:19:09 +08:00
|
|
|
|
}
|