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()
|
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
|
|
|
|
2012-02-11 07:54:10 -04:00
|
|
|
PID(const float &initial_p = 0.0,
|
2011-02-14 00:45:31 -04:00
|
|
|
const float &initial_i = 0.0,
|
|
|
|
const float &initial_d = 0.0,
|
2012-02-12 23:11:21 -04:00
|
|
|
const int16_t &initial_imax = 0) :
|
|
|
|
_kp (initial_p),
|
|
|
|
_ki (initial_i),
|
|
|
|
_kd (initial_d),
|
|
|
|
_imax(initial_imax)
|
2011-02-14 00:45:31 -04:00
|
|
|
{
|
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
|
|
|
///
|
2012-07-06 06:55:48 -03:00
|
|
|
int32_t get_pid(int32_t error, 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)); }
|
2010-11-25 21:30:21 -04:00
|
|
|
|
2011-02-14 00:45:31 -04:00
|
|
|
float get_integrator() const { return _integrator; }
|
|
|
|
|
2012-02-11 07:54:10 -04:00
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
2011-02-14 00:45:31 -04:00
|
|
|
private:
|
2012-02-11 07:54:10 -04:00
|
|
|
AP_Float _kp;
|
|
|
|
AP_Float _ki;
|
|
|
|
AP_Float _kd;
|
2011-02-14 00:45:31 -04:00
|
|
|
AP_Int16 _imax;
|
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-09-04 14:47:07 -03:00
|
|
|
float _last_derivative; ///< last derivative for low-pass filter
|
2012-07-06 06:55:48 -03:00
|
|
|
uint32_t _last_t;
|
|
|
|
|
|
|
|
int32_t _get_pid(int32_t error, uint16_t dt, float scaler);
|
2012-02-12 05:32:03 -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.
|
|
|
|
///
|
|
|
|
static const uint8_t _fCut = 20;
|
2010-10-28 01:59:24 -03:00
|
|
|
};
|
|
|
|
|
2010-11-25 19:14:37 -04:00
|
|
|
#endif
|