50 z = cvCreateMat(
m,1,CV_64FC1); cvSetZero(
z);
51 H = cvCreateMat(
m,
n,CV_64FC1); cvSetZero(
H);
53 K = cvCreateMat(
n,
m,CV_64FC1); cvSetZero(
K);
74 cvScaleAdd(x_pred, cvScalar(1),
x_gain, x);
79 cvMatMul(F,
x, x_pred);
86 x_pred = cvCloneMat(s.
x_pred);
87 F_trans = cvCloneMat(s.
F_trans);
92 x = cvCreateMat(
n,1,CV_64FC1); cvSetZero(
x);
93 F = cvCreateMat(
n,
n,CV_64FC1); cvSetIdentity(F);
94 F_trans = cvCreateMat(
n,
n,CV_64FC1); cvSetIdentity(F_trans);
95 x_pred = cvCreateMat(
n,1,CV_64FC1); cvSetZero(x_pred);
101 cvReleaseMat(&F_trans);
102 cvReleaseMat(&x_pred);
123 R = cvCreateMat(
m,
m,CV_64FC1); cvSetZero(
R);
124 R_tmp = cvCreateMat(
m,
m,CV_64FC1); cvSetZero(
R);
125 P_tmp = cvCreateMat(n,n,CV_64FC1); cvSetZero(
P_tmp);
130 cvReleaseMat(&
R_tmp);
131 cvReleaseMat(&
P_tmp);
142 cvMatMul(P_pred,
K,
K);
149 cvScaleAdd(
P_tmp, cvScalar(-1), P, P);
150 cvMatMul(P, P_pred, P);
155 cvTranspose(F, F_trans);
156 cvMatMul(P, F_trans, P_pred);
157 cvMatMul(F, P_pred, P_pred);
158 cvScaleAdd(P_pred, cvScalar(1), Q, P_pred);
163 Q = cvCreateMat(
n,
n,CV_64FC1); cvSetZero(
Q);
164 P = cvCreateMat(
n,
n,CV_64FC1); cvSetZero(
P);
197 return ((
double)tick_diff/1000.0);
202 const double step = 0.000001;
204 for (
int i=0; i<
n; i++) {
206 cvGetCol(H, &H_column, i);
209 cvmSet(delta, i, 0, step);
210 cvAdd(x_pred, delta, x_plus);
211 cvmSet(delta, i, 0, -step);
212 cvAdd(x_pred, delta, x_minus);
216 cvSub(z_tmp1, z_tmp2, &H_column);
217 cvScale(&H_column, &H_column, 1.0/(2*step));
224 cvScaleAdd(z_pred, cvScalar(-1),
z, z_residual);
225 cvMatMul(K, z_residual, x_gain);
226 cvScaleAdd(x_pred, cvScalar(1), x_gain, x);
238 delta = cvCreateMat(
n,1,CV_64FC1); cvSetZero(
delta);
246 cvReleaseMat(&
delta);
256 double dt = (tick-prev_tick)/1000.0;
257 const double step = 0.000001;
259 for (
int i=0; i<
n; i++) {
261 cvGetCol(F, &F_column, i);
264 cvmSet(
delta, i, 0, step);
266 cvmSet(
delta, i, 0, -step);
271 cvSub(x_tmp1, x_tmp2, &F_column);
272 cvScale(&F_column, &F_column, 1.0/(2*step));
277 double dt = (tick-prev_tick)/1000.0;
282 delta = cvCreateMat(
n,1,CV_64FC1); cvSetZero(
delta);
290 cvReleaseMat(&
delta);
298 cvSetImageROI(img, cvRect(top, left, mat->cols, mat->rows));
299 for (
int j=0; j<mat->rows; j++) {
300 for (
int i=0; i<mat->cols; i++) {
301 double d = cvGet2D(mat, j, i).val[0];
303 double c1=0, c2=0, c3=0;
305 c1 = 0 + ((d - 0.0)/(0.1 - 0.0)*(127 - 0));
307 c1 = 127 + ((d - 0.1)/(1.0 - 0.1)*(255 - 127));
308 }
else if (d < 10.0) {
310 c2 = 0 + ((d - 1.0)/(10.0 - 1.0)*(255 - 0));
311 }
else if (d < 100.0) {
314 c3 = 0 + ((d - 10.0)/(100.0 - 10.0)*(255 - 0));
316 c1 = 255; c2 = 255; c3 = 255;
319 cvSet2D(img, j, i, cvScalar(c3, c2, c1));
321 cvSet2D(img, j, i, cvScalar(c2, c1, c3));
325 cvResetImageROI(img);
331 int img_width = std::max(3+
n+3+
n+5+m+5, 1+
n+1+
n+1+
n+1+m+1+
n+1);
332 int img_height = 1+
n+1+std::max(
n, m+1+m)+1;
333 img = cvCreateImage(cvSize(img_width, img_height), IPL_DEPTH_8U, 3);
334 cvSet(img, cvScalar(64,64,64));
335 img_legend = cvLoadImage(
"Legend.png");
337 for (img_scale=1; img_scale<50; img_scale++) {
338 if (img_scale*img_width > img_legend->width) {
342 img_show = cvCreateImage(cvSize(img_width*img_scale, img_legend->height + img_height*img_scale), IPL_DEPTH_8U, 3);
343 cvSet(img_show, cvScalar(64,64,64));
344 cvSetImageROI(img_show, cvRect(0, 0, img_legend->width, img_legend->height));
345 cvCopy(img_legend, img_show);
346 cvResetImageROI(img_show);
347 cvNamedWindow(
"KalmanVisualize");
350 img_show = cvCreateImage(cvSize(img_width*img_scale, img_height*img_scale), IPL_DEPTH_8U, 3);
351 cvSet(img_show, cvScalar(64,64,64));
352 cvNamedWindow(
"KalmanVisualize",0);
358 std::cout<<name<<
" = [";
359 for (
int j=0; j<m->rows; j++) {
360 std::cout<<
" "<<cvGet2D(m, j, 0).val[0];
362 std::cout<<
"]^T"<<std::endl;
363 }
else if (m->rows == 1) {
364 std::cout<<name<<
" = [";
365 for (
int i=0; i<m->cols; i++) {
366 std::cout<<
" "<<cvGet2D(m, 0, i).val[0];
368 std::cout<<
"]^T"<<std::endl;
370 std::cout<<name<<
" = ["<<std::endl;
371 for (
int j=0; j<m->rows; j++) {
372 for (
int i=0; i<m->cols; i++) {
373 std::cout<<
" "<<cvGet2D(m, j, i).val[0];
375 std::cout<<std::endl;
377 std::cout<<
"]"<<std::endl;
384 kalman_ext = _kalman;
385 sensor_ext = _sensor;
398 cvReleaseImage(&img);
402 img_matrix(kalman->x, 1, 1);
403 if (kalman_ext && sensor_ext) {
404 int y = std::max(2+
n, 3+m+m);
405 img_matrix(kalman_ext->P, 1, y);
410 img_matrix(kalman->F, 3, 1);
411 img_matrix(kalman->x_pred, 4+
n, 1);
412 img_matrix(sensor->H, 6+
n, 1);
413 img_matrix(sensor->z_pred, 7+
n+
n, 1);
414 img_matrix(sensor->z, 7+
n+
n, 2 + m);
415 img_matrix(sensor->z_residual, 9+
n+
n, 1);
416 img_matrix(sensor->K, 11+
n+
n, 1);
417 img_matrix(sensor->x_gain, 12+
n+
n+m, 1);
418 img_matrix(kalman->x, 14+
n+
n+m, 1);
419 if (kalman_ext && sensor_ext) {
420 int y = std::max(2+
n, 3+m+m);
421 img_matrix(kalman_ext->Q, 2+
n, y);
422 img_matrix(kalman_ext->P_pred, 3+
n+
n, y);
423 img_matrix(sensor_ext->R, 4+
n+
n+
n, y);
424 img_matrix(kalman_ext->P, img->width - 1 -
n, y);
427 cvSetImageROI(img_show, cvRect(0, img_legend->height, img->width * img_scale, img->height * img_scale));
428 cvResize(img, img_show, CV_INTER_NN);
429 cvResetImageROI(img_show);
431 cvResize(img, img_show, CV_INTER_NN);
437 cvShowImage(
"KalmanVisualize", img_show);
KalmanVisualize(Kalman *_kalman, KalmanSensor *_sensor)
Constructor for full Kalman implementation.
Extended Kalman Filter (EKF) sensor implementation.
Kalman(int _n)
Constructor.
~KalmanSensorCore()
Destructor.
virtual void update_K(CvMat *P_pred)
Method for updating the Kalman gain. This is called from predict_update() of Kalman.
KalmanCore(const KalmanCore &s)
Copy constructor.
Core implementation for Kalman.
virtual void predict_x(unsigned long tick)
KalmanSensorEkf(const KalmanSensorEkf &k)
virtual void update_H(CvMat *x_pred)
Method for updating how the Kalman state vector is mapped into this sensor's measurements vector...
CvMat * predict_update(KalmanSensor *sensor, unsigned long tick)
Predict the Kalman state vector for the given time step and update the state using the Kalman gain...
virtual void update_x(CvMat *x_pred, CvMat *x)
Method for updating the state estimate x This is called from predict_update() of Kalman. In KalmanSensorCore and in KalmanSensor this update is made linearly but KalmanSensorEkf will override this method to use unlinear estimation.
CvMat * K
The matrix (n*m) containing Kalman gain (something between 0 and H^-1). In this core-implementation w...
CvMat * predict_update(KalmanSensorCore *sensor)
Predict the Kalman state vector and update the state using the constant Kalman gain. x = x_pred + K* ( z - H*x_pred)
~KalmanVisualize()
Destructor.
CvMat * P
The error covariance matrix describing the accuracy of the state estimate.
TFSIMD_FORCE_INLINE const tfScalar & y() const
virtual CvMat * predict()
Predict the Kalman state vector for the given time step . x_pred = F * x.
CvMat * P_pred
The predicted error covariance matrix.
Core implementation for Kalman sensor.
CvMat * R
The covariance matrix for the observation noise.
virtual void update_F(unsigned long tick)
Kalman sensor implementation.
KalmanSensorCore(const KalmanSensorCore &k)
Copy constructor.
virtual void update_F(unsigned long tick)
This file implements a versatile Kalman filter.
virtual void update_P(CvMat *P_pred, CvMat *P)
Method for updating the error covariance matrix describing the accuracy of the state estimate...
void show()
Show the genrated visualization image.
TFSIMD_FORCE_INLINE const tfScalar & x() const
CvMat * H
The matrix (m*n) mapping Kalman state vector into this sensor's measurements vector.
CvMat * F
The matrix (n*n) containing the transition model for the internal state.
This file implements generic utility functions and a serialization interface.
TFSIMD_FORCE_INLINE const tfScalar & z() const
void update_pre()
Update the visualization image - call this before the Kalman's predict_update.
KalmanSensor(const KalmanSensor &k)
Copy constructor.
void img_matrix(CvMat *mat, int top, int left)
Add matrix to the image.
void update_post()
Update the visualization image - call this after the Kalman's predict_update.
void Init()
Init everything. Called from constructors.
CvMat * x_pred
Predicted state, TODO: should be protected?!
CvMat * Q
The covariance matrix for the process noise.
CvMat * z
Latest measurement vector (m*1)
static void out_matrix(CvMat *m, char *name)
Helper method for outputting matrices (for debug purposes)
CvMat * x
The Kalman state vector (n*1)
~KalmanSensor()
Destructor.
virtual void update_x(CvMat *x_pred, CvMat *x)
Method for updating the state estimate x This is called from predict_update() of Kalman. In KalmanSensorCore and in KalmanSensor this update is made linearly but KalmanSensorEkf will override this method to use unlinear estimation.
virtual void update_H(CvMat *x_pred)
Method for updating how the Kalman state vector is mapped into this sensor's measurements vector...
double seconds_since_update(unsigned long tick)
Helper method.
This file defines library export definitions, version numbers and build information.
virtual void predict_x(unsigned long tick)