blob: 851a90aadfe58ae892ff6e99ac2701e949b27864 [file] [log] [blame]
Brian Silverman8d3816a2017-07-03 18:52:15 -07001#ifndef MOTORS_MATH_H_
2#define MOTORS_MATH_H_
3
4#include <limits.h>
5
6#include <complex>
7#include <ratio>
8
9// This file has some specialized math functions useful for implementing our
10// controls in a minimal number of cycles.
11
12namespace frc971 {
13namespace salsa {
14
15inline constexpr unsigned int Log2RoundUp(unsigned int x) {
16 return (x < 2) ? x : (1 + Log2RoundUp(x / 2));
17}
18
19template <typename T>
20inline constexpr const T &ConstexprMax(const T &a, const T &b) {
21 return (a < b) ? b : a;
22}
23
24namespace math_internal {
25
26constexpr uint32_t SinCosTableSize() { return 1024; }
27
28constexpr float FloatMaxMagnitude() { return 1.0f; }
29
30constexpr bool IsPowerOf2(uint32_t value) {
31 return value == (1u << (Log2RoundUp(value) - 1));
32}
33
34static_assert(IsPowerOf2(SinCosTableSize()), "Tables need to be a power of 2");
35
36extern float sin_int_table[SinCosTableSize()];
37extern float cos_int_table[SinCosTableSize()];
38extern float sin_float_table[SinCosTableSize() + 1];
39extern float cos_float_table[SinCosTableSize() + 1];
40
41template <class Rotation>
42float FastTableLookupInt(uint32_t theta, const float *table) {
43 static_assert(IsPowerOf2(Rotation::den),
44 "Denominator needs to be a power of 2");
45
46 // Don't need to worry about the sizes of intermediates given this constraint.
47 static_assert(
48 ConstexprMax<uint32_t>(Rotation::den, SinCosTableSize()) * Rotation::num <
49 UINT32_MAX,
50 "Numerator and denominator are too big");
51
52 // Rounding/truncating here isn't supported.
53 static_assert(Rotation::den <= SinCosTableSize(),
54 "Tables need to be bigger");
55
56 // Don't feel like thinking through the consequences of this not being true.
57 static_assert(Rotation::num > 0 && Rotation::den > 0,
58 "Need a positive ratio");
59
60 constexpr uint32_t kDenominatorRatio = SinCosTableSize() / Rotation::den;
61
62 // These should always be true given the other constraints.
63 static_assert(kDenominatorRatio * Rotation::den == SinCosTableSize(),
64 "Math is broken");
65 static_assert(IsPowerOf2(kDenominatorRatio), "Math is broken");
66
67 return table[(theta * kDenominatorRatio * Rotation::num +
68 kDenominatorRatio * Rotation::num / 2) %
69 SinCosTableSize()];
70}
71
72inline float FastTableLookupFloat(float theta, const float *table) {
73 const int index = (SinCosTableSize() / 2) +
74 static_cast<int32_t>(theta * ((SinCosTableSize() / 2) /
75 FloatMaxMagnitude()));
76 return table[index];
77}
78
79} // namespace math_internal
80
81// All theta arguments to the float-based functions must be in [-0.2, 0.2].
82
83inline float FastSinFloat(float theta) {
84 return math_internal::FastTableLookupFloat(theta,
85 math_internal::sin_float_table);
86}
87
88inline float FastCosFloat(float theta) {
89 return math_internal::FastTableLookupFloat(theta,
90 math_internal::cos_float_table);
91}
92
93inline ::std::complex<float> ImaginaryExpFloat(float theta) {
94 return ::std::complex<float>(FastCosFloat(theta), FastSinFloat(theta));
95}
96
97// The integer-based sin/cos functions all have a Rotation template argument,
98// which should be a ::std::ratio. The real argument to the trigonometric
99// function is this ratio multiplied by the theta argument.
100//
101// Specifically, they return the function evaluated at
102// (Rotation * (theta + 0.5)).
103//
104// All theta arguments must be in [0, Rotation::den).
105//
106// All denominators must be powers of 2.
107
108template<class Rotation>
109float FastSinInt(uint32_t theta) {
110 return math_internal::FastTableLookupInt<Rotation>(
111 theta, math_internal::sin_int_table);
112}
113
114template<class Rotation>
115float FastCosInt(uint32_t theta) {
116 return math_internal::FastTableLookupInt<Rotation>(
117 theta, math_internal::cos_int_table);
118}
119
120template<class Rotation>
121::std::complex<float> ImaginaryExpInt(uint32_t theta) {
122 return ::std::complex<float>(FastCosInt<Rotation>(theta),
123 FastSinInt<Rotation>(theta));
124}
125
126void MathInit();
127
128} // namespace salsa
129} // namespace frc971
130
131#endif // MOTORS_MATH_H_