fast_math.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef _Fast_Math_H__
  2. #define _Fast_Math_H__
  3. #include <arm_math.h>
  4. #include "libs/utils.h"
  5. // Constants
  6. #define ONE_BY_SQRT3 (0.57735026919f) // 1/sqrt(3)
  7. #define TWO_BY_SQRT3 (2.0f * 0.57735026919f)
  8. #define SQRT3_BY_2 (0.86602540378f)
  9. #define SQRT3 (1.73205080757f)
  10. #define SQRT2_BY_SQRT3 (0.8164966f)
  11. #define M_PI (3.14159265f)
  12. #define ONE_BY_SQRT3_Q14 (9459L) //0.57735026919 * 16384.0F
  13. #define SQRT3_BY_2_Q14 (14189L)//0.86602540378 * 16384.0F
  14. #define TWO_BY_SQRT3_Q14 (18918L)
  15. #ifndef SQ
  16. #define SQ(x) ((x)*(x))
  17. #endif
  18. // nan and infinity check for floats
  19. #define UTILS_IS_INF(x) ((x) == (1.0F / 0.0F) || (x) == (-1.0F / 0.0F))
  20. #define UTILS_IS_NAN(x) ((x) != (x))
  21. #define UTILS_NAN_ZERO(x) (x = UTILS_IS_NAN(x) ? 0.0F : x)
  22. void fast_sincos(float angle, float *sin, float *cos);
  23. float fast_arctan2(float y, float x);
  24. void SinCos_Lut(float angle, float *s, float *c);
  25. #define MATH_sat(in, minOut, maxOut) (min((maxOut), MAX((in), (minOut))))
  26. static __INLINE int32_t sclamp(int32_t v, int32_t minv, int32_t maxv) {
  27. if (v < minv) {
  28. return minv;
  29. }else if (v > maxv) {
  30. return maxv;
  31. }
  32. return v;
  33. }
  34. static __INLINE float fclamp(float v, float minv, float maxv) {
  35. if (v < minv) {
  36. return minv;
  37. }else if (v > maxv) {
  38. return maxv;
  39. }
  40. return v;
  41. }
  42. static void fast_norm_angle(float *angle) {
  43. *angle = fmodf(*angle, 360.0f);
  44. if (*angle < 0.0f) {
  45. *angle += 360.0f;
  46. }
  47. }
  48. static __INLINE float f_map(float x, float in_min, float in_max, float out_min, float out_max) {
  49. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  50. }
  51. static __INLINE void step_towards(float *value, float goal, float step) {
  52. if (*value < goal) {
  53. if ((*value + step) < goal) {
  54. *value += step;
  55. } else {
  56. *value = goal;
  57. }
  58. } else if (*value > goal) {
  59. if ((*value - step) > goal) {
  60. *value -= step;
  61. } else {
  62. *value = goal;
  63. }
  64. }
  65. }
  66. /**
  67. * Fast atan2
  68. *
  69. * See http://www.dspguru.com/dsp/tricks/fixed-point-atan2-with-self-normalization
  70. *
  71. * @param y
  72. * y
  73. *
  74. * @param x
  75. * x
  76. *
  77. * @return
  78. * The angle in radians
  79. */
  80. static __INLINE float fast_atan2(float y, float x) {
  81. float abs_y = fabsf(y) + 1e-20f; // kludge to prevent 0/0 condition
  82. float angle;
  83. if (x >= 0) {
  84. float r = (x - abs_y) / (x + abs_y);
  85. float rsq = r * r;
  86. angle = ((0.1963f * rsq) - 0.9817f) * r + (M_PI / 4.0f);
  87. } else {
  88. float r = (x + abs_y) / (abs_y - x);
  89. float rsq = r * r;
  90. angle = ((0.1963f * rsq) - 0.9817f) * r + (3.0f * M_PI / 4.0f);
  91. }
  92. UTILS_NAN_ZERO(angle);
  93. if (y < 0) {
  94. return(-angle);
  95. } else {
  96. return(angle);
  97. }
  98. }
  99. static void normal_sincosf(float angle, float *sin, float *cos) {
  100. *sin = arm_sin_f32(angle);
  101. *cos = arm_cos_f32(angle);
  102. }
  103. #define degree_2_pi(d) ((float)(d) * M_PI / 180.0f)
  104. #define pi_2_degree(d) ((float)(d) * 180.0f / M_PI)
  105. #define INVALID_ANGLE 0x3DFF
  106. #define SIGN(x) (((x) < 0.0f) ? -1.0f : 1.0f)
  107. /**
  108. * A simple low pass filter.
  109. *
  110. * @param value
  111. * The filtered value.
  112. *
  113. * @param sample
  114. * Next sample.
  115. *
  116. * @param filter_constant
  117. * Filter constant. Range 0.0 to 1.0, where 1.0 gives the unfiltered value.
  118. */
  119. //#define LowPass_Filter(value, sample, filter_constant) (value = value * (1.0f - filter_constant) + sample * filter_constant)
  120. #define LowPass_Filter(value, sample, filter_constant) (value = ((float)sample - (float)value) * filter_constant + value)
  121. #define do_lpf(value, sample, filter_constant) (((float)sample - (float)value) * filter_constant + value)
  122. static float limitRPM(float vel_limit, float vel_estimate, float vel_gain, float torque) {
  123. float Tmax = (vel_limit - vel_estimate) * vel_gain;
  124. float Tmin = (-vel_limit - vel_estimate) * vel_gain;
  125. if (torque < Tmin) {
  126. return Tmin;
  127. }
  128. if (Tmax > torque) {
  129. return torque;
  130. }
  131. return Tmax;
  132. }
  133. #define MAX_H 1.1F
  134. static float lp_compestion(float w, float wc) {
  135. float comp = sqrtf(1.0f + SQ(w/wc));
  136. if (comp > MAX_H) {
  137. comp = MAX_H;
  138. }else if (comp < -MAX_H) {
  139. comp = -MAX_H;
  140. }
  141. return comp;
  142. }
  143. typedef struct { /* '<Root>/in' */
  144. float time; /* '<Root>/time' */
  145. float target;
  146. float diff;
  147. float Integrator;
  148. float Integrator1;
  149. float DT;
  150. }TD_t;
  151. void TD_run(TD_t *td, float in);
  152. void TD_Init(TD_t *td, float wc, float DT);
  153. #endif /* _Fast_Math_H__ */