a fork of priustroller

Dependencies:   mbed

Fork of priustroller_current by N K

Committer:
nki
Date:
Tue Mar 10 17:47:08 2015 +0000
Revision:
25:0003b824dd7d
Parent:
24:f1ff9c7256b5
Child:
50:16b43e8fe04f
fixed swapped _kp and _kd in context.cpp; doesn't run great with the loop closed.;

Who changed what in which revision?

UserRevisionLine numberNew 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;
bwang 11:dccbaa9274c5 30 }