2020-01-04 02:25:34 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/// @file AC_P_2D.h
|
|
|
|
/// @brief 2-axis P controller with EEPROM-backed storage of constants.
|
|
|
|
|
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_Param/AP_Param.h>
|
|
|
|
|
|
|
|
/// @class AC_P_2D
|
|
|
|
/// @brief 2-axis P controller
|
|
|
|
class AC_P_2D {
|
|
|
|
public:
|
|
|
|
|
|
|
|
// constructor
|
2022-12-02 08:34:53 -04:00
|
|
|
AC_P_2D(float initial_p);
|
2020-01-04 02:25:34 -04:00
|
|
|
|
2021-06-06 05:33:35 -03:00
|
|
|
CLASS_NO_COPY(AC_P_2D);
|
|
|
|
|
2020-01-04 02:25:34 -04:00
|
|
|
// set target and measured inputs to P controller and calculate outputs
|
2021-11-21 08:29:20 -04:00
|
|
|
Vector2f update_all(postype_t &target_x, postype_t &target_y, const Vector2f &measurement) WARN_IF_UNUSED;
|
2020-01-04 02:25:34 -04:00
|
|
|
|
|
|
|
// set target and measured inputs to P controller and calculate outputs
|
|
|
|
// measurement is provided as 3-axis vector but only x and y are used
|
2021-11-21 08:29:20 -04:00
|
|
|
Vector2f update_all(postype_t &target_x, postype_t &target_y, const Vector3f &measurement) WARN_IF_UNUSED {
|
|
|
|
return update_all(target_x, target_y, Vector2f{measurement.x, measurement.y});
|
2020-01-04 02:25:34 -04:00
|
|
|
}
|
|
|
|
|
2021-04-16 00:34:59 -03:00
|
|
|
// set_limits - sets the maximum error to limit output and first and second derivative of output
|
|
|
|
void set_limits(float output_max, float D_Out_max = 0.0f, float D2_Out_max = 0.0f);
|
|
|
|
|
|
|
|
// set_error_max - reduce maximum position error to error_max
|
|
|
|
// to be called after setting limits
|
|
|
|
void set_error_max(float error_max);
|
|
|
|
|
|
|
|
// get_error_max - return maximum position error
|
|
|
|
float get_error_max() { return _error_max; }
|
|
|
|
|
|
|
|
// save gain to eeprom
|
|
|
|
void save_gains() { _kp.save(); }
|
|
|
|
|
2020-01-04 02:25:34 -04:00
|
|
|
// get accessors
|
|
|
|
AP_Float &kP() WARN_IF_UNUSED { return _kp; }
|
|
|
|
const AP_Float &kP() const WARN_IF_UNUSED { return _kp; }
|
2021-04-16 00:34:59 -03:00
|
|
|
const Vector2f& get_error() const { return _error; }
|
2020-01-04 02:25:34 -04:00
|
|
|
|
2021-04-16 00:34:59 -03:00
|
|
|
// set accessors
|
2020-01-04 02:25:34 -04:00
|
|
|
void kP(float v) { _kp.set(v); }
|
|
|
|
|
|
|
|
// parameter var table
|
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
|
|
|
private:
|
2021-04-16 00:34:59 -03:00
|
|
|
|
2020-01-04 02:25:34 -04:00
|
|
|
// parameters
|
|
|
|
AP_Float _kp;
|
|
|
|
|
|
|
|
// internal variables
|
2021-04-16 00:34:59 -03:00
|
|
|
Vector2f _error; // time step in seconds
|
2022-01-10 23:07:53 -04:00
|
|
|
float _error_max; // error limit in positive direction
|
2021-04-16 00:34:59 -03:00
|
|
|
float _D1_max; // maximum first derivative of output
|
2023-01-03 13:22:36 -04:00
|
|
|
|
|
|
|
const float default_kp;
|
2020-01-04 02:25:34 -04:00
|
|
|
};
|