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_PI_2D.h
Views: 1798
1
#pragma once
2
3
/// @file AC_PI_2D.h
4
/// @brief 2-axis PI controller with EEPROM-backed storage of constants.
5
6
#include <AP_Common/AP_Common.h>
7
#include <AP_Param/AP_Param.h>
8
#include <stdlib.h>
9
#include <cmath>
10
11
#define AC_PI_2D_FILT_HZ_MIN 0.01f // minimum input filter frequency
12
13
/// @class AC_PI_2D
14
/// @brief 2-axis PI controller
15
class AC_PI_2D {
16
public:
17
18
// constructor
19
AC_PI_2D(float initial_p, float initial_i, float initial_imax, float initial_filt_hz, float dt);
20
21
CLASS_NO_COPY(AC_PI_2D);
22
23
// set time step in seconds
24
void set_dt(float dt);
25
26
// set_input - set input to PI controller
27
// input is filtered before the PI controllers are run
28
// this should be called before any other calls to get_p, get_i or get_d
29
void set_input(const Vector2f &input);
30
void set_input(const Vector3f &input) { set_input(Vector2f{input.x, input.y}); }
31
32
// get_pi - get results from pid controller
33
Vector2f get_pi();
34
Vector2f get_p() const;
35
Vector2f get_i();
36
Vector2f get_i_shrink(); // get_i but do not allow integrator to grow (it may shrink)
37
38
// reset_I - reset the integrator
39
void reset_I();
40
41
// reset_filter - input filter will be reset to the next value provided to set_input()
42
void reset_filter();
43
44
// load gain from eeprom
45
void load_gains();
46
47
// save gain to eeprom
48
void save_gains();
49
50
// get accessors
51
AP_Float &kP() { return _kp; }
52
AP_Float &kI() { return _ki; }
53
float imax() const { return _imax.get(); }
54
float filt_hz() const { return _filt_hz.get(); }
55
float get_filt_alpha() const { return _filt_alpha; }
56
57
// set accessors
58
void kP(float v) { _kp.set(v); }
59
void kI(float v) { _ki.set(v); }
60
void imax(float v) { _imax.set(fabsf(v)); }
61
void filt_hz(float hz);
62
63
Vector2f get_integrator() const { return _integrator; }
64
void set_integrator(const Vector2f &i) { _integrator = i; }
65
void set_integrator(const Vector3f &i) { _integrator.x = i.x; _integrator.y = i.y; }
66
67
// parameter var table
68
static const struct AP_Param::GroupInfo var_info[];
69
70
private:
71
72
// calc_filt_alpha - recalculate the input filter alpha
73
void calc_filt_alpha();
74
75
// parameters
76
AP_Float _kp;
77
AP_Float _ki;
78
AP_Float _imax;
79
AP_Float _filt_hz; // PI Input filter frequency in Hz
80
81
// flags
82
struct ac_pid_flags {
83
bool _reset_filter : 1; // true when input filter should be reset during next call to set_input
84
} _flags;
85
86
// internal variables
87
float _dt; // time step in seconds
88
Vector2f _integrator; // integrator value
89
Vector2f _input; // last input for derivative
90
float _filt_alpha; // input filter alpha
91
92
const float default_kp;
93
const float default_ki;
94
const float default_imax;
95
const float default_filt_hz;
96
97
};
98
99