@@ -34,18 +34,28 @@ namespace frc {
34
34
* @return State excursion or control effort cost matrix.
35
35
*/
36
36
template <std::same_as<double >... Ts>
37
- Matrixd<sizeof ...(Ts), sizeof ...(Ts)> MakeCostMatrix (Ts... tolerances) {
38
- Eigen::DiagonalMatrix<double , sizeof ...(Ts)> result;
39
- auto & diag = result.diagonal ();
37
+ constexpr Matrixd<sizeof ...(Ts), sizeof ...(Ts)> MakeCostMatrix (
38
+ Ts... tolerances) {
39
+ Matrixd<sizeof ...(Ts), sizeof ...(Ts)> result;
40
+
41
+ for (int row = 0 ; row < result.rows (); ++row) {
42
+ for (int col = 0 ; col < result.cols (); ++col) {
43
+ if (row != col) {
44
+ result.coeffRef (row, col) = 0.0 ;
45
+ }
46
+ }
47
+ }
48
+
40
49
wpi::for_each (
41
50
[&](int i, double tolerance) {
42
51
if (tolerance == std::numeric_limits<double >::infinity ()) {
43
- diag ( i) = 0.0 ;
52
+ result. coeffRef (i, i) = 0.0 ;
44
53
} else {
45
- diag (i ) = 1.0 / std::pow (tolerance, 2 );
54
+ result. coeffRef (i, i ) = 1.0 / (tolerance * tolerance );
46
55
}
47
56
},
48
57
tolerances...);
58
+
49
59
return result;
50
60
}
51
61
@@ -62,11 +72,21 @@ Matrixd<sizeof...(Ts), sizeof...(Ts)> MakeCostMatrix(Ts... tolerances) {
62
72
* @return Process noise or measurement noise covariance matrix.
63
73
*/
64
74
template <std::same_as<double >... Ts>
65
- Matrixd<sizeof ...(Ts), sizeof ...(Ts)> MakeCovMatrix (Ts... stdDevs) {
66
- Eigen::DiagonalMatrix<double , sizeof ...(Ts)> result;
67
- auto & diag = result.diagonal ();
68
- wpi::for_each ([&](int i, double stdDev) { diag (i) = std::pow (stdDev, 2 ); },
69
- stdDevs...);
75
+ constexpr Matrixd<sizeof ...(Ts), sizeof ...(Ts)> MakeCovMatrix (Ts... stdDevs) {
76
+ Matrixd<sizeof ...(Ts), sizeof ...(Ts)> result;
77
+
78
+ for (int row = 0 ; row < result.rows (); ++row) {
79
+ for (int col = 0 ; col < result.cols (); ++col) {
80
+ if (row != col) {
81
+ result.coeffRef (row, col) = 0.0 ;
82
+ }
83
+ }
84
+ }
85
+
86
+ wpi::for_each (
87
+ [&](int i, double stdDev) { result.coeffRef (i, i) = stdDev * stdDev; },
88
+ stdDevs...);
89
+
70
90
return result;
71
91
}
72
92
@@ -84,16 +104,23 @@ Matrixd<sizeof...(Ts), sizeof...(Ts)> MakeCovMatrix(Ts... stdDevs) {
84
104
* @return State excursion or control effort cost matrix.
85
105
*/
86
106
template <size_t N>
87
- Matrixd<N, N> MakeCostMatrix (const std::array<double , N>& costs) {
88
- Eigen::DiagonalMatrix<double , N> result;
89
- auto & diag = result.diagonal ();
90
- for (size_t i = 0 ; i < costs.size (); ++i) {
91
- if (costs[i] == std::numeric_limits<double >::infinity ()) {
92
- diag (i) = 0.0 ;
93
- } else {
94
- diag (i) = 1.0 / std::pow (costs[i], 2 );
107
+ constexpr Matrixd<N, N> MakeCostMatrix (const std::array<double , N>& costs) {
108
+ Matrixd<N, N> result;
109
+
110
+ for (int row = 0 ; row < result.rows (); ++row) {
111
+ for (int col = 0 ; col < result.cols (); ++col) {
112
+ if (row == col) {
113
+ if (costs[row] == std::numeric_limits<double >::infinity ()) {
114
+ result.coeffRef (row, col) = 0.0 ;
115
+ } else {
116
+ result.coeffRef (row, col) = 1.0 / (costs[row] * costs[row]);
117
+ }
118
+ } else {
119
+ result.coeffRef (row, col) = 0.0 ;
120
+ }
95
121
}
96
122
}
123
+
97
124
return result;
98
125
}
99
126
@@ -110,12 +137,19 @@ Matrixd<N, N> MakeCostMatrix(const std::array<double, N>& costs) {
110
137
* @return Process noise or measurement noise covariance matrix.
111
138
*/
112
139
template <size_t N>
113
- Matrixd<N, N> MakeCovMatrix (const std::array<double , N>& stdDevs) {
114
- Eigen::DiagonalMatrix<double , N> result;
115
- auto & diag = result.diagonal ();
116
- for (size_t i = 0 ; i < N; ++i) {
117
- diag (i) = std::pow (stdDevs[i], 2 );
140
+ constexpr Matrixd<N, N> MakeCovMatrix (const std::array<double , N>& stdDevs) {
141
+ Matrixd<N, N> result;
142
+
143
+ for (int row = 0 ; row < result.rows (); ++row) {
144
+ for (int col = 0 ; col < result.cols (); ++col) {
145
+ if (row == col) {
146
+ result.coeffRef (row, col) = stdDevs[row] * stdDevs[row];
147
+ } else {
148
+ result.coeffRef (row, col) = 0.0 ;
149
+ }
150
+ }
118
151
}
152
+
119
153
return result;
120
154
}
121
155
0 commit comments