fast_math.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 void fast_norm_angle(float *angle) {
  25. *angle = fmodf(*angle, 360.0f);
  26. if (*angle < 0.0f) {
  27. *angle += 360.0f;
  28. }
  29. }
  30. static void normal_sincosf(float angle, float *sin, float *cos) {
  31. *sin = arm_sin_f32(angle);
  32. *cos = arm_cos_f32(angle);
  33. }
  34. #define degree_2_pi(d) ((float)(d) * M_PI / 180.0f)
  35. #define pi_2_degree(d) ((float)(d) * 180.0f / M_PI)
  36. #define INVALID_ANGLE 0x3DFF
  37. #define SIGN(x) (((x) < 0.0f) ? -1.0f : 1.0f)
  38. /**
  39. * A simple low pass filter.
  40. *
  41. * @param value
  42. * The filtered value.
  43. *
  44. * @param sample
  45. * Next sample.
  46. *
  47. * @param filter_constant
  48. * Filter constant. Range 0.0 to 1.0, where 1.0 gives the unfiltered value.
  49. */
  50. //#define LowPass_Filter(value, sample, filter_constant) (value = value * (1.0f - filter_constant) + sample * filter_constant)
  51. #define LowPass_Filter(value, sample, filter_constant) (value = ((float)sample - (float)value) * filter_constant + value)
  52. static float limitRPM(float vel_limit, float vel_estimate, float vel_gain, float torque) {
  53. float Tmax = (vel_limit - vel_estimate) * vel_gain;
  54. float Tmin = (-vel_limit - vel_estimate) * vel_gain;
  55. if (torque < Tmin) {
  56. return Tmin;
  57. }
  58. if (Tmax > torque) {
  59. return torque;
  60. }
  61. return Tmax;
  62. }
  63. #define MAX_H 1.1F
  64. static float lp_compestion(float w, float wc) {
  65. float comp = sqrtf(1.0f + SQ(w/wc));
  66. if (comp > MAX_H) {
  67. comp = MAX_H;
  68. }else if (comp < -MAX_H) {
  69. comp = -MAX_H;
  70. }
  71. return comp;
  72. }
  73. typedef struct { /* '<Root>/in' */
  74. float time; /* '<Root>/time' */
  75. float target;
  76. float diff;
  77. float Integrator;
  78. float Integrator1;
  79. float DT;
  80. }TD_t;
  81. void TD_run(TD_t *td, float in);
  82. void TD_Init(TD_t *td, float wc, float DT);
  83. #endif /* _Fast_Math_H__ */