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.cpp
Views: 1795
1
/*
2
Generic PI algorithm for controllers that don't need filtering (such as heaters)
3
*/
4
5
#include <AP_Math/AP_Math.h>
6
#include "AC_PI.h"
7
8
const AP_Param::GroupInfo AC_PI::var_info[] = {
9
// @Param: P
10
// @DisplayName: PID Proportional Gain
11
// @Description: P Gain which produces an output value that is proportional to the current error value
12
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("P", 1, AC_PI, kP, default_kp),
13
14
// @Param: I
15
// @DisplayName: PID Integral Gain
16
// @Description: I Gain which produces an output that is proportional to both the magnitude and the duration of the error
17
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("I", 2, AC_PI, kI, default_ki),
18
19
// @Param: IMAX
20
// @DisplayName: PID Integral Maximum
21
// @Description: The maximum/minimum value that the I term can output
22
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("IMAX", 3, AC_PI, imax, default_imax),
23
24
AP_GROUPEND
25
};
26
27
// Constructor
28
AC_PI::AC_PI(float initial_p, float initial_i, float initial_imax) :
29
default_kp(initial_p),
30
default_ki(initial_i),
31
default_imax(initial_imax)
32
{
33
// load parameter values from eeprom
34
AP_Param::setup_object_defaults(this, var_info);
35
}
36
37
float AC_PI::update(float measurement, float target, float dt)
38
{
39
const float err = target - measurement;
40
41
integrator += kI * err * dt;
42
integrator = constrain_float(integrator, 0, imax);
43
output_P = kP * err;
44
45
return output_P + integrator;
46
}
47
48