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