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"
|
|
|
|
|
|
|
|
long
|
2011-09-19 18:02:42 -03:00
|
|
|
APM_PI::get_pi(int32_t error, float dt)
|
2011-09-04 21:56:50 -03:00
|
|
|
{
|
|
|
|
float output = 0;
|
2011-09-19 18:02:42 -03:00
|
|
|
//float delta_time = (float)dt / 1000.0;
|
2011-09-04 21:56:50 -03:00
|
|
|
|
|
|
|
// Compute proportional component
|
|
|
|
output += error * _kp;
|
|
|
|
|
|
|
|
// Compute integral component if time has elapsed
|
2011-09-19 18:02:42 -03:00
|
|
|
_integrator += (error * _ki) * dt;
|
2011-09-04 21:56:50 -03:00
|
|
|
|
|
|
|
if (_integrator < -_imax) {
|
|
|
|
_integrator = -_imax;
|
|
|
|
} else if (_integrator > _imax) {
|
|
|
|
_integrator = _imax;
|
|
|
|
}
|
|
|
|
|
|
|
|
output += _integrator;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
APM_PI::reset_I()
|
|
|
|
{
|
|
|
|
_integrator = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
APM_PI::load_gains()
|
|
|
|
{
|
|
|
|
_group.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
APM_PI::save_gains()
|
|
|
|
{
|
|
|
|
_group.save();
|
|
|
|
}
|