Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
testKalmanVelocity.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
35 
43 #include <fstream>
44 #include <iostream>
45 #include <visp3/core/vpLinearKalmanFilterInstantiation.h>
46 
47 typedef enum {
48  Position, // Considered measures are the successive positions of the target
49  Velocity // Considered measures are the successive velocities of the target
50 } vpMeasureType;
51 
52 int main()
53 {
54  try {
55  unsigned int nsignal = 2; // Number of signal to filter
56  unsigned int niter = 200;
57  unsigned int size_state_vector = 2 * nsignal;
58  unsigned int size_measure_vector = 1 * nsignal;
59  // vpMeasureType measure_t = Velocity;
60  vpMeasureType measure_t = Position;
61 
62  std::string filename = "/tmp/log.dat";
63  std::ofstream flog(filename.c_str());
64 
66 
67  vpColVector sigma_measure(size_measure_vector);
68  for (unsigned int signal = 0; signal < nsignal; signal++)
69  sigma_measure = 0.000001;
70  vpColVector sigma_state(size_state_vector);
71 
72  switch (measure_t) {
73  case Velocity:
74  for (unsigned int signal = 0; signal < nsignal; signal++) {
75  sigma_state[2 * signal] = 0.; // not used
76  sigma_state[2 * signal + 1] = 0.000001;
77  }
78  break;
79  case Position:
80  for (unsigned int signal = 0; signal < nsignal; signal++) {
81  sigma_state[2 * signal] = 0.000001;
82  sigma_state[2 * signal + 1] = 0; // not used
83  }
84  break;
85  }
86 
87  vpColVector measure(size_measure_vector);
88 
89  for (unsigned int signal = 0; signal < nsignal; signal++) {
90  measure[signal] = 3 + 2 * signal;
91  }
92 
93  kalman.verbose(true);
94 
96  double dt = 0.04; // Sampling period
97  double rho = 0.5;
98  double dummy = 0; // non used parameter
99  switch (measure_t) {
100  case Velocity:
102  kalman.setStateModel(model);
103  kalman.initFilter(nsignal, sigma_state, sigma_measure, rho, dummy);
104  break;
105  case Position:
107  kalman.setStateModel(model);
108  kalman.initFilter(nsignal, sigma_state, sigma_measure, dummy, dt);
109  break;
110  }
111 
112  for (unsigned int iter = 0; iter <= niter; iter++) {
113  std::cout << "-------- iter " << iter << " ------------" << std::endl;
114  for (unsigned int signal = 0; signal < nsignal; signal++) {
115  measure[signal] = 3 + 2 * signal + 0.3 * sin(vpMath::rad(360. / niter * iter));
116  }
117  std::cout << "measure : " << measure.t() << std::endl;
118 
119  flog << measure.t();
120 
121  // kalman.prediction();
122  kalman.filter(measure);
123  flog << kalman.Xest.t() << std::endl;
124 
125  std::cout << "Xest: " << kalman.Xest.t() << std::endl;
126  }
127 
128  flog.close();
129  return EXIT_SUCCESS;
130  } catch (const vpException &e) {
131  std::cout << "Catch an exception: " << e << std::endl;
132  return EXIT_FAILURE;
133  }
134 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpRowVector t() const
error that can be emitted by ViSP classes.
Definition: vpException.h:59
void verbose(bool on)
vpColVector Xest
This class provides an implementation of some specific linear Kalman filters.
void initFilter(unsigned int nsignal, vpColVector &sigma_state, vpColVector &sigma_measure, double rho, double dt)
static double rad(double deg)
Definition: vpMath.h:127