WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
AngleStatistics.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 <functional>
8#include <numbers>
9
10#include "frc/EigenCore.h"
11#include "frc/MathUtil.h"
12
13namespace frc {
14
15/**
16 * Subtracts a and b while normalizing the resulting value in the selected row
17 * as if it were an angle.
18 *
19 * @tparam States Number of states.
20 * @param a A vector to subtract from.
21 * @param b A vector to subtract with.
22 * @param angleStateIdx The row containing angles to be normalized.
23 */
24template <int States>
26 const Vectord<States>& b, int angleStateIdx) {
27 Vectord<States> ret = a - b;
28 ret[angleStateIdx] =
29 AngleModulus(units::radian_t{ret[angleStateIdx]}).value();
30 return ret;
31}
32
33/**
34 * Returns a function that subtracts two vectors while normalizing the resulting
35 * value in the selected row as if it were an angle.
36 *
37 * @tparam States Number of states.
38 * @param angleStateIdx The row containing angles to be normalized.
39 */
40template <int States>
41std::function<Vectord<States>(const Vectord<States>&, const Vectord<States>&)>
42AngleResidual(int angleStateIdx) {
43 return [=](auto a, auto b) {
44 return AngleResidual<States>(a, b, angleStateIdx);
45 };
46}
47
48/**
49 * Adds a and b while normalizing the resulting value in the selected row as an
50 * angle.
51 *
52 * @tparam States Number of states.
53 * @param a A vector to add with.
54 * @param b A vector to add with.
55 * @param angleStateIdx The row containing angles to be normalized.
56 */
57template <int States>
59 int angleStateIdx) {
60 Vectord<States> ret = a + b;
61 ret[angleStateIdx] =
62 InputModulus(ret[angleStateIdx], -std::numbers::pi, std::numbers::pi);
63 return ret;
64}
65
66/**
67 * Returns a function that adds two vectors while normalizing the resulting
68 * value in the selected row as an angle.
69 *
70 * @tparam States Number of states.
71 * @param angleStateIdx The row containing angles to be normalized.
72 */
73template <int States>
74std::function<Vectord<States>(const Vectord<States>&, const Vectord<States>&)>
75AngleAdd(int angleStateIdx) {
76 return [=](auto a, auto b) { return AngleAdd<States>(a, b, angleStateIdx); };
77}
78
79/**
80 * Computes the mean of sigmas with the weights Wm while computing a special
81 * angle mean for a select row.
82 *
83 * @tparam CovDim Dimension of covariance of sigma points after passing through
84 * the transform.
85 * @tparam NumSigmas Number of sigma points.
86 * @param sigmas Sigma points.
87 * @param Wm Weights for the mean.
88 * @param angleStatesIdx The row containing the angles.
89 */
90template <int CovDim, int NumSigmas>
92 const Vectord<NumSigmas>& Wm, int angleStatesIdx) {
93 double sumSin = (sigmas.row(angleStatesIdx).unaryExpr([](auto it) {
94 return std::sin(it);
95 }) *
96 Wm)
97 .sum();
98 double sumCos = (sigmas.row(angleStatesIdx).unaryExpr([](auto it) {
99 return std::cos(it);
100 }) *
101 Wm)
102 .sum();
103
104 Vectord<CovDim> ret = sigmas * Wm;
105 ret[angleStatesIdx] = std::atan2(sumSin, sumCos);
106 return ret;
107}
108
109/**
110 * Returns a function that computes the mean of sigmas with the weights Wm while
111 * computing a special angle mean for a select row.
112 *
113 * @tparam CovDim Dimension of covariance of sigma points after passing through
114 * the transform.
115 * @tparam NumSigmas Number of sigma points.
116 * @param angleStateIdx The row containing the angles.
117 */
118template <int CovDim, int NumSigmas>
119std::function<Vectord<CovDim>(const Matrixd<CovDim, NumSigmas>&,
120 const Vectord<NumSigmas>&)>
121AngleMean(int angleStateIdx) {
122 return [=](auto sigmas, auto Wm) {
123 return AngleMean<CovDim, NumSigmas>(sigmas, Wm, angleStateIdx);
124 };
125}
126
127} // namespace frc
Definition SystemServer.h:9
Vectord< States > AngleAdd(const Vectord< States > &a, const Vectord< States > &b, int angleStateIdx)
Adds a and b while normalizing the resulting value in the selected row as an angle.
Definition AngleStatistics.h:58
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.h:21
WPILIB_DLLEXPORT constexpr units::radian_t AngleModulus(units::radian_t angle)
Wraps an angle to the range -π to π radians (-180 to 180 degrees).
Definition MathUtil.h:213
Eigen::Vector< double, Size > Vectord
Definition EigenCore.h:12
Vectord< States > AngleResidual(const Vectord< States > &a, const Vectord< States > &b, int angleStateIdx)
Subtracts a and b while normalizing the resulting value in the selected row as if it were an angle.
Definition AngleStatistics.h:25
Vectord< CovDim > AngleMean(const Matrixd< CovDim, NumSigmas > &sigmas, const Vectord< NumSigmas > &Wm, int angleStatesIdx)
Computes the mean of sigmas with the weights Wm while computing a special angle mean for a select row...
Definition AngleStatistics.h:91
constexpr T InputModulus(T input, T minimumInput, T maximumInput)
Returns modulus of input.
Definition MathUtil.h:141