WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
MerweScaledSigmaPoints.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <cmath>
8
9#include "frc/EigenCore.h"
10
11namespace frc {
12
13/**
14 * Generates sigma points and weights according to Van der Merwe's 2004
15 * dissertation[1] for the UnscentedKalmanFilter class.
16 *
17 * It parametrizes the sigma points using alpha, beta, kappa terms, and is the
18 * version seen in most publications. S3SigmaPoints is generally preferred due
19 * to its greater performance with nearly identical accuracy.
20 *
21 * [1] R. Van der Merwe "Sigma-Point Kalman Filters for Probabilistic
22 * Inference in Dynamic State-Space Models" (Doctoral dissertation)
23 *
24 * @tparam States The dimensionality of the state. 2 * States + 1 weights will
25 * be generated.
26 */
27template <int States>
29 public:
30 static constexpr int NumSigmas = 2 * States + 1;
31
32 /**
33 * Constructs a generator for Van der Merwe scaled sigma points.
34 *
35 * @param alpha Determines the spread of the sigma points around the mean.
36 * Usually a small positive value (1e-3).
37 * @param beta Incorporates prior knowledge of the distribution of the mean.
38 * For Gaussian distributions, beta = 2 is optimal.
39 * @param kappa Secondary scaling parameter usually set to 0 or 3 - States.
40 */
41 explicit MerweScaledSigmaPoints(double alpha = 1e-3, double beta = 2,
42 int kappa = 3 - States) {
43 m_alpha = alpha;
44 m_kappa = kappa;
45
46 ComputeWeights(beta);
47 }
48
49 /**
50 * Computes the sigma points for an unscented Kalman filter given the mean
51 * (x) and square-root covariance (S) of the filter.
52 *
53 * @param x An array of the means.
54 * @param S Square-root covariance of the filter.
55 *
56 * @return Two dimensional array of sigma points. Each column contains all of
57 * the sigmas for one dimension in the problem space. Ordered by
58 * Xi_0, Xi_{1..n}, Xi_{n+1..2n}.
59 *
60 */
62 const Vectord<States>& x, const Matrixd<States, States>& S) {
63 double lambda = std::pow(m_alpha, 2) * (States + m_kappa) - States;
64 double eta = std::sqrt(lambda + States);
65 Matrixd<States, States> U = eta * S;
66
68
69 // equation (17)
70 sigmas.template block<States, 1>(0, 0) = x;
71 for (int k = 0; k < States; ++k) {
72 sigmas.template block<States, 1>(0, k + 1) =
73 x + U.template block<States, 1>(0, k);
74 sigmas.template block<States, 1>(0, States + k + 1) =
75 x - U.template block<States, 1>(0, k);
76 }
77
78 return sigmas;
79 }
80
81 /**
82 * Returns the weight for each sigma point for the mean.
83 */
84 const Vectord<NumSigmas>& Wm() const { return m_Wm; }
85
86 /**
87 * Returns an element of the weight for each sigma point for the mean.
88 *
89 * @param i Element of vector to return.
90 */
91 double Wm(int i) const { return m_Wm(i, 0); }
92
93 /**
94 * Returns the weight for each sigma point for the covariance.
95 */
96 const Vectord<NumSigmas>& Wc() const { return m_Wc; }
97
98 /**
99 * Returns an element of the weight for each sigma point for the covariance.
100 *
101 * @param i Element of vector to return.
102 */
103 double Wc(int i) const { return m_Wc(i, 0); }
104
105 private:
108 double m_alpha;
109 int m_kappa;
110
111 /**
112 * Computes the weights for the scaled unscented Kalman filter.
113 *
114 * @param beta Incorporates prior knowledge of the distribution of the mean.
115 */
116 void ComputeWeights(double beta) {
117 double lambda = std::pow(m_alpha, 2) * (States + m_kappa) - States;
118
119 double c = 0.5 / (States + lambda);
122
123 m_Wm(0) = lambda / (States + lambda);
124 m_Wc(0) = lambda / (States + lambda) + (1 - std::pow(m_alpha, 2) + beta);
125 }
126};
127
128} // namespace frc
Generates sigma points and weights according to Van der Merwe's 2004 dissertation[1] for the Unscente...
Definition MerweScaledSigmaPoints.h:28
MerweScaledSigmaPoints(double alpha=1e-3, double beta=2, int kappa=3 - States)
Constructs a generator for Van der Merwe scaled sigma points.
Definition MerweScaledSigmaPoints.h:41
const Vectord< NumSigmas > & Wm() const
Returns the weight for each sigma point for the mean.
Definition MerweScaledSigmaPoints.h:84
double Wc(int i) const
Returns an element of the weight for each sigma point for the covariance.
Definition MerweScaledSigmaPoints.h:103
static constexpr int NumSigmas
Definition MerweScaledSigmaPoints.h:30
double Wm(int i) const
Returns an element of the weight for each sigma point for the mean.
Definition MerweScaledSigmaPoints.h:91
Matrixd< States, NumSigmas > SquareRootSigmaPoints(const Vectord< States > &x, const Matrixd< States, States > &S)
Computes the sigma points for an unscented Kalman filter given the mean (x) and square-root covarianc...
Definition MerweScaledSigmaPoints.h:61
const Vectord< NumSigmas > & Wc() const
Returns the weight for each sigma point for the covariance.
Definition MerweScaledSigmaPoints.h:96
Definition SystemServer.h:9
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.h:21
Eigen::Vector< double, Size > Vectord
Definition EigenCore.h:12
#define S(label, offset, message)
Definition Errors.h:113