clamp pid

This commit is contained in:
Matthew Kennedy 2020-12-11 15:24:56 -08:00
parent a8bdedaef6
commit 94828339f9
4 changed files with 11 additions and 5 deletions

View File

@ -73,7 +73,7 @@ static HeaterState GetNextState(HeaterState state, float sensorEsr)
return state;
}
static Pid heaterPid(0.01f, 0.05f, HEATER_CONTROL_PERIOD);
static Pid heaterPid(0.01f, 0.05f, 1, HEATER_CONTROL_PERIOD);
static float GetDutyForState(HeaterState state, float heaterEsr)
{

View File

@ -5,8 +5,12 @@ float Pid::GetOutput(float setpoint, float observation)
float error = setpoint - observation;
// Integrate error
m_integrator += error * m_period;
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;
// Multiply by gains and sum
return m_kp * error + m_ki * m_integrator;
return m_kp * error + m_integrator;
}

View File

@ -3,10 +3,11 @@
class Pid
{
public:
Pid(float kP, float kI, int periodMs)
Pid(float kP, float kI, float clamp, int periodMs)
: m_period(periodMs / 1000.0f)
, m_kp(kP)
, m_ki(kI)
, m_clamp(clamp)
{
}
@ -16,6 +17,7 @@ private:
const float m_period;
const float m_kp;
const float m_ki;
const float m_clamp;
float m_lastError;
float m_integrator;

View File

@ -8,7 +8,7 @@
#include "ch.h"
// Bosch CJ125 is somewhere VERY ROUGHLY like 200-400A/(v*s) integrator gain
static Pid pumpPid(50.0f, 5000.0f, 2);
static Pid pumpPid(50.0f, 5000.0f, 10, 2);
static THD_WORKING_AREA(waPumpThread, 256);
static void PumpThread(void*)