Fang-Robotics-MCB
Fang Robotics Team Codebase
Loading...
Searching...
No Matches
math_user_utils.hpp
Go to the documentation of this file.
1/*****************************************************************************/
2/********** !!! WARNING: CODE GENERATED BY TAPROOT. DO NOT EDIT !!! **********/
3/*****************************************************************************/
4
5/*
6 * Copyright (c) 2020-2023 Advanced Robotics at the University of Washington <robomstr@uw.edu>
7 *
8 * This file is part of Taproot.
9 *
10 * Taproot is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Taproot is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Taproot. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifndef TAPROOT_MATH_USER_UTILS_HPP_
25#define TAPROOT_MATH_USER_UTILS_HPP_
26
27#include <array>
28#include <cinttypes>
29#include <cmath>
30#include <cstring>
31
32#include "modm/architecture/interface/assert.hpp"
33#include "modm/math/geometry/angle.hpp"
34#include "modm/math/geometry/quaternion.hpp"
35#include "modm/math/geometry/vector3.hpp"
36
37#include "cmsis_mat.hpp"
38
39namespace tap
40{
41namespace algorithms
42{
44static constexpr float ACCELERATION_GRAVITY = 9.80665f;
45
57inline bool compareFloatClose(float val1, float val2, float epsilon)
58{
59 return fabsf(val1 - val2) <= epsilon;
60}
61
71template <typename T>
72inline T limitVal(T val, T min, T max)
73{
74 if (min > max)
75 {
76 return val;
77 }
78 if (val < min)
79 {
80 return min;
81 }
82 else if (val > max)
83 {
84 return max;
85 }
86 else
87 {
88 return val;
89 }
90}
91
113inline float lowPassFilter(float prevValue, float newValue, float alpha)
114{
115 if (alpha < 0.0f || alpha > 1.0f)
116 {
117 return newValue;
118 }
119 return alpha * newValue + (1.0f - alpha) * prevValue;
120}
121
122template <typename From, typename To>
123To reinterpretCopy(From from)
124{
125 static_assert(sizeof(From) == sizeof(To), "can only reinterpret-copy types of the same size");
126 To result;
127 memcpy(static_cast<void*>(&result), static_cast<void*>(&from), sizeof(To));
128 return result;
129}
130
137float fastInvSqrt(float x);
138
142CMSISMat<3, 1> cross(const CMSISMat<3, 1>& a, const CMSISMat<3, 1>& b);
143
147CMSISMat<3, 3> fromEulerAngles(const float roll, const float pitch, const float yaw);
148
157void rotateVector(float* x, float* y, float radians);
158
163constexpr int32_t ceil(float num)
164{
165 return (static_cast<float>(static_cast<int32_t>(num)) == num)
166 ? static_cast<int32_t>(num)
167 : static_cast<int32_t>(num) + ((num > 0) ? 1 : 0);
168}
169
173modm::Vector3f eulerAnglesFromQuaternion(modm::Quaternion<float>& q);
174
179template <typename T>
180int getSign(T val)
181{
182 return (T(0) < val) - (val < T(0));
183}
184
191template <typename T, size_t xSize, size_t ySize>
193 const std::array<std::array<T, ySize>, xSize>& values,
194 const float xMin,
195 const float xMax,
196 const float dx,
197 const float yMin,
198 const float yMax,
199 const float dy,
200 float xDes,
201 float yDes)
202{
203 modm_assert((xMax - xMin) / dx == xSize - 1, "Bilinear Interpolator", "x range error");
204 modm_assert((yMax - yMin) / dy == ySize - 1, "Bilinear Interpolator", "y range error");
205 float xDesBounded = limitVal(xDes, xMin, xMax); // no extrapolation allowed
206 float yDesBounded = limitVal(yDes, yMin, yMax); // no extrapolation allowed
207
208 // In each dimension, find the index of the closest point in the LUT below the desired point,
209 // then use that index to find the value of the two points which the desired point lies between
210 int xIndex = floor((xDesBounded - xMin) / dx);
211 xIndex = limitVal(xIndex, 0, static_cast<int>(xSize) - 2); // Prevent OOB errors
212 float x1 = xMin + xIndex * dx; // gets value from index
213 float x2 = xMin + (xIndex + 1) * dx;
214
215 int yIndex = floor((yDesBounded - yMin) / dy);
216 yIndex = limitVal(yIndex, 0, static_cast<int>(ySize) - 2);
217 float y1 = yMin + yIndex * dy;
218 float y2 = yMin + (yIndex + 1) * dy;
219
220 float q11, q12, q21, q22; // values of x1y1, x1y2, x2y1, x2y2
221 q11 = static_cast<float>(values.at(xIndex).at(yIndex));
222 q12 = static_cast<float>(values.at(xIndex).at(yIndex + 1));
223 q21 = static_cast<float>(values.at(xIndex + 1).at(yIndex));
224 q22 = static_cast<float>(values.at(xIndex + 1).at(yIndex + 1));
225
226 float x2x, y2y, yy1, xx1; // deltas from each pt to sample pt
227 x2x = x2 - xDesBounded;
228 y2y = y2 - yDesBounded;
229 yy1 = yDesBounded - y1;
230 xx1 = xDesBounded - x1;
231 // it's essentially a weighted average
232 return 1.0 / (dx * dy) *
233 (q11 * x2x * y2y + q21 * xx1 * y2y + q12 * x2x * yy1 + q22 * xx1 * yy1);
234}
235
236} // namespace algorithms
237
238} // namespace tap
239
240#endif // TAPROOT_MATH_USER_UTILS_HPP_
To reinterpretCopy(From from)
Definition math_user_utils.hpp:123
modm::Vector3f eulerAnglesFromQuaternion(modm::Quaternion< float > &q)
Definition math_user_utils.cpp:74
CMSISMat< 3, 1 > cross(const CMSISMat< 3, 1 > &a, const CMSISMat< 3, 1 > &b)
Definition math_user_utils.cpp:47
T limitVal(T val, T min, T max)
Definition math_user_utils.hpp:72
float fastInvSqrt(float x)
Definition math_user_utils.cpp:28
float lowPassFilter(float prevValue, float newValue, float alpha)
Definition math_user_utils.hpp:113
bool compareFloatClose(float val1, float val2, float epsilon)
Definition math_user_utils.hpp:57
int getSign(T val)
Definition math_user_utils.hpp:180
float interpolateLinear2D(const std::array< std::array< T, ySize >, xSize > &values, const float xMin, const float xMax, const float dx, const float yMin, const float yMax, const float dy, float xDes, float yDes)
Bilinear Interpolation of a regularly-spaced grid of values. Let x = dimension 1 and y = dimension 2 ...
Definition math_user_utils.hpp:192
CMSISMat< 3, 3 > fromEulerAngles(const float roll, const float pitch, const float yaw)
Definition math_user_utils.cpp:57
constexpr int32_t ceil(float num)
Definition math_user_utils.hpp:163
void rotateVector(float *x, float *y, float radians)
Definition math_user_utils.cpp:40
Definition ballistics.cpp:29