2010-11-25 21:30:21 -04:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
|
|
|
|
/// @file PID.h
|
|
|
|
/// @brief Generic PID algorithm, with EEPROM-backed storage of constants.
|
|
|
|
|
2010-10-28 01:59:24 -03:00
|
|
|
#ifndef PID_h
|
|
|
|
#define PID_h
|
|
|
|
|
2011-02-14 00:45:31 -04:00
|
|
|
#include <AP_Common.h>
|
|
|
|
#include <math.h> // for fabs()
|
2011-08-14 02:44:29 -03:00
|
|
|
#define PID_FILTER_SIZE 6
|
2010-10-28 01:59:24 -03:00
|
|
|
|
2010-11-25 21:30:21 -04:00
|
|
|
/// @class PID
|
|
|
|
/// @brief Object managing one PID control
|
2010-10-28 01:59:24 -03:00
|
|
|
class PID {
|
|
|
|
public:
|
2010-11-27 16:30:53 -04:00
|
|
|
|
2011-02-14 00:45:31 -04:00
|
|
|
/// Constructor for PID that saves its settings to EEPROM
|
|
|
|
///
|
|
|
|
/// @note PIDs must be named to avoid either multiple parameters with the
|
|
|
|
/// same name, or an overly complex constructor.
|
|
|
|
///
|
|
|
|
/// @param key Storage key assigned to this PID. Should be unique.
|
|
|
|
/// @param name Name by which the PID is known, or NULL for an anonymous PID.
|
|
|
|
/// The name is prefixed to the P, I, D, IMAX variable names when
|
|
|
|
/// they are reported.
|
|
|
|
/// @param initial_p Initial value for the P term.
|
|
|
|
/// @param initial_i Initial value for the I term.
|
|
|
|
/// @param initial_d Initial value for the D term.
|
|
|
|
/// @param initial_imax Initial value for the imax term.4
|
|
|
|
///
|
|
|
|
PID(AP_Var::Key key,
|
2011-03-21 04:25:48 -03:00
|
|
|
const prog_char_t *name,
|
2011-02-14 00:45:31 -04:00
|
|
|
const float &initial_p = 0.0,
|
|
|
|
const float &initial_i = 0.0,
|
|
|
|
const float &initial_d = 0.0,
|
|
|
|
const int16_t &initial_imax = 0.0) :
|
|
|
|
|
|
|
|
_group(key, name),
|
|
|
|
// group, index, initial value, name
|
|
|
|
_kp (&_group, 0, initial_p, PSTR("P")),
|
|
|
|
_ki (&_group, 1, initial_i, PSTR("I")),
|
|
|
|
_kd (&_group, 2, initial_d, PSTR("D")),
|
|
|
|
_imax(&_group, 3, initial_imax, PSTR("IMAX"))
|
2010-12-06 07:52:31 -04:00
|
|
|
{
|
2011-02-14 00:45:31 -04:00
|
|
|
// no need for explicit load, assuming that the main code uses AP_Var::load_all.
|
2010-12-06 07:52:31 -04:00
|
|
|
}
|
2010-11-25 21:30:21 -04:00
|
|
|
|
2011-02-14 00:45:31 -04:00
|
|
|
/// Constructor for PID that does not save its settings.
|
|
|
|
///
|
|
|
|
/// @param name Name by which the PID is known, or NULL for an anonymous PID.
|
|
|
|
/// The name is prefixed to the P, I, D, IMAX variable names when
|
|
|
|
/// they are reported.
|
|
|
|
/// @param initial_p Initial value for the P term.
|
|
|
|
/// @param initial_i Initial value for the I term.
|
|
|
|
/// @param initial_d Initial value for the D term.
|
|
|
|
/// @param initial_imax Initial value for the imax term.4
|
|
|
|
///
|
2011-03-21 04:25:48 -03:00
|
|
|
PID(const prog_char_t *name,
|
2011-02-14 00:45:31 -04:00
|
|
|
const float &initial_p = 0.0,
|
|
|
|
const float &initial_i = 0.0,
|
|
|
|
const float &initial_d = 0.0,
|
|
|
|
const int16_t &initial_imax = 0.0) :
|
|
|
|
|
|
|
|
_group(AP_Var::k_key_none, name),
|
|
|
|
// group, index, initial value, name
|
|
|
|
_kp (&_group, 0, initial_p, PSTR("P")),
|
|
|
|
_ki (&_group, 1, initial_i, PSTR("I")),
|
|
|
|
_kd (&_group, 2, initial_d, PSTR("D")),
|
|
|
|
_imax(&_group, 3, initial_imax, PSTR("IMAX"))
|
|
|
|
{
|
2010-12-06 07:52:31 -04:00
|
|
|
}
|
2010-11-25 21:30:21 -04:00
|
|
|
|
|
|
|
/// Iterate the PID, return the new control value
|
|
|
|
///
|
|
|
|
/// Positive error produces positive output.
|
|
|
|
///
|
2010-11-26 00:36:43 -04:00
|
|
|
/// @param error The measured error value
|
|
|
|
/// @param dt The time delta in milliseconds (note
|
|
|
|
/// that update interval cannot be more
|
|
|
|
/// than 65.535 seconds due to limited range
|
|
|
|
/// of the data type).
|
2010-11-25 21:30:21 -04:00
|
|
|
/// @param scaler An arbitrary scale factor
|
|
|
|
///
|
|
|
|
/// @returns The updated control output.
|
2011-02-14 00:45:31 -04:00
|
|
|
///
|
2010-11-26 00:36:43 -04:00
|
|
|
long get_pid(int32_t error, uint16_t dt, float scaler = 1.0);
|
2010-11-25 21:30:21 -04:00
|
|
|
|
2011-05-16 13:05:49 -03:00
|
|
|
long get_pi(int32_t error, uint16_t dt, float scaler = 1.0);
|
|
|
|
|
|
|
|
|
2010-11-25 21:30:21 -04:00
|
|
|
/// Reset the PID integrator
|
|
|
|
///
|
2011-02-14 00:45:31 -04:00
|
|
|
void reset_I();
|
2010-11-25 21:30:21 -04:00
|
|
|
|
|
|
|
/// Load gain properties
|
|
|
|
///
|
2010-11-25 20:06:06 -04:00
|
|
|
void load_gains();
|
2010-11-25 21:30:21 -04:00
|
|
|
|
|
|
|
/// Save gain properties
|
|
|
|
///
|
2010-11-25 20:06:06 -04:00
|
|
|
void save_gains();
|
2010-11-25 21:30:21 -04:00
|
|
|
|
|
|
|
/// @name parameter accessors
|
|
|
|
//@{
|
2010-10-28 01:59:24 -03:00
|
|
|
|
2011-02-14 00:45:31 -04:00
|
|
|
/// Overload the function call operator to permit relatively easy initialisation
|
|
|
|
void operator() (const float p,
|
|
|
|
const float i,
|
|
|
|
const float d,
|
2011-06-15 09:25:00 -03:00
|
|
|
const int16_t imaxval) {
|
|
|
|
_kp = p; _ki = i; _kd = d; _imax = imaxval;
|
2011-02-14 00:45:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
float kP() const { return _kp.get(); }
|
|
|
|
float kI() const { return _ki.get(); }
|
|
|
|
float kD() const { return _kd.get(); }
|
|
|
|
int16_t imax() const { return _imax.get(); }
|
|
|
|
|
2011-02-19 17:01:32 -04:00
|
|
|
void kP(const float v) { _kp.set(v); }
|
|
|
|
void kI(const float v) { _ki.set(v); }
|
|
|
|
void kD(const float v) { _kd.set(v); }
|
|
|
|
void imax(const int16_t v) { _imax.set(abs(v)); }
|
2011-07-29 18:52:37 -03:00
|
|
|
//void filter_size(uint8_t v) { _filter_size = min(v, PID_FILTER_SIZE); }
|
2010-11-25 21:30:21 -04:00
|
|
|
|
2011-02-14 00:45:31 -04:00
|
|
|
float get_integrator() const { return _integrator; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
AP_Var_group _group;
|
|
|
|
AP_Float16 _kp;
|
|
|
|
AP_Float16 _ki;
|
|
|
|
AP_Float16 _kd;
|
|
|
|
AP_Int16 _imax;
|
2011-07-29 18:52:37 -03:00
|
|
|
float _filter[PID_FILTER_SIZE];
|
|
|
|
uint8_t _filter_index;
|
2010-11-25 21:30:21 -04:00
|
|
|
|
2011-02-14 00:45:31 -04:00
|
|
|
float _integrator; ///< integrator value
|
|
|
|
int32_t _last_error; ///< last error for derivative
|
2011-07-29 18:52:37 -03:00
|
|
|
//float _last_derivative; ///< last derivative for low-pass filter
|
2010-11-25 21:30:21 -04:00
|
|
|
|
2010-11-26 00:36:43 -04:00
|
|
|
/// Low pass filter cut frequency for derivative calculation.
|
|
|
|
///
|
|
|
|
/// 20 Hz becasue anything over that is probably noise, see
|
|
|
|
/// http://en.wikipedia.org/wiki/Low-pass_filter.
|
|
|
|
///
|
2011-02-14 00:45:31 -04:00
|
|
|
static const uint8_t _fCut = 20;
|
2010-10-28 01:59:24 -03:00
|
|
|
};
|
|
|
|
|
2010-11-25 19:14:37 -04:00
|
|
|
#endif
|