Mercurial > hg > beaglert
annotate core/Kalman.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 |
rev | line source |
---|---|
giuliomoro@149 | 1 /* |
giuliomoro@149 | 2 * Kalman.cpp |
giuliomoro@149 | 3 * |
giuliomoro@149 | 4 * Created on: 20 Sep 2015 |
giuliomoro@149 | 5 * Author: giulio |
giuliomoro@149 | 6 */ |
giuliomoro@149 | 7 #include "Kalman.h" |
giuliomoro@149 | 8 void KalmanOne::init(double newQ, double newR, double newX){ |
giuliomoro@149 | 9 A = 1; |
giuliomoro@149 | 10 H = 1; |
giuliomoro@149 | 11 Q = newQ; // covariance of the error on the prediction |
giuliomoro@149 | 12 R = newR; // covariance of the measurement error |
giuliomoro@149 | 13 x = newX; // 5805.09230769231; % predicted x |
giuliomoro@149 | 14 P = 6; // predicted covariance |
giuliomoro@149 | 15 return; |
giuliomoro@149 | 16 } |
giuliomoro@149 | 17 double KalmanOne:: process(double z){ |
giuliomoro@149 | 18 double xp = A*x; // I. Prediction of the estimate |
giuliomoro@149 | 19 double Pp = A*P*A + Q; // Prediction of the error covariance |
giuliomoro@149 | 20 double K = Pp*H/(H*Pp*H + R); // II. Computation of Kalman gain |
giuliomoro@149 | 21 x = xp + K*(z - H*xp); // III. Computation of the estimate |
giuliomoro@149 | 22 P = Pp - K*H*Pp; // IV. Computation of the error covariance |
giuliomoro@149 | 23 return x; |
giuliomoro@149 | 24 } |