motor.c 14 KB

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