motor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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/samples.h"
  18. #include "foc/motor/motor_param.h"
  19. #include "foc/core/torque.h"
  20. #include "app/nv_storage.h"
  21. static bool mc_is_hwbrake(void);
  22. static void _pwm_brake_timer_handler(shark_timer_t *);
  23. static shark_timer_t _brake_timer = TIMER_INIT(_brake_timer, _pwm_brake_timer_handler);
  24. static motor_t motor = {
  25. .s_direction = POSITIVE,
  26. };
  27. static void mc_gpio_init(void) {
  28. #ifdef GPIO_BRAKE_IN_GROUP
  29. rcu_periph_clock_enable(GPIO_BRAKE_IN_RCU);
  30. gpio_init(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_MODE, GPIO_OSPEED_50MHZ, GPIO_BRAKE_IN_PIN);
  31. gpio_exti_source_select(GPIO_BRAKE_EXIT_SRC_GROUP, GPIO_BRAKE_EXIT_SRC_PIN);
  32. exti_init(GPIO_BRAKE_EXTI, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
  33. nvic_irq_enable(GPIO_BRAKE_IRQ, EBREAK_IRQ_PRIORITY, 0U);
  34. exti_interrupt_flag_clear(GPIO_BRAKE_EXTI);
  35. exti_interrupt_enable(GPIO_BRAKE_EXTI);
  36. #endif
  37. }
  38. static void MC_Check_MosVbusThrottle(void) {
  39. int count = 1000;
  40. gpio_phase_u_detect(true);
  41. while(count-- >= 0) {
  42. task_udelay(20);
  43. sample_uvw_phase();
  44. sample_throttle();
  45. sample_vbus();
  46. }
  47. gpio_phase_u_detect(false);
  48. float abc[3];
  49. get_phase_vols(abc);
  50. if (abc[0] > 10 || abc[1] > 10 || abc[2] > 10) {
  51. PMSM_FOC_SetCriticalError(FOC_CRIT_H_MOS_Err);
  52. }else if (abc[0] < 0.001f){
  53. PMSM_FOC_SetCriticalError(FOC_CRIT_L_MOS_Err);
  54. }else if ((abc[0] > 0.5f) && (abc[1] < 0.001f || abc[2] < 0.001f)) {
  55. PMSM_FOC_SetCriticalError(FOC_CRIT_Phase_Conn_Err);
  56. }
  57. if (get_vbus_float() < nv_get_foc_params()->s_minDCVol) {
  58. PMSM_FOC_SetCriticalError(FOC_CRIT_UN_Vol_Err);
  59. }
  60. if (get_vbus_float() > nv_get_foc_params()->s_maxDCVol) {
  61. PMSM_FOC_SetCriticalError(FOC_CRIT_OV_Vol_Err);
  62. }
  63. }
  64. static u32 _self_check_task(void *p) {
  65. if (ENC_Check_error()) {
  66. err_add_record(FOC_CRIT_Encoder_Err, 0);
  67. PMSM_FOC_SetCriticalError(FOC_CRIT_Encoder_Err);
  68. }
  69. if (get_vbus_float() < nv_get_foc_params()->s_minDCVol) {
  70. PMSM_FOC_SetCriticalError(FOC_CRIT_UN_Vol_Err);
  71. }
  72. return 0;
  73. }
  74. void mc_init(void) {
  75. adc_init();
  76. pwm_3phase_init();
  77. samples_init();
  78. motor_encoder_init();
  79. foc_command_init();
  80. PMSM_FOC_CoreInit();
  81. mc_gpio_init();
  82. MC_Check_MosVbusThrottle();
  83. sched_timer_enable(CONFIG_SPD_CTRL_US);
  84. shark_task_create(_self_check_task, NULL);
  85. pwm_up_enable(true);
  86. }
  87. motor_t * mc_params(void) {
  88. return &motor;
  89. }
  90. bool mc_start(u8 mode) {
  91. if (motor.b_start) {
  92. return true;
  93. }
  94. MC_Check_MosVbusThrottle();
  95. if (PMSM_FOC_GetCriticalError() != 0) {
  96. PMSM_FOC_SetErrCode(FOC_Have_CritiCal_Err);
  97. return false;
  98. }
  99. if (mode > CTRL_MODE_CURRENT) {
  100. PMSM_FOC_SetErrCode(FOC_Param_Err);
  101. return false;
  102. }
  103. if (PMSM_FOC_GetSpeed() > 10.0f) {
  104. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  105. return false;
  106. }
  107. if (!mc_throttle_released()) {
  108. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  109. return false;
  110. }
  111. pwm_up_enable(false);
  112. motor.mode = mode;
  113. eCtrl_init(1000, 3000);
  114. motor_encoder_start(motor.s_direction);
  115. PMSM_FOC_Start(mode);
  116. pwm_turn_on_low_side();
  117. delay_ms(100);
  118. phase_current_offset_calibrate();
  119. pwm_start();
  120. adc_start_convert();
  121. phase_current_calibrate_wait();
  122. motor.throttle = 0;
  123. motor.b_start = true;
  124. motor.b_runStall = false;
  125. motor.runStall_time = 0;
  126. if (phase_curr_offset_check()) {
  127. PMSM_FOC_SetCriticalError(FOC_CRIT_CURR_OFF_Err);
  128. mc_stop();
  129. return false;
  130. }
  131. if (mc_is_hwbrake()) {
  132. PMSM_FOC_Brake(true);
  133. }
  134. gpio_beep(1000);
  135. return true;
  136. }
  137. bool mc_stop(void) {
  138. if (!motor.b_start) {
  139. return true;
  140. }
  141. if (PMSM_FOC_GetSpeed() > 10.0f) {
  142. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  143. sys_debug("speed error\n");
  144. return false;
  145. }
  146. if (!mc_throttle_released()) {
  147. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  148. sys_debug("throttle error\n");
  149. return false;
  150. }
  151. motor.mode = CTRL_MODE_OPEN;
  152. adc_stop_convert();
  153. pwm_stop();
  154. PMSM_FOC_Stop();
  155. pwm_up_enable(true);
  156. motor.b_start = false;
  157. motor.b_epm = false;
  158. motor.epm_dir = EPM_Dir_None;
  159. gpio_led2_enable(false);
  160. return true;
  161. }
  162. bool mc_set_foc_mode(u8 mode) {
  163. if (mode == motor.mode) {
  164. return true;
  165. }
  166. if (!motor.b_start) {
  167. return false;
  168. }
  169. u32 mask = cpu_enter_critical();
  170. bool ret = false;
  171. if (PMSM_FOC_SetCtrlMode(mode)) {
  172. motor.mode = mode;
  173. if (mode == CTRL_MODE_OPEN) {
  174. PMSM_FOC_Start(motor.mode);
  175. pwm_enable_channel();
  176. }
  177. ret = true;
  178. }
  179. cpu_exit_critical(mask);
  180. return ret;
  181. }
  182. bool mc_start_epm(bool epm) {
  183. if (motor.b_epm == epm) {
  184. return true;
  185. }
  186. if (!motor.b_start) {
  187. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  188. return false;
  189. }
  190. if (PMSM_FOC_GetSpeed() != 0.0f) {
  191. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  192. return false;
  193. }
  194. if (!mc_throttle_released()) {
  195. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  196. return false;
  197. }
  198. u32 mask = cpu_enter_critical();
  199. motor.b_epm = epm;
  200. if (epm) {
  201. motor.lim_rpm = PMSM_FOC_GetSpeedLimit();
  202. eCtrl_set_TgtSpeed(0);
  203. motor.mode = CTRL_MODE_SPD;
  204. PMSM_FOC_SpeedLimit(nv_get_foc_params()->s_maxEpmRPM);
  205. PMSM_FOC_SetCtrlMode(CTRL_MODE_SPD);
  206. }else {
  207. motor.epm_dir = EPM_Dir_None;
  208. motor.mode = CTRL_MODE_TRQ;
  209. PMSM_FOC_SetCtrlMode(CTRL_MODE_TRQ);
  210. PMSM_FOC_SpeedLimit(motor.lim_rpm);
  211. }
  212. cpu_exit_critical(mask);
  213. return false;
  214. }
  215. bool mc_is_epm(void) {
  216. return motor.b_epm;
  217. }
  218. bool mc_start_epm_move(EPM_Dir_t dir) {
  219. if (!motor.b_epm || !motor.b_start) {
  220. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  221. return false;
  222. }
  223. if (dir == motor.epm_dir) {
  224. return true;
  225. }
  226. motor.epm_dir = dir;
  227. if (dir != EPM_Dir_None) {
  228. u32 mask = cpu_enter_critical();
  229. if (!PMSM_FOC_Is_Start()) {
  230. PMSM_FOC_Start(motor.mode);
  231. pwm_enable_channel();
  232. }
  233. cpu_exit_critical(mask);
  234. float rpm = nv_get_foc_params()->s_maxEpmRPM;
  235. if (dir == EPM_Dir_Back) {
  236. rpm = -rpm;
  237. }
  238. PMSM_FOC_Set_Speed(rpm);
  239. }else {
  240. PMSM_FOC_Set_Speed(0);
  241. }
  242. return true;
  243. }
  244. void mc_set_spd_torque(s32 target) {
  245. motor.b_ignor_throttle = true;
  246. motor.s_targetFix = target;
  247. }
  248. void mc_use_throttle(void) {
  249. motor.b_ignor_throttle = false;
  250. }
  251. void mc_encoder_off_calibrate(s16 vd) {
  252. if (PMSM_FOC_Is_Start()) {
  253. return;
  254. }
  255. motor.b_calibrate = true;
  256. pwm_turn_on_low_side();
  257. task_udelay(500);
  258. PMSM_FOC_Start(CTRL_MODE_OPEN);
  259. phase_current_offset_calibrate();
  260. pwm_start();
  261. adc_start_convert();
  262. phase_current_calibrate_wait();
  263. PMSM_FOC_Set_Angle(0);
  264. PMSM_FOC_SetOpenVdq(vd, 0);
  265. delay_ms(2000);
  266. motor_encoder_set_direction(POSITIVE);
  267. for (int i = 0; i < 100000; i++) {
  268. for (float angle = 0; angle < 360; angle++) {
  269. PMSM_FOC_Set_Angle(angle);
  270. delay_ms(2);
  271. if (i > 10) {
  272. motor_encoder_offset(angle);
  273. }
  274. }
  275. wdog_reload();
  276. if (motor_encoder_offset_is_finish()) {
  277. break;
  278. }
  279. }
  280. motor_encoder_set_direction(NEGATIVE);
  281. delay_ms(100);
  282. for (int i = 0; i < 100000; i++) {
  283. for (float angle = 360; angle > 0; angle--) {
  284. PMSM_FOC_Set_Angle(angle);
  285. delay_ms(2);
  286. if (i > 10) {
  287. motor_encoder_offset(angle);
  288. }
  289. }
  290. wdog_reload();
  291. if (motor_encoder_offset_is_finish()) {
  292. break;
  293. }
  294. }
  295. delay_ms(500);
  296. PMSM_FOC_SetOpenVdq(0, 0);
  297. delay_ms(500);
  298. wdog_reload();
  299. adc_stop_convert();
  300. pwm_stop();
  301. PMSM_FOC_Stop();
  302. motor.b_calibrate = false;
  303. }
  304. bool mc_encoder_zero_calibrate(s16 vd) {
  305. if (PMSM_FOC_Is_Start()) {
  306. return false;
  307. }
  308. motor.b_calibrate = true;
  309. pwm_turn_on_low_side();
  310. task_udelay(500);
  311. PMSM_FOC_Start(CTRL_MODE_OPEN);
  312. phase_current_offset_calibrate();
  313. pwm_start();
  314. adc_start_convert();
  315. phase_current_calibrate_wait();
  316. PMSM_FOC_Set_Angle(0);
  317. PMSM_FOC_SetOpenVdq(vd, 0);
  318. delay_ms(2000);
  319. float phase = motor_encoder_zero_phase_detect();
  320. delay_ms(500);
  321. PMSM_FOC_SetOpenVdq(0, 0);
  322. delay_ms(500);
  323. adc_stop_convert();
  324. pwm_stop();
  325. PMSM_FOC_Stop();
  326. motor.b_calibrate = false;
  327. if (phase != INVALID_ANGLE) {
  328. nv_save_angle_offset(phase);
  329. return true;
  330. }
  331. return false;
  332. }
  333. bool mc_current_sensor_calibrate(float current) {
  334. if (!mc_start(CTRL_MODE_OPEN)) {
  335. return false;
  336. }
  337. phase_current_sensor_start_calibrate(current);
  338. phase_current_calibrate_wait();
  339. return true;
  340. }
  341. bool mc_lock_motor(bool lock) {
  342. if (lock && (PMSM_FOC_GetSpeed() > 10)) {
  343. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  344. return false;
  345. }
  346. PMSM_FOC_LockMotor(lock); //if mot enabled, foc core will do lock
  347. if (!motor.b_start) {
  348. if (lock) {
  349. pwm_start();
  350. pwm_update_duty(0, 0, 0);
  351. }else {
  352. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  353. pwm_stop();
  354. }
  355. }
  356. return true;
  357. }
  358. bool mc_throttle_released(void) {
  359. return get_throttle_float() < CONFIG_THROTTLE_LOW_VALUE;
  360. }
  361. static bool mc_is_hwbrake(void) {
  362. #ifdef GPIO_BRAKE_IN_GROUP
  363. int count = 50;
  364. int settimes = 0;
  365. while(count-- > 0) {
  366. bool b1 = gpio_input_bit_get(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_PIN) == SET;
  367. if (b1) {
  368. settimes ++;
  369. }
  370. delay_us(1);
  371. }
  372. if (settimes == 0) {
  373. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  374. return true;
  375. #else
  376. return false;
  377. #endif
  378. }else if (settimes == 50) {
  379. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  380. return false;
  381. #else
  382. return true;
  383. #endif
  384. }else {
  385. //有干扰,do nothing
  386. motor.n_brake_errors++;
  387. return false;
  388. }
  389. #else
  390. return false;
  391. #endif
  392. }
  393. void MC_Brake_IRQHandler(void) {
  394. if (!motor.b_start) {
  395. return;
  396. }
  397. if (mc_is_hwbrake()) {
  398. PMSM_FOC_Brake(true);
  399. }else {
  400. PMSM_FOC_Brake(false);
  401. }
  402. }
  403. static void _pwm_brake_timer_handler(shark_timer_t *t){
  404. pwm_brake_enable(true);
  405. }
  406. void MC_Protect_IRQHandler(void){
  407. pwm_brake_enable(false);
  408. shark_timer_post(&_brake_timer, 1000);
  409. if (!motor.b_start) {
  410. return;
  411. }
  412. PMSM_FOC_SetCriticalError(FOC_CRIT_Phase_Err);
  413. }
  414. measure_time_t g_meas_timeup = {.intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  415. void TIMER_UP_IRQHandler(void){
  416. motor_encoder_update();
  417. }
  418. measure_time_t g_meas_foc = {.exec_max_time = 20, .intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  419. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  420. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  421. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  422. void ADC_IRQHandler(void) {
  423. if (phase_current_offset()) {//check if is adc offset checked
  424. return;
  425. }
  426. if (phase_current_sensor_do_calibrate()){
  427. pwm_update_duty(100, FOC_PWM_Half_Period-100, 100);
  428. pwm_update_sample(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period+1, PHASE_BC);
  429. return;
  430. }
  431. TIME_MEATURE_START();
  432. PMSM_FOC_Schedule();
  433. TIME_MEATURE_END();
  434. }
  435. static bool mc_can_stop_foc(void) {
  436. if (mc_throttle_released() && PMSM_FOC_GetSpeed() == 0.0f) {
  437. if (!PMSM_FOC_MotorLocking() && motor.epm_dir == EPM_Dir_None) {
  438. return true;
  439. }
  440. }
  441. return false;
  442. }
  443. //#define ANGLE_TEST
  444. #ifdef ANGLE_TEST
  445. static void _debug_angle(void) {
  446. if (motor.b_start) {
  447. PMSM_FOC_Set_Angle(motor.s_testAngle);
  448. if (++motor.s_testAngle >= 360) {
  449. motor.s_testAngle = 0;
  450. }
  451. }
  452. }
  453. #endif
  454. /*FOC 的部分处理,比如速度环,状态机,转把采集等*/
  455. measure_time_t g_meas_MCTask;
  456. void Sched_MC_mTask(void) {
  457. time_measure_start(&g_meas_MCTask);
  458. u8 runMode = PMSM_FOC_CtrlMode();
  459. #if ANGLE_TEST
  460. _debug_angle();
  461. #endif
  462. PMSM_FOC_Calc_iDC();
  463. if (motor.b_calibrate || (motor.mode == CTRL_MODE_OPEN)) {
  464. return;
  465. }
  466. if ((runMode == CTRL_MODE_TRQ || runMode == CTRL_MODE_SPD) && !PMSM_FOC_MotorLocking()) {
  467. //堵转判断
  468. if (motor.b_runStall) {
  469. if (!mc_throttle_released()) {
  470. return;
  471. }
  472. motor.b_runStall = false; //转把释放,清除堵转标志
  473. }else if ((ABS(PMSM_FOC_GetSpeed()) < 1.0f) && (PMSM_FOC_Get()->out.s_RealIdq.q >= CONFIG_STALL_MAX_CURRENT)){
  474. if (motor.runStall_time == 0) {
  475. motor.runStall_time = get_tick_ms();
  476. }else {
  477. if (get_delta_ms(motor.runStall_time) >= CONFIG_STALL_MAX_TIME) {
  478. motor.b_runStall = true;
  479. motor.runStall_time = 0;
  480. torque_speed_target(runMode, 0.0f);
  481. return;
  482. }
  483. }
  484. }else {
  485. motor.runStall_time = 0;
  486. }
  487. }
  488. if ((runMode != CTRL_MODE_OPEN) || (motor.mode != CTRL_MODE_OPEN)) {
  489. if (motor.mode != CTRL_MODE_OPEN) {
  490. u32 mask;
  491. if (mc_can_stop_foc()) {
  492. if (PMSM_FOC_Is_Start()) {
  493. mask = cpu_enter_critical();
  494. PMSM_FOC_Stop();
  495. pwm_disable_channel();
  496. cpu_exit_critical(mask);
  497. }
  498. }else {
  499. if (!PMSM_FOC_Is_Start()) {
  500. mask = cpu_enter_critical();
  501. PMSM_FOC_Start(motor.mode);
  502. pwm_enable_channel();
  503. cpu_exit_critical(mask);
  504. }
  505. }
  506. }
  507. if (runMode != CTRL_MODE_OPEN) {
  508. eCtrl_Running();
  509. float f_throttle = get_throttle_float();
  510. if (f_throttle != motor.throttle) {
  511. motor.throttle = f_throttle;
  512. torque_speed_target(runMode, f_throttle);
  513. }
  514. PMSM_FOC_Slow_Task();
  515. }
  516. }
  517. }