Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
testKalmanVelocity.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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 
41 #include <fstream>
42 #include <iostream>
43 #include <visp3/core/vpLinearKalmanFilterInstantiation.h>
44 
45 typedef enum
46 {
47  Position, // Considered measures are the successive positions of the target
48  Velocity // Considered measures are the successive velocities of the target
49 } vpMeasureType;
50 
51 int main()
52 {
53 #ifdef ENABLE_VISP_NAMESPACE
54  using namespace VISP_NAMESPACE_NAME;
55 #endif
56  try {
57  unsigned int nsignal = 2; // Number of signal to filter
58  unsigned int niter = 200;
59  unsigned int size_state_vector = 2 * nsignal;
60  unsigned int size_measure_vector = 1 * nsignal;
61  // vpMeasureType measure_t = Velocity;
62  vpMeasureType measure_t = Position;
63 
64  std::string filename = "/tmp/log.dat";
65  std::ofstream flog(filename.c_str());
66 
68 
69  vpColVector sigma_measure(size_measure_vector);
70  for (unsigned int signal = 0; signal < nsignal; signal++)
71  sigma_measure = 0.000001;
72  vpColVector sigma_state(size_state_vector);
73 
74  switch (measure_t) {
75  case Velocity:
76  for (unsigned int signal = 0; signal < nsignal; signal++) {
77  sigma_state[2 * signal] = 0.; // not used
78  sigma_state[2 * signal + 1] = 0.000001;
79  }
80  break;
81  case Position:
82  for (unsigned int signal = 0; signal < nsignal; signal++) {
83  sigma_state[2 * signal] = 0.000001;
84  sigma_state[2 * signal + 1] = 0; // not used
85  }
86  break;
87  }
88 
89  vpColVector measure(size_measure_vector);
90 
91  for (unsigned int signal = 0; signal < nsignal; signal++) {
92  measure[signal] = 3 + 2 * signal;
93  }
94 
95  kalman.verbose(true);
96 
98  double dt = 0.04; // Sampling period
99  double rho = 0.5;
100  double dummy = 0; // non used parameter
101  switch (measure_t) {
102  case Velocity:
104  kalman.setStateModel(model);
105  kalman.initFilter(nsignal, sigma_state, sigma_measure, rho, dummy);
106  break;
107  case Position:
109  kalman.setStateModel(model);
110  kalman.initFilter(nsignal, sigma_state, sigma_measure, dummy, dt);
111  break;
112  }
113 
114  for (unsigned int iter = 0; iter <= niter; iter++) {
115  std::cout << "-------- iter " << iter << " ------------" << std::endl;
116  for (unsigned int signal = 0; signal < nsignal; signal++) {
117  measure[signal] = 3 + 2 * signal + 0.3 * sin(vpMath::rad(360. / niter * iter));
118  }
119  std::cout << "measure : " << measure.t() << std::endl;
120 
121  flog << measure.t();
122 
123  // kalman.prediction();
124  kalman.filter(measure);
125  flog << kalman.Xest.t() << std::endl;
126 
127  std::cout << "Xest: " << kalman.Xest.t() << std::endl;
128  }
129 
130  flog.close();
131  return EXIT_SUCCESS;
132  }
133  catch (const vpException &e) {
134  std::cout << "Catch an exception: " << e << std::endl;
135  return EXIT_FAILURE;
136  }
137 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
vpRowVector t() const
error that can be emitted by ViSP classes.
Definition: vpException.h:60
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:129