motor.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "foc/motor/motor.h"
  2. #include "foc/motor/current.h"
  3. #include "foc/core/PMSM_FOC_Core.h"
  4. #include "foc/foc_config.h"
  5. #include "foc/samples.h"
  6. #include "math/fast_math.h"
  7. #include "bsp/timer_count32.h"
  8. #include "libs/time_measure.h"
  9. #include "os/os_task.h"
  10. #include "bsp/delay.h"
  11. #include "bsp/bsp.h"
  12. #include "bsp/adc.h"
  13. #include "bsp/pwm.h"
  14. #include "foc/commands.h"
  15. #include "libs/logger.h"
  16. static bool _motor_started = false;
  17. static float _motor_throttle = 0;
  18. static u32 mc_main_task_handler(void *param);
  19. void mc_init(void) {
  20. adc_init();
  21. pwm_3phase_init();
  22. samples_init();
  23. foc_command_init();
  24. PMSM_FOC_CoreInit();
  25. shark_task_create(mc_main_task_handler, NULL);
  26. }
  27. bool mc_start(u8 mode) {
  28. if (_motor_started) {
  29. return true;
  30. }
  31. if (mode > TRQ_MODE) {
  32. PMSM_FOC_SetErrCode(FOC_Param_Err);
  33. return false;
  34. }
  35. if (PMSM_FOC_GetSpeed() > 10) {
  36. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  37. return false;
  38. }
  39. if (!mc_throttle_released()) {
  40. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  41. return false;
  42. }
  43. pwm_turn_on_low_side();
  44. task_udelay(500);
  45. phase_current_start_cali();
  46. pwm_start();
  47. adc_start_convert();
  48. PMSM_FOC_Start(mode);
  49. _motor_throttle = 0;
  50. _motor_started = true;
  51. return true;
  52. }
  53. bool mc_stop(void) {
  54. if (!_motor_started) {
  55. return true;
  56. }
  57. if (PMSM_FOC_GetSpeed() > 10) {
  58. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  59. return false;
  60. }
  61. if (!mc_throttle_released()) {
  62. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  63. return false;
  64. }
  65. adc_stop_convert();
  66. pwm_stop();
  67. PMSM_FOC_Stop();
  68. _motor_started = false;
  69. return true;
  70. }
  71. void mc_hall_calibrate(s16 vd) {
  72. if (PMSM_FOC_Is_Start()) {
  73. return;
  74. }
  75. pwm_turn_on_low_side();
  76. task_udelay(500);
  77. PMSM_FOC_Start(OPEN_MODE);
  78. pwm_start();
  79. adc_start_convert();
  80. PMSM_FOC_SetOpenVdq(vd, 0);
  81. delay_ms(1000);
  82. for (int i = 0; i < 5; i++) {
  83. for (s16 angle = 0; angle < 360; angle++) {
  84. PMSM_FOC_Set_Angle(angle);
  85. }
  86. }
  87. delay_ms(1000);
  88. adc_stop_convert();
  89. pwm_stop();
  90. PMSM_FOC_Stop();
  91. }
  92. bool mc_lock_motor(bool lock) {
  93. if (lock && (PMSM_FOC_GetSpeed() > 10)) {
  94. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  95. return false;
  96. }
  97. PMSM_FOC_LockMotor(lock); //if mot enabled, foc core will do lock
  98. if (!_motor_started) {
  99. if (lock) {
  100. pwm_start();
  101. pwm_update_duty(0, 0, 0);
  102. }else {
  103. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  104. pwm_stop();
  105. }
  106. }
  107. return true;
  108. }
  109. bool mc_throttle_released(void) {
  110. return get_throttle_float() < THROTTLE_LOW_VALUE;
  111. }
  112. static float _get_speed_from_throttle(float throttle) {
  113. float f_throttle = (throttle);
  114. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  115. return 0;
  116. }
  117. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  118. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  119. return (THROTTLE_MIN_RPM + PMSM_FOC_GetSpeedLimit() * ration);
  120. }
  121. static float _get_idq_from_throttle(float throttle) {
  122. float f_throttle = (throttle);
  123. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  124. return 0;
  125. }
  126. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  127. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  128. return (THROTTLE_MIN_IDQ + MAX_iDQ * ration);
  129. }
  130. /*do 50 times filter*/
  131. static void brake_timer_handler(shark_timer_t *t) {
  132. int count = 50;
  133. int settimes = 0;
  134. while(count-- >= 0) {
  135. bool b1 = gpio_input_bit_get(GPIOB, GPIO_PIN_4) == SET;
  136. bool b2 = gpio_input_bit_get(GPIOB, GPIO_PIN_5) == SET;
  137. if (b1 && b2) {
  138. settimes++;
  139. }
  140. }
  141. if (settimes == 0) {
  142. PMSM_FOC_Brake(true);
  143. }else if (settimes == 50) {
  144. PMSM_FOC_Brake(false);
  145. }else {
  146. //有干扰,do nothing
  147. }
  148. }
  149. static shark_timer_t _brake_timer = TIMER_INIT(_brake_timer, brake_timer_handler);
  150. void MC_Brake_IRQHandler(void){
  151. shark_timer_post(&_brake_timer, 0);
  152. }
  153. measure_time_t g_meas_timeup = {.intval_max_time = 50, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  154. void TIMER_UP_IRQHandler(void){
  155. //phase_current_adc_triger();
  156. time_measure_start(&g_meas_timeup);
  157. }
  158. measure_time_t g_meas_foc = {.intval_max_time = 50, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  159. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  160. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  161. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  162. void ADC_IRQHandler(void) {
  163. if (phase_current_offset()) {//check if is adc offset checked
  164. return;
  165. }
  166. TIME_MEATURE_START();
  167. PMSM_FOC_Schedule();
  168. TIME_MEATURE_END();
  169. }
  170. static u32 mc_main_task_handler(void *param) {
  171. if (_motor_started) {
  172. if (get_throttle_float() != _motor_throttle) {
  173. _motor_throttle = get_throttle_float();
  174. float speed_Ref = _get_speed_from_throttle(_motor_throttle);
  175. PMSM_FOC_Set_Speed(speed_Ref);
  176. float torque_idq = _get_idq_from_throttle(_motor_throttle);
  177. PMSM_FOC_Set_Trque(torque_idq);
  178. }
  179. }
  180. return 0;
  181. }