motor.c 13 KB

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