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
|
|
|
|
|
|
|
long
|
2010-11-26 00:36:43 -04:00
|
|
|
PID::get_pid(int32_t error, uint16_t dt, float scaler)
|
2010-10-28 01:59:24 -03:00
|
|
|
{
|
2010-11-25 21:30:21 -04:00
|
|
|
float output = 0;
|
2010-11-28 21:44:25 -04:00
|
|
|
float 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)) {
|
2010-11-25 21:30:21 -04: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
|
2010-12-27 01:59:49 -04:00
|
|
|
float RC = 1/(2*M_PI*_fCut);
|
2011-01-17 18:57:48 -04:00
|
|
|
derivative = _last_derivative +
|
2011-02-14 00:45:31 -04:00
|
|
|
(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;
|
2010-11-25 19:14:37 -04: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;
|
|
|
|
_last_derivative = 0;
|
|
|
|
}
|
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
|
|
|
{
|
2011-02-14 00:45:31 -04:00
|
|
|
_group.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
|
|
|
{
|
2011-02-14 00:45:31 -04:00
|
|
|
_group.save();
|
2010-10-28 01:59:24 -03:00
|
|
|
}
|