motor.c 14 KB

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