motor.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. void mc_hall_calibrate(s16 vd) {
  66. if (PMSM_FOC_Is_Start()) {
  67. return;
  68. }
  69. pwm_turn_on_low_side();
  70. task_udelay(500);
  71. phase_current_calibrate();
  72. PMSM_FOC_Start(OPEN_MODE);
  73. pwm_start();
  74. adc_start_convert();
  75. PMSM_FOC_Set_Vdq(vd, 0);
  76. delay_ms(1000);
  77. for (int i = 0; i < 5; i++) {
  78. for (s16 angle = 0; angle < 360; angle++) {
  79. PMSM_FOC_Set_Angle(angle);
  80. }
  81. }
  82. delay_ms(1000);
  83. adc_stop_convert();
  84. pwm_stop();
  85. PMSM_FOC_Stop();
  86. }
  87. bool mc_lock_motor(bool lock) {
  88. if (lock && (PMSM_FOC_GetSpeed() > 10)) {
  89. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  90. return false;
  91. }
  92. PMSM_FOC_LockMotor(lock); //if mot enabled, foc core will do lock
  93. if (!_motor_started) {
  94. if (lock) {
  95. pwm_start();
  96. pwm_update_duty(0, 0, 0);
  97. }else {
  98. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  99. pwm_stop();
  100. }
  101. }
  102. return true;
  103. }
  104. bool mc_throttle_released(void) {
  105. return get_throttle_float() < THROTTLE_LOW_VALUE;
  106. }
  107. static float _get_speed_from_throttle(float throttle) {
  108. float f_throttle = (throttle);
  109. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  110. return 0;
  111. }
  112. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  113. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  114. return (THROTTLE_MIN_RPM + PMSM_FOC_GetSpeedLimit() * ration);
  115. }
  116. static float _get_idq_from_throttle(float throttle) {
  117. float f_throttle = (throttle);
  118. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  119. return 0;
  120. }
  121. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  122. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  123. return (THROTTLE_MIN_IDQ + MAX_iDQ * ration);
  124. }
  125. /*do 50 times filter*/
  126. static void brake_timer_handler(shark_timer_t *t) {
  127. int count = 50;
  128. int settimes = 0;
  129. while(count-- >= 0) {
  130. bool b1 = gpio_input_bit_get(GPIOB, GPIO_PIN_4) == SET;
  131. bool b2 = gpio_input_bit_get(GPIOB, GPIO_PIN_5) == SET;
  132. if (b1 && b2) {
  133. settimes++;
  134. }
  135. }
  136. if (settimes == 0) {
  137. PMSM_FOC_Brake(true);
  138. }else if (settimes == 50) {
  139. PMSM_FOC_Brake(false);
  140. }else {
  141. //有干扰,do nothing
  142. }
  143. }
  144. static shark_timer_t _brake_timer = TIMER_INIT(_brake_timer, brake_timer_handler);
  145. void mc_brake_handler(void){
  146. shark_timer_post(&_brake_timer, 0);
  147. }
  148. void mc_pwm_up_handler(void){
  149. phase_current_adc_triger();
  150. }
  151. measure_time_t g_meas_foc = {.exec_max_time = 15,};
  152. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  153. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  154. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  155. void mc_phase_current_handler(void) {
  156. if (phase_current_offset()) {//check if is adc offset checked
  157. return;
  158. }
  159. TIME_MEATURE_START();
  160. PMSM_FOC_Schedule();
  161. TIME_MEATURE_END();
  162. }
  163. static u32 mc_main_task_handler(void *param) {
  164. if (_motor_started) {
  165. if (get_throttle_float() != _motor_throttle) {
  166. _motor_throttle = get_throttle_float();
  167. float speed_Ref = _get_speed_from_throttle(_motor_throttle);
  168. PMSM_FOC_Set_Speed(speed_Ref);
  169. float torque_idq = _get_idq_from_throttle(_motor_throttle);
  170. PMSM_FOC_Set_Current(torque_idq);
  171. }
  172. }
  173. return 0;
  174. }