Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testKalmanVelocity.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  * Tests some vpLinearKalmanFilterInstantiation functionalities.
32  *
33  * Authors:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
45 #include <visp3/core/vpLinearKalmanFilterInstantiation.h>
46 #include <iostream>
47 #include <fstream>
48 
49 typedef enum {
50  Position, // Considered measures are the succesive positions of the target
51  Velocity // Considered measures are the succesive velocities of the target
52 } vpMeasureType;
53 
54 int
55 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  }
134  catch(vpException &e) {
135  std::cout << "Catch an exception: " << e << std::endl;
136  return 1;
137  }
138 }
error that can be emited by ViSP classes.
Definition: vpException.h:73
static double rad(double deg)
Definition: vpMath.h:104
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
This class provides an implementation of some specific linear Kalman filters.