Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpLinearKalmanFilterInstantiation.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
44 #include <visp3/core/vpLinearKalmanFilterInstantiation.h>
45 #include <visp3/core/vpDebug.h>
46 #include <visp3/core/vpException.h>
47 
48 #include <math.h>
49 #include <stdlib.h>
50 
173 void
175  vpColVector &sigma_state,
176  vpColVector &sigma_measure,
177  double rho, double delta_t)
178 {
179  switch (model) {
181  initStateConstVelWithColoredNoise_MeasureVel(n_signal, sigma_state,
182  sigma_measure, rho);
183  break;
185  initStateConstVel_MeasurePos(n_signal, sigma_state,
186  sigma_measure, delta_t);
187  break;
189  initStateConstAccWithColoredNoise_MeasureVel(n_signal, sigma_state,
190  sigma_measure, rho, delta_t);
191  break;
192  case unknown:
193  vpERROR_TRACE("Kalman state model is not set") ;
194  throw(vpException(vpException::notInitialized, "Kalman state model is not set")) ;
195  }
196 }
197 
295 void
297  vpColVector &sigma_state,
298  vpColVector &sigma_measure,
299  double delta_t )
300 {
301  // init_done = true ;
303 
304  init(size_state, size_measure, n_signal);
305 
306  iter = 0;
307  Pest = 0;
308  Xest = 0;
309  F = 0;
310  H = 0;
311  R = 0;
312  Q = 0;
313  this->dt = delta_t ;
314 
315  double dt2 = dt*dt ;
316  double dt3 = dt2*dt ;
317 
318  for (unsigned int i=0; i < size_measure*n_signal ; i++ ) {
319  // State model
320  // | 1 dt |
321  // F = | |
322  // | 0 1 |
323 
324  F[2*i][2*i] = 1 ;
325  F[2*i][2*i+1] = dt ;
326  F[2*i+1][2*i+1] = 1 ;
327 
328  // Measure model
329  H[i][2*i] = 1 ;
330  H[i][2*i+1] = 0 ;
331 
332  double sR = sigma_measure[i] ;
333  double sQ = sigma_state[2*i] ; // sigma_state[2*i+1] is not used
334 
335  // Measure noise
336  R[i][i] = sR ;
337 
338  // State covariance matrix 6.2.2.12
339  Q[2*i][2*i] = sQ * dt3/3;
340  Q[2*i][2*i+1] = sQ * dt2/2;
341  Q[2*i+1][2*i] = sQ * dt2/2;
342  Q[2*i+1][2*i+1] = sQ * dt;
343 
344  Pest[2*i][2*i] = sR ;
345  Pest[2*i][2*i+1] = sR/(2*dt) ;
346  Pest[2*i+1][2*i] = sR/(2*dt) ;
347  Pest[2*i+1][2*i+1] = sQ*2*dt/3.0+ sR/(2*dt2) ;
348  }
349 }
350 
504 void
506  vpColVector &sigma_state,
507  vpColVector &sigma_measure,
508  double rho)
509 {
510  if ((rho < 0) || (rho >= 1)) {
511  vpERROR_TRACE("Bad rho value %g; should be in [0:1[", rho) ;
512  throw(vpException(vpException::badValue, "Bad rho value")) ;
513  }
514 
516 
517  init(size_state, size_measure, n_signal);
518 
519  iter = 0;
520  Pest = 0;
521  Xest = 0;
522  F = 0;
523  H = 0;
524  R = 0;
525  Q = 0;
526 
527  for (unsigned int i=0; i < size_measure*n_signal ; i++ ) {
528  // State model
529  // | 1 1 |
530  // F = | |
531  // | 0 rho |
532 
533  F[2*i][2*i] = 1 ;
534  F[2*i][2*i+1] = 1 ;
535  F[2*i+1][2*i+1] = rho ;
536 
537  // Measure model
538  H[i][2*i] = 1 ;
539  H[i][2*i+1] = 0 ;
540 
541  double sR = sigma_measure[i] ;
542  double sQ = sigma_state[2*i+1] ; // sigma_state[2*i] is not used
543 
544  // Measure noise
545  R[i][i] = sR ;
546 
547  // State covariance matrix
548  Q[2*i][2*i] = 0 ;
549  Q[2*i][2*i+1] = 0;
550  Q[2*i+1][2*i] = 0;
551  Q[2*i+1][2*i+1] = sQ ;
552 
553  Pest[2*i][2*i] = sR ;
554  Pest[2*i][2*i+1] = 0. ;
555  Pest[2*i+1][2*i] = 0 ;
556  Pest[2*i+1][2*i+1] = sQ/(1-rho*rho) ;
557  }
558 }
559 
727 void
729  vpColVector &sigma_state,
730  vpColVector &sigma_measure,
731  double rho,
732  double delta_t)
733 {
734  if ((rho < 0) || (rho >= 1)) {
735  vpERROR_TRACE("Bad rho value %g; should be in [0:1[", rho) ;
736  throw(vpException(vpException::badValue, "Bad rho value")) ;
737  }
739 
740  init(size_state, size_measure, n_signal);
741 
742  iter = 0;
743  Pest = 0;
744  Xest = 0;
745  F = 0;
746  H = 0;
747  R = 0;
748  Q = 0;
749  this->dt = delta_t;
750  // initialise les matrices decrivant les modeles
751  for (unsigned int i=0; i < size_measure*nsignal ; i++ ) {
752  // State model
753  // | 1 1 dt |
754  // F = | o rho 0 |
755  // | 0 0 1 |
756 
757  F[3*i][3*i] = 1 ;
758  F[3*i][3*i+1] = 1 ;
759  F[3*i][3*i+2] = dt ;
760  F[3*i+1][3*i+1] = rho ;
761  F[3*i+2][3*i+2] = 1 ;
762 
763  // Measure model
764  H[i][3*i] = 1 ;
765  H[i][3*i+1] = 0 ;
766  H[i][3*i+2] = 0 ;
767 
768  double sR = sigma_measure[i] ;
769  double sQ1 = sigma_state[3*i+1] ;
770  double sQ2 = sigma_state[3*i+2] ;
771 
772  // Measure noise
773  R[i][i] = sR ;
774 
775  // State covariance matrix
776  Q[3*i+1][3*i+1] = sQ1;
777  Q[3*i+2][3*i+2] = sQ2;
778 
779  Pest[3*i][3*i] = sR ;
780  Pest[3*i][3*i+1] = 0. ;
781  Pest[3*i][3*i+2] = sR/dt ;
782  Pest[3*i+1][3*i+1] = sQ1/(1-rho*rho) ;
783  Pest[3*i+1][3*i+2] = -rho*sQ1/((1-rho*rho)*dt) ;
784  Pest[3*i+2][3*i+2] = (2*sR+sQ1/(1-rho*rho))/(dt*dt) ;
785  // complete the lower triangle
786  Pest[3*i+1][3*i] = Pest[3*i][3*i+1];
787  Pest[3*i+2][3*i] = Pest[3*i][3*i+2];
788  Pest[3*i+2][3*i+1] = Pest[3*i+1][3*i+2];
789  }
790 }
791 
805 void
807 {
808  if (nsignal < 1) {
809  vpERROR_TRACE("Bad signal number. You need to initialize the Kalman filter") ;
811  "Bad signal number")) ;
812  }
813 
814  // Specific initialization of the filter that depends on the state model
815  if (iter == 0) {
816  Xest = 0;
817  switch (model) {
821  for (unsigned int i=0; i < size_measure*nsignal ; i++ ) {
822  Xest[size_state*i] = z[i] ;
823  }
824  prediction();
825  // init_done = true;
826  break;
827  case unknown:
828  vpERROR_TRACE("Kalman state model is not set") ;
829  throw(vpException(vpException::notInitialized, "Kalman state model is not set")) ;
830  break;
831  }
832  iter ++;
833 
834  return;
835  }
836  else if (iter == 1) {
838  for (unsigned int i=0; i < size_measure*nsignal ; i++ ) {
839  double z_prev = Xest[size_state*i]; // Previous mesured position
840  // std::cout << "Mesure pre: " << z_prev << std::endl;
841  Xest[size_state*i] = z[i] ;
842  Xest[size_state*i+1] = (z[i] - z_prev) / dt ;
843  }
844  prediction();
845  iter ++;
846 
847  return;
848  }
849  }
850 
851  filtering(z);
852  prediction();
853 
854 }
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)
long iter
Filter step or iteration. When set to zero, initialize the filter.
#define vpERROR_TRACE
Definition: vpDebug.h:391
unsigned int size_state
Size of the state vector .
vpMatrix R
Measurement noise covariance matrix .
unsigned int size_measure
Size of the measure vector .
error that can be emited by ViSP classes.
Definition: vpException.h:73
void initStateConstVelWithColoredNoise_MeasureVel(unsigned int nsignal, vpColVector &sigma_state, vpColVector &sigma_measure, double rho)
vpColVector Xest
void filtering(const vpColVector &z)
unsigned int nsignal
Number of signal to filter.
void init(unsigned int size_state, unsigned int size_measure, unsigned int n_signal)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void initStateConstVel_MeasurePos(unsigned int nsignal, vpColVector &sigma_state, vpColVector &sigma_measure, double dt)
vpMatrix F
Transition matrix that describes the evolution of the state.
vpMatrix H
Matrix that describes the evolution of the measurements.
vpMatrix Q
Process noise covariance matrix .