foc_core.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "os/os_task.h"
  2. #include "bsp/pwm.h"
  3. #include "bsp/adc.h"
  4. #include "bsp/mc_hall_gpio.h"
  5. #include "foc/core/foc_core.h"
  6. #include "foc/motor/current.h"
  7. #include "bsp/timer_count32.h"
  8. #include "libs/time_measure.h"
  9. #include "libs/logger.h"
  10. pmsm_foc_t pmsm_foc = {0};
  11. extern void PMSM_FOC_Init(void);
  12. extern ExtU *PMSM_FOC_GetInputs(void);
  13. extern ExtY *PMSM_FOC_GetOutputs(void);
  14. extern void PMSM_FOC_Step(void);
  15. void PMSM_FOC_CoreInit(void) {
  16. mc_hall_init();
  17. PMSM_FOC_Init();
  18. pmsm_foc.FOC_In = PMSM_FOC_GetInputs();
  19. pmsm_foc.FOC_Out = PMSM_FOC_GetOutputs();
  20. }
  21. static __INLINE void PMSM_FOC_PWMCurrent_Update(void) {
  22. current_samp_t *cs = get_phase_sample_point(pmsm_foc.FOC_Out->sector);
  23. pwm_update_duty(pmsm_foc.FOC_Out->PWM[0], pmsm_foc.FOC_Out->PWM[1], pmsm_foc.FOC_Out->PWM[2]);
  24. pwm_update_sample(cs->time.Samp_p1, cs->time.Samp_p2, cs->sector);
  25. }
  26. static __INLINE void PMSM_FOC_Controller(void) {
  27. u8 hall[3];
  28. pwm_clear_updata();
  29. phase_current_sample(&pmsm_foc.FOC_In->adc_a, &pmsm_foc.FOC_In->adc_b);
  30. pmsm_foc.FOC_In->hw_count = hall_get_hwcount(hall);
  31. pmsm_foc.FOC_In->hall_a = hall[0];
  32. pmsm_foc.FOC_In->hall_b = hall[1];
  33. pmsm_foc.FOC_In->hall_c = hall[2];
  34. pmsm_foc.FOC_In->input_target = (s16)ramp_get_target(&pmsm_foc.speed_ramp);
  35. if (pmsm_foc.b_brake_in) {
  36. pmsm_foc.FOC_In->b_motEna = false;
  37. }else {
  38. pmsm_foc.FOC_In->b_motEna = pmsm_foc.b_FocEna;
  39. }
  40. PMSM_FOC_Step();
  41. PMSM_FOC_PWMCurrent_Update();
  42. }
  43. void PMSM_FOC_Start(u8 nCtrlMode) {
  44. if (pmsm_foc.b_FocEna) {
  45. return;
  46. }
  47. pmsm_foc.b_FocEna = true;
  48. pmsm_foc.FOC_In->b_motEna = true;
  49. pmsm_foc.FOC_In->n_ctrlModReq = nCtrlMode;
  50. pmsm_foc.FOC_In->b_cruiseEna = 0;
  51. pmsm_foc.FOC_In->b_hall_calibrate = 0;
  52. pmsm_foc.FOC_In->input_target = 0;
  53. pmsm_foc.FOC_In->vd_open_target = 0;
  54. pmsm_foc.FOC_In->vq_open_target = 0;
  55. }
  56. void PMSM_FOC_Stop(void) {
  57. if (!pmsm_foc.b_FocEna) {
  58. return;
  59. }
  60. pmsm_foc.b_FocEna = false;
  61. memset(pmsm_foc.FOC_In, 0, sizeof(ExtU));
  62. }
  63. void PMSM_FOC_iBusLimit(int16_T ibusLimit) {
  64. pmsm_foc.FOC_In->i_dc_limit = ibusLimit;
  65. }
  66. void PMSM_FOC_SpeedLimit(int16_T speedLimit) {
  67. pmsm_foc.FOC_In->speed_limit = speedLimit;
  68. }
  69. void PMSM_FOC_VbusVoltage(int16_T vbusVol) {
  70. pmsm_foc.FOC_In->vbus_voltage = vbusVol;
  71. }
  72. void PMSM_FOC_SetCtrlMode(uint8_T mode) {
  73. pmsm_foc.FOC_In->n_ctrlModReq = mode;
  74. }
  75. void PMSM_FOC_SetOpenVdq(int16_T vd, int16_T vq) {
  76. pmsm_foc.FOC_In->vd_open_target = vd;
  77. pmsm_foc.FOC_In->vq_open_target = vq;
  78. }
  79. bool PMSM_FOC_EnableCruise(boolean_T enable) {
  80. if (enable) {
  81. if (pmsm_foc.FOC_Out->rpm < 100) { //
  82. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  83. return false;
  84. }
  85. ramp_set_target(&pmsm_foc.speed_ramp, ramp_get_target(&pmsm_foc.speed_ramp), pmsm_foc.FOC_Out->rpm, 500);
  86. }
  87. pmsm_foc.FOC_In->b_cruiseEna = enable;
  88. return true;
  89. }
  90. bool PMSM_FOC_Is_CruiseEnabled(void) {
  91. return (pmsm_foc.FOC_In->b_cruiseEna && (pmsm_foc.FOC_Out->running_mode == SPD_MODE));
  92. }
  93. bool PMSM_FOC_Set_Speed(s16 rpm, u32 ramp) {
  94. if (!pmsm_foc.FOC_In->b_cruiseEna) {
  95. ramp_set_target(&pmsm_foc.speed_ramp, ramp_get_target(&pmsm_foc.speed_ramp), rpm, ramp);
  96. }
  97. return true;
  98. }
  99. bool PMSM_FOC_Set_CruiseSpeed(s16 rpm) {
  100. if (PMSM_FOC_Is_CruiseEnabled()) {
  101. ramp_set_target(&pmsm_foc.speed_ramp, ramp_get_target(&pmsm_foc.speed_ramp), rpm, 0);
  102. return true;
  103. }
  104. PMSM_FOC_SetErrCode(FOC_NotCruiseMode);
  105. return false;
  106. }
  107. void PMSM_FOC_HallCalibrate(boolean_T b_caliHall, int16_T open_vd) {
  108. if (pmsm_foc.FOC_In->b_hall_calibrate == b_caliHall) {
  109. return;
  110. }
  111. if (pmsm_foc.FOC_In->b_motEna == b_caliHall) { //motor must be stoped when start cali
  112. return;
  113. }
  114. pmsm_foc.FOC_In->b_motEna = b_caliHall;
  115. pmsm_foc.FOC_In->b_hall_calibrate = b_caliHall;
  116. pmsm_foc.FOC_In->vq_open_target = 0;
  117. pmsm_foc.FOC_In->vd_open_target = open_vd;
  118. pmsm_foc.FOC_In->n_ctrlModReq = 0;
  119. }
  120. s16 PMSM_FOC_GetSpeed(void) {
  121. return pmsm_foc.FOC_Out->rpm;
  122. }
  123. void PMSM_FOC_SetErrCode(u8 code) {
  124. pmsm_foc.error_code = code;
  125. }
  126. u8 PMSM_FOC_GetErrCode(void) {
  127. return pmsm_foc.error_code;
  128. }
  129. void foc_brake_handler(bool brake) {
  130. pmsm_foc.b_brake_in = brake;
  131. if (pmsm_foc.b_brake_in & pmsm_foc.FOC_In->b_cruiseEna) {
  132. pmsm_foc.FOC_In->b_cruiseEna = false;
  133. }
  134. }
  135. void foc_pwm_up_handler(void){
  136. phase_current_adc_triger();
  137. }
  138. measure_time_t g_meas_foc = {.exec_max_time = 15,};
  139. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  140. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  141. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  142. void mc_phase_current_irq(void) {
  143. if (phase_current_offset()) {//check if is adc offset checked
  144. return;
  145. }
  146. TIME_MEATURE_START();
  147. PMSM_FOC_Controller();
  148. TIME_MEATURE_END();
  149. }