motor.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. if (lock) {
  71. adc_stop_convert();
  72. pwm_stop();
  73. }else {
  74. pwm_start();
  75. adc_start_convert();
  76. }
  77. return true;
  78. }
  79. bool mc_throttle_released(void) {
  80. return get_throttle_float() < THROTTLE_LOW_VALUE;
  81. }
  82. static float _get_speed_from_throttle(float throttle) {
  83. float f_throttle = (throttle);
  84. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  85. return 0;
  86. }
  87. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  88. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  89. return (THROTTLE_MIN_RPM + PMSM_FOC_GetSpeedLimit() * ration);
  90. }
  91. static float _get_idq_from_throttle(float throttle) {
  92. float f_throttle = (throttle);
  93. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  94. return 0;
  95. }
  96. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  97. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  98. return (THROTTLE_MIN_IDQ + MAX_iDQ * ration);
  99. }
  100. /*do 50 times filter*/
  101. static void brake_timer_handler(shark_timer_t *t) {
  102. int count = 50;
  103. int settimes = 0;
  104. while(count-- >= 0) {
  105. bool b1 = gpio_input_bit_get(GPIOB, GPIO_PIN_4) == SET;
  106. bool b2 = gpio_input_bit_get(GPIOB, GPIO_PIN_5) == SET;
  107. if (b1 && b2) {
  108. settimes++;
  109. }
  110. }
  111. if (settimes == 0) {
  112. PMSM_FOC_Brake(true);
  113. }else if (settimes == 50) {
  114. PMSM_FOC_Brake(false);
  115. }else {
  116. //有干扰,do nothing
  117. }
  118. }
  119. static shark_timer_t _brake_timer = TIMER_INIT(_brake_timer, brake_timer_handler);
  120. void mc_brake_handler(void){
  121. shark_timer_post(&_brake_timer, 0);
  122. }
  123. void mc_pwm_up_handler(void){
  124. phase_current_adc_triger();
  125. }
  126. measure_time_t g_meas_foc = {.exec_max_time = 15,};
  127. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  128. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  129. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  130. void mc_phase_current_handler(void) {
  131. if (phase_current_offset()) {//check if is adc offset checked
  132. return;
  133. }
  134. TIME_MEATURE_START();
  135. PMSM_FOC_Schedule();
  136. TIME_MEATURE_END();
  137. }
  138. static u32 mc_main_task_handler(void *param) {
  139. if (_motor_started) {
  140. if (get_throttle_float() != _motor_throttle) {
  141. _motor_throttle = get_throttle_float();
  142. float speed_Ref = _get_speed_from_throttle(_motor_throttle);
  143. PMSM_FOC_Set_Speed(speed_Ref);
  144. float torque_idq = _get_idq_from_throttle(_motor_throttle);
  145. PMSM_FOC_Set_Current(torque_idq);
  146. }
  147. }
  148. return 0;
  149. }