2011-09-04 21:56:50 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
|
|
|
|
/// @file ACM_PI.cpp
|
|
|
|
/// @brief Generic PI algorithm
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "APM_PI.h"
|
|
|
|
|
2012-02-12 07:26:12 -04:00
|
|
|
const AP_Param::GroupInfo APM_PI::var_info[] PROGMEM = {
|
2012-02-12 18:55:13 -04:00
|
|
|
AP_GROUPINFO("P", 0, APM_PI, _kp),
|
|
|
|
AP_GROUPINFO("I", 1, APM_PI, _ki),
|
|
|
|
AP_GROUPINFO("IMAX", 2, APM_PI, _imax),
|
2012-02-12 07:26:12 -04:00
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
2011-12-07 01:08:47 -04:00
|
|
|
int32_t APM_PI::get_p(int32_t error)
|
2011-09-04 21:56:50 -03:00
|
|
|
{
|
2011-12-07 01:08:47 -04:00
|
|
|
return (float)error * _kp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t APM_PI::get_i(int32_t error, float dt)
|
|
|
|
{
|
|
|
|
if(dt != 0){
|
2011-11-07 18:32:39 -04:00
|
|
|
_integrator += ((float)error * _ki) * dt;
|
2011-10-26 13:46:16 -03:00
|
|
|
|
2011-11-08 18:41:21 -04:00
|
|
|
if (_integrator < -_imax) {
|
|
|
|
_integrator = -_imax;
|
|
|
|
} else if (_integrator > _imax) {
|
|
|
|
_integrator = _imax;
|
|
|
|
}
|
2011-10-26 13:46:16 -03:00
|
|
|
}
|
2011-12-07 01:08:47 -04:00
|
|
|
return _integrator;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t APM_PI::get_pi(int32_t error, float dt)
|
|
|
|
{
|
|
|
|
return get_p(error) + get_i(error, dt);
|
2011-09-04 21:56:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
APM_PI::reset_I()
|
|
|
|
{
|
|
|
|
_integrator = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
APM_PI::load_gains()
|
|
|
|
{
|
2012-02-12 07:26:12 -04:00
|
|
|
_kp.load();
|
|
|
|
_ki.load();
|
|
|
|
_imax.load();
|
2011-09-04 21:56:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
APM_PI::save_gains()
|
|
|
|
{
|
2012-02-12 07:26:12 -04:00
|
|
|
_kp.save();
|
|
|
|
_ki.save();
|
|
|
|
_imax.save();
|
2011-09-04 21:56:50 -03:00
|
|
|
}
|