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_Basic.h
Views: 1798
#pragma once12/// @file AC_PID_Basic.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 "AP_PIDInfo.h"89/// @class AC_PID_Basic10/// @brief Copter PID control class11class AC_PID_Basic {12public:1314// Constructor for PID15AC_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);1617// set target and measured inputs to PID controller and calculate outputs18// target and error are filtered19// the derivative is then calculated and filtered20// the integral is then updated based on the setting of the limit flag21float update_all(float target, float measurement, float dt, bool limit = false) WARN_IF_UNUSED;22float update_all(float target, float measurement, float dt, bool limit_neg, bool limit_pos) WARN_IF_UNUSED;2324// update the integral25// if the limit flags are set the integral is only allowed to shrink26void update_i(float dt, bool limit_neg, bool limit_pos);2728// get results from pid controller29float get_p() const WARN_IF_UNUSED { return _error * _kp; }30float get_i() const WARN_IF_UNUSED { return _integrator; }31float get_d() const WARN_IF_UNUSED { return _derivative * _kd; }32float get_ff() const WARN_IF_UNUSED { return _target * _kff; }33float get_error() const WARN_IF_UNUSED { return _error; }3435// reset the integrator36void reset_I();3738// input and D term filter will be reset to the next value provided to set_input()39void reset_filter() { _reset_filter = true; }4041// save gain to eeprom42void save_gains();4344// get accessors45AP_Float &kP() WARN_IF_UNUSED { return _kp; }46AP_Float &kI() WARN_IF_UNUSED { return _ki; }47AP_Float &kD() WARN_IF_UNUSED { return _kd; }48AP_Float &ff() WARN_IF_UNUSED { return _kff;}49AP_Float &filt_E_hz() WARN_IF_UNUSED { return _filt_E_hz; }50AP_Float &filt_D_hz() WARN_IF_UNUSED { return _filt_D_hz; }51float imax() const WARN_IF_UNUSED { return _kimax.get(); }52float get_filt_E_alpha(float dt) const WARN_IF_UNUSED;53float get_filt_D_alpha(float dt) const WARN_IF_UNUSED;5455// set accessors56void set_kP(float v) { _kp.set(v); }57void set_kI(float v) { _ki.set(v); }58void set_kD(float v) { _kd.set(v); }59void set_ff(float v) { _kff.set(v); }60void set_imax(float v) { _kimax.set(fabsf(v)); }61void set_filt_E_hz(float hz) { _filt_E_hz.set(fabsf(hz)); }62void set_filt_D_hz(float hz) { _filt_D_hz.set(fabsf(hz)); }6364// integrator setting functions65void set_integrator(float target, float measurement, float i);66void set_integrator(float error, float i);67void set_integrator(float i);6869const AP_PIDInfo& get_pid_info(void) const WARN_IF_UNUSED { return _pid_info; }7071// parameter var table72static const struct AP_Param::GroupInfo var_info[];7374protected:7576// parameters77AP_Float _kp;78AP_Float _ki;79AP_Float _kd;80AP_Float _kff;81AP_Float _kimax;82AP_Float _filt_E_hz; // PID error filter frequency in Hz83AP_Float _filt_D_hz; // PID derivative filter frequency in Hz8485// internal variables86float _target; // target value to enable filtering87float _error; // error value to enable filtering88float _derivative; // last derivative for low-pass filter89float _integrator; // integrator value90bool _reset_filter; // true when input filter should be reset during next call to set_input9192AP_PIDInfo _pid_info;9394private:95const float default_kp;96const float default_ki;97const float default_kd;98const float default_kff;99const float default_kimax;100const float default_filt_E_hz;101const float default_filt_D_hz;102};103104105