fast_math.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. void fast_sincos(float angle, float *sin, float *cos);
  18. static void fast_norm_angle(float *angle) {
  19. *angle = fmodf(*angle, 360.0f);
  20. if (*angle < 0.0f) {
  21. *angle += 360.0f;
  22. }
  23. }
  24. static void normal_sincosf(float angle, float *sin, float *cos) {
  25. *sin = arm_sin_f32(angle);
  26. *cos = arm_cos_f32(angle);
  27. }
  28. #define degree_2_pi(d) ((float)(d) * M_PI / 180.0f)
  29. #define pi_2_degree(d) ((float)(d) * 180.0f / M_PI)
  30. #define INVALID_ANGLE 0x3DFF
  31. #define SIGN(x) (((x) < 0.0f) ? -1.0f : 1.0f)
  32. /**
  33. * A simple low pass filter.
  34. *
  35. * @param value
  36. * The filtered value.
  37. *
  38. * @param sample
  39. * Next sample.
  40. *
  41. * @param filter_constant
  42. * Filter constant. Range 0.0 to 1.0, where 1.0 gives the unfiltered value.
  43. */
  44. //#define LowPass_Filter(value, sample, filter_constant) (value = value * (1.0f - filter_constant) + sample * filter_constant)
  45. #define LowPass_Filter(value, sample, filter_constant) (value = ((float)sample - (float)value) * filter_constant + value)
  46. static float limitRPM(float vel_limit, float vel_estimate, float vel_gain, float torque) {
  47. float Tmax = (vel_limit - vel_estimate) * vel_gain;
  48. float Tmin = (-vel_limit - vel_estimate) * vel_gain;
  49. if (torque < Tmin) {
  50. return Tmin;
  51. }
  52. if (Tmax > torque) {
  53. return torque;
  54. }
  55. return Tmax;
  56. }
  57. #endif /* _Fast_Math_H__ */