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.h
Views: 1798
#pragma once12/// @file AC_PID_2D.h3/// @brief Generic PID algorithm, with EEPROM-backed storage of constants.45#include <AP_Common/AP_Common.h>6#include <AP_Param/AP_Param.h>7#include <stdlib.h>8#include <cmath>9#include <AC_PID/AP_PIDInfo.h>10#include <Filter/SlewCalculator2D.h>1112/// @class AC_PID_2D13/// @brief Copter PID control class14class AC_PID_2D {15public:1617// Constructor for PID18AC_PID_2D(float initial_kP, float initial_kI, float initial_kD, float initial_kFF, float initial_imax, float initial_filt_hz, float initial_filt_d_hz);1920CLASS_NO_COPY(AC_PID_2D);2122// update_all - set target and measured inputs to PID controller and calculate outputs23// target and error are filtered24// the derivative is then calculated and filtered25// the integral is then updated if it does not increase in the direction of the limit vector26Vector2f update_all(const Vector2f &target, const Vector2f &measurement, float dt, const Vector2f &limit);27Vector2f update_all(const Vector3f &target, const Vector3f &measurement, float dt, const Vector3f &limit);2829// update the integral30// if the limit flag is set the integral is only allowed to shrink31void update_i(float dt, const Vector2f &limit);3233// get results from pid controller34Vector2f get_p() const;35const Vector2f& get_i() const;36Vector2f get_d() const;37Vector2f get_ff();38const Vector2f& get_error() const { return _error; }3940// reset the integrator41void reset_I();4243// reset_filter - input and D term filter will be reset to the next value provided to set_input()44void reset_filter() { _reset_filter = true; }4546// save gain to eeprom47void save_gains();4849// get accessors50AP_Float &kP() { return _kp; }51AP_Float &kI() { return _ki; }52AP_Float &kD() { return _kd; }53AP_Float &ff() { return _kff;}54AP_Float &filt_E_hz() { return _filt_E_hz; }55AP_Float &filt_D_hz() { return _filt_D_hz; }56float imax() const { return _kimax.get(); }57float get_filt_E_alpha(float dt) const;58float get_filt_D_alpha(float dt) const;5960// set accessors61void set_kP(float v) { _kp.set(v); }62void set_kI(float v) { _ki.set(v); }63void set_kD(float v) { _kd.set(v); }64void set_ff(float v) { _kff.set(v); }65void set_imax(float v) { _kimax.set(fabsf(v)); }66void set_filt_E_hz(float hz) { _filt_E_hz.set(fabsf(hz)); }67void set_filt_D_hz(float hz) { _filt_D_hz.set(fabsf(hz)); }6869// integrator setting functions70void set_integrator(const Vector2f& target, const Vector2f& measurement, const Vector2f& i);71void set_integrator(const Vector2f& error, const Vector2f& i);72void set_integrator(const Vector3f& i) { set_integrator(Vector2f{i.x, i.y}); }73void set_integrator(const Vector2f& i);7475// return current slew rate of slew limiter. Will return 0 if SMAX is zero76float get_slew_rate(void) const { return _slew_calc.get_slew_rate(); }7778const AP_PIDInfo& get_pid_info_x(void) const { return _pid_info_x; }79const AP_PIDInfo& get_pid_info_y(void) const { return _pid_info_y; }8081// parameter var table82static const struct AP_Param::GroupInfo var_info[];8384protected:8586// parameters87AP_Float _kp;88AP_Float _ki;89AP_Float _kd;90AP_Float _kff;91AP_Float _kimax;92AP_Float _filt_E_hz; // PID error filter frequency in Hz93AP_Float _filt_D_hz; // PID derivative filter frequency in Hz9495// internal variables96Vector2f _target; // target value to enable filtering97Vector2f _error; // error value to enable filtering98Vector2f _derivative; // last derivative from low-pass filter99Vector2f _integrator; // integrator value100bool _reset_filter; // true when input filter should be reset during next call to update_all101102AP_PIDInfo _pid_info_x;103AP_PIDInfo _pid_info_y;104105SlewCalculator2D _slew_calc; // 2D slew rate calculator106107private:108const float default_kp;109const float default_ki;110const float default_kd;111const float default_kff;112const float default_kimax;113const float default_filt_E_hz;114const float default_filt_D_hz;115};116117118