motor.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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_up_enable(false);
  285. pwm_turn_on_low_side();
  286. task_udelay(500);
  287. PMSM_FOC_Start(CTRL_MODE_OPEN);
  288. phase_current_offset_calibrate();
  289. pwm_start();
  290. adc_start_convert();
  291. phase_current_calibrate_wait();
  292. PMSM_FOC_Set_Angle(0);
  293. PMSM_FOC_SetOpenVdq(vd, 0);
  294. delay_ms(2000);
  295. motor_encoder_set_direction(POSITIVE);
  296. for (int i = 0; i < 5000; i++) {
  297. for (float angle = 0; angle < 360; angle++) {
  298. PMSM_FOC_Set_Angle(angle);
  299. delay_ms(1);
  300. if (i > 20) {
  301. motor_encoder_offset(angle);
  302. }
  303. }
  304. wdog_reload();
  305. if (motor_encoder_offset_is_finish()) {
  306. break;
  307. }
  308. }
  309. motor_encoder_set_direction(NEGATIVE);
  310. delay_ms(100);
  311. for (int i = 0; i < 5000; i++) {
  312. for (float angle = 360; angle > 0; angle--) {
  313. PMSM_FOC_Set_Angle(angle);
  314. delay_ms(1);
  315. if (i > 10) {
  316. motor_encoder_offset(angle);
  317. }
  318. }
  319. wdog_reload();
  320. if (motor_encoder_offset_is_finish()) {
  321. break;
  322. }
  323. }
  324. delay_ms(500);
  325. PMSM_FOC_SetOpenVdq(0, 0);
  326. delay_ms(500);
  327. wdog_reload();
  328. adc_stop_convert();
  329. pwm_stop();
  330. PMSM_FOC_Stop();
  331. pwm_up_enable(true);
  332. motor.b_calibrate = false;
  333. }
  334. bool mc_encoder_zero_calibrate(s16 vd) {
  335. if (PMSM_FOC_Is_Start()) {
  336. return false;
  337. }
  338. motor.b_calibrate = true;
  339. pwm_turn_on_low_side();
  340. task_udelay(500);
  341. PMSM_FOC_Start(CTRL_MODE_OPEN);
  342. phase_current_offset_calibrate();
  343. pwm_start();
  344. adc_start_convert();
  345. phase_current_calibrate_wait();
  346. PMSM_FOC_Set_Angle(0);
  347. PMSM_FOC_SetOpenVdq(vd, 0);
  348. delay_ms(2000);
  349. float phase = motor_encoder_zero_phase_detect();
  350. delay_ms(500);
  351. PMSM_FOC_SetOpenVdq(0, 0);
  352. delay_ms(500);
  353. adc_stop_convert();
  354. pwm_stop();
  355. PMSM_FOC_Stop();
  356. motor.b_calibrate = false;
  357. if (phase != INVALID_ANGLE) {
  358. nv_save_angle_offset(phase);
  359. return true;
  360. }
  361. return false;
  362. }
  363. bool mc_current_sensor_calibrate(float current) {
  364. if (!mc_start(CTRL_MODE_OPEN)) {
  365. return false;
  366. }
  367. phase_current_sensor_start_calibrate(current);
  368. phase_current_calibrate_wait();
  369. return true;
  370. }
  371. bool mc_lock_motor(bool lock) {
  372. if (lock && (PMSM_FOC_GetSpeed() > 10)) {
  373. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  374. return false;
  375. }
  376. PMSM_FOC_LockMotor(lock); //if mot enabled, foc core will do lock
  377. if (!motor.b_start) {
  378. if (lock) {
  379. pwm_start();
  380. pwm_update_duty(0, 0, 0);
  381. }else {
  382. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  383. pwm_stop();
  384. }
  385. }
  386. return true;
  387. }
  388. bool mc_throttle_released(void) {
  389. return get_throttle_float() < CONFIG_THROTTLE_LOW_VALUE;
  390. }
  391. static bool mc_is_hwbrake(void) {
  392. #ifdef GPIO_BRAKE_IN_GROUP
  393. int count = 50;
  394. int settimes = 0;
  395. while(count-- > 0) {
  396. bool b1 = gpio_input_bit_get(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_PIN) == SET;
  397. if (b1) {
  398. settimes ++;
  399. }
  400. delay_us(1);
  401. }
  402. if (settimes == 0) {
  403. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  404. return true;
  405. #else
  406. return false;
  407. #endif
  408. }else if (settimes == 50) {
  409. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  410. return false;
  411. #else
  412. return true;
  413. #endif
  414. }else {
  415. //有干扰,do nothing
  416. motor.n_brake_errors++;
  417. return false;
  418. }
  419. #else
  420. return false;
  421. #endif
  422. }
  423. void MC_Brake_IRQHandler(void) {
  424. if (!motor.b_start) {
  425. return;
  426. }
  427. if (mc_is_hwbrake()) {
  428. PMSM_FOC_Brake(true);
  429. }else {
  430. PMSM_FOC_Brake(false);
  431. }
  432. }
  433. static void _pwm_brake_timer_handler(shark_timer_t *t){
  434. pwm_brake_enable(true);
  435. }
  436. void MC_Protect_IRQHandler(void){
  437. pwm_brake_enable(false);
  438. shark_timer_post(&_brake_timer, 1000);
  439. if (!motor.b_start) {
  440. return;
  441. }
  442. PMSM_FOC_SetCriticalError(FOC_CRIT_Phase_Err);
  443. }
  444. measure_time_t g_meas_timeup = {.intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  445. void TIMER_UP_IRQHandler(void){
  446. motor_encoder_update();
  447. }
  448. measure_time_t g_meas_foc = {.exec_max_time = 20, .intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  449. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  450. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  451. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  452. void ADC_IRQHandler(void) {
  453. if (phase_current_offset()) {//check if is adc offset checked
  454. return;
  455. }
  456. if (phase_current_sensor_do_calibrate()){
  457. pwm_update_duty(100, FOC_PWM_Half_Period-100, 100);
  458. pwm_update_sample(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period+1, PHASE_BC);
  459. return;
  460. }
  461. TIME_MEATURE_START();
  462. PMSM_FOC_Schedule();
  463. TIME_MEATURE_END();
  464. }
  465. #ifndef CONFIG_DQ_STEP_RESPONSE
  466. static bool mc_can_stop_foc(void) {
  467. if (mc_throttle_released() && PMSM_FOC_GetSpeed() == 0.0f) {
  468. if (!PMSM_FOC_MotorLocking() && motor.epm_dir == EPM_Dir_None) {
  469. return true;
  470. }
  471. }
  472. return false;
  473. }
  474. #endif
  475. static bool mc_run_stall_process(u8 run_mode) {
  476. if ((run_mode == CTRL_MODE_TRQ || run_mode == CTRL_MODE_SPD) && !PMSM_FOC_MotorLocking()) {
  477. //堵转判断
  478. if (motor.b_runStall) {
  479. if (!mc_throttle_released()) {
  480. return true;
  481. }
  482. motor.b_runStall = false; //转把释放,清除堵转标志
  483. }else if ((ABS(PMSM_FOC_GetSpeed()) < 1.0f) && (PMSM_FOC_Get()->out.s_RealIdq.q >= CONFIG_STALL_MAX_CURRENT)){
  484. if (motor.runStall_time == 0) {
  485. motor.runStall_time = get_tick_ms();
  486. }else {
  487. if (get_delta_ms(motor.runStall_time) >= CONFIG_STALL_MAX_TIME) {
  488. motor.b_runStall = true;
  489. motor.runStall_time = 0;
  490. torque_speed_target(run_mode, 0.0f);
  491. return true;
  492. }
  493. }
  494. }else {
  495. motor.runStall_time = 0;
  496. }
  497. }
  498. return false;
  499. }
  500. /*FOC 的部分处理,比如速度环,状态机,转把采集等*/
  501. measure_time_t g_meas_MCTask;
  502. void Sched_MC_mTask(void) {
  503. time_measure_start(&g_meas_MCTask);
  504. u8 runMode = PMSM_FOC_CtrlMode();
  505. /* 母线电流计算 */
  506. PMSM_FOC_Calc_iDC();
  507. if (motor.b_calibrate || (motor.mode == CTRL_MODE_OPEN)) {
  508. return;
  509. }
  510. /* 堵转处理 */
  511. if (mc_run_stall_process(runMode)) {
  512. return;
  513. }
  514. if ((runMode != CTRL_MODE_OPEN) || (motor.mode != CTRL_MODE_OPEN)) {
  515. #ifndef CONFIG_DQ_STEP_RESPONSE
  516. if (motor.mode != CTRL_MODE_OPEN) {
  517. u32 mask;
  518. if (mc_can_stop_foc()) {
  519. if (PMSM_FOC_Is_Start()) {
  520. mask = cpu_enter_critical();
  521. PMSM_FOC_Stop();
  522. pwm_disable_channel();
  523. cpu_exit_critical(mask);
  524. }
  525. }else {
  526. if (!PMSM_FOC_Is_Start()) {
  527. mask = cpu_enter_critical();
  528. PMSM_FOC_Start(motor.mode);
  529. pwm_enable_channel();
  530. cpu_exit_critical(mask);
  531. }
  532. }
  533. }
  534. if (runMode != CTRL_MODE_OPEN) {
  535. eCtrl_Running();
  536. float f_throttle = get_throttle_float();
  537. if (f_throttle != motor.throttle) {
  538. motor.throttle = f_throttle;
  539. if (!motor.b_epm_cmd_move) {//通过命令前进后退,不处理转把
  540. torque_speed_target(runMode, f_throttle);
  541. }
  542. }
  543. PMSM_FOC_Slow_Task();
  544. }
  545. #endif
  546. }
  547. }