ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testKalmanVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: testKalmanVelocity.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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  unsigned int nsignal = 2; // Number of signal to filter
62  unsigned int niter = 200;
63  unsigned int size_state_vector = 2*nsignal;
64  unsigned int size_measure_vector = 1*nsignal;
65  //vpMeasureType measure_t = Velocity;
66  vpMeasureType measure_t = Position;
67 
68  std::string filename = "/tmp/log.dat";
69  std::ofstream flog(filename.c_str());
70 
72 
73  vpColVector sigma_measure(size_measure_vector);
74  for (unsigned int signal=0; signal < nsignal; signal ++)
75  sigma_measure = 0.000001;
76  vpColVector sigma_state(size_state_vector);
77 
78  switch (measure_t) {
79  case Velocity:
80  for (unsigned int signal=0; signal < nsignal; signal ++) {
81  sigma_state[2*signal] = 0.; // not used
82  sigma_state[2*signal+1] = 0.000001;
83  }
84  break;
85  case Position:
86  for (unsigned int signal=0; signal < nsignal; signal ++) {
87  sigma_state[2*signal] = 0.000001;
88  sigma_state[2*signal+1] = 0; // not used
89  }
90  break;
91  }
92 
93  vpColVector measure(size_measure_vector);
94 
95  for (unsigned int signal=0; signal < nsignal; signal ++) {
96  measure[signal] = 3+2*signal;
97  }
98 
99  kalman.verbose(true);
100 
102  double dt = 0.04; // Sampling period
103  double rho = 0.5;
104  double dummy = 0; // non used parameter
105  switch (measure_t) {
106  case Velocity:
108  kalman.setStateModel(model);
109  kalman.initFilter(nsignal, sigma_state, sigma_measure, rho, dummy);
110  break;
111  case Position:
113  kalman.setStateModel(model);
114  kalman.initFilter(nsignal, sigma_state, sigma_measure, dummy, dt);
115  break;
116  }
117 
118  for (unsigned int iter=0; iter <= niter; iter++) {
119  std::cout << "-------- iter " << iter << " ------------" << std::endl;
120  for (unsigned int signal=0; signal < nsignal; signal ++) {
121  measure[signal] = 3+2*signal + 0.3*sin(vpMath::rad(360./niter*iter));
122  }
123  std::cout << "measure : " << measure.t() << std::endl;
124 
125  flog << measure.t();
126 
127  // kalman.prediction();
128  kalman.filter(measure);
129  flog << kalman.Xest.t() << std::endl;
130 
131  std::cout << "Xest: " << kalman.Xest.t() << std::endl;
132  }
133 
134  flog.close();
135  return 0;
136 }
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.