24#ifndef TAPROOT_MATH_USER_UTILS_HPP_
25#define TAPROOT_MATH_USER_UTILS_HPP_
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"
44static constexpr float ACCELERATION_GRAVITY = 9.80665f;
59 return fabsf(val1 - val2) <= epsilon;
115 if (alpha < 0.0f || alpha > 1.0f)
119 return alpha * newValue + (1.0f - alpha) * prevValue;
122template <
typename From,
typename To>
125 static_assert(
sizeof(From) ==
sizeof(To),
"can only reinterpret-copy types of the same size");
127 memcpy(
static_cast<void*
>(&result),
static_cast<void*
>(&from),
sizeof(To));
142CMSISMat<3, 1>
cross(
const CMSISMat<3, 1>& a,
const CMSISMat<3, 1>& b);
147CMSISMat<3, 3>
fromEulerAngles(
const float roll,
const float pitch,
const float yaw);
163constexpr int32_t
ceil(
float num)
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);
182 return (T(0) < val) - (val < T(0));
191template <
typename T,
size_t xSize,
size_t ySize>
193 const std::array<std::array<T, ySize>, xSize>& values,
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);
206 float yDesBounded =
limitVal(yDes, yMin, yMax);
210 int xIndex = floor((xDesBounded - xMin) / dx);
211 xIndex =
limitVal(xIndex, 0,
static_cast<int>(xSize) - 2);
212 float x1 = xMin + xIndex * dx;
213 float x2 = xMin + (xIndex + 1) * dx;
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;
220 float q11, q12, q21, q22;
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));
226 float x2x, y2y, yy1, xx1;
227 x2x = x2 - xDesBounded;
228 y2y = y2 - yDesBounded;
229 yy1 = yDesBounded - y1;
230 xx1 = xDesBounded - x1;
232 return 1.0 / (dx * dy) *
233 (q11 * x2x * y2y + q21 * xx1 * y2y + q12 * x2x * yy1 + q22 * xx1 * yy1);
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