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_2D.cpp
Views: 1798
/// @file AC_PID_2D.cpp1/// @brief Generic PID algorithm23#include <AP_Math/AP_Math.h>4#include "AC_PID_2D.h"56#define AC_PID_2D_FILT_D_HZ_MIN 0.005f // minimum input filter frequency78const AP_Param::GroupInfo AC_PID_2D::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_2D, _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_2D, _ki, default_ki),1819// @Param: IMAX20// @DisplayName: PID Integral Maximum21// @Description: The maximum/minimum value that the I term can output22AP_GROUPINFO_FLAGS_DEFAULT_POINTER("IMAX", 2, AC_PID_2D, _kimax, default_kimax),2324// @Param: FLTE25// @DisplayName: PID Input filter frequency in Hz26// @Description: Input filter frequency in Hz27// @Units: Hz28AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FLTE", 3, AC_PID_2D, _filt_E_hz, default_filt_E_hz),2930// @Param: D31// @DisplayName: PID Derivative Gain32// @Description: D Gain which produces an output that is proportional to the rate of change of the error33AP_GROUPINFO_FLAGS_DEFAULT_POINTER("D", 4, AC_PID_2D, _kd, default_kd),3435// @Param: FLTD36// @DisplayName: D term filter frequency in Hz37// @Description: D term filter frequency in Hz38// @Units: Hz39AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FLTD", 5, AC_PID_2D, _filt_D_hz, default_filt_D_hz),4041// @Param: FF42// @DisplayName: PID Feed Forward Gain43// @Description: FF Gain which produces an output that is proportional to the magnitude of the target44AP_GROUPINFO_FLAGS_DEFAULT_POINTER("FF", 6, AC_PID_2D, _kff, default_kff),4546AP_GROUPEND47};4849// Constructor50AC_PID_2D::AC_PID_2D(float initial_kP, float initial_kI, float initial_kD, float initial_kFF, float initial_imax, float initial_filt_E_hz, float initial_filt_D_hz) :51default_kp(initial_kP),52default_ki(initial_kI),53default_kd(initial_kD),54default_kff(initial_kFF),55default_kimax(initial_imax),56default_filt_E_hz(initial_filt_E_hz),57default_filt_D_hz(initial_filt_D_hz)58{59// load parameter values from eeprom60AP_Param::setup_object_defaults(this, var_info);6162// reset input filter to first value received63_reset_filter = true;64}6566// update_all - set target and measured inputs to PID controller and calculate outputs67// target and error are filtered68// the derivative is then calculated and filtered69// the integral is then updated if it does not increase in the direction of the limit vector70Vector2f AC_PID_2D::update_all(const Vector2f &target, const Vector2f &measurement, float dt, const Vector2f &limit)71{72// don't process inf or NaN73if (target.is_nan() || target.is_inf() ||74measurement.is_nan() || measurement.is_inf()) {75return Vector2f{};76}7778_target = target;7980// reset input filter to value received81if (_reset_filter) {82_reset_filter = false;83_error = _target - measurement;84_derivative.zero();85} else {86Vector2f error_last{_error};87_error += ((_target - measurement) - _error) * get_filt_E_alpha(dt);8889// calculate and filter derivative90if (is_positive(dt)) {91const Vector2f derivative{(_error - error_last) / dt};92_derivative += (derivative - _derivative) * get_filt_D_alpha(dt);93}94}9596// update I term97update_i(dt, limit);9899// calculate slew limit100_slew_calc.update(Vector2f{_pid_info_x.P + _pid_info_x.D, _pid_info_y.P + _pid_info_y.D}, dt);101_pid_info_x.slew_rate = _pid_info_y.slew_rate = _slew_calc.get_slew_rate();102103_pid_info_x.target = _target.x;104_pid_info_x.actual = measurement.x;105_pid_info_x.error = _error.x;106_pid_info_x.P = _error.x * _kp;107_pid_info_x.I = _integrator.x;108_pid_info_x.D = _derivative.x * _kd;109_pid_info_x.FF = _target.x * _kff;110111_pid_info_y.target = _target.y;112_pid_info_y.actual = measurement.y;113_pid_info_y.error = _error.y;114_pid_info_y.P = _error.y * _kp;115_pid_info_y.I = _integrator.y;116_pid_info_y.D = _derivative.y * _kd;117_pid_info_y.FF = _target.y * _kff;118119return _error * _kp + _integrator + _derivative * _kd + _target * _kff;120}121122Vector2f AC_PID_2D::update_all(const Vector3f &target, const Vector3f &measurement, float dt, const Vector3f &limit)123{124return update_all(Vector2f{target.x, target.y}, Vector2f{measurement.x, measurement.y}, dt, Vector2f{limit.x, limit.y});125}126127// update_i - update the integral128// If the limit is set the integral is only allowed to reduce in the direction of the limit129void AC_PID_2D::update_i(float dt, const Vector2f &limit)130{131_pid_info_x.limit = false;132_pid_info_y.limit = false;133134Vector2f delta_integrator = (_error * _ki) * dt;135float integrator_length = _integrator.length();136_integrator += delta_integrator;137// do not let integrator increase in length if delta_integrator is in the direction of limit138if (is_positive(delta_integrator * limit) && _integrator.limit_length(integrator_length)) {139_pid_info_x.limit = true;140_pid_info_y.limit = true;141}142143_integrator.limit_length(_kimax);144}145146Vector2f AC_PID_2D::get_p() const147{148return _error * _kp;149}150151const Vector2f& AC_PID_2D::get_i() const152{153return _integrator;154}155156Vector2f AC_PID_2D::get_d() const157{158return _derivative * _kd;159}160161Vector2f AC_PID_2D::get_ff()162{163_pid_info_x.FF = _target.x * _kff;164_pid_info_y.FF = _target.y * _kff;165return _target * _kff;166}167168void AC_PID_2D::reset_I()169{170_integrator.zero();171}172173// save_gains - save gains to eeprom174void AC_PID_2D::save_gains()175{176_kp.save();177_ki.save();178_kd.save();179_kff.save();180_kimax.save();181_filt_E_hz.save();182_filt_D_hz.save();183}184185// get the target filter alpha186float AC_PID_2D::get_filt_E_alpha(float dt) const187{188return calc_lowpass_alpha_dt(dt, _filt_E_hz);189}190191// get the derivative filter alpha192float AC_PID_2D::get_filt_D_alpha(float dt) const193{194return calc_lowpass_alpha_dt(dt, _filt_D_hz);195}196197void AC_PID_2D::set_integrator(const Vector2f& target, const Vector2f& measurement, const Vector2f& i)198{199set_integrator(target - measurement, i);200}201202void AC_PID_2D::set_integrator(const Vector2f& error, const Vector2f& i)203{204set_integrator(i - error * _kp);205}206207void AC_PID_2D::set_integrator(const Vector2f& i)208{209_integrator = i;210_integrator.limit_length(_kimax);211}212213214215