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
|
|
|
|
|
2015-08-11 03:28:41 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2012-01-29 01:21:43 -04:00
|
|
|
#include "AC_PID.h"
|
|
|
|
|
2015-10-25 14:03:46 -03:00
|
|
|
const AP_Param::GroupInfo AC_PID::var_info[] = {
|
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),
|
2015-02-11 08:06:04 -04:00
|
|
|
|
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),
|
2015-02-11 08:06:04 -04:00
|
|
|
|
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),
|
2015-02-11 08:06:04 -04:00
|
|
|
|
|
|
|
// 3 was for uint16 IMAX
|
|
|
|
// 4 is used by TradHeli for FF
|
|
|
|
|
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
|
2015-02-11 08:06:04 -04:00
|
|
|
AP_GROUPINFO("IMAX", 5, AC_PID, _imax, 0),
|
|
|
|
|
|
|
|
// @Param: FILT_HZ
|
|
|
|
// @DisplayName: PID Input filter frequency in Hz
|
|
|
|
// @Description: Input filter frequency in Hz
|
|
|
|
// @Unit: Hz
|
|
|
|
AP_GROUPINFO("FILT_HZ", 6, AC_PID, _filt_hz, AC_PID_FILT_HZ_DEFAULT),
|
|
|
|
|
2012-08-17 02:37:26 -03:00
|
|
|
AP_GROUPEND
|
2012-02-12 07:25:50 -04:00
|
|
|
};
|
2012-01-29 01:21:43 -04:00
|
|
|
|
2015-02-11 08:06:04 -04:00
|
|
|
// Constructor
|
|
|
|
AC_PID::AC_PID(float initial_p, float initial_i, float initial_d, float initial_imax, float initial_filt_hz, float dt) :
|
|
|
|
_dt(dt),
|
|
|
|
_integrator(0.0f),
|
|
|
|
_input(0.0f),
|
|
|
|
_derivative(0.0f)
|
|
|
|
{
|
|
|
|
// load parameter values from eeprom
|
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
|
|
|
|
|
|
|
_kp = initial_p;
|
|
|
|
_ki = initial_i;
|
|
|
|
_kd = initial_d;
|
2015-05-08 15:42:13 -03:00
|
|
|
_imax = fabsf(initial_imax);
|
2015-03-11 02:16:26 -03:00
|
|
|
filt_hz(initial_filt_hz);
|
2015-02-11 08:06:04 -04:00
|
|
|
|
|
|
|
// reset input filter to first value received
|
|
|
|
_flags._reset_filter = true;
|
2015-05-21 22:43:43 -03:00
|
|
|
|
|
|
|
memset(&_pid_info, 0, sizeof(_pid_info));
|
2015-02-11 08:06:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set_dt - set time step in seconds
|
|
|
|
void AC_PID::set_dt(float dt)
|
|
|
|
{
|
|
|
|
// set dt and calculate the input filter alpha
|
|
|
|
_dt = dt;
|
|
|
|
}
|
|
|
|
|
2015-03-11 02:16:26 -03:00
|
|
|
// filt_hz - set input filter hz
|
|
|
|
void AC_PID::filt_hz(float hz)
|
2015-02-11 08:06:04 -04:00
|
|
|
{
|
2015-05-08 15:42:13 -03:00
|
|
|
_filt_hz.set(fabsf(hz));
|
2015-03-11 02:16:26 -03:00
|
|
|
|
|
|
|
// sanity check _filt_hz
|
|
|
|
_filt_hz = max(_filt_hz, AC_PID_FILT_HZ_MIN);
|
2015-02-11 08:06:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set_input_filter_all - set input to PID controller
|
|
|
|
// input is filtered before the PID controllers are run
|
|
|
|
// this should be called before any other calls to get_p, get_i or get_d
|
|
|
|
void AC_PID::set_input_filter_all(float input)
|
|
|
|
{
|
2015-04-09 00:06:07 -03:00
|
|
|
// don't process inf or NaN
|
|
|
|
if (!isfinite(input)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-29 07:02:46 -03:00
|
|
|
|
2015-04-09 00:06:07 -03:00
|
|
|
// reset input filter to value received
|
|
|
|
if (_flags._reset_filter) {
|
|
|
|
_flags._reset_filter = false;
|
|
|
|
_input = input;
|
|
|
|
_derivative = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update filter and calculate derivative
|
2015-03-27 21:19:24 -03:00
|
|
|
float input_filt_change = get_filt_alpha() * (input - _input);
|
2015-04-09 00:06:07 -03:00
|
|
|
_input = _input + input_filt_change;
|
|
|
|
if (_dt > 0.0f) {
|
|
|
|
_derivative = input_filt_change / _dt;
|
2015-02-11 08:06:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set_input_filter_d - set input to PID controller
|
|
|
|
// only input to the D portion of the controller is filtered
|
|
|
|
// this should be called before any other calls to get_p, get_i or get_d
|
|
|
|
void AC_PID::set_input_filter_d(float input)
|
2012-01-29 01:21:43 -04:00
|
|
|
{
|
2015-04-09 00:06:07 -03:00
|
|
|
// don't process inf or NaN
|
|
|
|
if (!isfinite(input)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-11 08:06:04 -04:00
|
|
|
// reset input filter to value received
|
|
|
|
if (_flags._reset_filter) {
|
|
|
|
_flags._reset_filter = false;
|
|
|
|
_derivative = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update filter and calculate derivative
|
|
|
|
if (_dt > 0.0f) {
|
|
|
|
float derivative = (input - _input) / _dt;
|
2015-03-27 21:19:24 -03:00
|
|
|
_derivative = _derivative + get_filt_alpha() * (derivative-_derivative);
|
2015-02-11 08:06:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_input = input;
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|
|
|
|
|
2015-05-21 22:43:43 -03:00
|
|
|
float AC_PID::get_p()
|
2012-01-29 01:21:43 -04:00
|
|
|
{
|
2015-05-21 22:43:43 -03:00
|
|
|
_pid_info.P = (_input * _kp);
|
|
|
|
return _pid_info.P;
|
2015-02-11 08:06:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
float AC_PID::get_i()
|
|
|
|
{
|
2015-05-04 23:34:48 -03:00
|
|
|
if(!is_zero(_ki) && !is_zero(_dt)) {
|
2015-02-11 08:06:04 -04:00
|
|
|
_integrator += ((float)_input * _ki) * _dt;
|
2012-08-17 02:37:26 -03:00
|
|
|
if (_integrator < -_imax) {
|
|
|
|
_integrator = -_imax;
|
|
|
|
} else if (_integrator > _imax) {
|
|
|
|
_integrator = _imax;
|
|
|
|
}
|
2015-05-21 22:43:43 -03:00
|
|
|
_pid_info.I = _integrator;
|
2012-08-17 02:37:26 -03:00
|
|
|
return _integrator;
|
|
|
|
}
|
|
|
|
return 0;
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|
|
|
|
|
2015-05-21 22:43:43 -03:00
|
|
|
float AC_PID::get_d()
|
2012-01-29 01:21:43 -04:00
|
|
|
{
|
2015-05-21 22:43:43 -03:00
|
|
|
// derivative component
|
|
|
|
_pid_info.D = (_kd * _derivative);
|
|
|
|
return _pid_info.D;
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|
|
|
|
|
2015-02-11 08:06:04 -04:00
|
|
|
float AC_PID::get_pi()
|
2012-01-29 01:21:43 -04:00
|
|
|
{
|
2015-02-11 08:06:04 -04:00
|
|
|
return get_p() + get_i();
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|
|
|
|
|
2015-02-11 08:06:04 -04:00
|
|
|
float AC_PID::get_pid()
|
2012-01-29 01:21:43 -04:00
|
|
|
{
|
2015-02-11 08:06:04 -04:00
|
|
|
return get_p() + get_i() + get_d();
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|
|
|
|
|
2014-02-14 02:52:24 -04:00
|
|
|
void AC_PID::reset_I()
|
2012-01-29 01:21:43 -04:00
|
|
|
{
|
2012-08-17 02:37:26 -03:00
|
|
|
_integrator = 0;
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|
|
|
|
|
2014-02-14 02:52:24 -04:00
|
|
|
void AC_PID::load_gains()
|
2012-01-29 01:21:43 -04:00
|
|
|
{
|
2012-08-17 02:37:26 -03:00
|
|
|
_kp.load();
|
|
|
|
_ki.load();
|
|
|
|
_kd.load();
|
|
|
|
_imax.load();
|
2015-05-08 15:42:13 -03:00
|
|
|
_imax = fabsf(_imax);
|
2015-02-11 08:06:04 -04:00
|
|
|
_filt_hz.load();
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|
|
|
|
|
2015-02-11 08:06:04 -04:00
|
|
|
// save_gains - save gains to eeprom
|
2014-02-14 02:52:24 -04:00
|
|
|
void AC_PID::save_gains()
|
2012-01-29 01:21:43 -04:00
|
|
|
{
|
2012-08-17 02:37:26 -03:00
|
|
|
_kp.save();
|
|
|
|
_ki.save();
|
|
|
|
_kd.save();
|
|
|
|
_imax.save();
|
2015-02-11 08:06:04 -04:00
|
|
|
_filt_hz.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Overload the function call operator to permit easy initialisation
|
|
|
|
void AC_PID::operator() (float p, float i, float d, float imaxval, float input_filt_hz, float dt)
|
|
|
|
{
|
|
|
|
_kp = p;
|
|
|
|
_ki = i;
|
|
|
|
_kd = d;
|
2015-05-08 15:42:13 -03:00
|
|
|
_imax = fabsf(imaxval);
|
2015-02-11 08:06:04 -04:00
|
|
|
_filt_hz = input_filt_hz;
|
|
|
|
_dt = dt;
|
2012-01-29 01:21:43 -04:00
|
|
|
}
|
2014-04-17 18:03:53 -03:00
|
|
|
|
2015-02-11 08:06:04 -04:00
|
|
|
// calc_filt_alpha - recalculate the input filter alpha
|
2015-03-27 21:19:24 -03:00
|
|
|
float AC_PID::get_filt_alpha() const
|
2015-03-11 02:16:26 -03:00
|
|
|
{
|
2015-05-04 23:34:48 -03:00
|
|
|
if (is_zero(_filt_hz)) {
|
2015-03-27 21:19:24 -03:00
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
|
2014-04-17 18:03:53 -03:00
|
|
|
// calculate alpha
|
2015-04-28 02:33:39 -03:00
|
|
|
float rc = 1/(M_2PI_F*_filt_hz);
|
2015-03-27 21:19:24 -03:00
|
|
|
return _dt / (_dt + rc);
|
2014-04-17 18:03:53 -03:00
|
|
|
}
|