fast_math.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. void SinCos_Lut(float angle, float *s, float *c);
  24. #define MATH_sat(in, minOut, maxOut) (min((maxOut), MAX((in), (minOut))))
  25. static __INLINE int32_t sclamp(int32_t v, int32_t minv, int32_t maxv) {
  26. if (v < minv) {
  27. return minv;
  28. }else if (v > maxv) {
  29. return maxv;
  30. }
  31. return v;
  32. }
  33. static __INLINE float fclamp(float v, float minv, float maxv) {
  34. if (v < minv) {
  35. return minv;
  36. }else if (v > maxv) {
  37. return maxv;
  38. }
  39. return v;
  40. }
  41. static void fast_norm_angle(float *angle) {
  42. *angle = fmodf(*angle, 360.0f);
  43. if (*angle < 0.0f) {
  44. *angle += 360.0f;
  45. }
  46. }
  47. static __INLINE float f_map(float x, float in_min, float in_max, float out_min, float out_max) {
  48. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  49. }
  50. static __INLINE void step_towards(float *value, float goal, float step) {
  51. if (*value < goal) {
  52. if ((*value + step) < goal) {
  53. *value += step;
  54. } else {
  55. *value = goal;
  56. }
  57. } else if (*value > goal) {
  58. if ((*value - step) > goal) {
  59. *value -= step;
  60. } else {
  61. *value = goal;
  62. }
  63. }
  64. }
  65. /**
  66. * Fast atan2
  67. *
  68. * See http://www.dspguru.com/dsp/tricks/fixed-point-atan2-with-self-normalization
  69. *
  70. * @param y
  71. * y
  72. *
  73. * @param x
  74. * x
  75. *
  76. * @return
  77. * The angle in radians
  78. */
  79. static __INLINE float fast_atan2(float y, float x) {
  80. float abs_y = fabsf(y) + 1e-20f; // kludge to prevent 0/0 condition
  81. float angle;
  82. if (x >= 0) {
  83. float r = (x - abs_y) / (x + abs_y);
  84. float rsq = r * r;
  85. angle = ((0.1963f * rsq) - 0.9817f) * r + (M_PI / 4.0f);
  86. } else {
  87. float r = (x + abs_y) / (abs_y - x);
  88. float rsq = r * r;
  89. angle = ((0.1963f * rsq) - 0.9817f) * r + (3.0f * M_PI / 4.0f);
  90. }
  91. UTILS_NAN_ZERO(angle);
  92. if (y < 0) {
  93. return(-angle);
  94. } else {
  95. return(angle);
  96. }
  97. }
  98. static __INLINE float fast_atan_2(float y, float x) {
  99. // a := min (|x|, |y|) / max (|x|, |y|)
  100. float abs_y = ABS(y);
  101. float abs_x = ABS(x);
  102. // inject FLT_MIN in denominator to avoid division by zero
  103. float a = min(abs_x, abs_y) / (MAX(abs_x, abs_y) + 1e-20f);
  104. // s := a * a
  105. float s = a * a;
  106. // r := ((-0.0464964749 * s + 0.15931422) * s - 0.327622764) * s * a + a
  107. float r = ((-0.0464964749f * s + 0.15931422f) * s - 0.327622764f) * s * a + a;
  108. // if |y| > |x| then r := 1.57079637 - r
  109. if (abs_y > abs_x)
  110. r = 1.57079637f - r;
  111. // if x < 0 then r := 3.14159274 - r
  112. if (x < 0.0f)
  113. r = 3.14159274f - r;
  114. // if y < 0 then r := -r
  115. if (y < 0.0f)
  116. r = -r;
  117. return r;
  118. }
  119. static void normal_sincosf(float angle, float *sin, float *cos) {
  120. *sin = arm_sin_f32(angle);
  121. *cos = arm_cos_f32(angle);
  122. }
  123. #define degree_2_pi(d) ((float)(d) * M_PI / 180.0f)
  124. #define pi_2_degree(d) ((float)(d) * 180.0f / M_PI)
  125. #define INVALID_ANGLE 0x3DFF
  126. #define SIGN(x) (((x) < 0.0f) ? -1.0f : 1.0f)
  127. /**
  128. * A simple low pass filter.
  129. *
  130. * @param value
  131. * The filtered value.
  132. *
  133. * @param sample
  134. * Next sample.
  135. *
  136. * @param filter_constant
  137. * Filter constant. Range 0.0 to 1.0, where 1.0 gives the unfiltered value.
  138. */
  139. /* 前向差分离散化 */
  140. #define LowPass_Filter(value, sample, filter_constant) (value = ((float)sample - (float)value) * filter_constant + value)
  141. /* 后向差分离散化 */
  142. #define do_lpf(value, sample, filter_constant) ((sample * filter_constant + value)/(1.0f + filter_constant))
  143. static float limitRPM(float vel_limit, float vel_estimate, float vel_gain, float torque) {
  144. float Tmax = (vel_limit - vel_estimate) * vel_gain;
  145. float Tmin = (-vel_limit - vel_estimate) * vel_gain;
  146. if (torque < Tmin) {
  147. return Tmin;
  148. }
  149. if (Tmax > torque) {
  150. return torque;
  151. }
  152. return Tmax;
  153. }
  154. #define MAX_H 1.1F
  155. static float lp_compestion(float w, float wc) {
  156. float comp = sqrtf(1.0f + SQ(w/wc));
  157. if (comp > MAX_H) {
  158. comp = MAX_H;
  159. }else if (comp < -MAX_H) {
  160. comp = -MAX_H;
  161. }
  162. return comp;
  163. }
  164. typedef struct { /* '<Root>/in' */
  165. float time; /* '<Root>/time' */
  166. float target;
  167. float diff;
  168. float Integrator;
  169. float Integrator1;
  170. float DT;
  171. }TD_t;
  172. void TD_run(TD_t *td, float in);
  173. void TD_Init(TD_t *td, float wc, float DT);
  174. #endif /* _Fast_Math_H__ */