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
|
|
|
|
|
2010-11-25 21:30:21 -04:00
|
|
|
#include <stdint.h>
|
2010-12-25 21:17:04 -04:00
|
|
|
#include <AP_EEPROMB.h>
|
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-12-04 06:32:19 -04:00
|
|
|
///
|
|
|
|
// EEProm Storage Type
|
|
|
|
enum storage_t
|
|
|
|
{
|
|
|
|
STORE_OFF,
|
|
|
|
STORE_EEPROM_FLOAT,
|
|
|
|
STORE_EEPROM_UINT16
|
|
|
|
} _storage;
|
|
|
|
|
2010-11-27 16:30:53 -04:00
|
|
|
/// Constructor
|
|
|
|
///
|
|
|
|
/// A PID constructed in this fashion does not support save/restore.
|
2010-11-28 17:51:10 -04:00
|
|
|
/// Gains are managed internally, and must be read/written using the
|
|
|
|
/// accessor functions.
|
2010-11-27 16:30:53 -04:00
|
|
|
///
|
|
|
|
PID() :
|
2010-12-04 06:32:19 -04:00
|
|
|
_storage(STORE_OFF),
|
2010-11-28 17:51:10 -04:00
|
|
|
_address(0),
|
|
|
|
_gain_array(&_local_gains[0])
|
2010-11-27 16:30:53 -04:00
|
|
|
{}
|
|
|
|
|
2010-11-25 21:30:21 -04:00
|
|
|
/// Constructor
|
|
|
|
///
|
2010-11-28 17:51:10 -04:00
|
|
|
/// The PID will manage gains internally, and the load/save functions
|
|
|
|
/// will use 16 bytes of EEPROM storage to store gain values.
|
|
|
|
///
|
2010-11-25 21:30:21 -04:00
|
|
|
/// @param address EEPROM base address at which PID parameters
|
2010-11-28 17:51:10 -04:00
|
|
|
/// are stored.
|
2010-11-25 21:30:21 -04:00
|
|
|
///
|
2010-12-04 06:32:19 -04:00
|
|
|
PID(uint16_t address, storage_t storage = STORE_EEPROM_UINT16) :
|
|
|
|
_storage(storage),
|
2010-11-28 17:51:10 -04:00
|
|
|
_address(address),
|
|
|
|
_gain_array(&_local_gains[0])
|
2010-12-06 07:52:31 -04:00
|
|
|
{
|
|
|
|
load_gains();
|
|
|
|
}
|
2010-11-25 21:30:21 -04:00
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
///
|
2010-11-28 17:51:10 -04:00
|
|
|
/// Gain values for the PID are managed externally; load/save are a NOP.
|
|
|
|
///
|
|
|
|
/// @param gain_array Address of an array of float values. The
|
|
|
|
/// array is used as kP, kI, kD and imax
|
|
|
|
/// respectively.
|
2010-11-25 21:30:21 -04:00
|
|
|
///
|
2010-12-25 21:17:04 -04:00
|
|
|
|
2010-11-25 21:30:21 -04:00
|
|
|
PID(float *gain_array) :
|
2010-12-04 06:32:19 -04:00
|
|
|
_storage(STORE_OFF),
|
2010-11-28 17:51:10 -04:00
|
|
|
_gain_array(gain_array)
|
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.
|
2010-12-25 21:17:04 -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
|
|
|
|
|
|
|
/// Reset the PID integrator
|
|
|
|
///
|
2010-11-25 21:53:12 -04:00
|
|
|
void reset_I() {
|
|
|
|
_integrator = 0;
|
|
|
|
_last_error = 0;
|
|
|
|
_last_derivative = 0;
|
|
|
|
}
|
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-12-13 00:05:55 -04:00
|
|
|
float kP() { return _gain_array[0]; }
|
|
|
|
float kI() { return _gain_array[1]; }
|
|
|
|
float kD() { return _gain_array[2]; }
|
|
|
|
float imax() { return _gain_array[3]; }
|
2010-11-28 17:51:10 -04:00
|
|
|
|
|
|
|
void kP(const float v) { _gain_array[0] = v; }
|
|
|
|
void kI(const float v) { _gain_array[1] = v; }
|
|
|
|
void kD(const float v) { _gain_array[2] = v; }
|
|
|
|
void imax(const float v);
|
2010-12-25 21:17:04 -04:00
|
|
|
|
|
|
|
void address(const uint16_t v) { _address = v; }
|
|
|
|
|
2010-11-28 17:51:10 -04:00
|
|
|
// one-shot operator for setting all of the gain properties at once
|
2010-12-25 21:17:04 -04:00
|
|
|
//void operator ()(const float p, const float i, const float d, const float max)
|
|
|
|
//{ kP(p); kI(i); kD(d); imax(max); }
|
2010-11-25 21:30:21 -04:00
|
|
|
//@}
|
2010-12-13 00:05:55 -04:00
|
|
|
|
|
|
|
float get_integrator() { return _integrator; }
|
2010-10-28 01:59:24 -03:00
|
|
|
|
|
|
|
private:
|
2010-12-25 21:17:04 -04:00
|
|
|
AP_EEPROMB _ee;
|
2010-11-28 17:51:10 -04:00
|
|
|
uint16_t _address; ///< EEPROM address for save/restore of P/I/D
|
|
|
|
float *_gain_array; ///< pointer to the gains for this pid
|
2010-11-25 21:30:21 -04:00
|
|
|
|
2010-11-28 17:51:10 -04:00
|
|
|
float _local_gains[4]; ///< local storage for gains when not globally managed
|
2010-11-25 21:30:21 -04:00
|
|
|
|
2010-11-28 17:51:10 -04:00
|
|
|
float _integrator; ///< integrator value
|
|
|
|
int32_t _last_error; ///< last error for derivative
|
|
|
|
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.
|
|
|
|
///
|
2010-11-25 21:53:12 -04:00
|
|
|
static const uint8_t _RC = 20;
|
2010-10-28 01:59:24 -03:00
|
|
|
};
|
|
|
|
|
2010-11-25 19:14:37 -04:00
|
|
|
#endif
|