WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
S3SigmaPoints.hpp
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
8#include "wpi/util/array.hpp"
9
10namespace wpi::math {
11
12/**
13 * Generates sigma points and weights according to Papakonstantinou's paper[1]
14 * for the UnscentedKalmanFilter class.
15 *
16 * It parameterizes the sigma points using alpha and beta terms. Unless you know
17 * better, this should be your default choice due to its high accuracy and
18 * performance.
19 *
20 * [1] K. Papakonstantinou "A Scaled Spherical Simplex Filter (S3F) with a
21 * decreased n + 2 sigma points set size and equivalent 2n + 1 Unscented Kalman
22 * Filter (UKF) accuracy"
23 *
24 * @tparam States The dimenstionality of the state. States + 2 weights will be
25 * generated.
26 */
27template <int States>
29 public:
30 static constexpr int NumSigmas = States + 2;
31
32 /**
33 * Constructs a generator for Papakonstantinou 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 */
40 explicit S3SigmaPoints(double alpha = 1e-3, double beta = 2)
41 : m_alpha{alpha} {
42 ComputeWeights(beta);
43 }
44
45 /**
46 * Computes the sigma points for an unscented Kalman filter given the mean (x)
47 * and square-root covariance (S) of the filter.
48 *
49 * @param x An array of the means.
50 * @param S Square-root covariance of the filter.
51 *
52 * @return Two dimensional array of sigma points. Each column contains all of
53 * the sigmas for one dimension in the problem space. Ordered by Xi_0,
54 * Xi_{1..n+1}.
55 */
57 const Vectord<States>& x, const Matrixd<States, States>& S) const {
58 // table (1), equation (12)
60 for (size_t t = 1; t <= States; ++t) {
61 q[t - 1] = m_alpha * std::sqrt(static_cast<double>(t * (States + 1)) /
62 static_cast<double>(t + 1));
63 }
64
66 C.template block<States, 1>(0, 0) = Vectord<States>::Constant(0.0);
67 for (int col = 1; col < NumSigmas; ++col) {
68 for (int row = 0; row < States; ++row) {
69 if (row < col - 2) {
70 C(row, col) = 0.0;
71 } else if (row == col - 2) {
72 C(row, col) = q[row];
73 } else {
74 C(row, col) = -q[row] / (row + 1);
75 }
76 }
77 }
78
80 for (int col = 0; col < NumSigmas; ++col) {
81 sigmas.col(col) = x + S * C.col(col);
82 }
83
84 return sigmas;
85 }
86
87 /**
88 * Returns the weight for each sigma point for the mean.
89 */
90 const Vectord<NumSigmas>& Wm() const { return m_Wm; }
91
92 /**
93 * Returns an element of the weight for each sigma point for the mean.
94 *
95 * @param i Element of vector to return.
96 */
97 double Wm(int i) const { return m_Wm(i, 0); }
98
99 /**
100 * Returns the weight for each sigma point for the covariance.
101 */
102 const Vectord<NumSigmas>& Wc() const { return m_Wc; }
103
104 /**
105 * Returns an element of the weight for each sigma point for the covariance.
106 *
107 * @param i Element of vector to return.
108 */
109 double Wc(int i) const { return m_Wc(i, 0); }
110
111 private:
114 double m_alpha;
115
116 /**
117 * Computes the weights for the scaled unscented Kalman filter.
118 *
119 * @param beta Incorporates prior knowledge of the distribution of the mean.
120 */
121 void ComputeWeights(double beta) {
122 double alpha_sq = m_alpha * m_alpha;
123
124 double c = 1.0 / (alpha_sq * (States + 1));
127
128 m_Wm(0) = 1.0 - 1.0 / alpha_sq;
129 m_Wc(0) = 1.0 - 1.0 / alpha_sq + (1 - alpha_sq + beta);
130 }
131};
132
133} // namespace wpi::math
#define S(label, offset, message)
Definition Errors.hpp:113
double Wm(int i) const
Returns an element of the weight for each sigma point for the mean.
Definition S3SigmaPoints.hpp:97
S3SigmaPoints(double alpha=1e-3, double beta=2)
Constructs a generator for Papakonstantinou sigma points.
Definition S3SigmaPoints.hpp:40
Matrixd< States, NumSigmas > SquareRootSigmaPoints(const Vectord< States > &x, const Matrixd< States, States > &S) const
Computes the sigma points for an unscented Kalman filter given the mean (x) and square-root covarianc...
Definition S3SigmaPoints.hpp:56
double Wc(int i) const
Returns an element of the weight for each sigma point for the covariance.
Definition S3SigmaPoints.hpp:109
static constexpr int NumSigmas
Definition S3SigmaPoints.hpp:30
const Vectord< NumSigmas > & Wm() const
Returns the weight for each sigma point for the mean.
Definition S3SigmaPoints.hpp:90
const Vectord< NumSigmas > & Wc() const
Returns the weight for each sigma point for the covariance.
Definition S3SigmaPoints.hpp:102
This class is a wrapper around std::array that does compile time size checking.
Definition array.hpp:26
Definition LinearSystem.hpp:20
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.hpp:21
Eigen::Vector< double, Size > Vectord
Definition EigenCore.hpp:12
constexpr empty_array_t empty_array
Definition array.hpp:16