hpm_bldc_define.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (c) 2021-2022 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_BLDC_DEFINE_H
  8. #define HPM_BLDC_DEFINE_H
  9. #if defined(__cplusplus)
  10. extern "C" {
  11. #endif /* __cplusplus */
  12. /**
  13. * @addtogroup middleware_mcl_interfaces HPMicro Motor Control Library
  14. * @ingroup middleware_interfaces
  15. *
  16. */
  17. #ifdef CONFIG_BLDC_HAS_EXTRA_CONFIG
  18. #include CONFIG_BLDC_HAS_EXTRA_CONFIG
  19. #endif
  20. #include "hpm_motor_math.h"
  21. /**
  22. * @addtogroup mcl_common HPMicro MCL
  23. * @ingroup middleware_mcl_interfaces
  24. * @{
  25. *
  26. */
  27. /**
  28. * @brief bldc motor rotation direction
  29. *
  30. */
  31. #define BLDC_MOTOR_DIR_FORWARD 0
  32. #define BLDC_MOTOR_DIR_REVERSE 1
  33. /**
  34. * @brief Motor pin definition U, V, W three pairs
  35. *
  36. */
  37. #define BLDC_PWM_PIN_UH 0
  38. #define BLDC_PWM_PIN_UL 1
  39. #define BLDC_PWM_PIN_VH 2
  40. #define BLDC_PWM_PIN_VL 3
  41. #define BLDC_PWM_PIN_WH 4
  42. #define BLDC_PWM_PIN_WL 5
  43. /**
  44. * @brief Motor serial number, listing four motors
  45. *
  46. */
  47. #define BLDC_MOTOR0_INDEX (1)
  48. #define BLDC_MOTOR1_INDEX (2)
  49. #define BLDC_MOTOR2_INDEX (3)
  50. #define BLDC_MOTOR3_INDEX (4)
  51. /**
  52. * @brief Serial number of the current sampling array
  53. *
  54. */
  55. #define ADCU_INDEX (0)
  56. #define ADCV_INDEX (1)
  57. #define ADCW_INDEX (2)
  58. /**
  59. * @brief PWM output channel definition, used for internal calculations
  60. *
  61. */
  62. #define BLDC_PWM_U 0
  63. #define BLDC_PWM_V 1
  64. #define BLDC_PWM_W 2
  65. /**
  66. * @brief Get ADC data with 12bit valid bits
  67. *
  68. */
  69. #define GET_ADC_12BIT_VALID_DATA(x) ((x & 0xffff) >> 4)
  70. /**
  71. * @brief All function pointers defined in the middleware point to this function,
  72. * and if the implementation of the function is not initialized to make the call,
  73. * an error will be reported through this function.
  74. *
  75. */
  76. extern void hpm_mcl_nullcallback_func(void);
  77. /**
  78. * @brief The mounting angle of the Hall sensor, 60 degrees or 120 degrees,
  79. * is an inherent property of the motor.
  80. *
  81. */
  82. typedef enum bldc_hall_phase {
  83. bldc_hall_phase_60, /*60 °*/
  84. bldc_hall_phase_120 /*120 °*/
  85. } bldc_hall_phase_t;
  86. /**
  87. * @brief Motor related structure definition.
  88. * Parameters that need to be initialized follow this rule
  89. * Naming rules:
  90. * physical property description definition.
  91. * Input (i)/output (o)_physical_volume_description_units_of_physical_volume.
  92. *
  93. */
  94. typedef struct hpm_motor_par {
  95. float i_rstator_ohm; /**< Stator resistance (in ohm) */
  96. float i_poles_n; /**< polar logarithm */
  97. float i_maxspeed_rs; /**< Maximum speed r/s */
  98. float i_lstator_h; /**< Stator inductor */
  99. float i_phasecur_a; /**< Rated current */
  100. float i_phasevol_v; /**< Rated voltage */
  101. float i_samplingper_s; /**< Current sampling period */
  102. HPM_MOTOR_MATH_TYPE o_smc_f; /**< Sliding mode control factor1 */
  103. HPM_MOTOR_MATH_TYPE o_smc_g; /**< Sliding mode control factor2 */
  104. void (*func_smc_const)(void *str); /**< Calculate the function of the sliding mode control coefficient */
  105. } hpm_motor_para_t;
  106. #define BLDC_MOTOR_PARA_DEFAULTS {0, 0, 0,\
  107. 0, 0, 0,\
  108. 0, 0, 0,\
  109. NULL}
  110. /**
  111. * @brief Speed calculation parameters
  112. *
  113. */
  114. typedef struct bldc_contrl_spd_par {
  115. uint16_t i_speedacq; /**< Update velocity data once after collecting n times of angle data */
  116. uint16_t num; /**< Internal Data */
  117. HPM_MOTOR_MATH_TYPE i_speedlooptime_s; /**< Time for n cycles. Unit s */
  118. HPM_MOTOR_MATH_TYPE speedtheta; /**< Current motor angle */
  119. HPM_MOTOR_MATH_TYPE speedlasttheta; /**< Internal Data */
  120. HPM_MOTOR_MATH_TYPE speedthetalastn; /**< Internal Data, Initialization Clear */
  121. HPM_MOTOR_MATH_TYPE i_speedfilter; /**< Low-pass filter coefficient */
  122. HPM_MOTOR_MATH_TYPE o_speedout_filter; /**< Speed after filter */
  123. HPM_MOTOR_MATH_TYPE o_speedout; /**< Speed before filter */
  124. hpm_motor_para_t *i_motorpar; /**< Motor operating parameters */
  125. void (*func_getspd)(void *str); /**< Speed calculation function */
  126. } BLDC_CONTRL_SPD_PARA;
  127. #define BLDC_CONTRL_SPD_PARA_DEFAULTS {0, 0, 0, 0, 0,\
  128. 0, 0, 0, 0, NULL,\
  129. NULL}
  130. /**
  131. * @brief pid control parameters
  132. *
  133. */
  134. typedef struct bldc_contrl_pid_par {
  135. HPM_MOTOR_MATH_TYPE i_kp; /**< Kp */
  136. HPM_MOTOR_MATH_TYPE i_ki; /**< Ki */
  137. HPM_MOTOR_MATH_TYPE i_kd; /**< Kd */
  138. HPM_MOTOR_MATH_TYPE i_max; /**< Output max, min = -max */
  139. HPM_MOTOR_MATH_TYPE target; /**< Target parameters */
  140. HPM_MOTOR_MATH_TYPE mem; /**< Intenal Data */
  141. HPM_MOTOR_MATH_TYPE cur; /**< Sampling data */
  142. HPM_MOTOR_MATH_TYPE outval; /**< Output Data */
  143. void (*func_pid)(void *str); /**< Pid function */
  144. } BLDC_CONTRL_PID_PARA;
  145. #define BLDC_CONTRL_PID_PARA_DEFAULTS {0, 0, 0, 0,\
  146. 0, 0, 0, 0,\
  147. NULL}
  148. /**
  149. * @brief Current sampling parameters
  150. *
  151. */
  152. typedef struct bldc_contrl_current_par {
  153. uint16_t adc_u; /**< u Phase current AD sampling value */
  154. uint16_t adc_v; /**< v Phase current AD sampling value */
  155. uint16_t adc_w; /**< W Phase current AD sampling value */
  156. uint16_t adc_u_middle; /**< u Phase current midpoint AD sampling value */
  157. uint16_t adc_v_middle; /**< v Phase current midpoint AD sampling value */
  158. uint16_t adc_w_middle; /**< w Phase current midpoint AD sampling value */
  159. HPM_MOTOR_MATH_TYPE cal_u; /**< Calculated U-phase current */
  160. HPM_MOTOR_MATH_TYPE cal_v; /**< Calculated V-phase current */
  161. HPM_MOTOR_MATH_TYPE cal_w; /**< Calculated W-phase current */
  162. void *userdata; /**< user data */
  163. void (*func_sampl)(void *str); /**< current samples */
  164. } BLDC_CONTROL_CURRENT_PARA;
  165. #define BLDC_CONTROL_CURRENT_PARA_DEFAULTS {0, 0, 0,\
  166. 0, 0, 0,\
  167. 0, 0, 0,\
  168. NULL, NULL}
  169. /**
  170. * @brief PWM output parameters
  171. *
  172. */
  173. typedef struct bldc_control_pwmout_par {
  174. uint8_t i_motor_id; /**< Motor id @ref BLDC_MOTOR0_INDEX ... BLDC_MOTOR3_INDEX */
  175. uint8_t i_sync_id; /**< Synchronization id */
  176. uint32_t pwm_u; /**< u pwm duty cycle */
  177. uint32_t pwm_v; /**< v pwm duty cycle */
  178. uint32_t pwm_w; /**< w pwm duty cycle */
  179. uint32_t i_pwm_reload; /**< pwm reload value, pwm configuration related */
  180. void (*func_set_pwm)(void *str); /**< pwm output function */
  181. } BLDC_CONTROL_PWMOUT_PARA;
  182. #define BLDC_CONTROL_PWMOUT_PARA_DEFAULTS {0, 0, 0,\
  183. 0, 0, 0,\
  184. NULL}
  185. /**
  186. * @brief svpwm parameters
  187. *
  188. */
  189. typedef struct bldc_control_pwm_par {
  190. HPM_MOTOR_MATH_TYPE target_alpha; /**< alpha voltage */
  191. HPM_MOTOR_MATH_TYPE target_beta; /**< beta voltage */
  192. int8_t sector; /**< Sector Number */
  193. uint32_t i_pwm_reload_max; /**< Maximum duty cycle the pwm module can output */
  194. BLDC_CONTROL_PWMOUT_PARA pwmout; /**< @ref BLDC_CONTROL_PWMOUT_PARA */
  195. void (*func_spwm)(void *str); /**< svpwm function */
  196. } BLDC_CONTROL_PWM_PARA;
  197. #define BLDC_CONTROL_PWM_PARA_DEFAULTS {0, 0, 0, 0,\
  198. BLDC_CONTROL_PWMOUT_PARA_DEFAULTS,\
  199. NULL}
  200. /**
  201. * @brief Location estimation function
  202. *
  203. */
  204. typedef struct bldc_func_cal {
  205. void *par;
  206. void (*func)(void *str);
  207. } BLDC_FUNC_CAL;
  208. #define BLDC_FUNC_CAL_DEFAULTS {NULL, NULL}
  209. /**
  210. * @brief foc control
  211. *
  212. */
  213. typedef struct bldc_contrl_foc_par {
  214. BLDC_CONTRL_PID_PARA currentdpipar; /**< D-axis current pi parameters */
  215. BLDC_CONTRL_PID_PARA currentqpipar; /**< Q-axis current pi parameters */
  216. BLDC_CONTRL_SPD_PARA speedcalpar; /**< Speed calculation parameters */
  217. HPM_MOTOR_MATH_TYPE electric_angle; /**< Electric angle */
  218. BLDC_CONTROL_CURRENT_PARA samplcurpar; /**< Sampling current */
  219. hpm_motor_para_t motorpar; /**< Motor parameters */
  220. BLDC_CONTROL_PWM_PARA pwmpar; /**< PWM parameters */
  221. BLDC_FUNC_CAL pos_estimator_par; /**< Null pointers do not run the position estimation algorithm, pointers are assigned for position estimation */
  222. HPM_MOTOR_MATH_TYPE ualpha; /**< alpha voltage */
  223. HPM_MOTOR_MATH_TYPE ubeta; /**< beta voltage */
  224. HPM_MOTOR_MATH_TYPE ialpha; /**< alpha current */
  225. HPM_MOTOR_MATH_TYPE ibeta; /**< beta current */
  226. void (*func_dqsvpwm)(void *str, void *str1, void *str2, void *data); /**< dq axis current to svpwm function */
  227. } BLDC_CONTROL_FOC_PARA;
  228. #define BLDC_CONTROL_FOC_PARA_DEFAULTS {BLDC_CONTRL_PID_PARA_DEFAULTS, BLDC_CONTRL_PID_PARA_DEFAULTS,\
  229. BLDC_CONTRL_SPD_PARA_DEFAULTS, 0,\
  230. BLDC_CONTROL_CURRENT_PARA_DEFAULTS, BLDC_MOTOR_PARA_DEFAULTS,\
  231. BLDC_CONTROL_PWM_PARA_DEFAULTS,\
  232. BLDC_FUNC_CAL_DEFAULTS,\
  233. 0, 0, 0, 0, NULL}
  234. /**
  235. * @}
  236. *
  237. */
  238. #if defined(__cplusplus)
  239. }
  240. #endif /* __cplusplus */
  241. #endif