2013-05-29 20:54:53 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2012-01-29 01:21:43 -04:00
|
|
|
|
|
|
|
/// @file AC_PID.cpp
|
|
|
|
/// @brief Generic PID algorithm
|
|
|
|
|
2013-01-10 15:22:37 -04:00
|
|
|
#include <AP_Math.h>
|
2012-01-29 01:21:43 -04:00
|
|
|
#include "AC_PID.h"
|
|
|
|
|
2013-01-02 07:03:00 -04:00
|
|
|
// Examples for _filter:
|
|
|
|
// f_cut = 10 Hz -> _filter = 15.9155e-3
|
|
|
|
// f_cut = 15 Hz -> _filter = 10.6103e-3
|
|
|
|
// f_cut = 20 Hz -> _filter = 7.9577e-3
|
|
|
|
// f_cut = 25 Hz -> _filter = 6.3662e-3
|
|
|
|
// f_cut = 30 Hz -> _filter = 5.3052e-3
|
|
|
|
const float AC_PID::_filter = 7.9577e-3; // Set to "1 / ( 2 * PI * f_cut )";
|
|
|
|
|
2012-02-12 07:25:50 -04:00
|
|
|
const AP_Param::GroupInfo AC_PID::var_info[] PROGMEM = {
|
2012-12-10 09:28:39 -04:00
|
|
|
// @Param: P
|
|
|
|
// @DisplayName: PID Proportional Gain
|
|
|
|
// @Description: P Gain which produces an output value that is proportional to the current error value
|
2012-08-17 02:37:26 -03:00
|
|
|
AP_GROUPINFO("P", 0, AC_PID, _kp, 0),
|
2012-12-10 09:28:39 -04:00
|
|
|
// @Param: I
|
|
|
|
// @DisplayName: PID Integral Gain
|
|
|
|
// @Description: I Gain which produces an output that is proportional to both the magnitude and the duration of the error
|
2012-08-17 02:37:26 -03:00
|
|
|
AP_GROUPINFO("I", 1, AC_PID, _ki, 0),
|
2012-12-10 09:28:39 -04:00
|
|
|
// @Param: D
|
|
|
|
// @DisplayName: PID Derivative Gain
|
|
|
|
// @Description: D Gain which produces an output that is proportional to the rate of change of the error
|
2012-08-17 02:37:26 -03:00
|
|
|
AP_GROUPINFO("D", 2, AC_PID, _kd, 0),
|
2012-12-10 09:28:39 -04:00
|
|
|
// @Param: IMAX
|
|
|
|
// @DisplayName: PID Integral Maximum
|
|
|
|
// @Description: The maximum/minimum value that the I term can output
|
2012-08-17 02:37:26 -03:00
|
|
|
AP_GROUPINFO("IMAX", 3, AC_PID, _imax, 0),
|
|
|
|
AP_GROUPEND
|
2012-02-12 07:25:50 -04:00
|
|
|
};
|
2012-01-29 01:21:43 -04:00
|
|
|
|
|
|
|
int32_t AC_PID::get_p(int32_t error)
|
|
|
|
{
|
2012-08-17 02:37:26 -03:00
|
|
|
return (float)error * _kp;
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t AC_PID::get_i(int32_t error, float dt)
|
|
|
|
{
|
2012-08-17 02:37:26 -03:00
|
|
|
if((_ki != 0) && (dt != 0)) {
|
|
|
|
_integrator += ((float)error * _ki) * dt;
|
|
|
|
if (_integrator < -_imax) {
|
|
|
|
_integrator = -_imax;
|
|
|
|
} else if (_integrator > _imax) {
|
|
|
|
_integrator = _imax;
|
|
|
|
}
|
|
|
|
return _integrator;
|
|
|
|
}
|
|
|
|
return 0;
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|
|
|
|
|
2012-11-26 22:01:20 -04:00
|
|
|
// This is an integrator which tends to decay to zero naturally
|
|
|
|
// if the error is zero.
|
|
|
|
|
|
|
|
int32_t AC_PID::get_leaky_i(int32_t error, float dt, float leak_rate)
|
|
|
|
{
|
|
|
|
if((_ki != 0) && (dt != 0)){
|
|
|
|
_integrator -= (float)_integrator * leak_rate;
|
|
|
|
_integrator += ((float)error * _ki) * dt;
|
|
|
|
if (_integrator < -_imax) {
|
|
|
|
_integrator = -_imax;
|
|
|
|
} else if (_integrator > _imax) {
|
|
|
|
_integrator = _imax;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _integrator;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-29 01:21:43 -04:00
|
|
|
int32_t AC_PID::get_d(int32_t input, float dt)
|
|
|
|
{
|
2012-08-17 02:37:26 -03:00
|
|
|
if ((_kd != 0) && (dt != 0)) {
|
2012-11-27 03:45:49 -04:00
|
|
|
float derivative;
|
|
|
|
if (isnan(_last_derivative)) {
|
|
|
|
// we've just done a reset, suppress the first derivative
|
|
|
|
// term as we don't want a sudden change in input to cause
|
|
|
|
// a large D output change
|
|
|
|
derivative = 0;
|
|
|
|
_last_derivative = 0;
|
|
|
|
} else {
|
|
|
|
// calculate instantaneous derivative
|
|
|
|
derivative = (input - _last_input) / dt;
|
|
|
|
}
|
2012-08-17 02:37:26 -03:00
|
|
|
|
|
|
|
// discrete low pass filter, cuts out the
|
|
|
|
// high frequency noise that can drive the controller crazy
|
2012-11-27 03:06:22 -04:00
|
|
|
derivative = _last_derivative +
|
|
|
|
(dt / ( _filter + dt)) * (derivative - _last_derivative);
|
2012-08-17 02:37:26 -03:00
|
|
|
|
|
|
|
// update state
|
|
|
|
_last_input = input;
|
2012-11-27 03:06:22 -04:00
|
|
|
_last_derivative = derivative;
|
2012-08-17 02:37:26 -03:00
|
|
|
|
|
|
|
// add in derivative component
|
2012-11-27 03:06:22 -04:00
|
|
|
return _kd * derivative;
|
2012-08-17 02:37:26 -03:00
|
|
|
}
|
|
|
|
return 0;
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t AC_PID::get_pi(int32_t error, float dt)
|
|
|
|
{
|
2012-08-17 02:37:26 -03:00
|
|
|
return get_p(error) + get_i(error, dt);
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32_t AC_PID::get_pid(int32_t error, float dt)
|
|
|
|
{
|
2012-08-17 02:37:26 -03:00
|
|
|
return get_p(error) + get_i(error, dt) + get_d(error, dt);
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AC_PID::reset_I()
|
|
|
|
{
|
2012-08-17 02:37:26 -03:00
|
|
|
_integrator = 0;
|
2012-11-27 03:45:49 -04:00
|
|
|
// mark derivative as invalid
|
|
|
|
_last_derivative = NAN;
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AC_PID::load_gains()
|
|
|
|
{
|
2012-08-17 02:37:26 -03:00
|
|
|
_kp.load();
|
|
|
|
_ki.load();
|
|
|
|
_kd.load();
|
|
|
|
_imax.load();
|
|
|
|
_imax = abs(_imax);
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AC_PID::save_gains()
|
|
|
|
{
|
2012-08-17 02:37:26 -03:00
|
|
|
_kp.save();
|
|
|
|
_ki.save();
|
|
|
|
_kd.save();
|
|
|
|
_imax.save();
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|