Path: blob/master/libraries/AC_PID/AC_PID_Basic.cpp
9417 views
/// @file AC_PID_Basic.cpp1/// @brief Lightweight PID controller with error and derivative filtering, integrator limit, and EEPROM gain storage.23#include <AP_Math/AP_Math.h>4#include <AP_InternalError/AP_InternalError.h>5#include "AC_PID_Basic.h"67#define AC_PID_Basic_FILT_E_HZ_MIN 0.01f // minimum input filter frequency8#define AC_PID_Basic_FILT_D_HZ_MIN 0.005f // minimum input filter frequency910const AP_Param::GroupInfo AC_PID_Basic::var_info[] = {11// @Param: P12// @DisplayName: PID Proportional Gain13// @Description: P Gain which produces an output value that is proportional to the current error value14AP_GROUPINFO_FLAGS_DEFAULT_POINTER("P", 0, AC_PID_Basic, _kp, default_kp),1516// @Param: I17// @DisplayName: PID Integral Gain18// @Description: I Gain which produces an output that is proportional to both the magnitude and the duration of the error19AP_GROUPINFO_FLAGS_DEFAULT_POINTER("I", 1, AC_PID_Basic, _ki, default_ki),2021// @Param: IMAX22// @DisplayName: PID Integral Maximum23// @Description: The maximum/minimum value that the I term can output24AP_GROUPINFO_FLAGS_DEFAULT_POINTER("IMAX", 2, AC_PID_Basic, _kimax, default_kimax),2526// @Param: FLTE27// @DisplayName: PID Error filter frequency in Hz28// @Description: Low-pass filter frequency applied to the error (Hz)29// @Units: Hz30AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FLTE", 3, AC_PID_Basic, _filt_E_hz, default_filt_E_hz),3132// @Param: D33// @DisplayName: PID Derivative Gain34// @Description: D Gain which produces an output that is proportional to the rate of change of the error35AP_GROUPINFO_FLAGS_DEFAULT_POINTER("D", 4, AC_PID_Basic, _kd, default_kd),3637// @Param: FLTD38// @DisplayName: D term filter frequency in Hz39// @Description: Low-pass filter frequency applied to the derivative (Hz)40// @Units: Hz41AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FLTD", 5, AC_PID_Basic, _filt_D_hz, default_filt_D_hz),4243// @Param: FF44// @DisplayName: PID Feed Forward Gain45// @Description: FF Gain which produces an output that is proportional to the magnitude of the target46AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FF", 6, AC_PID_Basic, _kff, default_kff),4748AP_GROUPEND49};5051// Constructor52AC_PID_Basic::AC_PID_Basic(float initial_p, float initial_i, float initial_d, float initial_ff, float initial_imax, float initial_filt_E_hz, float initial_filt_D_hz) :53default_kp(initial_p),54default_ki(initial_i),55default_kd(initial_d),56default_kff(initial_ff),57default_kimax(initial_imax),58default_filt_E_hz(initial_filt_E_hz),59default_filt_D_hz(initial_filt_D_hz)60{61// load parameter values from eeprom62AP_Param::setup_object_defaults(this, var_info);6364// reset input filter to first value received65_reset_filter = true;66}6768float AC_PID_Basic::update_all(float target, float measurement, float dt, bool limit)69{70return update_all(target, measurement, dt, (limit && is_negative(_integrator)), (limit && is_positive(_integrator)));71}7273// Computes the PID output using a target and measurement input.74// Applies filters to the error and derivative, then updates the integrator.75// If `limit` is true, the integrator is allowed to shrink but not grow.76float AC_PID_Basic::update_all(float target, float measurement, float dt, bool limit_neg, bool limit_pos)77{78// Return zero if inputs are invalid (NaN or infinite)79if (!isfinite(target) || isnan(target) ||80!isfinite(measurement) || isnan(measurement)) {81INTERNAL_ERROR(AP_InternalError::error_t::invalid_arg_or_result);82return 0.0f;83}8485_target = target;8687// Reset filter state to match current inputs (on first run or after reset)88if (_reset_filter) {89// Reset filters to match the current inputs90_reset_filter = false;91_error = _target - measurement;92_derivative = 0.0f;93} else {94float error_last = _error;95_error += get_filt_E_alpha(dt) * ((_target - measurement) - _error);9697// Compute and low-pass filter the error derivative (D term)98if (is_positive(dt)) {99float derivative = (_error - error_last) / dt;100_derivative += get_filt_D_alpha(dt) * (derivative - _derivative);101}102}103104// update I term105update_i(dt, limit_neg, limit_pos);106107const float P_out = _error * _kp;108const float D_out = _derivative * _kd;109110_pid_info.target = _target;111_pid_info.actual = measurement;112_pid_info.error = _error;113_pid_info.P = _error * _kp;114_pid_info.I = _integrator;115_pid_info.D = _derivative * _kd;116_pid_info.FF = _target * _kff;117118return P_out + _integrator + D_out + _target * _kff;119}120121// Updates the integrator using current error and dt.122// If `limit_neg` is true, integrator may only increase.123// If `limit_pos` is true, integrator may only decrease.124void AC_PID_Basic::update_i(float dt, bool limit_neg, bool limit_pos)125{126if (!is_zero(_ki)) {127// Ensure that integrator can only be reduced if the output is saturated128if (!((limit_neg && is_negative(_error)) || (limit_pos && is_positive(_error)))) {129_integrator += ((float)_error * _ki) * dt;130_integrator = constrain_float(_integrator, -_kimax, _kimax);131}132} else {133_integrator = 0.0f;134}135}136137void AC_PID_Basic::reset_I()138{139_integrator = 0.0;140}141142// Saves controller configuration from EEPROM, including gains and filter frequencies. (not used)143void AC_PID_Basic::save_gains()144{145_kp.save();146_ki.save();147_kd.save();148_kff.save();149_kimax.save();150_filt_E_hz.save();151_filt_D_hz.save();152}153154// Returns alpha value for the error low-pass filter (based on filter frequency and dt)155float AC_PID_Basic::get_filt_E_alpha(float dt) const156{157return calc_lowpass_alpha_dt(dt, _filt_E_hz);158}159160// Returns alpha value for the derivative low-pass filter (based on filter frequency and dt)161float AC_PID_Basic::get_filt_D_alpha(float dt) const162{163return calc_lowpass_alpha_dt(dt, _filt_D_hz);164}165166// Sets integrator based on target, measurement, and desired total PID output.167void AC_PID_Basic::set_integrator(float target, float measurement, float i)168{169set_integrator(target - measurement, i);170}171172// Sets integrator using error and desired total output.173void AC_PID_Basic::set_integrator(float error, float i)174{175set_integrator(i - error * _kp);176}177178// Sets the integrator directly.179void AC_PID_Basic::set_integrator(float i)180{181_integrator = constrain_float(i, -_kimax, _kimax);182}183184185