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_2D.cpp
Views: 1798
/// @file AC_P_2D.cpp1/// @brief 2-axis P controller23#include <AP_Math/AP_Math.h>4#include "AC_P_2D.h"56const AP_Param::GroupInfo AC_P_2D::var_info[] = {7// @Param: P8// @DisplayName: 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_2D, _kp, default_kp),11AP_GROUPEND12};1314// Constructor15AC_P_2D::AC_P_2D(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 outputs23Vector2f AC_P_2D::update_all(postype_t &target_x, postype_t &target_y, const Vector2f &measurement)24{25// calculate distance _error26_error = (Vector2p{target_x, target_y} - measurement.topostype()).tofloat();2728// Constrain _error and target position29// Constrain the maximum length of _vel_target to the maximum position correction velocity30if (is_positive(_error_max) && _error.limit_length(_error_max)) {31target_x = measurement.x + _error.x;32target_y = measurement.y + _error.y;33}3435// MIN(_Dmax, _D2max / _kp) limits the max accel to the point where max jerk is exceeded36return sqrt_controller(_error, _kp, _D1_max, 0.0);37}3839// set_limits - sets the maximum error to limit output and first and second derivative of output40// 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 jerk41void AC_P_2D::set_limits(float output_max, float D_Out_max, float D2_Out_max)42{43_D1_max = 0.0f;44_error_max = 0.0f;4546if (is_positive(D_Out_max)) {47_D1_max = D_Out_max;48}4950if (is_positive(D2_Out_max) && is_positive(_kp)) {51// limit the first derivative so as not to exceed the second derivative52_D1_max = MIN(_D1_max, D2_Out_max / _kp);53}5455if (is_positive(output_max) && is_positive(_kp)) {56_error_max = inv_sqrt_controller(output_max, _kp, _D1_max);57}58}5960// set_error_max - reduce maximum position error to error_max61// to be called after setting limits62void AC_P_2D::set_error_max(float error_max)63{64if (is_positive(error_max)) {65if (!is_zero(_error_max) ) {66_error_max = MIN(_error_max, error_max);67} else {68_error_max = error_max;69}70}71}727374