Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpLinearKalmanFilterInstantiation.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Kalman filtering.
32 */
33
38
39#include <visp3/core/vpDebug.h>
40#include <visp3/core/vpException.h>
41#include <visp3/core/vpLinearKalmanFilterInstantiation.h>
42
43#include <math.h>
44#include <stdlib.h>
45
177 void vpLinearKalmanFilterInstantiation::initFilter(unsigned int n_signal, vpColVector &sigma_state,
178 vpColVector &sigma_measure, double rho, double delta_t)
179{
180 switch (model) {
182 initStateConstVelWithColoredNoise_MeasureVel(n_signal, sigma_state, sigma_measure, rho);
183 break;
185 initStateConstVel_MeasurePos(n_signal, sigma_state, sigma_measure, delta_t);
186 break;
188 initStateConstAccWithColoredNoise_MeasureVel(n_signal, sigma_state, sigma_measure, rho, delta_t);
189 break;
190 case unknown:
191 default:
192 throw(vpException(vpException::notInitialized, "Unsupported Kalman state model in vpLinearKalmanFilterInstantiation::initFilter()"));
193 }
194}
195
292 vpColVector &sigma_measure, double delta_t)
293{
294 // init_done = true ;
296
297 init(size_state, size_measure, n_signal);
298
299 iter = 0;
300 Pest = 0;
301 Xest = 0;
302 F = 0;
303 H = 0;
304 R = 0;
305 Q = 0;
306 this->dt = delta_t;
307
308 double dt2 = dt * dt;
309 double dt3 = dt2 * dt;
310
311 for (unsigned int i = 0; i < size_measure * n_signal; i++) {
312 // State model
313 // | 1 dt |
314 // F = | |
315 // | 0 1 |
316
317 F[2 * i][2 * i] = 1;
318 F[2 * i][2 * i + 1] = dt;
319 F[2 * i + 1][2 * i + 1] = 1;
320
321 // Measure model
322 H[i][2 * i] = 1;
323 H[i][2 * i + 1] = 0;
324
325 double sR = sigma_measure[i];
326 double sQ = sigma_state[2 * i]; // sigma_state[2*i+1] is not used
327
328 // Measure noise
329 R[i][i] = sR;
330
331 // State covariance matrix 6.2.2.12
332 Q[2 * i][2 * i] = sQ * dt3 / 3;
333 Q[2 * i][2 * i + 1] = sQ * dt2 / 2;
334 Q[2 * i + 1][2 * i] = sQ * dt2 / 2;
335 Q[2 * i + 1][2 * i + 1] = sQ * dt;
336
337 Pest[2 * i][2 * i] = sR;
338 Pest[2 * i][2 * i + 1] = sR / (2 * dt);
339 Pest[2 * i + 1][2 * i] = sR / (2 * dt);
340 Pest[2 * i + 1][2 * i + 1] = sQ * 2 * dt / 3.0 + sR / (2 * dt2);
341 }
342}
343
502 vpColVector &sigma_state,
503 vpColVector &sigma_measure,
504 double rho)
505{
506 if ((rho < 0) || (rho >= 1)) {
507 vpERROR_TRACE("Bad rho value %g; should be in [0:1[", rho);
508 throw(vpException(vpException::badValue, "Bad rho value"));
509 }
510
512
513 init(size_state, size_measure, n_signal);
514
515 iter = 0;
516 Pest = 0;
517 Xest = 0;
518 F = 0;
519 H = 0;
520 R = 0;
521 Q = 0;
522
523 for (unsigned int i = 0; i < size_measure * n_signal; i++) {
524 // State model
525 // | 1 1 |
526 // F = | |
527 // | 0 rho |
528
529 F[2 * i][2 * i] = 1;
530 F[2 * i][2 * i + 1] = 1;
531 F[2 * i + 1][2 * i + 1] = rho;
532
533 // Measure model
534 H[i][2 * i] = 1;
535 H[i][2 * i + 1] = 0;
536
537 double sR = sigma_measure[i];
538 double sQ = sigma_state[2 * i + 1]; // sigma_state[2*i] is not used
539
540 // Measure noise
541 R[i][i] = sR;
542
543 // State covariance matrix
544 Q[2 * i][2 * i] = 0;
545 Q[2 * i][2 * i + 1] = 0;
546 Q[2 * i + 1][2 * i] = 0;
547 Q[2 * i + 1][2 * i + 1] = sQ;
548
549 Pest[2 * i][2 * i] = sR;
550 Pest[2 * i][2 * i + 1] = 0.;
551 Pest[2 * i + 1][2 * i] = 0;
552 Pest[2 * i + 1][2 * i + 1] = sQ / (1 - rho * rho);
553 }
554}
555
726 vpColVector &sigma_state,
727 vpColVector &sigma_measure,
728 double rho, double delta_t)
729{
730 if ((rho < 0) || (rho >= 1)) {
731 vpERROR_TRACE("Bad rho value %g; should be in [0:1[", rho);
732 throw(vpException(vpException::badValue, "Bad rho value"));
733 }
735
736 init(size_state, size_measure, n_signal);
737
738 iter = 0;
739 Pest = 0;
740 Xest = 0;
741 F = 0;
742 H = 0;
743 R = 0;
744 Q = 0;
745 this->dt = delta_t;
746 // initialise les matrices decrivant les modeles
747 for (unsigned int i = 0; i < size_measure * nsignal; i++) {
748 // State model
749 // | 1 1 dt |
750 // F = | o rho 0 |
751 // | 0 0 1 |
752
753 F[3 * i][3 * i] = 1;
754 F[3 * i][3 * i + 1] = 1;
755 F[3 * i][3 * i + 2] = dt;
756 F[3 * i + 1][3 * i + 1] = rho;
757 F[3 * i + 2][3 * i + 2] = 1;
758
759 // Measure model
760 H[i][3 * i] = 1;
761 H[i][3 * i + 1] = 0;
762 H[i][3 * i + 2] = 0;
763
764 double sR = sigma_measure[i];
765 double sQ1 = sigma_state[3 * i + 1];
766 double sQ2 = sigma_state[3 * i + 2];
767
768 // Measure noise
769 R[i][i] = sR;
770
771 // State covariance matrix
772 Q[3 * i + 1][3 * i + 1] = sQ1;
773 Q[3 * i + 2][3 * i + 2] = sQ2;
774
775 Pest[3 * i][3 * i] = sR;
776 Pest[3 * i][3 * i + 1] = 0.;
777 Pest[3 * i][3 * i + 2] = sR / dt;
778 Pest[3 * i + 1][3 * i + 1] = sQ1 / (1 - rho * rho);
779 Pest[3 * i + 1][3 * i + 2] = -rho * sQ1 / ((1 - rho * rho) * dt);
780 Pest[3 * i + 2][3 * i + 2] = (2 * sR + sQ1 / (1 - rho * rho)) / (dt * dt);
781 // complete the lower triangle
782 Pest[3 * i + 1][3 * i] = Pest[3 * i][3 * i + 1];
783 Pest[3 * i + 2][3 * i] = Pest[3 * i][3 * i + 2];
784 Pest[3 * i + 2][3 * i + 1] = Pest[3 * i + 1][3 * i + 2];
785 }
786}
787
802{
803 if (nsignal < 1) {
804 vpERROR_TRACE("Bad signal number. You need to initialize the Kalman filter");
805 throw(vpException(vpException::notInitialized, "Bad signal number"));
806 }
807
808 // Specific initialization of the filter that depends on the state model
809 if (iter == 0) {
810 Xest = 0;
811 switch (model) {
815 for (unsigned int i = 0; i < size_measure * nsignal; i++) {
816 Xest[size_state * i] = z[i];
817 }
818 prediction();
819 // init_done = true;
820 break;
821 case unknown:
822 default:
823 throw(vpException(vpException::notInitialized, "Unsupported Kalman state model in vpLinearKalmanFilterInstantiation::filter()"));
824 }
825 iter++;
826
827 return;
828 }
829 else if (iter == 1) {
831 for (unsigned int i = 0; i < size_measure * nsignal; i++) {
832 double z_prev = Xest[size_state * i]; // Previous measured position
833 Xest[size_state * i] = z[i];
834 Xest[size_state * i + 1] = (z[i] - z_prev) / dt;
835 }
836 prediction();
837 iter++;
838
839 return;
840 }
841 }
842
843 filtering(z);
844 prediction();
845}
846END_VISP_NAMESPACE
Implementation of column vector and the associated operations.
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ badValue
Used to indicate that a value is not in the allowed range.
Definition vpException.h:73
@ notInitialized
Used to indicate that a parameter is not initialized.
Definition vpException.h:74
unsigned int size_state
Size of the state vector .
long iter
Filter step or iteration. When set to zero, initialize the filter.
vpMatrix R
Measurement noise covariance matrix .
void init(unsigned int size_state, unsigned int size_measure, unsigned int n_signal)
unsigned int nsignal
Number of signal to filter.
vpMatrix Q
Process noise covariance matrix .
void filtering(const vpColVector &z)
vpColVector Xest
unsigned int size_measure
Size of the measure vector .
vpMatrix H
Matrix that describes the evolution of the measurements.
void initStateConstVel_MeasurePos(unsigned int nsignal, vpColVector &sigma_state, vpColVector &sigma_measure, double dt)
void initStateConstAccWithColoredNoise_MeasureVel(unsigned int nsignal, vpColVector &sigma_state, vpColVector &sigma_measure, double rho, double dt)
void initFilter(unsigned int nsignal, vpColVector &sigma_state, vpColVector &sigma_measure, double rho, double dt)
void initStateConstVelWithColoredNoise_MeasureVel(unsigned int nsignal, vpColVector &sigma_state, vpColVector &sigma_measure, double rho)
#define vpERROR_TRACE
Definition vpDebug.h:423