motor.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "foc/motor/motor.h"
  2. #include "foc/motor/current.h"
  3. #include "foc/core/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. static bool _motor_started = false;
  15. static float _motor_throttle = 0;
  16. static u32 mc_main_task_handler(void *param);
  17. void mc_init(void) {
  18. PMSM_FOC_CoreInit();
  19. shark_task_create(mc_main_task_handler, NULL);
  20. }
  21. bool mc_start(u8 mode) {
  22. if (_motor_started) {
  23. return true;
  24. }
  25. if (mode > TRQ_MODE) {
  26. PMSM_FOC_SetErrCode(FOC_Param_Err);
  27. return false;
  28. }
  29. if (PMSM_FOC_GetSpeed() > 10) {
  30. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  31. return false;
  32. }
  33. if (!mc_throttle_released()) {
  34. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  35. return false;
  36. }
  37. pwm_turn_on_low_side();
  38. task_udelay(500);
  39. phase_current_calibrate();
  40. PMSM_FOC_Start(mode);
  41. pwm_start();
  42. adc_start_convert();
  43. _motor_throttle = 0;
  44. _motor_started = true;
  45. return true;
  46. }
  47. bool mc_stop(void) {
  48. if (!_motor_started) {
  49. return true;
  50. }
  51. if (PMSM_FOC_GetSpeed() > 10) {
  52. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  53. return false;
  54. }
  55. if (!mc_throttle_released()) {
  56. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  57. return false;
  58. }
  59. adc_stop_convert();
  60. pwm_stop();
  61. PMSM_FOC_Stop();
  62. _motor_started = false;
  63. return true;
  64. }
  65. bool mc_lock_motor(bool lock) {
  66. if (lock && (PMSM_FOC_GetSpeed() > 10)) {
  67. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  68. return false;
  69. }
  70. PMSM_FOC_LockMotor(lock); //if mot enabled, foc core will do lock
  71. if (!_motor_started) {
  72. if (lock) {
  73. pwm_start();
  74. pwm_update_duty(0, 0, 0);
  75. }else {
  76. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  77. pwm_stop();
  78. }
  79. }
  80. return true;
  81. }
  82. bool mc_throttle_released(void) {
  83. return get_throttle_float() < THROTTLE_LOW_VALUE;
  84. }
  85. static float _get_speed_from_throttle(float throttle) {
  86. float f_throttle = (throttle);
  87. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  88. return 0;
  89. }
  90. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  91. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  92. return (THROTTLE_MIN_RPM + PMSM_FOC_GetSpeedLimit() * ration);
  93. }
  94. static float _get_idq_from_throttle(float throttle) {
  95. float f_throttle = (throttle);
  96. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  97. return 0;
  98. }
  99. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  100. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  101. return (THROTTLE_MIN_IDQ + MAX_iDQ * ration);
  102. }
  103. /*do 50 times filter*/
  104. static void brake_timer_handler(shark_timer_t *t) {
  105. int count = 50;
  106. int settimes = 0;
  107. while(count-- >= 0) {
  108. bool b1 = gpio_input_bit_get(GPIOB, GPIO_PIN_4) == SET;
  109. bool b2 = gpio_input_bit_get(GPIOB, GPIO_PIN_5) == SET;
  110. if (b1 && b2) {
  111. settimes++;
  112. }
  113. }
  114. if (settimes == 0) {
  115. PMSM_FOC_Brake(true);
  116. }else if (settimes == 50) {
  117. PMSM_FOC_Brake(false);
  118. }else {
  119. //有干扰,do nothing
  120. }
  121. }
  122. static shark_timer_t _brake_timer = TIMER_INIT(_brake_timer, brake_timer_handler);
  123. void mc_brake_handler(void){
  124. shark_timer_post(&_brake_timer, 0);
  125. }
  126. void mc_pwm_up_handler(void){
  127. phase_current_adc_triger();
  128. }
  129. measure_time_t g_meas_foc = {.exec_max_time = 15,};
  130. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  131. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  132. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  133. void mc_phase_current_handler(void) {
  134. if (phase_current_offset()) {//check if is adc offset checked
  135. return;
  136. }
  137. TIME_MEATURE_START();
  138. PMSM_FOC_Schedule();
  139. TIME_MEATURE_END();
  140. }
  141. static u32 mc_main_task_handler(void *param) {
  142. if (_motor_started) {
  143. if (get_throttle_float() != _motor_throttle) {
  144. _motor_throttle = get_throttle_float();
  145. float speed_Ref = _get_speed_from_throttle(_motor_throttle);
  146. PMSM_FOC_Set_Speed(speed_Ref);
  147. float torque_idq = _get_idq_from_throttle(_motor_throttle);
  148. PMSM_FOC_Set_Current(torque_idq);
  149. }
  150. }
  151. return 0;
  152. }