mc_math.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. ******************************************************************************
  3. * @file mc_math.h
  4. * @author Motor Control SDK Team, ST Microelectronics
  5. * @brief This file provides mathematics functions useful for and specific to
  6. * Motor Control.
  7. *
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  12. * All rights reserved.</center></h2>
  13. *
  14. * This software component is licensed by ST under Ultimate Liberty license
  15. * SLA0044, the "License"; You may not use this file except in compliance with
  16. * the License. You may obtain a copy of the License at:
  17. * www.st.com/SLA0044
  18. *
  19. ******************************************************************************
  20. * @ingroup MC_Math
  21. */
  22. /* Define to prevent recursive inclusion -------------------------------------*/
  23. #ifndef MC_MATH_H
  24. #define MC_MATH_H
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "mc_type.h"
  27. /** @addtogroup MCSDK
  28. * @{
  29. */
  30. /** @addtogroup MC_Math
  31. * @{
  32. */
  33. #define SQRT_2 1.4142
  34. #define SQRT_3 1.732
  35. /**
  36. * @brief Macro to compute logarithm of two
  37. */
  38. #define LOG2(x) \
  39. ((x) == 65535 ? 16 : \
  40. ((x) == 2*2*2*2*2*2*2*2*2*2*2*2*2*2*2 ? 15 : \
  41. ((x) == 2*2*2*2*2*2*2*2*2*2*2*2*2*2 ? 14 : \
  42. ((x) == 2*2*2*2*2*2*2*2*2*2*2*2*2 ? 13 : \
  43. ((x) == 2*2*2*2*2*2*2*2*2*2*2*2 ? 12 : \
  44. ((x) == 2*2*2*2*2*2*2*2*2*2*2 ? 11 : \
  45. ((x) == 2*2*2*2*2*2*2*2*2*2 ? 10 : \
  46. ((x) == 2*2*2*2*2*2*2*2*2 ? 9 : \
  47. ((x) == 2*2*2*2*2*2*2*2 ? 8 : \
  48. ((x) == 2*2*2*2*2*2*2 ? 7 : \
  49. ((x) == 2*2*2*2*2*2 ? 6 : \
  50. ((x) == 2*2*2*2*2 ? 5 : \
  51. ((x) == 2*2*2*2 ? 4 : \
  52. ((x) == 2*2*2 ? 3 : \
  53. ((x) == 2*2 ? 2 : \
  54. ((x) == 2 ? 1 : \
  55. ((x) == 1 ? 0 : -1)))))))))))))))))
  56. /**
  57. * @brief Trigonometrical functions type definition
  58. */
  59. typedef struct
  60. {
  61. int16_t hCos;
  62. int16_t hSin;
  63. } Trig_Components;
  64. /**
  65. * @brief This function transforms stator currents Ia and qIb (which are
  66. * directed along axes each displaced by 120 degrees) into currents
  67. * Ialpha and Ibeta in a stationary qd reference frame.
  68. * Ialpha = Ia
  69. * Ibeta = -(2*Ib+Ia)/sqrt(3)
  70. * @param Curr_Input: stator current Ia and Ib in ab_t format
  71. * @retval Stator current Ialpha and Ibeta in alphabeta_t format
  72. */
  73. alphabeta_t MCM_Clarke( ab_t Input );
  74. /**
  75. * @brief This function transforms stator values alpha and beta, which
  76. * belong to a stationary qd reference frame, to a rotor flux
  77. * synchronous reference frame (properly oriented), so as Iq and Id.
  78. * Id= Ialpha *sin(theta)+qIbeta *cos(Theta)
  79. * Iq=qIalpha *cos(Theta)-qIbeta *sin(Theta)
  80. * @param Curr_Input: stator values alpha and beta in alphabeta_t format
  81. * @param Theta: rotating frame angular position in q1.15 format
  82. * @retval Stator current q and d in qd_t format
  83. */
  84. qd_t MCM_Park( alphabeta_t Input, int16_t Theta );
  85. /**
  86. * @brief This function transforms stator voltage qVq and qVd, that belong to
  87. * a rotor flux synchronous rotating frame, to a stationary reference
  88. * frame, so as to obtain qValpha and qVbeta:
  89. * Valfa= Vq*Cos(theta)+ Vd*Sin(theta)
  90. * Vbeta=-Vq*Sin(theta)+ Vd*Cos(theta)
  91. * @param Curr_Input: stator voltage Vq and Vd in qd_t format
  92. * @param Theta: rotating frame angular position in q1.15 format
  93. * @retval Stator values alpha and beta in alphabeta_t format
  94. */
  95. alphabeta_t MCM_Rev_Park( qd_t Input, int16_t Theta );
  96. /**
  97. * @brief This function returns cosine and sine functions of the angle fed in
  98. * input
  99. * @param hAngle: angle in q1.15 format
  100. * @retval Trig_Components Cos(angle) and Sin(angle) in Trig_Components format
  101. */
  102. Trig_Components MCM_Trig_Functions( int16_t hAngle );
  103. /**
  104. * @brief It calculates the square root of a non-negative s32. It returns 0
  105. * for negative s32.
  106. * @param Input int32_t number
  107. * @retval int32_t Square root of Input (0 if Input<0)
  108. */
  109. int32_t MCM_Sqrt( int32_t wInput );
  110. int16_t MCM_PhaseComputation( int32_t wBemf_alfa_est, int32_t wBemf_beta_est );
  111. /**
  112. * @brief This function codify a floting point number into the relative
  113. * 32bit integer.
  114. * @param float Floting point number to be coded.
  115. * @retval uint32_t Coded 32bit integer.
  116. */
  117. uint32_t MCM_floatToIntBit( float x );
  118. /**
  119. * @}
  120. */
  121. /**
  122. * @}
  123. */
  124. #endif /* MC_MATH_H*/
  125. /******************* (C) COPYRIGHT 2019 STMicroelectronics *****END OF FILE****/