foc_core.c 4.6 KB

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