foc_core.c 4.4 KB

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