N K
/
GaNtroller
a fork of priustroller
Fork of priustroller_current by
meta/pidcontroller.cpp@50:16b43e8fe04f, 2015-04-24 (annotated)
- Committer:
- nki
- Date:
- Fri Apr 24 16:19:29 2015 +0000
- Revision:
- 50:16b43e8fe04f
- Parent:
- 25:0003b824dd7d
inverter disable below 5% throttle. integrator zeroing implemented but not confirmed working;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bwang | 11:dccbaa9274c5 | 1 | #include "includes.h" |
bwang | 11:dccbaa9274c5 | 2 | #include "filters.h" |
bwang | 11:dccbaa9274c5 | 3 | |
bwang | 11:dccbaa9274c5 | 4 | PidController::PidController(float ki, float kp, float kd, float out_max, float out_min) { |
bwang | 11:dccbaa9274c5 | 5 | _ki = ki; |
bwang | 11:dccbaa9274c5 | 6 | _kp = kp; |
bwang | 11:dccbaa9274c5 | 7 | _kd = kd; |
bwang | 11:dccbaa9274c5 | 8 | _last_in = 0.0f; |
bwang | 11:dccbaa9274c5 | 9 | _integral = 0.0f; |
bwang | 11:dccbaa9274c5 | 10 | _out_max = out_max; |
bwang | 11:dccbaa9274c5 | 11 | _out_min = out_min; |
bwang | 11:dccbaa9274c5 | 12 | } |
bwang | 11:dccbaa9274c5 | 13 | |
bwang | 11:dccbaa9274c5 | 14 | float PidController::Update(float ref, float in) { |
bwang | 11:dccbaa9274c5 | 15 | float error = ref - in; |
nki | 25:0003b824dd7d | 16 | |
bwang | 11:dccbaa9274c5 | 17 | _integral += error; |
nki | 25:0003b824dd7d | 18 | |
bwang | 11:dccbaa9274c5 | 19 | if (_integral * _ki > _out_max) _integral = _out_max / _ki; |
bwang | 11:dccbaa9274c5 | 20 | if (_integral * _ki < _out_min) _integral = _out_min / _ki; |
nki | 25:0003b824dd7d | 21 | |
bwang | 11:dccbaa9274c5 | 22 | float deriv = _last_in - in; |
bwang | 11:dccbaa9274c5 | 23 | _last_in = in; |
bwang | 11:dccbaa9274c5 | 24 | float tmp = _ki * _integral + _kp * error + _kd * deriv; |
nki | 25:0003b824dd7d | 25 | |
bwang | 11:dccbaa9274c5 | 26 | if (tmp > _out_max) tmp = _out_max; |
bwang | 11:dccbaa9274c5 | 27 | if (tmp < _out_min) tmp = _out_min; |
nki | 25:0003b824dd7d | 28 | |
bwang | 11:dccbaa9274c5 | 29 | return tmp; |
nki | 50:16b43e8fe04f | 30 | } |
nki | 50:16b43e8fe04f | 31 | |
nki | 50:16b43e8fe04f | 32 | void PidController::ZeroIntegrator() { |
nki | 50:16b43e8fe04f | 33 | _integral = 0.0f; |
nki | 50:16b43e8fe04f | 34 | } |