2016-02-17 21:25:08 -04:00
|
|
|
#pragma once
|
2012-01-29 01:21:43 -04:00
|
|
|
|
|
|
|
/// @file AC_PID.h
|
|
|
|
/// @brief Generic PID algorithm, with EEPROM-backed storage of constants.
|
|
|
|
|
2015-08-11 03:28:41 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_Param/AP_Param.h>
|
2012-10-09 18:01:58 -03:00
|
|
|
#include <stdlib.h>
|
2016-03-31 18:43:36 -03:00
|
|
|
#include <cmath>
|
2019-01-18 00:23:42 -04:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
2012-01-29 01:21:43 -04:00
|
|
|
|
2015-02-11 08:06:04 -04:00
|
|
|
#define AC_PID_FILT_HZ_DEFAULT 20.0f // default input filter frequency
|
2015-03-11 02:16:26 -03:00
|
|
|
#define AC_PID_FILT_HZ_MIN 0.01f // minimum input filter frequency
|
2014-02-14 02:52:24 -04:00
|
|
|
|
2012-01-29 01:21:43 -04:00
|
|
|
/// @class AC_PID
|
2015-02-11 08:06:04 -04:00
|
|
|
/// @brief Copter PID control class
|
2012-01-29 01:21:43 -04:00
|
|
|
class AC_PID {
|
|
|
|
public:
|
|
|
|
|
2015-02-11 08:06:04 -04:00
|
|
|
// Constructor for PID
|
2018-01-02 22:41:37 -04:00
|
|
|
AC_PID(float initial_p, float initial_i, float initial_d, float initial_imax, float initial_filt_hz, float dt, float initial_ff = 0);
|
2015-02-11 08:06:04 -04:00
|
|
|
|
|
|
|
// set_dt - set time step in seconds
|
|
|
|
void set_dt(float dt);
|
|
|
|
|
|
|
|
// set_input_filter_all - set input to PID controller
|
|
|
|
// input is filtered before the PID controllers are run
|
|
|
|
// this should be called before any other calls to get_p, get_i or get_d
|
|
|
|
void set_input_filter_all(float input);
|
|
|
|
|
|
|
|
// set_input_filter_d - set input to PID controller
|
|
|
|
// only input to the D portion of the controller is filtered
|
|
|
|
// this should be called before any other calls to get_p, get_i or get_d
|
|
|
|
void set_input_filter_d(float input);
|
|
|
|
|
|
|
|
// get_pid - get results from pid controller
|
|
|
|
float get_pid();
|
|
|
|
float get_pi();
|
2015-05-21 22:43:43 -03:00
|
|
|
float get_p();
|
2015-02-11 08:06:04 -04:00
|
|
|
float get_i();
|
2015-05-21 22:43:43 -03:00
|
|
|
float get_d();
|
2017-02-10 01:25:27 -04:00
|
|
|
float get_ff(float requested_rate);
|
|
|
|
|
2015-02-11 08:06:04 -04:00
|
|
|
// reset_I - reset the integrator
|
2012-08-17 02:37:26 -03:00
|
|
|
void reset_I();
|
|
|
|
|
2015-02-11 08:06:04 -04:00
|
|
|
// reset_filter - input filter will be reset to the next value provided to set_input()
|
2017-10-26 02:57:22 -03:00
|
|
|
void reset_filter() { _flags._reset_filter = true; }
|
2015-02-11 08:06:04 -04:00
|
|
|
|
|
|
|
// load gain from eeprom
|
2012-08-17 02:37:26 -03:00
|
|
|
void load_gains();
|
|
|
|
|
2015-02-11 08:06:04 -04:00
|
|
|
// save gain to eeprom
|
2012-08-17 02:37:26 -03:00
|
|
|
void save_gains();
|
2015-02-11 08:06:04 -04:00
|
|
|
|
|
|
|
/// operator function call for easy initialisation
|
2018-01-02 22:41:37 -04:00
|
|
|
void operator() (float p, float i, float d, float imaxval, float input_filt_hz, float dt, float ffval = 0);
|
2015-02-11 08:06:04 -04:00
|
|
|
|
|
|
|
// get accessors
|
2016-05-06 01:43:12 -03:00
|
|
|
AP_Float &kP() { return _kp; }
|
|
|
|
AP_Float &kI() { return _ki; }
|
|
|
|
AP_Float &kD() { return _kd; }
|
2017-01-09 05:52:02 -04:00
|
|
|
AP_Float &filt_hz() { return _filt_hz; }
|
2015-02-11 08:06:04 -04:00
|
|
|
float imax() const { return _imax.get(); }
|
2015-03-27 21:19:24 -03:00
|
|
|
float get_filt_alpha() const;
|
2017-02-10 01:25:27 -04:00
|
|
|
float ff() const { return _ff.get(); }
|
2015-02-11 08:06:04 -04:00
|
|
|
|
|
|
|
// set accessors
|
2014-02-14 02:52:24 -04:00
|
|
|
void kP(const float v) { _kp.set(v); }
|
|
|
|
void kI(const float v) { _ki.set(v); }
|
|
|
|
void kD(const float v) { _kd.set(v); }
|
2015-05-08 15:42:13 -03:00
|
|
|
void imax(const float v) { _imax.set(fabsf(v)); }
|
2015-03-11 02:16:26 -03:00
|
|
|
void filt_hz(const float v);
|
2017-02-10 01:25:27 -04:00
|
|
|
void ff(const float v) { _ff.set(v); }
|
2015-02-11 08:06:04 -04:00
|
|
|
|
2014-02-14 02:52:24 -04:00
|
|
|
float get_integrator() const { return _integrator; }
|
|
|
|
void set_integrator(float i) { _integrator = i; }
|
2012-08-17 02:37:26 -03:00
|
|
|
|
2018-08-08 01:11:59 -03:00
|
|
|
// set the desired and actual rates (for logging purposes)
|
2015-05-24 02:51:44 -03:00
|
|
|
void set_desired_rate(float desired) { _pid_info.desired = desired; }
|
2018-08-08 01:11:59 -03:00
|
|
|
void set_actual_rate(float actual) { _pid_info.actual = actual; }
|
2015-05-24 02:51:44 -03:00
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
const AP_Logger::PID_Info& get_pid_info(void) const { return _pid_info; }
|
2015-05-21 22:43:43 -03:00
|
|
|
|
2015-02-11 08:06:04 -04:00
|
|
|
// parameter var table
|
2012-08-17 02:37:26 -03:00
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
2012-02-12 07:25:50 -04:00
|
|
|
|
2014-05-02 18:17:32 -03:00
|
|
|
protected:
|
2015-02-11 08:06:04 -04:00
|
|
|
|
|
|
|
// parameters
|
2012-08-17 02:37:26 -03:00
|
|
|
AP_Float _kp;
|
|
|
|
AP_Float _ki;
|
|
|
|
AP_Float _kd;
|
2015-02-11 08:06:04 -04:00
|
|
|
AP_Float _imax;
|
|
|
|
AP_Float _filt_hz; // PID Input filter frequency in Hz
|
2017-02-10 01:25:27 -04:00
|
|
|
AP_Float _ff;
|
2015-02-11 08:06:04 -04:00
|
|
|
|
|
|
|
// flags
|
|
|
|
struct ac_pid_flags {
|
|
|
|
bool _reset_filter : 1; // true when input filter should be reset during next call to set_input
|
|
|
|
} _flags;
|
|
|
|
|
|
|
|
// internal variables
|
2015-05-21 22:43:43 -03:00
|
|
|
float _dt; // timestep in seconds
|
|
|
|
float _integrator; // integrator value
|
|
|
|
float _input; // last input for derivative
|
|
|
|
float _derivative; // last derivative for low-pass filter
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
AP_Logger::PID_Info _pid_info;
|
2012-01-29 01:21:43 -04:00
|
|
|
};
|