ViSP  2.9.0
testKalmanVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: testKalmanVelocity.cpp 4574 2014-01-09 08:48:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Tests some vpLinearKalmanFilterInstantiation functionalities.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
49 #include <visp/vpLinearKalmanFilterInstantiation.h>
50 #include <iostream>
51 #include <fstream>
52 
53 typedef enum {
54  Position, // Considered measures are the succesive positions of the target
55  Velocity // Considered measures are the succesive velocities of the target
56 } vpMeasureType;
57 
58 int
59 main()
60 {
61  try {
62  unsigned int nsignal = 2; // Number of signal to filter
63  unsigned int niter = 200;
64  unsigned int size_state_vector = 2*nsignal;
65  unsigned int size_measure_vector = 1*nsignal;
66  //vpMeasureType measure_t = Velocity;
67  vpMeasureType measure_t = Position;
68 
69  std::string filename = "/tmp/log.dat";
70  std::ofstream flog(filename.c_str());
71 
73 
74  vpColVector sigma_measure(size_measure_vector);
75  for (unsigned int signal=0; signal < nsignal; signal ++)
76  sigma_measure = 0.000001;
77  vpColVector sigma_state(size_state_vector);
78 
79  switch (measure_t) {
80  case Velocity:
81  for (unsigned int signal=0; signal < nsignal; signal ++) {
82  sigma_state[2*signal] = 0.; // not used
83  sigma_state[2*signal+1] = 0.000001;
84  }
85  break;
86  case Position:
87  for (unsigned int signal=0; signal < nsignal; signal ++) {
88  sigma_state[2*signal] = 0.000001;
89  sigma_state[2*signal+1] = 0; // not used
90  }
91  break;
92  }
93 
94  vpColVector measure(size_measure_vector);
95 
96  for (unsigned int signal=0; signal < nsignal; signal ++) {
97  measure[signal] = 3+2*signal;
98  }
99 
100  kalman.verbose(true);
101 
103  double dt = 0.04; // Sampling period
104  double rho = 0.5;
105  double dummy = 0; // non used parameter
106  switch (measure_t) {
107  case Velocity:
109  kalman.setStateModel(model);
110  kalman.initFilter(nsignal, sigma_state, sigma_measure, rho, dummy);
111  break;
112  case Position:
114  kalman.setStateModel(model);
115  kalman.initFilter(nsignal, sigma_state, sigma_measure, dummy, dt);
116  break;
117  }
118 
119  for (unsigned int iter=0; iter <= niter; iter++) {
120  std::cout << "-------- iter " << iter << " ------------" << std::endl;
121  for (unsigned int signal=0; signal < nsignal; signal ++) {
122  measure[signal] = 3+2*signal + 0.3*sin(vpMath::rad(360./niter*iter));
123  }
124  std::cout << "measure : " << measure.t() << std::endl;
125 
126  flog << measure.t();
127 
128  // kalman.prediction();
129  kalman.filter(measure);
130  flog << kalman.Xest.t() << std::endl;
131 
132  std::cout << "Xest: " << kalman.Xest.t() << std::endl;
133  }
134 
135  flog.close();
136  return 0;
137  }
138  catch(vpException e) {
139  std::cout << "Catch an exception: " << e << std::endl;
140  return 1;
141  }
142 }
error that can be emited by ViSP classes.
Definition: vpException.h:76
static double rad(double deg)
Definition: vpMath.h:100
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
This class provides an implementation of some specific linear Kalman filters.