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
|
|
|
|
/// @brief Generic PID algorithm, with EEPROM-backed storage of constants.
|
|
|
|
|
2010-11-25 21:53:12 -04:00
|
|
|
#include <math.h>
|
2010-11-25 21:54:47 -04:00
|
|
|
#include <avr/eeprom.h>
|
|
|
|
|
2010-11-25 21:30:21 -04:00
|
|
|
#include "PID.h"
|
2010-10-28 01:59:24 -03:00
|
|
|
|
2010-11-28 17:51:10 -04:00
|
|
|
// make the gain array members a little easier to identify
|
|
|
|
#define _kp _gain_array[0]
|
|
|
|
#define _ki _gain_array[1]
|
|
|
|
#define _kd _gain_array[2]
|
|
|
|
#define _imax _gain_array[3]
|
|
|
|
|
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
|
|
|
|
if (_kd && (dt > 0)) {
|
|
|
|
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 +
|
2010-12-27 01:59:49 -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
|
|
|
|
if (_ki && (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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-25 21:53:12 -04:00
|
|
|
PID::imax(float v)
|
2010-10-28 01:59:24 -03:00
|
|
|
{
|
2011-01-17 18:57:48 -04:00
|
|
|
_imax = fabs(v);
|
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
|
|
|
{
|
2010-12-04 06:32:19 -04:00
|
|
|
switch(_storage)
|
|
|
|
{
|
|
|
|
case STORE_OFF:
|
|
|
|
// load is a NOP if the gain array is managed externally
|
|
|
|
break;
|
|
|
|
case STORE_EEPROM_UINT16:
|
2010-12-28 14:42:56 -04:00
|
|
|
_kp = (float)(eeprom_read_word((uint16_t *) _address)) / 1000.0;
|
|
|
|
_ki = (float)(eeprom_read_word((uint16_t *) (_address + 2))) / 1000.0;
|
|
|
|
_kd = (float)(eeprom_read_word((uint16_t *) (_address + 4))) / 1000.0;
|
|
|
|
_imax = eeprom_read_word((uint16_t *) (_address + 6)) * 100;
|
2011-01-17 18:57:48 -04:00
|
|
|
|
2010-12-28 14:42:56 -04:00
|
|
|
//_kp = (float)_ee.read_int(_address) / 1000.0;
|
|
|
|
//_ki = (float)_ee.read_int(_address + 2) / 1000.0;
|
|
|
|
//_kd = (float)_ee.read_int(_address + 4) / 1000.0;
|
|
|
|
//_imax = (float)_ee.read_int(_address + 6) * 100;
|
2010-12-25 21:17:04 -04:00
|
|
|
|
2010-12-04 06:32:19 -04:00
|
|
|
break;
|
2011-01-17 18:57:48 -04:00
|
|
|
|
2010-12-04 06:32:19 -04:00
|
|
|
case STORE_EEPROM_FLOAT:
|
2011-01-17 18:57:48 -04:00
|
|
|
eeprom_read_block((void*)&_kp,(const void*)(_address),sizeof(_kp));
|
|
|
|
eeprom_read_block((void*)&_ki,(const void*)(_address+4),sizeof(_ki));
|
|
|
|
eeprom_read_block((void*)&_kd,(const void*)(_address+8),sizeof(_kd));
|
|
|
|
eeprom_read_block((void*)&_imax,(const void*)(_address+12),sizeof(_imax));
|
2010-12-04 06:32:19 -04:00
|
|
|
break;
|
2010-11-25 21:30:21 -04:00
|
|
|
}
|
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
|
|
|
{
|
2010-12-04 06:32:19 -04:00
|
|
|
switch(_storage)
|
|
|
|
{
|
|
|
|
case STORE_OFF:
|
|
|
|
// save is a NOP if the gain array is managed externally
|
|
|
|
break;
|
|
|
|
case STORE_EEPROM_UINT16:
|
|
|
|
eeprom_write_word((uint16_t *) _address, (int)(_kp * 1000));
|
|
|
|
eeprom_write_word((uint16_t *) (_address + 2), (int)(_ki * 1000));
|
|
|
|
eeprom_write_word((uint16_t *) (_address + 4), (int)(_kd * 1000));
|
|
|
|
eeprom_write_word((uint16_t *) (_address + 6), (int)_imax/100);
|
2010-12-28 14:42:56 -04:00
|
|
|
|
|
|
|
/*_ee.write_int(_address, (int)(_kp * 1000));
|
2010-12-25 21:17:04 -04:00
|
|
|
_ee.write_int(_address + 2, (int)(_ki * 1000));
|
|
|
|
_ee.write_int(_address + 4, (int)(_kd * 1000));
|
|
|
|
_ee.write_int(_address + 6, (int)(_imax /100));
|
2010-12-28 14:42:56 -04:00
|
|
|
*/
|
2010-12-04 06:32:19 -04:00
|
|
|
break;
|
|
|
|
case STORE_EEPROM_FLOAT:
|
2011-01-17 18:57:48 -04:00
|
|
|
eeprom_write_block((const void *)&_kp,(void *)(_address),sizeof(_kp));
|
|
|
|
eeprom_write_block((const void *)&_ki,(void *)(_address+4),sizeof(_ki));
|
|
|
|
eeprom_write_block((const void *)&_kd,(void *)(_address+8),sizeof(_kd));
|
|
|
|
eeprom_write_block((const void *)&_imax,(void *)(_address+12),sizeof(_imax));
|
2010-12-04 06:32:19 -04:00
|
|
|
break;
|
2010-11-25 21:30:21 -04:00
|
|
|
}
|
2010-10-28 01:59:24 -03:00
|
|
|
}
|
|
|
|
|