Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AC_PID/AC_P_1D.cpp
Views: 1798
/// @file AC_P_1D.cpp1/// @brief Generic P algorithm23#include <AP_Math/AP_Math.h>4#include "AC_P_1D.h"56const AP_Param::GroupInfo AC_P_1D::var_info[] = {7// @Param: P8// @DisplayName: P Proportional Gain9// @Description: P Gain which produces an output value that is proportional to the current error value10AP_GROUPINFO_FLAGS_DEFAULT_POINTER("P", 0, AC_P_1D, _kp, default_kp),11AP_GROUPEND12};1314// Constructor15AC_P_1D::AC_P_1D(float initial_p) :16default_kp(initial_p)17{18// load parameter values from eeprom19AP_Param::setup_object_defaults(this, var_info);20}2122// update_all - set target and measured inputs to P controller and calculate outputs23// target and measurement are filtered24float AC_P_1D::update_all(float &target, float measurement)25{26// calculate distance _error27_error = target - measurement;2829if (is_negative(_error_min) && (_error < _error_min)) {30_error = _error_min;31target = measurement + _error;32} else if (is_positive(_error_max) && (_error > _error_max)) {33_error = _error_max;34target = measurement + _error;35}3637// MIN(_Dxy_max, _D2xy_max / _kxy_P) limits the max accel to the point where max jerk is exceeded38return sqrt_controller(_error, _kp, _D1_max, 0.0);39}4041// set_limits - sets the maximum error to limit output and first and second derivative of output42// 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 jerk43void AC_P_1D::set_limits(float output_min, float output_max, float D_Out_max, float D2_Out_max)44{45_D1_max = 0.0f;46_error_min = 0.0f;47_error_max = 0.0f;4849if (is_positive(D_Out_max)) {50_D1_max = D_Out_max;51}5253if (is_positive(D2_Out_max) && is_positive(_kp)) {54// limit the first derivative so as not to exceed the second derivative55_D1_max = MIN(_D1_max, D2_Out_max / _kp);56}5758if (is_negative(output_min) && is_positive(_kp)) {59_error_min = inv_sqrt_controller(output_min, _kp, _D1_max);60}6162if (is_positive(output_max) && is_positive(_kp)) {63_error_max = inv_sqrt_controller(output_max, _kp, _D1_max);64}65}6667// set_error_limits - reduce maximum error to error_max68// to be called after setting limits69void AC_P_1D::set_error_limits(float error_min, float error_max)70{71if (is_negative(error_min)) {72if (!is_zero(_error_min)) {73_error_min = MAX(_error_min, error_min);74} else {75_error_min = error_min;76}77}7879if (is_positive(error_max)) {80if (!is_zero(_error_max)) {81_error_max = MIN(_error_max, error_max);82} else {83_error_max = error_max;84}85}86}878889