Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unscented Kalman filter (UKF) #1393

Merged
merged 61 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
f070ca5
[WIP] Added first draft of headers for the UKF and the Sigma Points d…
Apr 15, 2024
5218bcb
Merge branch 'master' into feat_unscented_kalman
Apr 17, 2024
a34d20c
[CORPS] Changed vpMatrix for vector<vpColVector> which makes more sen…
Apr 17, 2024
6bbc77a
[CORPS] First draft of the Merwe's algo
Apr 18, 2024
6bb83b5
Merge branch 'master' into feat_unscented_kalman
Apr 22, 2024
1aa0f97
[CORE] Added Cholesky's decomposition by Eigen, same by Lapack (and O…
Apr 22, 2024
60487ce
[CORE] First draft of the UKF, still missing some parts
Apr 22, 2024
3b5b1e6
[CORE] Use cholesky method to compute the square root of the (lambda …
Apr 22, 2024
a095c70
[CORE] Added Lapack Cholesky's decomposition
Apr 22, 2024
26c981f
[CORE] Removed OpenCV from the possible 3rd parties for Cholesky's de…
Apr 22, 2024
d135608
[FIX] Fixes undefined symbols
Apr 23, 2024
8e4f4e2
[FIX] Fixed Cholesky's decomposition by Lapack
Apr 23, 2024
c45a262
[TEST] Added a criterion to make the test succeed
Apr 23, 2024
0f450e1
[FIX] Fixed some mistakes in the UKF, such as the computation of the …
Apr 23, 2024
3e34b64
[TUTO] Added a tutorial on how one can use the UKF for a linear case
Apr 23, 2024
2a8895e
[TUTO] Added some comments to the linear example UKF tutorial
Apr 23, 2024
f79125c
[TUTO] The linear case example is now 2D
Apr 23, 2024
7868cfc
[TUTO] Added a non-linear example for the UKF
Apr 23, 2024
43872f1
Merge branch 'master' into feat_unscented_kalman
Apr 24, 2024
7f10ba2
[CORE] Added add, mean and residualfunctions for cases where simple o…
Apr 24, 2024
1ff50cd
[CORE] typedef vpAddSubFunction as both are required and have the sam…
Apr 24, 2024
fa6b45c
[TUTO] Complexified the trajectory of the robot
Apr 24, 2024
a40fa63
[TUTO] Fixed the commands sent to the simulated robot
Apr 25, 2024
d09e08b
[TUTO][DOC] Improvement of the documentation of the tutorials/examples
Apr 25, 2024
87fe258
[TUTO][FIX] Fixed missing protection around vpPlot, causing compilati…
Apr 25, 2024
1bea1f1
[FIX] Protected everything related to UKF by a check of C++ standard …
Apr 25, 2024
4dd8641
[FIX] Fix vpUKSgimaDrawerMerwe when using c++98
Apr 29, 2024
b2518f2
[DOC] Added equations in the documentation of the vpUnscentedKalman c…
Apr 29, 2024
5edcfc3
[DOC] Added the equations of vpUKSigmaDrawerMerwe
Apr 29, 2024
06c7d3e
Fix python bindings generator to support lambda function arguments an…
SamFlt Apr 29, 2024
f9fadf9
[EXAMPLE] Move the content of tutorial/kalman into example/kalman [TU…
Apr 30, 2024
9726699
[CLEAN] Removed debug log
Apr 30, 2024
407c96a
[DOC] Corrected typos
Apr 30, 2024
8dbc865
[CLEAN] Cleaned indentation
Apr 30, 2024
7b87be3
[CLEAN] Corrected Copyright dates
Apr 30, 2024
d995628
[FIX] Fixed missing #include<memory> to enable std::shared_ptr
Apr 30, 2024
e42c262
[EXAMPLE] Changed the sampling period and displacements to be closer …
Apr 30, 2024
75aedad
[EXAMPLE] Fine-tuned the Kalman of the linear example
Apr 30, 2024
a9e22bb
[CLEAN] Clean indentation problems
May 2, 2024
cfd9caa
Added missing copyrights
May 2, 2024
8cbcf9d
[TUTO][DOC] Added a .dox file to explain the tutorial
May 2, 2024
1282c60
[TUTO][DOC] Completing the documentation of the tutorial
May 2, 2024
3485908
[TUTO] Changed motion in the XZ plane into motion in the XY plane [TU…
May 2, 2024
73361d0
[TUTO][DOC] Continuing the doc of the tutorial
May 2, 2024
c1825dd
Introduce std::shared_ptr as default type holder for python bindings
SamFlt May 5, 2024
6d21f77
[DOC] Added reference to Merwe's article [TUTO][DOC] Finished the c++…
May 6, 2024
7e63f6d
[TEST] Use catch2 instead of plain old-school fashion c++ test
May 6, 2024
4b7e1c9
[TUTO][DOC] Improved schematic of the use case + added internal links…
May 6, 2024
133ecb1
[CORE] OpenCV can be used for Choleski's decomposition
May 6, 2024
2a6ee57
[EXAMPLE] Renamed examples
May 6, 2024
5c13af8
[EXAMPLE][PYTHON] Added a Python example of the linear use-case
May 6, 2024
ffdad52
[EXAMPLE] Improved simple non-linear example
May 7, 2024
c81a25a
[DOC] Added documentation in the vpUKSigmaDrawerMerwe class
May 13, 2024
cc1b5f4
[EXAMPLE] Renamed a misnamed variable
May 13, 2024
9dc4ad8
[PYTHON][EXAMPLE] Added a Python example of the nonlinear 'simple' case
May 13, 2024
691137c
[MERGE] Fixed merge conflict in doc/tutorial-users.dox
May 13, 2024
308bfaa
[TUTO][FIX] Fix missing protection around vpDisplay
May 13, 2024
abcd78b
Fix to introduce a virtual destructor when the class has virtual func…
fspindle May 19, 2024
d20e84b
Remove \mathcal that is not supported by mathjax + code indentation
fspindle May 19, 2024
5ad3c1e
Attempt to fix the following issue on ci.
fspindle May 20, 2024
49772bb
Set GIT_CLONE_PROTECTION_ACTIVE env var
fspindle May 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 33 additions & 9 deletions doc/tutorial/python/tutorial-install-python-bindings.dox
Original file line number Diff line number Diff line change
Expand Up @@ -672,26 +672,50 @@ If you have an issue that looks like:
from /usr/include/c++/11/bits/specfun.h:45,
from /usr/include/c++/11/cmath:1935,
from /usr/include/c++/11/math.h:36,
from /home/sfelton/miniconda3/envs/wrapper3.9/include/python3.9/pyport.h:205,
from /home/sfelton/miniconda3/envs/wrapper3.9/include/python3.9/Python.h:50,
from /home/sfelton/.local/include/pybind11/detail/common.h:266,
from /home/sfelton/.local/include/pybind11/attr.h:13,
from /home/sfelton/.local/include/pybind11/detail/class.h:12,
from /home/sfelton/.local/include/pybind11/pybind11.h:13,
from /home/sfelton/software/visp_build/modules/python/bindings/src/robot.cpp:3:
from /home/user/miniconda3/envs/wrapper3.9/include/python3.9/pyport.h:205,
from /home/user/miniconda3/envs/wrapper3.9/include/python3.9/Python.h:50,
from /home/user/.local/include/pybind11/detail/common.h:266,
from /home/user/.local/include/pybind11/attr.h:13,
from /home/user/.local/include/pybind11/detail/class.h:12,
from /home/user/.local/include/pybind11/pybind11.h:13,
from /home/user/software/visp_build/modules/python/bindings/src/robot.cpp:3:
/usr/include/c++/11/type_traits: **In instantiation of ‘struct std::is_move_constructible<vpImage<double> >’:**
rolalaro marked this conversation as resolved.
Show resolved Hide resolved
/usr/include/c++/11/type_traits:152:12: required from ‘struct std::__and_<std::is_move_constructible<vpImage<double> >, std::is_move_assignable<vpImage<double> > >’
/usr/include/c++/11/type_traits:157:12: required from ‘struct std::__and_<std::__not_<std::__is_tuple_like<vpImage<double> > >, std::is_move_constructible<vpImage<double> >, std::is_move_assignable<vpImage<double> > >’
/usr/include/c++/11/type_traits:2209:11: required by substitution of ‘template<class ... _Cond> using _Require = std::__enable_if_t<std::__and_< <template-parameter-1-1> >::value> [with _Cond = {std::__not_<std::__is_tuple_like<vpImage<double> > >, std::is_move_constructible<vpImage<double> >, std::is_move_assignable<vpImage<double> >}]’
/usr/include/c++/11/bits/move.h:196:5: required by substitution of ‘template<class _Tp> std::_Require<std::__not_<std::__is_tuple_like<_Tp> >, std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> > std::swap(_Tp&, _Tp&) [with _Tp = vpImage<double>]’
/home/sfelton/software/visp-sfelton/modules/core/include/visp3/core/vpImage.h:341:15: required from ‘class vpImage<double>’
/home/sfelton/software/visp-sfelton/modules/core/include/visp3/core/vpImage.h:369:17: required from here
/home/user/software/visp-user/modules/core/include/visp3/core/vpImage.h:341:15: required from ‘class vpImage<double>’
/home/user/software/visp-user/modules/core/include/visp3/core/vpImage.h:369:17: required from here
rolalaro marked this conversation as resolved.
Show resolved Hide resolved
/usr/include/c++/11/type_traits:1010:52: error: static assertion failed: template argument must be a complete class or an unbounded array
1010 | **static_assert(std::__is_complete_or_unbounded(__type_identity<_Tp>{}),**
\endverbatim

You should delete the files in `modules/python/` of the **build** directory.


If you have an error that looks like this:

\verbatim
/home/user/miniconda3/envs/visp-python11/include/pybind11/cast.h: In instantiation of ‘pybind11::arg_v::arg_v(pybind11::arg&&, T&&, const char*) [with T = vpColVector (&)(const vpColVector&, const vpColVector&)]’:
/home/user/miniconda3/envs/visp-python11/include/pybind11/cast.h:1452:53: required from ‘pybind11::arg_v::arg_v(const char*, T&&, const char*) [with T = vpColVector (&)(const vpColVector&, const vpColVector&)]’
/home/user/visp_ws/visp_ukf/modules/python/bindings/src/core.cpp:20491:347: required from here
/home/user/miniconda3/envs/visp-python11/include/pybind11/cast.h:1432:82: error: call of overloaded ‘cast(vpColVector (&)(const vpColVector&, const vpColVector&), pybind11::return_value_policy, <brace-enclosed initializer list>)’ is ambiguous
1432 | : arg(base), value(reinterpret_steal<object>(detail::make_caster<T>::cast(
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
1433 | std::forward<T>(x), return_value_policy::automatic, {}))),
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/user/miniconda3/envs/visp-python11/include/pybind11/cast.h:15,
from /home/user/miniconda3/envs/visp-python11/include/pybind11/attr.h:14,
from /home/user/miniconda3/envs/visp-python11/include/pybind11/detail/class.h:12,
from /home/user/miniconda3/envs/visp-python11/include/pybind11/pybind11.h:13,
from /home/user/visp_ws/visp_ukf/modules/python/bindings/src/core.cpp:3:
\endverbatim
rolalaro marked this conversation as resolved.
Show resolved Hide resolved

This is due to an argument that cannot have a default value. In the example above, this argument is **vpColVector (&)(const vpColVector&, const vpColVector&)** (converted from an std::function).
To solve this, go to `modules/python/generator/visp_python_bindgen/generator_config.py`. Then, add a regular expression in the **FORBIDDEN_DEFAULT_ARGUMENT_TYPES_REGEXS** list to match with this type.
Here, we would add '^std::function' to exclude the std::function from being constructed with a default value.


\subsubsection py_bindings_known_errors_import_dll ImportError: DLL load failed while importing _visp

The following error may occur on Windows
Expand Down
9 changes: 8 additions & 1 deletion modules/core/include/visp3/core/vpMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,15 @@ class VISP_EXPORT vpMatrix : public vpArray2D<double>
#if defined(VISP_HAVE_OPENCV)
double detByLUOpenCV() const;
#endif
vpMatrix cholesky() const;
#if defined(VISP_HAVE_EIGEN3)
vpMatrix choleskyByEigen3() const;
#endif
#if defined(VISP_HAVE_LAPACK)
vpMatrix choleskyByLapack() const;
#endif

// Compute the exponential matrix of a square matrix
// Compute the exponential matrix of a square matrix
rolalaro marked this conversation as resolved.
Show resolved Hide resolved
vpMatrix expm() const;

// operation A = A + B
Expand Down
83 changes: 83 additions & 0 deletions modules/core/include/visp3/core/vpUKSigmaDrawerAbstract.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* ViSP, open source Visual Servoing Platform software.
* Copyright (C) 2005 - 2024 by Inria. All rights reserved.
*
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file LICENSE.txt at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using ViSP with software that can not be combined with the GNU
* GPL, please contact Inria about acquiring a ViSP Professional
* Edition License.
*
* See https://visp.inria.fr for more information.
*
* This software was developed at:
* Inria Rennes - Bretagne Atlantique
* Campus Universitaire de Beaulieu
* 35042 Rennes Cedex
* France
*
* If you have questions regarding the use of this file, please contact
* Inria at visp@inria.fr
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Description:
* Display a point cloud using PCL library.
*/

#ifndef _vpUKSigmaDrawerAbstract_h_
#define _vpUKSigmaDrawerAbstract_h_

#include <vector>

#include <visp3/core/vpConfig.h>

#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
#include <visp3/core/vpColVector.h>

/*!
\class vpUKSigmaDrawerAbstract
\ingroup group_core_kalman
This abstract class defines the interface to draw the sigma points for the Unscented Kalman filter.
*/
class VISP_EXPORT vpUKSigmaDrawerAbstract
{
public:
/**
* \brief The weights corresponding to the sigma points drawing.
*/
typedef struct vpSigmaPointsWeights
{
std::vector<double> m_wm; /*!< The weights for the computation of the mean.*/
std::vector<double> m_wc; /*!< The weights for the computation of the covariance.*/
}vpSigmaPointsWeights;

inline vpUKSigmaDrawerAbstract(const unsigned int &n) : m_n(n) { }

/**
* \brief Draw the sigma points according to the current mean and covariance of the state
* of the Unscented Kalman filter.
*
* \param[in] mean The current mean of the state of the UKF.
* \param[in] covariance The current process covariance of the UKF.
* @return std::vector<vpColVector> The sigma points.
*/
virtual std::vector<vpColVector> drawSigmaPoints(const vpColVector &mean, const vpMatrix &covariance) = 0;

/**
* \brief Computed the weigths that correspond to the sigma poitns that have been drawn.
*
* \return vpSigmaPointsWeights The weights that correspond to the sigma points.
*/
virtual vpSigmaPointsWeights computeWeights() = 0;
protected:
unsigned int m_n; /*!< The size of the state of the UKF.*/
};
#endif
#endif
141 changes: 141 additions & 0 deletions modules/core/include/visp3/core/vpUKSigmaDrawerMerwe.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* ViSP, open source Visual Servoing Platform software.
* Copyright (C) 2005 - 2024 by Inria. All rights reserved.
*
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file LICENSE.txt at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using ViSP with software that can not be combined with the GNU
* GPL, please contact Inria about acquiring a ViSP Professional
* Edition License.
*
* See https://visp.inria.fr for more information.
*
* This software was developed at:
* Inria Rennes - Bretagne Atlantique
* Campus Universitaire de Beaulieu
* 35042 Rennes Cedex
* France
*
* If you have questions regarding the use of this file, please contact
* Inria at visp@inria.fr
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Description:
* Display a point cloud using PCL library.
*/

#ifndef _vpUKSigmaDrawerMerwe_h_
#define _vpUKSigmaDrawerMerwe_h_

#include <vector>

#include <visp3/core/vpConfig.h>

#include <visp3/core/vpUKSigmaDrawerAbstract.h>
#include <visp3/core/vpUnscentedKalman.h>

#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
/*!
\class vpUKSigmaDrawerMerwe
\ingroup group_core_kalman
This class defines a class to draw sigma points following the E. A. Wan and R. van der Merwe's method.

The method has four parameters: \f$ n \f$, which is the dimension of the input, and \f$ \alpha \f$, \f$ \beta \f$ and
\f$ \kappa \f$, which are three reals. For notational convenience, we define \f$ \lambda = \alpha^2 (n - \kappa) - n \f$.

Be \f$ \boldsymbol{\mu} \in \mathcal{R}^n \f$ the mean and \f$ \boldsymbol{\Sigma} \in \mathcal{R}^{n x n} \f$ the covariance matrix of the
input of the algorithm. The algorithm will draw \f$ 2n + 1 \f$ sigma points \f$ \chi_i \in \mathcal{R}^n \f$ such as:

\f{eqnarray*}{
\chi_0 &=& \boldsymbol{\mu} \\
\chi_i &=& \begin{cases} \boldsymbol{\mu} + \left[ \sqrt{(n + \lambda) \boldsymbol{\Sigma}} \right]_i^T & i = 1 .. n \\
\boldsymbol{\mu} - \left[ \sqrt{(n + \lambda) \boldsymbol{\Sigma}} \right]_{i - n}^T & i = n + 1 .. 2n
\end{cases}
\f}

where the subscript \f$ i \f$ denotes that we keep the \f$ i^{th} \f$ of the matrix.

Several definitions of the square root of a matrix exists. We decided to use the following definition: \f$ \textbf{L} \f$
is the square root of the matrix \f$ \boldsymbol{\Sigma} \f$ if \f$ \boldsymbol{\Sigma} \f$ can be written as:

\f$ \boldsymbol{\Sigma} = \textbf{L} \textbf{L}^T \f$

This definition is favored because it can be computed using the Cholesky's decomposition.

The computation of the weights that go along the sigma points is the following. The weight used for the
computation of the mean of \f$ \chi_0 \f$ is computed such as:

\f$ w_0^m = \frac{\lambda}{n + \lambda} \f$

The weight used for the computation of the mean of \f$ \chi_0 \f$ is computed such as:

\f$ w_0^c = \frac{\lambda}{n + \lambda} + 1 - \alpha^2 + \beta \f$

The weights for the other sigma points \f$ \chi_1 ... \chi_{2n} \f$ are the same for the mean and covariance
and are computed as follow:

\f$ w_i^m = w_i^c = \frac{1}{2(n + \lambda)} i = 1..2n \f$

\b Note: the weights do not sum to one. Negative values can even be expected.

\b Additionnal \b note: the original author recommended to set \f$ \beta = 2 \f$ for Gaussian problems, \f$ \kappa = 3 - n \f$
and \f$ 0 \leq \alpha \leq 1 \f$, where a larger value for \f$ \alpha \f$ spreads the sigma points further from the mean,
which can be a problem for highly non-linear problems.

*/
class VISP_EXPORT vpUKSigmaDrawerMerwe : public vpUKSigmaDrawerAbstract
{
public:
typedef vpUnscentedKalman::vpAddSubFunction vpAddSubFunction;

/**
* \brief Construct a new vpUKSigmaDrawerMerwe object.
*
* \param[in] n The size of the state vector.
* \param[in] alpha A factor, which should be a real in the interval [0; 1]. The larger alpha is,
* the further the sigma points are spread from the mean.
* \param[in] beta Another factor, which should be set to 2 if the problem is Gaussian.
* \param[in] kappa A third factor, whose value should be set to 3 - n for most problems.
*/
vpUKSigmaDrawerMerwe(const int &n, const double &alpha, const double &beta, const double &kappa,
const vpAddSubFunction &resFunc = vpUnscentedKalman::simpleResidual,
const vpAddSubFunction &addFunc = vpUnscentedKalman::simpleAdd);

/**
* \brief Draw the sigma points according to the current mean and covariance of the state
* of the Unscented Kalman filter.
*
* \param[in] mean The current mean of the state of the UKF.
* \param[in] covariance The current process covariance of the UKF.
* @return std::vector<vpColVector> The sigma points.
*/
virtual std::vector<vpColVector> drawSigmaPoints(const vpColVector &mean, const vpMatrix &covariance) override;

/**
* \brief Computed the weigths that correspond to the sigma poitns that have been drawn.
*
* \return vpSigmaPointsWeights The weights that correspond to the sigma points.
*/
virtual vpSigmaPointsWeights computeWeights() override;
protected:
inline void computeLambda()
{
m_lambda = m_alpha * m_alpha * (m_n + m_kappa) - m_n;
}

double m_alpha;
double m_beta;
double m_kappa;
double m_lambda;
vpAddSubFunction m_resFunc;
vpAddSubFunction m_addFunc;
};
#endif
#endif