Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AC_PID/AC_PID.cpp
Views: 1798
/// @file AC_PID.cpp1/// @brief Generic PID algorithm23#include <AP_Math/AP_Math.h>4#include "AC_PID.h"56#define AC_PID_DEFAULT_NOTCH_ATTENUATION 4078const AP_Param::GroupInfo AC_PID::var_info[] = {9// @Param: P10// @DisplayName: PID Proportional Gain11// @Description: P Gain which produces an output value that is proportional to the current error value12AP_GROUPINFO_FLAGS_DEFAULT_POINTER("P", 0, AC_PID, _kp, default_kp),1314// @Param: I15// @DisplayName: PID Integral Gain16// @Description: I Gain which produces an output that is proportional to both the magnitude and the duration of the error17AP_GROUPINFO_FLAGS_DEFAULT_POINTER("I", 1, AC_PID, _ki, default_ki),1819// @Param: D20// @DisplayName: PID Derivative Gain21// @Description: D Gain which produces an output that is proportional to the rate of change of the error22AP_GROUPINFO_FLAGS_DEFAULT_POINTER("D", 2, AC_PID, _kd, default_kd),2324// 3 was for uint16 IMAX2526// @Param: FF27// @DisplayName: FF FeedForward Gain28// @Description: FF Gain which produces an output value that is proportional to the demanded input29AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FF", 4, AC_PID, _kff, default_kff),3031// @Param: IMAX32// @DisplayName: PID Integral Maximum33// @Description: The maximum/minimum value that the I term can output34AP_GROUPINFO_FLAGS_DEFAULT_POINTER("IMAX", 5, AC_PID, _kimax, default_kimax),3536// 6 was for float FILT3738// 7 is for float ILMI and FF3940// index 8 was for AFF4142// @Param: FLTT43// @DisplayName: PID Target filter frequency in Hz44// @Description: Target filter frequency in Hz45// @Units: Hz46AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FLTT", 9, AC_PID, _filt_T_hz, default_filt_T_hz),4748// @Param: FLTE49// @DisplayName: PID Error filter frequency in Hz50// @Description: Error filter frequency in Hz51// @Units: Hz52AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FLTE", 10, AC_PID, _filt_E_hz, default_filt_E_hz),5354// @Param: FLTD55// @DisplayName: PID Derivative term filter frequency in Hz56// @Description: Derivative filter frequency in Hz57// @Units: Hz58AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FLTD", 11, AC_PID, _filt_D_hz, default_filt_D_hz),5960// @Param: SMAX61// @DisplayName: Slew rate limit62// @Description: Sets an upper limit on the slew rate produced by the combined P and D gains. If the amplitude of the control action produced by the rate feedback exceeds this value, then the D+P gain is reduced to respect the limit. This limits the amplitude of high frequency oscillations caused by an excessive gain. The limit should be set to no more than 25% of the actuators maximum slew rate to allow for load effects. Note: The gain will not be reduced to less than 10% of the nominal value. A value of zero will disable this feature.63// @Range: 0 20064// @Increment: 0.565// @User: Advanced66AP_GROUPINFO_FLAGS_DEFAULT_POINTER("SMAX", 12, AC_PID, _slew_rate_max, default_slew_rate_max),6768// @Param: PDMX69// @DisplayName: PD sum maximum70// @Description: The maximum/minimum value that the sum of the P and D term can output71// @User: Advanced72AP_GROUPINFO("PDMX", 13, AC_PID, _kpdmax, 0),7374// @Param: D_FF75// @DisplayName: PID Derivative FeedForward Gain76// @Description: FF D Gain which produces an output that is proportional to the rate of change of the target77// @Range: 0 0.0278// @Increment: 0.000179// @User: Advanced80AP_GROUPINFO_FLAGS_DEFAULT_POINTER("D_FF", 14, AC_PID, _kdff, default_kdff),8182#if AP_FILTER_ENABLED83// @Param: NTF84// @DisplayName: PID Target notch filter index85// @Description: PID Target notch filter index86// @Range: 1 887// @User: Advanced88AP_GROUPINFO("NTF", 15, AC_PID, _notch_T_filter, 0),8990// @Param: NEF91// @DisplayName: PID Error notch filter index92// @Description: PID Error notch filter index93// @Range: 1 894// @User: Advanced95AP_GROUPINFO("NEF", 16, AC_PID, _notch_E_filter, 0),96#endif9798AP_GROUPEND99};100101// Constructor102AC_PID::AC_PID(float initial_p, float initial_i, float initial_d, float initial_ff, float initial_imax, float initial_filt_T_hz, float initial_filt_E_hz, float initial_filt_D_hz,103float initial_srmax, float initial_srtau, float initial_dff) :104default_kp(initial_p),105default_ki(initial_i),106default_kd(initial_d),107default_kff(initial_ff),108default_kdff(initial_dff),109default_kimax(initial_imax),110default_filt_T_hz(initial_filt_T_hz),111default_filt_E_hz(initial_filt_E_hz),112default_filt_D_hz(initial_filt_D_hz),113default_slew_rate_max(initial_srmax)114{115// load parameter values from eeprom116AP_Param::setup_object_defaults(this, var_info);117118// this param is not in the table, so its default is no loaded in the call above119_slew_rate_tau.set(initial_srtau);120121// reset input filter to first value received122_flags._reset_filter = true;123124memset(&_pid_info, 0, sizeof(_pid_info));125126// slew limit scaler allows for plane to use degrees/sec slew127// limit128_slew_limit_scale = 1;129}130131// filt_T_hz - set target filter hz132void AC_PID::set_filt_T_hz(float hz)133{134_filt_T_hz.set(fabsf(hz));135}136137// filt_E_hz - set error filter hz138void AC_PID::set_filt_E_hz(float hz)139{140_filt_E_hz.set(fabsf(hz));141}142143// filt_D_hz - set derivative filter hz144void AC_PID::set_filt_D_hz(float hz)145{146_filt_D_hz.set(fabsf(hz));147}148149// slew_limit - set slew limit150void AC_PID::set_slew_limit(float smax)151{152_slew_rate_max.set(fabsf(smax));153}154155void AC_PID::set_notch_sample_rate(float sample_rate)156{157#if AP_FILTER_ENABLED158if (_notch_T_filter == 0 && _notch_E_filter == 0) {159return;160}161162if (_notch_T_filter != 0) {163if (_target_notch == nullptr) {164_target_notch = NEW_NOTHROW NotchFilterFloat();165}166AP_Filter* filter = AP::filters().get_filter(_notch_T_filter);167if (filter != nullptr && !filter->setup_notch_filter(*_target_notch, sample_rate)) {168delete _target_notch;169_target_notch = nullptr;170_notch_T_filter.set(0);171}172}173174if (_notch_E_filter != 0) {175if (_error_notch == nullptr) {176_error_notch = NEW_NOTHROW NotchFilterFloat();177}178AP_Filter* filter = AP::filters().get_filter(_notch_E_filter);179if (filter != nullptr && !filter->setup_notch_filter(*_error_notch, sample_rate)) {180delete _error_notch;181_error_notch = nullptr;182_notch_E_filter.set(0);183}184}185#endif186}187188// update_all - set target and measured inputs to PID controller and calculate outputs189// target and error are filtered190// the derivative is then calculated and filtered191// the integral is then updated based on the setting of the limit flag192float AC_PID::update_all(float target, float measurement, float dt, bool limit, float boost)193{194// don't process inf or NaN195if (!isfinite(target) || !isfinite(measurement)) {196return 0.0f;197}198199// reset input filter to value received200_pid_info.reset = _flags._reset_filter;201if (_flags._reset_filter) {202_flags._reset_filter = false;203204// Reset target filter205_target = target;206#if AP_FILTER_ENABLED207if (_target_notch != nullptr) {208_target_notch->reset();209_target = _target_notch->apply(_target);210}211#endif212213// Calculate error and reset error filter214_error = _target - measurement;215#if AP_FILTER_ENABLED216if (_error_notch != nullptr) {217_error_notch->reset();218_error = _error_notch->apply(_error);219}220#endif221// Zero derivatives222_derivative = 0.0f;223_target_derivative = 0.0f;224225} else {226227// Apply target filters228const float target_last = _target;229#if AP_FILTER_ENABLED230// apply notch filters before FTLD/FLTE to avoid shot noise231if (_target_notch != nullptr) {232target = _target_notch->apply(target);233}234#endif235_target += get_filt_T_alpha(dt) * (target - _target);236237// Calculate error and apply error filter238const float error_last = _error;239float error = _target - measurement;240#if AP_FILTER_ENABLED241if (_error_notch != nullptr) {242error = _error_notch->apply(error);243}244#endif245_error += get_filt_E_alpha(dt) * (error - _error);246247// calculate and filter derivative248if (is_positive(dt)) {249float derivative = (_error - error_last) / dt;250_derivative += get_filt_D_alpha(dt) * (derivative - _derivative);251_target_derivative = (_target - target_last) / dt;252}253}254255// update I term256update_i(dt, limit);257258float P_out = (_error * _kp);259float D_out = (_derivative * _kd);260261// calculate slew limit modifier for P+D262_pid_info.Dmod = _slew_limiter.modifier((_pid_info.P + _pid_info.D) * _slew_limit_scale, dt);263_pid_info.slew_rate = _slew_limiter.get_slew_rate();264265P_out *= _pid_info.Dmod;266D_out *= _pid_info.Dmod;267268// boost output if required269P_out *= boost;270D_out *= boost;271272_pid_info.PD_limit = false;273// Apply PD sum limit if enabled274if (is_positive(_kpdmax)) {275const float PD_sum_abs = fabsf(P_out + D_out);276if (PD_sum_abs > _kpdmax) {277const float PD_scale = _kpdmax / PD_sum_abs;278P_out *= PD_scale;279D_out *= PD_scale;280_pid_info.PD_limit = true;281}282}283284_pid_info.target = _target;285_pid_info.actual = measurement;286_pid_info.error = _error;287_pid_info.P = P_out;288_pid_info.D = D_out;289_pid_info.FF = _target * _kff;290_pid_info.DFF = _target_derivative * _kdff;291292return P_out + D_out + _integrator;293}294295// update_error - set error input to PID controller and calculate outputs296// target is set to zero and error is set and filtered297// the derivative then is calculated and filtered298// the integral is then updated based on the setting of the limit flag299// Target and Measured must be set manually for logging purposes.300// todo: remove function when it is no longer used.301float AC_PID::update_error(float error, float dt, bool limit)302{303// don't process inf or NaN304if (!isfinite(error)) {305return 0.0f;306}307308// Reuse update all code path, zero target and pass negative error as measurement309// Passing as measurement bypasses any target filtering to maintain behaviour310// Negate as update all calculates error as target - measurement311_target = 0.0;312const float output = update_all(0.0, -error, dt, limit);313314// Make sure logged target and actual are still 0 to maintain behaviour315_pid_info.target = 0.0;316_pid_info.actual = 0.0;317318return output;319}320321// update_i - update the integral322// If the limit flag is set the integral is only allowed to shrink323void AC_PID::update_i(float dt, bool limit)324{325if (!is_zero(_ki) && is_positive(dt)) {326// Ensure that integrator can only be reduced if the output is saturated327if (!limit || ((is_positive(_integrator) && is_negative(_error)) || (is_negative(_integrator) && is_positive(_error)))) {328_integrator += ((float)_error * _ki) * dt;329_integrator = constrain_float(_integrator, -_kimax, _kimax);330}331} else {332_integrator = 0.0f;333}334_pid_info.I = _integrator;335_pid_info.limit = limit;336337// Set I set flag for logging and clear338_pid_info.I_term_set = _flags._I_set;339_flags._I_set = false;340}341342float AC_PID::get_p() const343{344return _pid_info.P;345}346347float AC_PID::get_i() const348{349return _integrator;350}351352float AC_PID::get_d() const353{354return _pid_info.D;355}356357float AC_PID::get_ff() const358{359return _pid_info.FF + _pid_info.DFF;360}361362void AC_PID::reset_I()363{364_flags._I_set = true;365_integrator = 0.0;366}367368// load original gains from eeprom, used by autotune to restore gains after tuning369void AC_PID::load_gains()370{371_kp.load();372_ki.load();373_kd.load();374_kff.load();375_filt_T_hz.load();376_filt_E_hz.load();377_filt_D_hz.load();378}379380// save original gains to eeprom, used by autotune to save gains before tuning381void AC_PID::save_gains()382{383_kp.save();384_ki.save();385_kd.save();386_kff.save();387_filt_T_hz.save();388_filt_E_hz.save();389_filt_D_hz.save();390}391392// get_filt_T_alpha - get the target filter alpha393float AC_PID::get_filt_T_alpha(float dt) const394{395return calc_lowpass_alpha_dt(dt, _filt_T_hz);396}397398// get_filt_E_alpha - get the error filter alpha399float AC_PID::get_filt_E_alpha(float dt) const400{401return calc_lowpass_alpha_dt(dt, _filt_E_hz);402}403404// get_filt_D_alpha - get the derivative filter alpha405float AC_PID::get_filt_D_alpha(float dt) const406{407return calc_lowpass_alpha_dt(dt, _filt_D_hz);408}409410void AC_PID::set_integrator(float integrator)411{412_flags._I_set = true;413_integrator = constrain_float(integrator, -_kimax, _kimax);414}415416void AC_PID::relax_integrator(float integrator, float dt, float time_constant)417{418integrator = constrain_float(integrator, -_kimax, _kimax);419if (is_positive(dt)) {420_flags._I_set = true;421_integrator = _integrator + (integrator - _integrator) * (dt / (dt + time_constant));422}423}424425426