Mercurial > hg > beaglert
annotate core/Pid.cpp @ 149:134bff10e561 ClockSync
Added simple one-variable one-measurement Kalman filter, Pid controller(which output is not used). Virtual clock is now much more precise and reactive for period. Still it is lagging behind a bit on the overall offset.
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Mon, 21 Sep 2015 03:12:21 +0100 |
parents | |
children | 8f98b32d0e23 |
rev | line source |
---|---|
giuliomoro@149 | 1 /* |
giuliomoro@149 | 2 * Pid.cpp |
giuliomoro@149 | 3 * |
giuliomoro@149 | 4 * Created on: 14 Sep 2015 |
giuliomoro@149 | 5 * Author: giulio |
giuliomoro@149 | 6 */ |
giuliomoro@149 | 7 |
giuliomoro@149 | 8 #include "Pid.h" |
giuliomoro@149 | 9 |
giuliomoro@149 | 10 Pid::Pid(){ |
giuliomoro@149 | 11 integralError = 0; |
giuliomoro@149 | 12 pastError = 0; |
giuliomoro@149 | 13 error = 0; |
giuliomoro@149 | 14 differentialError = 0; |
giuliomoro@149 | 15 kp = 5; |
giuliomoro@149 | 16 ki = 2; |
giuliomoro@149 | 17 kd = 0.8; |
giuliomoro@149 | 18 idleTimeout = 0; |
giuliomoro@149 | 19 timeoutCount = 0; |
giuliomoro@149 | 20 output = 0; |
giuliomoro@149 | 21 // printf("----------%f %f %f %f %f %f\n", kp, error, ki, integralError, kd, differentialError); |
giuliomoro@149 | 22 } |
giuliomoro@149 | 23 void Pid::updateIntegralError(){ |
giuliomoro@149 | 24 integralError += (error + pastError) * 0.5 * ts; |
giuliomoro@149 | 25 } |
giuliomoro@149 | 26 void Pid::updateDifferentialError(){ |
giuliomoro@149 | 27 differentialError = (error - pastError) / ts; |
giuliomoro@149 | 28 } |
giuliomoro@149 | 29 float Pid::setError(float anError){ |
giuliomoro@149 | 30 pastError = error; |
giuliomoro@149 | 31 error = anError; |
giuliomoro@149 | 32 ts = 1; //TODO: this should be passed as a parameter, unless setError() gets always called at constant intervals |
giuliomoro@149 | 33 updateIntegralError(); |
giuliomoro@149 | 34 updateDifferentialError(); |
giuliomoro@149 | 35 output= kp * error + ki * integralError + kd * differentialError; |
giuliomoro@149 | 36 // printf("%f %f %f %f %f %f %f\n", output, kp, error, ki, integralError, kd, differentialError); |
giuliomoro@149 | 37 return output; |
giuliomoro@149 | 38 } |
giuliomoro@149 | 39 float Pid::getOutput(){ |
giuliomoro@149 | 40 return output; |
giuliomoro@149 | 41 } |
giuliomoro@149 | 42 |
giuliomoro@149 | 43 |