PID controller added with gain scaling
This commit is contained in:
parent
a3a83ea6f9
commit
cd0d81c72a
@ -26,7 +26,7 @@ controllerPD::controllerPD(float kP, float kD, float saturation)
|
|||||||
, m_kD(kD)
|
, m_kD(kD)
|
||||||
, m_saturation(saturation)
|
, m_saturation(saturation)
|
||||||
, m_lastError(0)
|
, m_lastError(0)
|
||||||
, m_pt1FilterApply4(100.0f)
|
, m_pt1FilterApply4(50.0f)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,4 +100,57 @@ float controllerPI::run(float dT, float input, float setPoint)
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*----------------------------------- PID Controller ---------------------------------*/
|
||||||
|
|
||||||
|
controllerPID::controllerPID(float kP, float kI, float kD, float saturation, float iTermSaturation)
|
||||||
|
: m_kP(kP)
|
||||||
|
, m_kI(kI)
|
||||||
|
, m_kD(kD)
|
||||||
|
, m_kPScale(0.0f)
|
||||||
|
, m_kIScale(0.0f)
|
||||||
|
, m_kDScale(0.0f)
|
||||||
|
, m_saturation(saturation)
|
||||||
|
, m_lastError(0.0f)
|
||||||
|
, m_iTermSaturation(iTermSaturation)
|
||||||
|
, m_integrator(0.0f)
|
||||||
|
, m_pt1FilterApply4(100.0f)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
float controllerPID::run(float dT, float input, float setPoint)
|
||||||
|
{
|
||||||
|
float error(setPoint-input);
|
||||||
|
|
||||||
|
float pTerm(m_kPScale * m_kP * error);
|
||||||
|
|
||||||
|
// Accumulate to the integrator
|
||||||
|
float iTerm(
|
||||||
|
constrain(
|
||||||
|
m_integrator + (m_kIScale * m_kI * error * dT),
|
||||||
|
m_iTermSaturation));
|
||||||
|
|
||||||
|
// Store
|
||||||
|
m_integrator = iTerm;
|
||||||
|
|
||||||
|
float dTerm((dT != 0.0f) ? m_kDScale * m_kD * ((error-m_lastError)/dT) : 0.0f);
|
||||||
|
|
||||||
|
// Store error for next iteration
|
||||||
|
m_lastError = error;
|
||||||
|
|
||||||
|
dTerm = m_pt1FilterApply4.filter(dTerm, dT);
|
||||||
|
|
||||||
|
float output(constrain(pTerm + iTerm + dTerm, m_saturation));
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
void controllerPID::setGainScaling(float kPScale, float kIScale, float kDScale)
|
||||||
|
{
|
||||||
|
static float scaleFactor(1.0f / 1000.0f);
|
||||||
|
|
||||||
|
m_kPScale = (1000.0f + kPScale) * scaleFactor;
|
||||||
|
m_kIScale = (1000.0f + kIScale) * scaleFactor;
|
||||||
|
m_kDScale = (1000.0f + kDScale) * scaleFactor;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -60,4 +60,25 @@ private:
|
|||||||
float m_integrator;
|
float m_integrator;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class controllerPID
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
controllerPID(float kP, float kI, float kD, float saturation, float iTermSaturation);
|
||||||
|
|
||||||
|
float run(float dT, float input, float setPoint);
|
||||||
|
|
||||||
|
//! Input values in range [-1000, 1000] will result in scales
|
||||||
|
//! from [0-2].
|
||||||
|
void setGainScaling(float kPScale, float kIScale, float kDScale);
|
||||||
|
|
||||||
|
private:
|
||||||
|
float m_kP, m_kI, m_kD;
|
||||||
|
float m_kPScale, m_kIScale, m_kDScale;
|
||||||
|
float m_saturation;
|
||||||
|
float m_lastError;
|
||||||
|
float m_iTermSaturation;
|
||||||
|
float m_integrator;
|
||||||
|
pt1FilterApply4 m_pt1FilterApply4;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user