fast_math.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef _Fast_Math_H__
  2. #define _Fast_Math_H__
  3. #include <arm_math.h>
  4. // Constants
  5. #define ONE_BY_SQRT3 (0.57735026919f) // 1/sqrt(3)
  6. #define TWO_BY_SQRT3 (2.0f * 0.57735026919f)
  7. #define SQRT3_BY_2 (0.86602540378f)
  8. #define SQRT3 (1.73205080757f)
  9. #define SQRT2_BY_SQRT3 (0.8164966f)
  10. #define M_PI (3.14159265f)
  11. #define ONE_BY_SQRT3_Q14 (9459L) //0.57735026919 * 16384.0F
  12. #define SQRT3_BY_2_Q14 (14189L)//0.86602540378 * 16384.0F
  13. #define TWO_BY_SQRT3_Q14 (18918L)
  14. #ifndef SQ
  15. #define SQ(x) ((x)*(x))
  16. #endif
  17. // nan and infinity check for floats
  18. #define UTILS_IS_INF(x) ((x) == (1.0F / 0.0F) || (x) == (-1.0F / 0.0F))
  19. #define UTILS_IS_NAN(x) ((x) != (x))
  20. #define UTILS_NAN_ZERO(x) (x = UTILS_IS_NAN(x) ? 0.0F : x)
  21. void fast_sincos(float angle, float *sin, float *cos);
  22. float fast_arctan2(float y, float x);
  23. void SinCos_Lut(float angle, float *s, float *c);
  24. static __INLINE int32_t sclamp(int32_t v, int32_t minv, int32_t maxv) {
  25. if (v < minv) {
  26. return minv;
  27. }else if (v > maxv) {
  28. return maxv;
  29. }
  30. return v;
  31. }
  32. static void fast_norm_angle(float *angle) {
  33. *angle = fmodf(*angle, 360.0f);
  34. if (*angle < 0.0f) {
  35. *angle += 360.0f;
  36. }
  37. }
  38. static __INLINE float f_map(float x, float in_min, float in_max, float out_min, float out_max) {
  39. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  40. }
  41. static __INLINE void step_towards(float *value, float goal, float step) {
  42. if (*value < goal) {
  43. if ((*value + step) < goal) {
  44. *value += step;
  45. } else {
  46. *value = goal;
  47. }
  48. } else if (*value > goal) {
  49. if ((*value - step) > goal) {
  50. *value -= step;
  51. } else {
  52. *value = goal;
  53. }
  54. }
  55. }
  56. static void normal_sincosf(float angle, float *sin, float *cos) {
  57. *sin = arm_sin_f32(angle);
  58. *cos = arm_cos_f32(angle);
  59. }
  60. #define degree_2_pi(d) ((float)(d) * M_PI / 180.0f)
  61. #define pi_2_degree(d) ((float)(d) * 180.0f / M_PI)
  62. #define INVALID_ANGLE 0x3DFF
  63. #define SIGN(x) (((x) < 0.0f) ? -1.0f : 1.0f)
  64. /**
  65. * A simple low pass filter.
  66. *
  67. * @param value
  68. * The filtered value.
  69. *
  70. * @param sample
  71. * Next sample.
  72. *
  73. * @param filter_constant
  74. * Filter constant. Range 0.0 to 1.0, where 1.0 gives the unfiltered value.
  75. */
  76. //#define LowPass_Filter(value, sample, filter_constant) (value = value * (1.0f - filter_constant) + sample * filter_constant)
  77. #define LowPass_Filter(value, sample, filter_constant) (value = ((float)sample - (float)value) * filter_constant + value)
  78. static float limitRPM(float vel_limit, float vel_estimate, float vel_gain, float torque) {
  79. float Tmax = (vel_limit - vel_estimate) * vel_gain;
  80. float Tmin = (-vel_limit - vel_estimate) * vel_gain;
  81. if (torque < Tmin) {
  82. return Tmin;
  83. }
  84. if (Tmax > torque) {
  85. return torque;
  86. }
  87. return Tmax;
  88. }
  89. #define MAX_H 1.1F
  90. static float lp_compestion(float w, float wc) {
  91. float comp = sqrtf(1.0f + SQ(w/wc));
  92. if (comp > MAX_H) {
  93. comp = MAX_H;
  94. }else if (comp < -MAX_H) {
  95. comp = -MAX_H;
  96. }
  97. return comp;
  98. }
  99. typedef struct { /* '<Root>/in' */
  100. float time; /* '<Root>/time' */
  101. float target;
  102. float diff;
  103. float Integrator;
  104. float Integrator1;
  105. float DT;
  106. }TD_t;
  107. void TD_run(TD_t *td, float in);
  108. void TD_Init(TD_t *td, float wc, float DT);
  109. #endif /* _Fast_Math_H__ */