motor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. motor.b_epm = false;
  127. motor.b_epm_cmd_move = false;
  128. motor.epm_dir = EPM_Dir_None;
  129. if (phase_curr_offset_check()) {
  130. PMSM_FOC_SetCriticalError(FOC_CRIT_CURR_OFF_Err);
  131. mc_stop();
  132. return false;
  133. }
  134. if (mc_is_hwbrake()) {
  135. PMSM_FOC_Brake(true);
  136. }
  137. gpio_beep(200);
  138. return true;
  139. }
  140. bool mc_stop(void) {
  141. if (!motor.b_start) {
  142. return true;
  143. }
  144. if (PMSM_FOC_GetSpeed() > 10.0f) {
  145. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  146. sys_debug("speed error\n");
  147. return false;
  148. }
  149. if (!mc_throttle_released()) {
  150. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  151. sys_debug("throttle error\n");
  152. return false;
  153. }
  154. motor.mode = CTRL_MODE_OPEN;
  155. adc_stop_convert();
  156. pwm_stop();
  157. PMSM_FOC_Stop();
  158. pwm_up_enable(true);
  159. motor.b_start = false;
  160. motor.b_epm = false;
  161. motor.epm_dir = EPM_Dir_None;
  162. gpio_led2_enable(false);
  163. return true;
  164. }
  165. bool mc_set_foc_mode(u8 mode) {
  166. if (mode == motor.mode) {
  167. return true;
  168. }
  169. if (!motor.b_start) {
  170. return false;
  171. }
  172. u32 mask = cpu_enter_critical();
  173. bool ret = false;
  174. if (PMSM_FOC_SetCtrlMode(mode)) {
  175. motor.mode = mode;
  176. if (mode == CTRL_MODE_OPEN) {
  177. PMSM_FOC_Start(motor.mode);
  178. pwm_enable_channel();
  179. }
  180. ret = true;
  181. }
  182. cpu_exit_critical(mask);
  183. return ret;
  184. }
  185. bool mc_start_epm(bool epm) {
  186. sys_debug("%s epm mode\n", epm?"enter":"exit");
  187. if (motor.b_epm == epm) {
  188. return true;
  189. }
  190. if (!motor.b_start) {
  191. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  192. return false;
  193. }
  194. if (PMSM_FOC_GetSpeed() != 0.0f) {
  195. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  196. return false;
  197. }
  198. if (!mc_throttle_released()) {
  199. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  200. return false;
  201. }
  202. u32 mask = cpu_enter_critical();
  203. motor.b_epm = epm;
  204. if (epm) {
  205. motor.lim_rpm = PMSM_FOC_GetSpeedLimit();
  206. motor.lim_phase_curr = PMSM_FOC_GetPhaseCurrLim();
  207. eCtrl_set_TgtSpeed(0);
  208. motor.mode = CTRL_MODE_SPD;
  209. PMSM_FOC_SpeedLimit(nv_get_foc_params()->s_maxEpmRPM);
  210. PMSM_FOC_PhaseCurrLim(nv_get_foc_params()->s_maxEpmPhaseCurrLim);
  211. PMSM_FOC_SetCtrlMode(CTRL_MODE_SPD);
  212. }else {
  213. motor.epm_dir = EPM_Dir_None;
  214. motor.mode = CTRL_MODE_TRQ;
  215. motor.b_epm_cmd_move = false;
  216. PMSM_FOC_SetCtrlMode(CTRL_MODE_TRQ);
  217. PMSM_FOC_SpeedLimit(motor.lim_rpm);
  218. PMSM_FOC_PhaseCurrLim(motor.lim_phase_curr);
  219. }
  220. cpu_exit_critical(mask);
  221. return false;
  222. }
  223. bool mc_is_epm(void) {
  224. return motor.b_epm;
  225. }
  226. bool mc_start_epm_move(EPM_Dir_t dir, bool is_command) {
  227. sys_debug("epm dir %d, %d\n", dir, motor.epm_dir);
  228. if (!motor.b_epm || !motor.b_start) {
  229. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  230. return false;
  231. }
  232. if ((dir == motor.epm_dir) && (is_command == motor.b_epm_cmd_move)) {
  233. return true;
  234. }
  235. motor.epm_dir = dir;
  236. if (dir != EPM_Dir_None) {
  237. motor.b_epm_cmd_move = is_command;
  238. u32 mask = cpu_enter_critical();
  239. if (!PMSM_FOC_Is_Start()) {
  240. PMSM_FOC_Start(motor.mode);
  241. pwm_enable_channel();
  242. }
  243. cpu_exit_critical(mask);
  244. float rpm = nv_get_foc_params()->s_maxEpmRPM;
  245. if (dir == EPM_Dir_Back) {
  246. rpm = -rpm;
  247. }
  248. PMSM_FOC_Set_Speed(rpm);
  249. }else {
  250. motor.b_epm_cmd_move = false;
  251. PMSM_FOC_Set_Speed(0);
  252. }
  253. return true;
  254. }
  255. bool mc_command_epm_move(EPM_Dir_t dir) {
  256. return mc_start_epm_move(dir, true);
  257. }
  258. bool mc_throttle_epm_move(EPM_Dir_t dir) {
  259. return mc_start_epm_move(dir, false);
  260. }
  261. void mc_set_spd_torque(s32 target) {
  262. motor.b_ignor_throttle = true;
  263. motor.s_targetFix = target;
  264. }
  265. void mc_use_throttle(void) {
  266. motor.b_ignor_throttle = false;
  267. }
  268. void mc_get_running_status(u8 *data) {
  269. data[0] = motor.mode;
  270. data[0] |= PMSM_FOC_Get()->out.n_RunMode << 2;
  271. data[0] |= (PMSM_FOC_Get()->in.b_cruiseEna?1:0) << 4;
  272. data[0] |= (PMSM_FOC_Is_CruiseEnabled()?1:0) << 5;
  273. data[0] |= (mc_is_epm()?1:0) << 6;
  274. data[0] |= (0) << 7; //motor locked
  275. }
  276. void mc_encoder_off_calibrate(s16 vd) {
  277. if (PMSM_FOC_Is_Start()) {
  278. return;
  279. }
  280. motor.b_calibrate = true;
  281. pwm_turn_on_low_side();
  282. task_udelay(500);
  283. PMSM_FOC_Start(CTRL_MODE_OPEN);
  284. phase_current_offset_calibrate();
  285. pwm_start();
  286. adc_start_convert();
  287. phase_current_calibrate_wait();
  288. PMSM_FOC_Set_Angle(0);
  289. PMSM_FOC_SetOpenVdq(vd, 0);
  290. delay_ms(2000);
  291. motor_encoder_set_direction(POSITIVE);
  292. for (int i = 0; i < 100000; i++) {
  293. for (float angle = 0; angle < 360; angle++) {
  294. PMSM_FOC_Set_Angle(angle);
  295. delay_ms(2);
  296. if (i > 10) {
  297. motor_encoder_offset(angle);
  298. }
  299. }
  300. wdog_reload();
  301. if (motor_encoder_offset_is_finish()) {
  302. break;
  303. }
  304. }
  305. motor_encoder_set_direction(NEGATIVE);
  306. delay_ms(100);
  307. for (int i = 0; i < 100000; i++) {
  308. for (float angle = 360; angle > 0; angle--) {
  309. PMSM_FOC_Set_Angle(angle);
  310. delay_ms(2);
  311. if (i > 10) {
  312. motor_encoder_offset(angle);
  313. }
  314. }
  315. wdog_reload();
  316. if (motor_encoder_offset_is_finish()) {
  317. break;
  318. }
  319. }
  320. delay_ms(500);
  321. PMSM_FOC_SetOpenVdq(0, 0);
  322. delay_ms(500);
  323. wdog_reload();
  324. adc_stop_convert();
  325. pwm_stop();
  326. PMSM_FOC_Stop();
  327. motor.b_calibrate = false;
  328. }
  329. bool mc_encoder_zero_calibrate(s16 vd) {
  330. if (PMSM_FOC_Is_Start()) {
  331. return false;
  332. }
  333. motor.b_calibrate = true;
  334. pwm_turn_on_low_side();
  335. task_udelay(500);
  336. PMSM_FOC_Start(CTRL_MODE_OPEN);
  337. phase_current_offset_calibrate();
  338. pwm_start();
  339. adc_start_convert();
  340. phase_current_calibrate_wait();
  341. PMSM_FOC_Set_Angle(0);
  342. PMSM_FOC_SetOpenVdq(vd, 0);
  343. delay_ms(2000);
  344. float phase = motor_encoder_zero_phase_detect();
  345. delay_ms(500);
  346. PMSM_FOC_SetOpenVdq(0, 0);
  347. delay_ms(500);
  348. adc_stop_convert();
  349. pwm_stop();
  350. PMSM_FOC_Stop();
  351. motor.b_calibrate = false;
  352. if (phase != INVALID_ANGLE) {
  353. nv_save_angle_offset(phase);
  354. return true;
  355. }
  356. return false;
  357. }
  358. bool mc_current_sensor_calibrate(float current) {
  359. if (!mc_start(CTRL_MODE_OPEN)) {
  360. return false;
  361. }
  362. phase_current_sensor_start_calibrate(current);
  363. phase_current_calibrate_wait();
  364. return true;
  365. }
  366. bool mc_lock_motor(bool lock) {
  367. if (lock && (PMSM_FOC_GetSpeed() > 10)) {
  368. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  369. return false;
  370. }
  371. PMSM_FOC_LockMotor(lock); //if mot enabled, foc core will do lock
  372. if (!motor.b_start) {
  373. if (lock) {
  374. pwm_start();
  375. pwm_update_duty(0, 0, 0);
  376. }else {
  377. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  378. pwm_stop();
  379. }
  380. }
  381. return true;
  382. }
  383. bool mc_throttle_released(void) {
  384. return get_throttle_float() < CONFIG_THROTTLE_LOW_VALUE;
  385. }
  386. static bool mc_is_hwbrake(void) {
  387. #ifdef GPIO_BRAKE_IN_GROUP
  388. int count = 50;
  389. int settimes = 0;
  390. while(count-- > 0) {
  391. bool b1 = gpio_input_bit_get(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_PIN) == SET;
  392. if (b1) {
  393. settimes ++;
  394. }
  395. delay_us(1);
  396. }
  397. if (settimes == 0) {
  398. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  399. return true;
  400. #else
  401. return false;
  402. #endif
  403. }else if (settimes == 50) {
  404. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  405. return false;
  406. #else
  407. return true;
  408. #endif
  409. }else {
  410. //有干扰,do nothing
  411. motor.n_brake_errors++;
  412. return false;
  413. }
  414. #else
  415. return false;
  416. #endif
  417. }
  418. void MC_Brake_IRQHandler(void) {
  419. if (!motor.b_start) {
  420. return;
  421. }
  422. if (mc_is_hwbrake()) {
  423. PMSM_FOC_Brake(true);
  424. }else {
  425. PMSM_FOC_Brake(false);
  426. }
  427. }
  428. static void _pwm_brake_timer_handler(shark_timer_t *t){
  429. pwm_brake_enable(true);
  430. }
  431. void MC_Protect_IRQHandler(void){
  432. pwm_brake_enable(false);
  433. shark_timer_post(&_brake_timer, 1000);
  434. if (!motor.b_start) {
  435. return;
  436. }
  437. PMSM_FOC_SetCriticalError(FOC_CRIT_Phase_Err);
  438. }
  439. measure_time_t g_meas_timeup = {.intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  440. void TIMER_UP_IRQHandler(void){
  441. motor_encoder_update();
  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. static bool mc_can_stop_foc(void) {
  461. if (mc_throttle_released() && PMSM_FOC_GetSpeed() == 0.0f) {
  462. if (!PMSM_FOC_MotorLocking() && motor.epm_dir == EPM_Dir_None) {
  463. return true;
  464. }
  465. }
  466. return false;
  467. }
  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_Calc_iDC();
  500. if (motor.b_calibrate || (motor.mode == CTRL_MODE_OPEN)) {
  501. return;
  502. }
  503. /* 堵转处理 */
  504. if (mc_run_stall_process(runMode)) {
  505. return;
  506. }
  507. if ((runMode != CTRL_MODE_OPEN) || (motor.mode != CTRL_MODE_OPEN)) {
  508. if (motor.mode != CTRL_MODE_OPEN) {
  509. u32 mask;
  510. if (mc_can_stop_foc()) {
  511. if (PMSM_FOC_Is_Start()) {
  512. mask = cpu_enter_critical();
  513. PMSM_FOC_Stop();
  514. pwm_disable_channel();
  515. cpu_exit_critical(mask);
  516. }
  517. }else {
  518. if (!PMSM_FOC_Is_Start()) {
  519. mask = cpu_enter_critical();
  520. PMSM_FOC_Start(motor.mode);
  521. pwm_enable_channel();
  522. cpu_exit_critical(mask);
  523. }
  524. }
  525. }
  526. if (runMode != CTRL_MODE_OPEN) {
  527. eCtrl_Running();
  528. float f_throttle = get_throttle_float();
  529. if (f_throttle != motor.throttle) {
  530. motor.throttle = f_throttle;
  531. if (!motor.b_epm_cmd_move) {//通过命令前进后退,不处理转把
  532. torque_speed_target(runMode, f_throttle);
  533. }
  534. }
  535. PMSM_FOC_Slow_Task();
  536. }
  537. }
  538. }