ml_math_func.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. $License:
  3. Copyright (C) 2011-2012 InvenSense Corporation, All Rights Reserved.
  4. See included License.txt for License information.
  5. $
  6. */
  7. #ifndef INVENSENSE_INV_MATH_FUNC_H__
  8. #define INVENSENSE_INV_MATH_FUNC_H__
  9. #include "mltypes.h"
  10. #define GYRO_MAG_SQR_SHIFT 6
  11. #define NUM_ROTATION_MATRIX_ELEMENTS (9)
  12. #define ROT_MATRIX_SCALE_LONG (1073741824L)
  13. #define ROT_MATRIX_SCALE_FLOAT (1073741824.0f)
  14. #define ROT_MATRIX_LONG_TO_FLOAT( longval ) \
  15. ((float) ((longval) / ROT_MATRIX_SCALE_FLOAT ))
  16. #define SIGNM(k)((int)(k)&1?-1:1)
  17. #define SIGNSET(x) ((x) ? -1 : +1)
  18. #define INV_TWO_POWER_NEG_30 9.313225746154785e-010f
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. typedef struct {
  23. float state[4];
  24. float c[5];
  25. float input;
  26. float output;
  27. } inv_biquad_filter_t;
  28. static inline float inv_q30_to_float(long q30)
  29. {
  30. return (float) q30 / ((float)(1L << 30));
  31. }
  32. static inline double inv_q30_to_double(long q30)
  33. {
  34. return (double) q30 / ((double)(1L << 30));
  35. }
  36. static inline float inv_q16_to_float(long q16)
  37. {
  38. return (float) q16 / (1L << 16);
  39. }
  40. static inline double inv_q16_to_double(long q16)
  41. {
  42. return (double) q16 / (1L << 16);
  43. }
  44. long inv_q29_mult(long a, long b);
  45. long inv_q30_mult(long a, long b);
  46. /* UMPL_ELIMINATE_64BIT Notes:
  47. * An alternate implementation using float instead of long long accudoublemulators
  48. * is provided for q29_mult and q30_mult.
  49. * When long long accumulators are used and an alternate implementation is not
  50. * available, we eliminate the entire function and header with a macro.
  51. */
  52. #ifndef UMPL_ELIMINATE_64BIT
  53. long inv_q30_div(long a, long b);
  54. long inv_q_shift_mult(long a, long b, int shift);
  55. #endif
  56. void inv_q_mult(const long *q1, const long *q2, long *qProd);
  57. void inv_q_add(long *q1, long *q2, long *qSum);
  58. void inv_q_normalize(long *q);
  59. void inv_q_invert(const long *q, long *qInverted);
  60. void inv_q_multf(const float *q1, const float *q2, float *qProd);
  61. void inv_q_addf(const float *q1, const float *q2, float *qSum);
  62. void inv_q_normalizef(float *q);
  63. void inv_q_norm4(float *q);
  64. void inv_q_invertf(const float *q, float *qInverted);
  65. void inv_quaternion_to_rotation(const long *quat, long *rot);
  66. unsigned char *inv_int32_to_big8(long x, unsigned char *big8);
  67. long inv_big8_to_int32(const unsigned char *big8);
  68. short inv_big8_to_int16(const unsigned char *big8);
  69. short inv_little8_to_int16(const unsigned char *little8);
  70. unsigned char *inv_int16_to_big8(short x, unsigned char *big8);
  71. float inv_matrix_det(float *p, int *n);
  72. void inv_matrix_det_inc(float *a, float *b, int *n, int x, int y);
  73. double inv_matrix_detd(double *p, int *n);
  74. void inv_matrix_det_incd(double *a, double *b, int *n, int x, int y);
  75. float inv_wrap_angle(float ang);
  76. float inv_angle_diff(float ang1, float ang2);
  77. void inv_quaternion_to_rotation_vector(const long *quat, long *rot);
  78. unsigned short inv_orientation_matrix_to_scalar(const signed char *mtx);
  79. void inv_convert_to_body(unsigned short orientation, const long *input, long *output);
  80. void inv_convert_to_chip(unsigned short orientation, const long *input, long *output);
  81. void inv_convert_to_body_with_scale(unsigned short orientation, long sensitivity, const long *input, long *output);
  82. void inv_q_rotate(const long *q, const long *in, long *out);
  83. void inv_vector_normalize(long *vec, int length);
  84. uint32_t inv_checksum(const unsigned char *str, int len);
  85. float inv_compass_angle(const long *compass, const long *grav,
  86. const long *quat);
  87. unsigned long inv_get_gyro_sum_of_sqr(const long *gyro);
  88. #ifdef EMPL
  89. float inv_sinf(float x);
  90. float inv_cosf(float x);
  91. /* eMPL timestamps are assumed to be in milliseconds. */
  92. static inline long inv_delta_time_ms(inv_time_t t1, inv_time_t t2)
  93. {
  94. return (long)((t1 - t2));
  95. }
  96. #else
  97. static inline long inv_delta_time_ms(inv_time_t t1, inv_time_t t2)
  98. {
  99. return (long)((t1 - t2) / 1000000L);
  100. }
  101. #endif
  102. double quaternion_to_rotation_angle(const long *quat);
  103. double inv_vector_norm(const float *x);
  104. void inv_init_biquad_filter(inv_biquad_filter_t *pFilter, float *pBiquadCoeff);
  105. float inv_biquad_filter_process(inv_biquad_filter_t *pFilter, float input);
  106. void inv_calc_state_to_match_output(inv_biquad_filter_t *pFilter, float input);
  107. void inv_get_cross_product_vec(float *cgcross, float compass[3], float grav[3]);
  108. void mlMatrixVectorMult(long matrix[9], const long vecIn[3], long *vecOut);
  109. #ifdef __cplusplus
  110. }
  111. #endif
  112. #endif // INVENSENSE_INV_MATH_FUNC_H__