wideband/firmware/pid.cpp

17 lines
399 B
C++
Raw Normal View History

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;
// 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
2020-12-11 15:24:56 -08:00
return m_kp * error + m_integrator;
2020-10-31 14:58:34 -07:00
}