Visual Servoing Platform  version 3.6.1 under development (2024-10-15)
testKalmanAcceleration.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 int main()
46 {
47 #ifdef ENABLE_VISP_NAMESPACE
48  using namespace VISP_NAMESPACE_NAME;
49 #endif
50 
51  try {
52  unsigned int nsignal = 1; // Number of signal to filter
53  unsigned int niter = 100;
54 
55  std::string filename = "/tmp/log.dat";
56  std::ofstream flog(filename.c_str());
57 
59 
62  kalman.setStateModel(model);
63 
64  unsigned int size_state_vector = kalman.getStateSize() * nsignal;
65  unsigned int size_measure_vector = kalman.getMeasureSize() * nsignal;
66 
67  vpColVector sigma_measure(size_measure_vector);
68  for (unsigned int signal = 0; signal < nsignal; signal++)
69  sigma_measure = 0.0001;
70  vpColVector sigma_state(size_state_vector);
71  for (unsigned int signal = 0; signal < nsignal; signal++) {
72  sigma_state[3 * signal] = 0.; // not used
73  sigma_state[3 * signal + 1] = 0.000001;
74  sigma_state[3 * signal + 2] = 0.000001;
75  }
76 
77  vpColVector velocity_measure(size_measure_vector);
78 
79  double rho = 0.9; // correlation
80  double dt = 0.2; // sampling period
81 
82  for (unsigned int signal = 0; signal < nsignal; signal++)
83  velocity_measure[signal] = 3 + 2 * signal;
84 
85  kalman.verbose(false);
86  kalman.initFilter(nsignal, sigma_state, sigma_measure, rho, dt);
87 
88  for (unsigned int iter = 0; iter <= niter; iter++) {
89  std::cout << "-------- iter " << iter << " ------------" << std::endl;
90  for (unsigned int signal = 0; signal < nsignal; signal++) {
91  velocity_measure[signal] = 3 + 2 * signal + 0.3 * sin(vpMath::rad(360. / niter * iter));
92  }
93  std::cout << "measure : " << velocity_measure.t() << std::endl;
94 
95  flog << velocity_measure.t();
96 
97  // kalman.prediction();
98  kalman.filter(velocity_measure);
99  flog << kalman.Xest.t();
100  flog << kalman.Xpre.t();
101 
102  std::cout << "Xest: " << kalman.Xest.t() << std::endl;
103  std::cout << "Xpre: " << kalman.Xpre.t() << std::endl;
104 
105  flog << std::endl;
106  }
107 
108  flog.close();
109  return EXIT_SUCCESS;
110  }
111  catch (const vpException &e) {
112  std::cout << "Catch an exception: " << e << std::endl;
113  return EXIT_FAILURE;
114  }
115 }
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
vpColVector Xpre
unsigned int getMeasureSize()
void verbose(bool on)
vpColVector Xest
unsigned int getStateSize()
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