CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AC_PID/AC_PID_Basic.h
Views: 1798
1
#pragma once
2
3
/// @file AC_PID_Basic.h
4
/// @brief Generic PID algorithm, with EEPROM-backed storage of constants.
5
6
#include <AP_Common/AP_Common.h>
7
#include <AP_Param/AP_Param.h>
8
#include "AP_PIDInfo.h"
9
10
/// @class AC_PID_Basic
11
/// @brief Copter PID control class
12
class AC_PID_Basic {
13
public:
14
15
// Constructor for PID
16
AC_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);
17
18
// set target and measured inputs to PID controller and calculate outputs
19
// target and error are filtered
20
// the derivative is then calculated and filtered
21
// the integral is then updated based on the setting of the limit flag
22
float update_all(float target, float measurement, float dt, bool limit = false) WARN_IF_UNUSED;
23
float update_all(float target, float measurement, float dt, bool limit_neg, bool limit_pos) WARN_IF_UNUSED;
24
25
// update the integral
26
// if the limit flags are set the integral is only allowed to shrink
27
void update_i(float dt, bool limit_neg, bool limit_pos);
28
29
// get results from pid controller
30
float get_p() const WARN_IF_UNUSED { return _error * _kp; }
31
float get_i() const WARN_IF_UNUSED { return _integrator; }
32
float get_d() const WARN_IF_UNUSED { return _derivative * _kd; }
33
float get_ff() const WARN_IF_UNUSED { return _target * _kff; }
34
float get_error() const WARN_IF_UNUSED { return _error; }
35
36
// reset the integrator
37
void reset_I();
38
39
// input and D term filter will be reset to the next value provided to set_input()
40
void reset_filter() { _reset_filter = true; }
41
42
// save gain to eeprom
43
void save_gains();
44
45
// get accessors
46
AP_Float &kP() WARN_IF_UNUSED { return _kp; }
47
AP_Float &kI() WARN_IF_UNUSED { return _ki; }
48
AP_Float &kD() WARN_IF_UNUSED { return _kd; }
49
AP_Float &ff() WARN_IF_UNUSED { return _kff;}
50
AP_Float &filt_E_hz() WARN_IF_UNUSED { return _filt_E_hz; }
51
AP_Float &filt_D_hz() WARN_IF_UNUSED { return _filt_D_hz; }
52
float imax() const WARN_IF_UNUSED { return _kimax.get(); }
53
float get_filt_E_alpha(float dt) const WARN_IF_UNUSED;
54
float get_filt_D_alpha(float dt) const WARN_IF_UNUSED;
55
56
// set accessors
57
void set_kP(float v) { _kp.set(v); }
58
void set_kI(float v) { _ki.set(v); }
59
void set_kD(float v) { _kd.set(v); }
60
void set_ff(float v) { _kff.set(v); }
61
void set_imax(float v) { _kimax.set(fabsf(v)); }
62
void set_filt_E_hz(float hz) { _filt_E_hz.set(fabsf(hz)); }
63
void set_filt_D_hz(float hz) { _filt_D_hz.set(fabsf(hz)); }
64
65
// integrator setting functions
66
void set_integrator(float target, float measurement, float i);
67
void set_integrator(float error, float i);
68
void set_integrator(float i);
69
70
const AP_PIDInfo& get_pid_info(void) const WARN_IF_UNUSED { return _pid_info; }
71
72
// parameter var table
73
static const struct AP_Param::GroupInfo var_info[];
74
75
protected:
76
77
// parameters
78
AP_Float _kp;
79
AP_Float _ki;
80
AP_Float _kd;
81
AP_Float _kff;
82
AP_Float _kimax;
83
AP_Float _filt_E_hz; // PID error filter frequency in Hz
84
AP_Float _filt_D_hz; // PID derivative filter frequency in Hz
85
86
// internal variables
87
float _target; // target value to enable filtering
88
float _error; // error value to enable filtering
89
float _derivative; // last derivative for low-pass filter
90
float _integrator; // integrator value
91
bool _reset_filter; // true when input filter should be reset during next call to set_input
92
93
AP_PIDInfo _pid_info;
94
95
private:
96
const float default_kp;
97
const float default_ki;
98
const float default_kd;
99
const float default_kff;
100
const float default_kimax;
101
const float default_filt_E_hz;
102
const float default_filt_D_hz;
103
};
104
105