Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow measurement with error. #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/kalman.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,23 @@ export default class KalmanFilter {

/**
* Filter a new value
* @param {Number} z Measurement
* @param {Number} z Measurement. Either a value or an array with measurement and error.
* @param {Number} u Control
* @return {Number}
*/
filter(z, u = 0) {

let zVal = z;
let zErr = this.Q;

if (Array.isArray(z)) {
zVal = z[0];
zErr = z[1];
}

if (isNaN(this.x)) {
this.x = (1 / this.C) * z;
this.cov = (1 / this.C) * this.Q * (1 / this.C);
this.x = (1 / this.C) * zVal;
this.cov = (1 / this.C) * zErr * (1 / this.C);
}
else {

Expand All @@ -50,10 +58,10 @@ export default class KalmanFilter {
const predCov = this.uncertainty();

// Kalman gain
const K = predCov * this.C * (1 / ((this.C * predCov * this.C) + this.Q));
const K = predCov * this.C * (1 / ((this.C * predCov * this.C) + zErr));

// Correction
this.x = predX + K * (z - (this.C * predX));
this.x = predX + K * (zVal - (this.C * predX));
this.cov = predCov - (K * this.C * predCov);
}

Expand All @@ -68,15 +76,15 @@ export default class KalmanFilter {
predict(u = 0) {
return (this.A * this.x) + (this.B * u);
}

/**
* Return uncertainty of filter
* @return {Number}
*/
uncertainty() {
return ((this.A * this.cov) * this.A) + this.R;
}

/**
* Return the last filtered measurement
* @return {Number}
Expand Down