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-08-17 03:22:43 -03:00
|
|
|
AP_GROUPINFO("P", 0, PID, _kp, 0),
|
|
|
|
AP_GROUPINFO("I", 1, PID, _ki, 0),
|
|
|
|
AP_GROUPINFO("D", 2, PID, _kd, 0),
|
|
|
|
AP_GROUPINFO("IMAX", 3, PID, _imax, 0),
|
|
|
|
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-08-17 03:22:43 -03:00
|
|
|
uint32_t tnow = millis();
|
|
|
|
uint32_t dt = tnow - _last_t;
|
|
|
|
float output = 0;
|
|
|
|
float delta_time;
|
|
|
|
|
|
|
|
if (_last_t == 0 || dt > 1000) {
|
|
|
|
dt = 0;
|
|
|
|
}
|
|
|
|
_last_t = tnow;
|
|
|
|
|
|
|
|
delta_time = (float)dt / 1000.0;
|
|
|
|
|
|
|
|
// Compute proportional component
|
|
|
|
output += error * _kp;
|
|
|
|
|
|
|
|
// Compute derivative component if time has elapsed
|
|
|
|
if ((fabs(_kd) > 0) && (dt > 0)) {
|
|
|
|
float derivative = (error - _last_error) / delta_time;
|
|
|
|
|
|
|
|
// discrete low pass filter, cuts out the
|
|
|
|
// high frequency noise that can drive the controller crazy
|
|
|
|
float RC = 1/(2*M_PI*_fCut);
|
|
|
|
derivative = _last_derivative +
|
|
|
|
(delta_time / (RC + delta_time)) * (derivative - _last_derivative);
|
|
|
|
|
|
|
|
// update state
|
|
|
|
_last_error = error;
|
|
|
|
_last_derivative = derivative;
|
|
|
|
|
|
|
|
// add in derivative component
|
|
|
|
output += _kd * derivative;
|
|
|
|
}
|
|
|
|
|
|
|
|
// scale the P and D components
|
|
|
|
output *= scaler;
|
|
|
|
|
|
|
|
// Compute integral component if time has elapsed
|
|
|
|
if ((fabs(_ki) > 0) && (dt > 0)) {
|
|
|
|
_integrator += (error * _ki) * scaler * delta_time;
|
|
|
|
if (_integrator < -_imax) {
|
|
|
|
_integrator = -_imax;
|
|
|
|
} else if (_integrator > _imax) {
|
|
|
|
_integrator = _imax;
|
|
|
|
}
|
|
|
|
output += _integrator;
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
2010-10-28 01:59:24 -03:00
|
|
|
}
|
|
|
|
|
2011-02-14 00:45:31 -04:00
|
|
|
void
|
|
|
|
PID::reset_I()
|
|
|
|
{
|
2012-08-17 03:22:43 -03:00
|
|
|
_integrator = 0;
|
|
|
|
_last_error = 0;
|
|
|
|
_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-08-17 03:22:43 -03:00
|
|
|
_kp.load();
|
|
|
|
_ki.load();
|
|
|
|
_kd.load();
|
|
|
|
_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-08-17 03:22:43 -03:00
|
|
|
_kp.save();
|
|
|
|
_ki.save();
|
|
|
|
_kd.save();
|
|
|
|
_imax.save();
|
2010-10-28 01:59:24 -03:00
|
|
|
}
|