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