40 #define MAX(a, b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a > _b ? _a : _b; })
41 #define MIN(a, b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a < _b ? _a : _b; })
44 #define sign(x) ((x) < 0 ? -1 : 1)
57 float expo3(
float x, int32_t g);
58 float expoM(
float x, int32_t g,
float exponent);
64 const float *desired,
float *out,
bool normalize);
66 void vector2_rotate(
const float *original,
float *out,
float angle);
67 float cubic_deadband(
float in,
float w,
float b,
float m,
float r);
69 float linear_interpolate(
float const input,
float const * curve, uint8_t num_points,
const float input_min,
const float input_max);
82 return (!isfinite(x));
106 return x * ( (3<<
s_qP) - (x*x>>qR) ) >> qS;
121 static inline void matrix_mul(
const float *a,
const float *b,
122 float *out,
int arows,
int acolsbrows,
int bcols)
127 for (
int ar = 0; ar < arows; ar++) {
128 for (
int bc = 0 ; bc < bcols; bc++) {
131 const float *
restrict bpos = b + bc;
148 for (acbr = 0; acbr < (acolsbrows & (~3)); acbr += 4) {
154 sum += (apos[acbr]) * (*bpos);
156 sum += (apos[acbr+1]) * (*bpos);
158 sum += (apos[acbr+2]) * (*bpos);
160 sum += (apos[acbr+3]) * (*bpos);
164 for (; acbr < acolsbrows; acbr++) {
170 sum += (apos[acbr]) * (*bpos);
196 int size = rows * cols;
198 const float * apos = a;
200 for (
int i = 0;
i < size;
i++) {
201 *out = (*apos) * scalar;
216 static inline void matrix_add(
const float *a,
const float *b,
217 float *out,
int rows,
int cols)
219 int size = rows * cols;
221 const float * apos = a;
222 const float * bpos = b;
225 for (
int i = 0;
i < size;
i++) {
226 *opos = (*apos) + (*bpos);
227 apos++; bpos++; opos++;
241 static inline void matrix_sub(
const float *a,
const float *b,
242 float *out,
int rows,
int cols)
244 int size = rows * cols;
246 const float * apos = a;
247 const float * bpos = b;
250 for (
int i = 0;
i < size;
i++) {
251 *opos = (*apos) - (*bpos);
252 apos++; bpos++; opos++;
268 for (
int i = 0;
i < arows;
i++) {
269 for (
int j = 0;
j < acols;
j++) {
270 out[
j * arows +
i] = a[
i * acols +
j];
275 #define TOL_EPS 0.0001f
281 const float *b,
const float *prod,
int rows,
int cols)
283 const float *
pos = prod;
287 for (
int i = 0;
i < cols;
i++) {
290 for (
int j = 0;
j < cols;
j++) {
332 int elems = rows * cols;
334 for (
int i = 0;
i < elems;
i++) {
335 float diff = (a[
i] - b[
i]);
355 int arows,
int acols)
357 float prod[acols * acols];
358 float invcheck[acols * arows];
363 matrix_mul(ainv, a, prod, acols, arows, acols);
366 matrix_mul(prod, ainv, invcheck, acols, acols, arows);
377 matrix_sub(ainv, invcheck, ainv, acols, arows);
392 const int size = arows * acols;
394 for (
int i = 0;
i < size;
i ++) {
395 float val = fabsf(a[
i]);
405 #ifndef PSEUDOINV_CONVERGE_LIMIT
406 #define PSEUDOINV_CONVERGE_LIMIT 75
424 int arows,
int acols)
447 #define matrix_mul_check(a, b, out, arows, acolsbrows, bcols) \
450 const float (*my_a)[arows*acolsbrows] = &(a); \
451 const float (*my_b)[acolsbrows*bcols] = &(b); \
452 float (*my_out)[arows*bcols] = &(out); \
453 matrix_mul(*my_a, *my_b, *my_out, arows, acolsbrows, bcols); \
456 #define matrix_mul_scalar_check(a, scalar, out, rows, cols) \
459 const float (*my_a)[rows*cols] = &(a); \
460 float (*my_out)[rows*cols] = &(out); \
461 matrix_mul_scalar(*my_a, scalar, my_out, rows, cols); \
464 #define matrix_add_check(a, b, out, rows, cols) \
467 const float (*my_a)[rows*cols] = &(a); \
468 const float (*my_b)[rows*cols] = &(b); \
469 float (*my_out)[rows*cols] = &(out); \
470 matrix_add(*my_a, *my_b, *my_out, rows, cols) \
473 #define matrix_sub_check(a, b, out, rows, cols) \
476 const float (*my_a)[rows*cols] = &(a); \
477 const float (*my_b)[rows*cols] = &(b); \
478 float (*my_out)[rows*cols] = &(out); \
479 matrix_sub(*my_a, *my_b, *my_out, rows, cols) \
482 #define matrix_transpose_check(a, b, out, rows, cols) \
485 const float (*my_a)[rows*cols] = &(a); \
486 const float (*my_b)[rows*cols] = &(b); \
487 float (*my_out)[rows*cols] = &(out); \
488 matrix_transpose(*my_a, *my_b, *my_out, rows, cols) \
535 #define cast_uint32_t static_cast<uint32_t>
537 #define cast_uint32_t (uint32_t)
543 union {
float f; uint32_t
i; } vx = { x };
544 union { uint32_t
i;
float f; } mx = { (vx.i & 0x007FFFFF) | (0x7e << 23) };
546 y *= 1.0f / (1 << 23);
548 return y - 124.22551499f
549 - 1.498030302f * mx.f
550 - 1.72587999f / (0.3520887068f + mx.f);
556 float offset = (p < 0) ? 1.0
f : 0.0
f;
559 union { uint32_t
i;
float f; } v = {
cast_uint32_t ((1 << 23) * (p + 121.2740838
f + 27.7280233
f / (4.84252568
f - z) - 1.49012907
f * z)) };
578 #define powapprox powf
579 #define expapprox expf
float cubic_deadband(float in, float w, float b, float m, float r)
float expoM(float x, int32_t g, float exponent)
float circular_modulus_rad(float err)
static void matrix_sub(const float *a, const float *b, float *out, int rows, int cols)
Subtracts two matrices Can safely be done in-place. (e.g. is slow and not vectorized/unrolled) ...
static float scale(float val, float inMin, float inMax, float outMin, float outMax)
float vector3_distances(const float *actual, const float *desired, float *out, bool normalize)
static bool matrix_pseudoinv_step(const float *a, float *ainv, int arows, int acols)
static bool matrix_pseudoinv(const float *a, float *out, int arows, int acols)
Finds a pseudo-inverse of a matrix.
static float fastpow(float x, float p)
float interpolate_value(const float fraction, const float beginVal, const float endVal)
#define PSEUDOINV_CONVERGE_LIMIT
static float fastlog2(float x)
static bool matrix_pseudoinv_convergecheck(const float *a, const float *b, const float *prod, int rows, int cols)
float circular_modulus_deg(float err)
Circular modulus.
static void matrix_add(const float *a, const float *b, float *out, int rows, int cols)
Adds two matrices Can safely be done in-place. (e.g. is slow and not vectorized/unrolled) ...
static int16_t sin_approx(int32_t x)
Fast approximation of sine; 3rd order taylor expansion. Based on http://www.coranac.com/2009/07/sines/.
void apply_channel_deadband(float *value, float deadband)
Apply deadband to Roll/Pitch/Yaw channels.
static void matrix_mul_scalar(const float *a, float scalar, float *out, int rows, int cols)
Multiplies a matrix by a scalar Can safely be done in-place. (e.g. is slow and not vectorized/unrolle...
void vector2_clip(float *vels, float limit)
uint32_t randomize_int(uint32_t interval)
float bound_min_max(float val, float min, float max)
Bound input value between min and max.
float bound_sym(float val, float range)
Bound input value within range (plus or minus)
static void matrix_mul(const float *a, const float *b, float *out, int arows, int acolsbrows, int bcols)
Multiplies out = a b.
void cubic_deadband_setup(float w, float b, float *m, float *r)
static float matrix_getmaxabs(const float *a, int arows, int acols)
Finds the largest value in a matrix.
static float fastexp(float p)
void randomize_addseed(uint32_t seed)
float linear_interpolate(float const input, float const *curve, uint8_t num_points, const float input_min, const float input_max)
float vectorn_magnitude(const float *v, int n)
static void matrix_transpose(const float *a, float *out, int arows, int acols)
Transposes a matrix.
static float fastpow2(float p)
float expo3(float x, int32_t g)
Approximation an exponential scale curve.
void vector2_rotate(const float *original, float *out, float angle)
static bool IS_NOT_FINITE(float x)