2020-10-31 14:58:34 -07:00
|
|
|
#include "pid.h"
|
|
|
|
|
|
|
|
float Pid::GetOutput(float setpoint, float observation)
|
|
|
|
{
|
|
|
|
float error = setpoint - observation;
|
|
|
|
|
|
|
|
// Integrate error
|
2020-12-11 15:24:56 -08:00
|
|
|
m_integrator += error * m_period * m_ki;
|
|
|
|
|
2021-07-09 11:16:46 -07:00
|
|
|
// Differentiate error
|
|
|
|
float errorDelta = error - m_lastError;
|
|
|
|
float dEdt = errorDelta / m_period;
|
|
|
|
m_lastError = error;
|
|
|
|
|
2020-12-11 15:24:56 -08:00
|
|
|
// Clamp to +- 1
|
|
|
|
if (m_integrator > m_clamp) m_integrator = m_clamp;
|
|
|
|
if (m_integrator < -m_clamp) m_integrator = -m_clamp;
|
2020-10-31 14:58:34 -07:00
|
|
|
|
|
|
|
// Multiply by gains and sum
|
2021-07-09 11:16:46 -07:00
|
|
|
return m_kp * error + m_integrator + m_kd * dEdt;
|
2020-10-31 14:58:34 -07:00
|
|
|
}
|