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_PI_2D.h
Views: 1798
#pragma once12/// @file AC_PI_2D.h3/// @brief 2-axis PI controller 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>910#define AC_PI_2D_FILT_HZ_MIN 0.01f // minimum input filter frequency1112/// @class AC_PI_2D13/// @brief 2-axis PI controller14class AC_PI_2D {15public:1617// constructor18AC_PI_2D(float initial_p, float initial_i, float initial_imax, float initial_filt_hz, float dt);1920CLASS_NO_COPY(AC_PI_2D);2122// set time step in seconds23void set_dt(float dt);2425// set_input - set input to PI controller26// input is filtered before the PI controllers are run27// this should be called before any other calls to get_p, get_i or get_d28void set_input(const Vector2f &input);29void set_input(const Vector3f &input) { set_input(Vector2f{input.x, input.y}); }3031// get_pi - get results from pid controller32Vector2f get_pi();33Vector2f get_p() const;34Vector2f get_i();35Vector2f get_i_shrink(); // get_i but do not allow integrator to grow (it may shrink)3637// reset_I - reset the integrator38void reset_I();3940// reset_filter - input filter will be reset to the next value provided to set_input()41void reset_filter();4243// load gain from eeprom44void load_gains();4546// save gain to eeprom47void save_gains();4849// get accessors50AP_Float &kP() { return _kp; }51AP_Float &kI() { return _ki; }52float imax() const { return _imax.get(); }53float filt_hz() const { return _filt_hz.get(); }54float get_filt_alpha() const { return _filt_alpha; }5556// set accessors57void kP(float v) { _kp.set(v); }58void kI(float v) { _ki.set(v); }59void imax(float v) { _imax.set(fabsf(v)); }60void filt_hz(float hz);6162Vector2f get_integrator() const { return _integrator; }63void set_integrator(const Vector2f &i) { _integrator = i; }64void set_integrator(const Vector3f &i) { _integrator.x = i.x; _integrator.y = i.y; }6566// parameter var table67static const struct AP_Param::GroupInfo var_info[];6869private:7071// calc_filt_alpha - recalculate the input filter alpha72void calc_filt_alpha();7374// parameters75AP_Float _kp;76AP_Float _ki;77AP_Float _imax;78AP_Float _filt_hz; // PI Input filter frequency in Hz7980// flags81struct ac_pid_flags {82bool _reset_filter : 1; // true when input filter should be reset during next call to set_input83} _flags;8485// internal variables86float _dt; // time step in seconds87Vector2f _integrator; // integrator value88Vector2f _input; // last input for derivative89float _filt_alpha; // input filter alpha9091const float default_kp;92const float default_ki;93const float default_imax;94const float default_filt_hz;9596};979899