ardupilot/libraries/APM_PI/APM_PI.cpp

62 lines
1.0 KiB
C++
Raw Normal View History

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