motor.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include "foc/motor/motor.h"
  2. #include "foc/motor/current.h"
  3. #include "foc/foc_config.h"
  4. #include "foc/samples.h"
  5. #include "math/fast_math.h"
  6. #include "bsp/timer_count32.h"
  7. #include "libs/time_measure.h"
  8. #include "bsp/delay.h"
  9. #include "bsp/bsp.h"
  10. #include "bsp/adc.h"
  11. #include "bsp/pwm.h"
  12. #include "foc/commands.h"
  13. #include "libs/logger.h"
  14. #include "bsp/sched_timer.h"
  15. #include "foc/motor/hall.h"
  16. static bool _motor_started = false;
  17. static float _motor_throttle = 0;
  18. void mc_init(void) {
  19. adc_init();
  20. pwm_3phase_init();
  21. samples_init();
  22. hall_sensor_init();
  23. foc_command_init();
  24. PMSM_FOC_CoreInit();
  25. sched_timer_enable(SPD_CTRL_MS);
  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. hall_sensor_clear();
  44. PMSM_FOC_Start(mode);
  45. pwm_turn_on_low_side();
  46. task_udelay(500);
  47. phase_current_start_cali();
  48. pwm_start();
  49. adc_start_convert();
  50. phase_current_wait_cali();
  51. _motor_throttle = 0;
  52. _motor_started = true;
  53. gpio_led2_enable(true);
  54. return true;
  55. }
  56. bool mc_stop(void) {
  57. if (!_motor_started) {
  58. return true;
  59. }
  60. if (PMSM_FOC_GetSpeed() > 10) {
  61. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  62. return false;
  63. }
  64. if (!mc_throttle_released()) {
  65. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  66. return false;
  67. }
  68. adc_stop_convert();
  69. pwm_stop();
  70. PMSM_FOC_Stop();
  71. _motor_started = false;
  72. gpio_led2_enable(false);
  73. return true;
  74. }
  75. void mc_hall_calibrate(s16 vd) {
  76. if (PMSM_FOC_Is_Start()) {
  77. return;
  78. }
  79. pwm_turn_on_low_side();
  80. task_udelay(500);
  81. PMSM_FOC_Start(OPEN_MODE);
  82. phase_current_start_cali();
  83. pwm_start();
  84. adc_start_convert();
  85. phase_current_wait_cali();
  86. PMSM_FOC_Set_Angle(0);
  87. PMSM_FOC_SetOpenVdq(vd, 0);
  88. delay_ms(3000);
  89. for (int i = 0; i < 50; i++) {
  90. for (s16 angle = 0; angle < 360; angle++) {
  91. PMSM_FOC_Set_Angle(angle);
  92. hall_detect_offset(angle);
  93. delay_ms(1);
  94. }
  95. }
  96. delay_ms(500);
  97. PMSM_FOC_SetOpenVdq(0, 0);
  98. delay_ms(500);
  99. adc_stop_convert();
  100. pwm_stop();
  101. PMSM_FOC_Stop();
  102. hall_detect_offset_finish();
  103. }
  104. bool mc_lock_motor(bool lock) {
  105. if (lock && (PMSM_FOC_GetSpeed() > 10)) {
  106. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  107. return false;
  108. }
  109. PMSM_FOC_LockMotor(lock); //if mot enabled, foc core will do lock
  110. if (!_motor_started) {
  111. if (lock) {
  112. pwm_start();
  113. pwm_update_duty(0, 0, 0);
  114. }else {
  115. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  116. pwm_stop();
  117. }
  118. }
  119. return true;
  120. }
  121. bool mc_throttle_released(void) {
  122. return get_throttle_float() < THROTTLE_LOW_VALUE;
  123. }
  124. static float _get_speed_from_throttle(float throttle) {
  125. float f_throttle = (throttle);
  126. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  127. return 0;
  128. }
  129. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  130. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  131. return (THROTTLE_MIN_RPM + PMSM_FOC_GetSpeedLimit() * ration);
  132. }
  133. static float _get_idq_from_throttle(float throttle) {
  134. float f_throttle = (throttle);
  135. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  136. return 0;
  137. }
  138. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  139. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  140. return (THROTTLE_MIN_IDQ + MAX_iDQ * ration);
  141. }
  142. /*do 50 times filter*/
  143. static void brake_timer_handler(shark_timer_t *t) {
  144. int count = 50;
  145. int settimes = 0;
  146. while(count-- >= 0) {
  147. bool b1 = gpio_input_bit_get(GPIOB, GPIO_PIN_4) == SET;
  148. bool b2 = gpio_input_bit_get(GPIOB, GPIO_PIN_5) == SET;
  149. if (b1 && b2) {
  150. settimes++;
  151. }
  152. }
  153. if (settimes == 0) {
  154. PMSM_FOC_Brake(true);
  155. }else if (settimes == 50) {
  156. PMSM_FOC_Brake(false);
  157. }else {
  158. //有干扰,do nothing
  159. }
  160. }
  161. static shark_timer_t _brake_timer = TIMER_INIT(_brake_timer, brake_timer_handler);
  162. void MC_Brake_IRQHandler(void){
  163. shark_timer_post(&_brake_timer, 0);
  164. }
  165. measure_time_t g_meas_timeup = {.intval_max_time = 50, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  166. void TIMER_UP_IRQHandler(void){
  167. //phase_current_adc_triger();
  168. time_measure_start(&g_meas_timeup);
  169. }
  170. measure_time_t g_meas_foc = {.exec_max_time = 20, .intval_max_time = 50, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  171. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  172. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  173. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  174. void ADC_IRQHandler(void) {
  175. if (phase_current_offset()) {//check if is adc offset checked
  176. return;
  177. }
  178. TIME_MEATURE_START();
  179. PMSM_FOC_Schedule();
  180. TIME_MEATURE_END();
  181. }
  182. //#define ANGLE_TEST
  183. #ifdef ANGLE_TEST
  184. static s16 _g_test_angle = 0;
  185. static void _debug_angle(void) {
  186. if (_motor_started) {
  187. PMSM_FOC_Set_Angle(_g_test_angle);
  188. if (++_g_test_angle >= 360) {
  189. _g_test_angle = 0;
  190. }
  191. }
  192. }
  193. #endif
  194. /*FOC 的部分处理,比如速度环,状态机,转把采集等*/
  195. void Sched_MC_mTask(void) {
  196. u8 runMode = PMSM_FOC_CtrlMode();
  197. #if ANGLE_TEST
  198. _debug_angle();
  199. #endif
  200. if (runMode != OPEN_MODE) {
  201. if (get_throttle_float() != _motor_throttle) {
  202. _motor_throttle = get_throttle_float();
  203. if (runMode == SPD_MODE) {
  204. float speed_Ref = _get_speed_from_throttle(_motor_throttle);
  205. PMSM_FOC_Set_Speed(speed_Ref);
  206. }else if (runMode == TRQ_MODE) {
  207. float torque_idq = _get_idq_from_throttle(_motor_throttle);
  208. PMSM_FOC_Set_Trque(torque_idq);
  209. }
  210. }
  211. PMSM_FOC_idqCalc();
  212. }
  213. }