comparison 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
comparison
equal deleted inserted replaced
148:6cd38e261027 149:134bff10e561
1 /*
2 * Pid.cpp
3 *
4 * Created on: 14 Sep 2015
5 * Author: giulio
6 */
7
8 #include "Pid.h"
9
10 Pid::Pid(){
11 integralError = 0;
12 pastError = 0;
13 error = 0;
14 differentialError = 0;
15 kp = 5;
16 ki = 2;
17 kd = 0.8;
18 idleTimeout = 0;
19 timeoutCount = 0;
20 output = 0;
21 // printf("----------%f %f %f %f %f %f\n", kp, error, ki, integralError, kd, differentialError);
22 }
23 void Pid::updateIntegralError(){
24 integralError += (error + pastError) * 0.5 * ts;
25 }
26 void Pid::updateDifferentialError(){
27 differentialError = (error - pastError) / ts;
28 }
29 float Pid::setError(float anError){
30 pastError = error;
31 error = anError;
32 ts = 1; //TODO: this should be passed as a parameter, unless setError() gets always called at constant intervals
33 updateIntegralError();
34 updateDifferentialError();
35 output= kp * error + ki * integralError + kd * differentialError;
36 // printf("%f %f %f %f %f %f %f\n", output, kp, error, ki, integralError, kd, differentialError);
37 return output;
38 }
39 float Pid::getOutput(){
40 return output;
41 }
42
43