motor.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "foc/motor/motor.h"
  2. #include "foc/motor/current.h"
  3. #include "foc/foc_config.h"
  4. #include "foc/mc_error.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 "bsp/delay.h"
  10. #include "bsp/bsp.h"
  11. #include "bsp/adc.h"
  12. #include "bsp/pwm.h"
  13. #include "foc/commands.h"
  14. #include "libs/logger.h"
  15. #include "bsp/sched_timer.h"
  16. #include "foc/core/e_ctrl.h"
  17. #include "foc/motor/motor_param.h"
  18. #include "foc/core/torque.h"
  19. static motor_t motor = {
  20. .s_direction = POSITIVE,
  21. };
  22. static void mc_gpio_init(void) {
  23. #ifdef GPIO_BRAKE_IN_GROUP
  24. rcu_periph_clock_enable(GPIO_BRAKE_IN_RCU);
  25. gpio_init(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_MODE, GPIO_OSPEED_50MHZ, GPIO_BRAKE_IN_PIN);
  26. gpio_exti_source_select(GPIO_BRAKE_EXIT_SRC_GROUP, GPIO_BRAKE_EXIT_SRC_PIN);
  27. exti_init(GPIO_BRAKE_EXTI, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
  28. nvic_irq_enable(GPIO_BRAKE_IRQ, EBREAK_IRQ_PRIORITY, 0U);
  29. exti_interrupt_flag_clear(GPIO_BRAKE_EXTI);
  30. exti_interrupt_enable(GPIO_BRAKE_EXTI);
  31. #endif
  32. }
  33. void mc_init(void) {
  34. adc_init();
  35. pwm_3phase_init();
  36. samples_init();
  37. motor_encoder_init();
  38. foc_command_init();
  39. PMSM_FOC_CoreInit();
  40. mc_gpio_init();
  41. sched_timer_enable(SPD_CTRL_MS);
  42. }
  43. motor_t * mc_params(void) {
  44. return &motor;
  45. }
  46. bool mc_start(u8 mode) {
  47. if (motor.b_start) {
  48. return true;
  49. }
  50. if (mode > CTRL_MODE_CURRENT) {
  51. PMSM_FOC_SetErrCode(FOC_Param_Err);
  52. return false;
  53. }
  54. if (PMSM_FOC_GetSpeed() > 10.0f) {
  55. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  56. err_add_record(FOC_NowAllowed_With_Speed, (s16)PMSM_FOC_GetSpeed());
  57. return false;
  58. }
  59. if (!mc_throttle_released()) {
  60. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  61. err_add_record(FOC_Throttle_Err, get_throttle_float() * 10);
  62. return false;
  63. }
  64. eCtrl_init(2000, 100);
  65. motor_encoder_start(motor.s_direction);
  66. PMSM_FOC_Start(mode);
  67. pwm_turn_on_low_side();
  68. task_udelay(500);
  69. phase_current_start_cali();
  70. pwm_start();
  71. adc_start_convert();
  72. phase_current_wait_cali();
  73. motor.throttle = 0;
  74. motor.b_start = true;
  75. gpio_led2_enable(true);
  76. return true;
  77. }
  78. bool mc_stop(void) {
  79. if (!motor.b_start) {
  80. return true;
  81. }
  82. if (PMSM_FOC_GetSpeed() > 10.0f) {
  83. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  84. err_add_record(FOC_NowAllowed_With_Speed, (s16)PMSM_FOC_GetSpeed());
  85. return false;
  86. }
  87. if (!mc_throttle_released()) {
  88. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  89. err_add_record(FOC_Throttle_Err, get_throttle_float() * 10);
  90. return false;
  91. }
  92. adc_stop_convert();
  93. pwm_stop();
  94. PMSM_FOC_Stop();
  95. motor.b_start = false;
  96. gpio_led2_enable(false);
  97. return true;
  98. }
  99. void mc_set_spd_torque(s32 target) {
  100. motor.b_ignor_throttle = true;
  101. motor.s_targetFix = target;
  102. }
  103. void mc_use_throttle(void) {
  104. motor.b_ignor_throttle = false;
  105. }
  106. void mc_encoder_calibrate(s16 vd) {
  107. if (PMSM_FOC_Is_Start()) {
  108. return;
  109. }
  110. pwm_turn_on_low_side();
  111. task_udelay(500);
  112. PMSM_FOC_Start(CTRL_MODE_OPEN);
  113. phase_current_start_cali();
  114. pwm_start();
  115. adc_start_convert();
  116. phase_current_wait_cali();
  117. PMSM_FOC_Set_Angle(0);
  118. PMSM_FOC_SetOpenVdq(vd, 0);
  119. delay_ms(3000);
  120. for (int i = 0; i < 50000000; i++) {
  121. for (s16 angle = 0; angle < 360; angle++) {
  122. PMSM_FOC_Set_Angle(angle);
  123. motor_encoder_offset(angle);
  124. delay_us(1000);
  125. }
  126. }
  127. delay_ms(500);
  128. PMSM_FOC_SetOpenVdq(0, 0);
  129. delay_ms(500);
  130. adc_stop_convert();
  131. pwm_stop();
  132. PMSM_FOC_Stop();
  133. motor_encoder_offset_finish();
  134. }
  135. bool mc_lock_motor(bool lock) {
  136. if (lock && (PMSM_FOC_GetSpeed() > 10)) {
  137. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  138. return false;
  139. }
  140. PMSM_FOC_LockMotor(lock); //if mot enabled, foc core will do lock
  141. if (!motor.b_start) {
  142. if (lock) {
  143. pwm_start();
  144. pwm_update_duty(0, 0, 0);
  145. }else {
  146. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  147. pwm_stop();
  148. }
  149. }
  150. return true;
  151. }
  152. bool mc_throttle_released(void) {
  153. return get_throttle_float() < THROTTLE_LOW_VALUE;
  154. }
  155. void MC_Brake_IRQHandler(void) {
  156. #ifdef GPIO_BRAKE_IN_GROUP
  157. int count = 50;
  158. int settimes = 0;
  159. while(count-- >= 0) {
  160. bool b1 = gpio_input_bit_get(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_PIN) == SET;
  161. if (b1) {
  162. settimes ++;
  163. }
  164. delay_us(1);
  165. }
  166. if (settimes == 0) {
  167. PMSM_FOC_Brake(false);
  168. }else if (settimes == 50) {
  169. PMSM_FOC_Brake(true);
  170. }else {
  171. //有干扰,do nothing
  172. motor.n_brake_errors++;
  173. }
  174. #endif
  175. }
  176. void MC_Protect_IRQHandler(void){
  177. PMSM_FOC_Stop(); //三相50%占空比输出,防止mos过压击穿
  178. pwm_start();
  179. adc_start_convert();
  180. PMSM_FOC_SetErrCode(FOC_Phase_Curr_OV_Err);
  181. }
  182. measure_time_t g_meas_timeup = {.intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  183. void TIMER_UP_IRQHandler(void){
  184. //phase_current_adc_triger();
  185. time_measure_start(&g_meas_timeup);
  186. }
  187. measure_time_t g_meas_foc = {.exec_max_time = 20, .intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  188. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  189. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  190. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  191. void ADC_IRQHandler(void) {
  192. if (phase_current_offset()) {//check if is adc offset checked
  193. return;
  194. }
  195. TIME_MEATURE_START();
  196. PMSM_FOC_Schedule();
  197. TIME_MEATURE_END();
  198. }
  199. //#define ANGLE_TEST
  200. #ifdef ANGLE_TEST
  201. static void _debug_angle(void) {
  202. if (motor.b_start) {
  203. PMSM_FOC_Set_Angle(motor.s_testAngle);
  204. if (++motor.s_testAngle >= 360) {
  205. motor.s_testAngle = 0;
  206. }
  207. }
  208. }
  209. #endif
  210. /*FOC 的部分处理,比如速度环,状态机,转把采集等*/
  211. void Sched_MC_mTask(void) {
  212. u8 runMode = PMSM_FOC_CtrlMode();
  213. #if ANGLE_TEST
  214. _debug_angle();
  215. #endif
  216. if (runMode != CTRL_MODE_OPEN) {
  217. eCtrl_Running();
  218. float f_throttle = get_throttle_float();
  219. if (f_throttle != motor.throttle) {
  220. motor.throttle = f_throttle;
  221. torque_speed_target(runMode, f_throttle);
  222. }
  223. PMSM_FOC_idqCalc();
  224. }
  225. }