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_P_2D.cpp
Views: 1798
1
/// @file AC_P_2D.cpp
2
/// @brief 2-axis P controller
3
4
#include <AP_Math/AP_Math.h>
5
#include "AC_P_2D.h"
6
7
const AP_Param::GroupInfo AC_P_2D::var_info[] = {
8
// @Param: P
9
// @DisplayName: Proportional Gain
10
// @Description: P Gain which produces an output value that is proportional to the current error value
11
AP_GROUPINFO_FLAGS_DEFAULT_POINTER("P", 0, AC_P_2D, _kp, default_kp),
12
AP_GROUPEND
13
};
14
15
// Constructor
16
AC_P_2D::AC_P_2D(float initial_p) :
17
default_kp(initial_p)
18
{
19
// load parameter values from eeprom
20
AP_Param::setup_object_defaults(this, var_info);
21
}
22
23
// update_all - set target and measured inputs to P controller and calculate outputs
24
Vector2f AC_P_2D::update_all(postype_t &target_x, postype_t &target_y, const Vector2f &measurement)
25
{
26
// calculate distance _error
27
_error = (Vector2p{target_x, target_y} - measurement.topostype()).tofloat();
28
29
// Constrain _error and target position
30
// Constrain the maximum length of _vel_target to the maximum position correction velocity
31
if (is_positive(_error_max) && _error.limit_length(_error_max)) {
32
target_x = measurement.x + _error.x;
33
target_y = measurement.y + _error.y;
34
}
35
36
// MIN(_Dmax, _D2max / _kp) limits the max accel to the point where max jerk is exceeded
37
return sqrt_controller(_error, _kp, _D1_max, 0.0);
38
}
39
40
// set_limits - sets the maximum error to limit output and first and second derivative of output
41
// when using for a position controller, lim_err will be position error, lim_out will be correction velocity, lim_D will be acceleration, lim_D2 will be jerk
42
void AC_P_2D::set_limits(float output_max, float D_Out_max, float D2_Out_max)
43
{
44
_D1_max = 0.0f;
45
_error_max = 0.0f;
46
47
if (is_positive(D_Out_max)) {
48
_D1_max = D_Out_max;
49
}
50
51
if (is_positive(D2_Out_max) && is_positive(_kp)) {
52
// limit the first derivative so as not to exceed the second derivative
53
_D1_max = MIN(_D1_max, D2_Out_max / _kp);
54
}
55
56
if (is_positive(output_max) && is_positive(_kp)) {
57
_error_max = inv_sqrt_controller(output_max, _kp, _D1_max);
58
}
59
}
60
61
// set_error_max - reduce maximum position error to error_max
62
// to be called after setting limits
63
void AC_P_2D::set_error_max(float error_max)
64
{
65
if (is_positive(error_max)) {
66
if (!is_zero(_error_max) ) {
67
_error_max = MIN(_error_max, error_max);
68
} else {
69
_error_max = error_max;
70
}
71
}
72
}
73
74