2010-11-25 21:30:21 -04:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*-
|
2010-10-28 01:59:24 -03:00
|
|
|
|
2010-11-25 21:30:21 -04:00
|
|
|
/// @file PID.cpp
|
2011-02-14 00:45:31 -04:00
|
|
|
/// @brief Generic PID algorithm
|
2010-11-25 21:30:21 -04:00
|
|
|
|
2010-11-25 21:53:12 -04:00
|
|
|
#include <math.h>
|
2010-11-25 21:54:47 -04:00
|
|
|
|
2010-11-25 21:30:21 -04:00
|
|
|
#include "PID.h"
|
2010-10-28 01:59:24 -03:00
|
|
|
|
2012-02-11 07:54:10 -04:00
|
|
|
const AP_Param::GroupInfo PID::var_info[] PROGMEM = {
|
2012-02-12 18:55:13 -04:00
|
|
|
AP_GROUPINFO("P", 0, PID, _kp),
|
|
|
|
AP_GROUPINFO("I", 1, PID, _ki),
|
|
|
|
AP_GROUPINFO("D", 2, PID, _kd),
|
|
|
|
AP_GROUPINFO("IMAX", 3, PID, _imax),
|
2012-02-12 03:23:19 -04:00
|
|
|
AP_GROUPEND
|
2012-02-11 07:54:10 -04:00
|
|
|
};
|
|
|
|
|
2012-07-06 06:55:48 -03:00
|
|
|
int32_t
|
|
|
|
PID::get_pid(int32_t error, float scaler)
|
2010-10-28 01:59:24 -03:00
|
|
|
{
|
2012-07-06 06:55:48 -03:00
|
|
|
uint32_t tnow = millis();
|
|
|
|
uint32_t dt = tnow - _last_t;
|
2010-11-25 21:30:21 -04:00
|
|
|
float output = 0;
|
2012-07-06 06:55:48 -03:00
|
|
|
float delta_time;
|
|
|
|
|
|
|
|
if (_last_t == 0 || dt > 1000) {
|
|
|
|
dt = 0;
|
|
|
|
}
|
|
|
|
_last_t = tnow;
|
|
|
|
|
|
|
|
delta_time = (float)dt / 1000.0;
|
2010-11-25 21:30:21 -04:00
|
|
|
|
2010-10-28 01:59:24 -03:00
|
|
|
// Compute proportional component
|
2010-11-25 21:30:21 -04:00
|
|
|
output += error * _kp;
|
2010-10-28 01:59:24 -03:00
|
|
|
|
2010-11-25 21:30:21 -04:00
|
|
|
// Compute derivative component if time has elapsed
|
2011-02-14 00:45:31 -04:00
|
|
|
if ((fabs(_kd) > 0) && (dt > 0)) {
|
2011-09-04 14:47:07 -03:00
|
|
|
float derivative = (error - _last_error) / delta_time;
|
2010-11-25 19:14:37 -04:00
|
|
|
|
2011-01-17 18:57:48 -04:00
|
|
|
// discrete low pass filter, cuts out the
|
2010-11-25 19:14:37 -04:00
|
|
|
// high frequency noise that can drive the controller crazy
|
2012-02-12 05:32:03 -04:00
|
|
|
float RC = 1/(2*M_PI*_fCut);
|
2011-09-04 14:47:07 -03:00
|
|
|
derivative = _last_derivative +
|
|
|
|
(delta_time / (RC + delta_time)) * (derivative - _last_derivative);
|
2010-11-25 19:14:37 -04:00
|
|
|
|
2010-11-25 21:30:21 -04:00
|
|
|
// update state
|
2010-10-28 01:59:24 -03:00
|
|
|
_last_error = error;
|
2011-09-04 14:47:07 -03:00
|
|
|
_last_derivative = derivative;
|
2010-11-25 21:30:21 -04:00
|
|
|
|
|
|
|
// add in derivative component
|
|
|
|
output += _kd * derivative;
|
2010-10-28 01:59:24 -03:00
|
|
|
}
|
2010-11-25 21:30:21 -04:00
|
|
|
|
|
|
|
// scale the P and D components
|
2010-10-28 01:59:24 -03:00
|
|
|
output *= scaler;
|
2011-01-17 18:57:48 -04:00
|
|
|
|
2010-11-25 21:30:21 -04:00
|
|
|
// Compute integral component if time has elapsed
|
2011-02-14 00:45:31 -04:00
|
|
|
if ((fabs(_ki) > 0) && (dt > 0)) {
|
2011-01-17 18:57:48 -04:00
|
|
|
_integrator += (error * _ki) * scaler * delta_time;
|
2010-11-25 21:53:12 -04:00
|
|
|
if (_integrator < -_imax) {
|
|
|
|
_integrator = -_imax;
|
|
|
|
} else if (_integrator > _imax) {
|
|
|
|
_integrator = _imax;
|
|
|
|
}
|
2010-10-28 01:59:24 -03:00
|
|
|
output += _integrator;
|
|
|
|
}
|
2011-01-17 18:57:48 -04:00
|
|
|
|
2010-10-28 01:59:24 -03:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2011-02-14 00:45:31 -04:00
|
|
|
void
|
|
|
|
PID::reset_I()
|
|
|
|
{
|
|
|
|
_integrator = 0;
|
|
|
|
_last_error = 0;
|
2011-09-04 14:47:07 -03:00
|
|
|
_last_derivative = 0;
|
2011-02-14 00:45:31 -04:00
|
|
|
}
|
2010-10-28 01:59:24 -03:00
|
|
|
|
2010-11-25 19:26:44 -04:00
|
|
|
void
|
2010-11-25 20:06:06 -04:00
|
|
|
PID::load_gains()
|
2010-11-25 19:26:44 -04:00
|
|
|
{
|
2012-02-11 07:54:10 -04:00
|
|
|
_kp.load();
|
|
|
|
_ki.load();
|
|
|
|
_kd.load();
|
2012-02-12 23:22:39 -04:00
|
|
|
_imax.load();
|
2010-11-25 19:26:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-25 20:06:06 -04:00
|
|
|
PID::save_gains()
|
2010-11-25 19:26:44 -04:00
|
|
|
{
|
2012-02-11 07:54:10 -04:00
|
|
|
_kp.save();
|
|
|
|
_ki.save();
|
|
|
|
_kd.save();
|
2012-02-12 23:22:39 -04:00
|
|
|
_imax.save();
|
2010-10-28 01:59:24 -03:00
|
|
|
}
|