motor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. }
  86. motor_t * mc_params(void) {
  87. return &motor;
  88. }
  89. bool mc_start(u8 mode) {
  90. if (motor.b_start) {
  91. return true;
  92. }
  93. MC_Check_MosVbusThrottle();
  94. if (PMSM_FOC_GetCriticalError() != 0) {
  95. PMSM_FOC_SetErrCode(FOC_Have_CritiCal_Err);
  96. return false;
  97. }
  98. if (mode > CTRL_MODE_CURRENT) {
  99. PMSM_FOC_SetErrCode(FOC_Param_Err);
  100. return false;
  101. }
  102. if (PMSM_FOC_GetSpeed() > 10.0f) {
  103. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  104. return false;
  105. }
  106. if (!mc_throttle_released()) {
  107. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  108. return false;
  109. }
  110. motor.mode = mode;
  111. eCtrl_init(1000, 3000);
  112. motor_encoder_start(motor.s_direction);
  113. PMSM_FOC_Start(mode);
  114. pwm_turn_on_low_side();
  115. delay_ms(100);
  116. phase_current_offset_calibrate();
  117. pwm_start();
  118. adc_start_convert();
  119. phase_current_calibrate_wait();
  120. motor.throttle = 0;
  121. motor.b_start = true;
  122. motor.b_runStall = false;
  123. motor.runStall_time = 0;
  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(1000);
  133. return true;
  134. }
  135. bool mc_stop(void) {
  136. if (!motor.b_start) {
  137. return true;
  138. }
  139. if (PMSM_FOC_GetSpeed() > 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. motor.b_start = false;
  154. gpio_led2_enable(false);
  155. return true;
  156. }
  157. bool mc_set_foc_mode(u8 mode) {
  158. if (mode == motor.mode) {
  159. return true;
  160. }
  161. if (!motor.b_start) {
  162. return false;
  163. }
  164. u32 mask = cpu_enter_critical();
  165. bool ret = false;
  166. if (PMSM_FOC_SetCtrlMode(mode)) {
  167. motor.mode = mode;
  168. if (mode == CTRL_MODE_OPEN) {
  169. PMSM_FOC_Start(motor.mode);
  170. pwm_enable_channel();
  171. }
  172. ret = true;
  173. }
  174. cpu_exit_critical(mask);
  175. return ret;
  176. }
  177. void mc_set_spd_torque(s32 target) {
  178. motor.b_ignor_throttle = true;
  179. motor.s_targetFix = target;
  180. }
  181. void mc_use_throttle(void) {
  182. motor.b_ignor_throttle = false;
  183. }
  184. void mc_encoder_off_calibrate(s16 vd) {
  185. if (PMSM_FOC_Is_Start()) {
  186. return;
  187. }
  188. motor.b_calibrate = true;
  189. pwm_turn_on_low_side();
  190. task_udelay(500);
  191. PMSM_FOC_Start(CTRL_MODE_OPEN);
  192. phase_current_offset_calibrate();
  193. pwm_start();
  194. adc_start_convert();
  195. phase_current_calibrate_wait();
  196. PMSM_FOC_Set_Angle(0);
  197. PMSM_FOC_SetOpenVdq(vd, 0);
  198. delay_ms(2000);
  199. motor_encoder_set_direction(POSITIVE);
  200. for (int i = 0; i < 100000; i++) {
  201. for (float angle = 0; angle < 360; angle++) {
  202. PMSM_FOC_Set_Angle(angle);
  203. delay_ms(2);
  204. if (i > 10) {
  205. motor_encoder_offset(angle);
  206. }
  207. }
  208. wdog_reload();
  209. if (motor_encoder_offset_is_finish()) {
  210. break;
  211. }
  212. }
  213. motor_encoder_set_direction(NEGATIVE);
  214. delay_ms(100);
  215. for (int i = 0; i < 100000; i++) {
  216. for (float angle = 360; angle > 0; angle--) {
  217. PMSM_FOC_Set_Angle(angle);
  218. delay_ms(2);
  219. if (i > 10) {
  220. motor_encoder_offset(angle);
  221. }
  222. }
  223. wdog_reload();
  224. if (motor_encoder_offset_is_finish()) {
  225. break;
  226. }
  227. }
  228. delay_ms(500);
  229. PMSM_FOC_SetOpenVdq(0, 0);
  230. delay_ms(500);
  231. wdog_reload();
  232. adc_stop_convert();
  233. pwm_stop();
  234. PMSM_FOC_Stop();
  235. motor.b_calibrate = false;
  236. }
  237. bool mc_encoder_zero_calibrate(s16 vd) {
  238. if (PMSM_FOC_Is_Start()) {
  239. return false;
  240. }
  241. motor.b_calibrate = true;
  242. pwm_turn_on_low_side();
  243. task_udelay(500);
  244. PMSM_FOC_Start(CTRL_MODE_OPEN);
  245. phase_current_offset_calibrate();
  246. pwm_start();
  247. adc_start_convert();
  248. phase_current_calibrate_wait();
  249. PMSM_FOC_Set_Angle(0);
  250. PMSM_FOC_SetOpenVdq(vd, 0);
  251. delay_ms(2000);
  252. float phase = motor_encoder_zero_phase_detect();
  253. delay_ms(500);
  254. PMSM_FOC_SetOpenVdq(0, 0);
  255. delay_ms(500);
  256. adc_stop_convert();
  257. pwm_stop();
  258. PMSM_FOC_Stop();
  259. motor.b_calibrate = false;
  260. if (phase != INVALID_ANGLE) {
  261. nv_save_angle_offset(phase);
  262. return true;
  263. }
  264. return false;
  265. }
  266. bool mc_current_sensor_calibrate(float current) {
  267. if (!mc_start(CTRL_MODE_OPEN)) {
  268. return false;
  269. }
  270. phase_current_sensor_start_calibrate(current);
  271. phase_current_calibrate_wait();
  272. return true;
  273. }
  274. bool mc_lock_motor(bool lock) {
  275. if (lock && (PMSM_FOC_GetSpeed() > 10)) {
  276. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  277. return false;
  278. }
  279. PMSM_FOC_LockMotor(lock); //if mot enabled, foc core will do lock
  280. if (!motor.b_start) {
  281. if (lock) {
  282. pwm_start();
  283. pwm_update_duty(0, 0, 0);
  284. }else {
  285. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  286. pwm_stop();
  287. }
  288. }
  289. return true;
  290. }
  291. bool mc_throttle_released(void) {
  292. return get_throttle_float() < CONFIG_THROTTLE_LOW_VALUE;
  293. }
  294. static bool mc_is_hwbrake(void) {
  295. #ifdef GPIO_BRAKE_IN_GROUP
  296. int count = 50;
  297. int settimes = 0;
  298. while(count-- > 0) {
  299. bool b1 = gpio_input_bit_get(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_PIN) == SET;
  300. if (b1) {
  301. settimes ++;
  302. }
  303. delay_us(1);
  304. }
  305. if (settimes == 0) {
  306. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  307. return true;
  308. #else
  309. return false;
  310. #endif
  311. }else if (settimes == 50) {
  312. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  313. return false;
  314. #else
  315. return true;
  316. #endif
  317. }else {
  318. //有干扰,do nothing
  319. motor.n_brake_errors++;
  320. return false;
  321. }
  322. #else
  323. return false;
  324. #endif
  325. }
  326. void MC_Brake_IRQHandler(void) {
  327. if (!motor.b_start) {
  328. return;
  329. }
  330. if (mc_is_hwbrake()) {
  331. PMSM_FOC_Brake(true);
  332. }else {
  333. PMSM_FOC_Brake(false);
  334. }
  335. }
  336. static void _pwm_brake_timer_handler(shark_timer_t *t){
  337. pwm_brake_enable(true);
  338. }
  339. void MC_Protect_IRQHandler(void){
  340. pwm_brake_enable(false);
  341. shark_timer_post(&_brake_timer, 1000);
  342. if (!motor.b_start) {
  343. return;
  344. }
  345. PMSM_FOC_SetCriticalError(FOC_CRIT_Phase_Err);
  346. }
  347. measure_time_t g_meas_timeup = {.intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  348. void TIMER_UP_IRQHandler(void){
  349. //phase_current_adc_triger();
  350. time_measure_start(&g_meas_timeup);
  351. }
  352. measure_time_t g_meas_foc = {.exec_max_time = 20, .intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  353. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  354. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  355. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  356. void ADC_IRQHandler(void) {
  357. if (phase_current_offset()) {//check if is adc offset checked
  358. return;
  359. }
  360. if (phase_current_sensor_do_calibrate()){
  361. pwm_update_duty(100, FOC_PWM_Half_Period-100, 100);
  362. pwm_update_sample(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period+1, PHASE_BC);
  363. return;
  364. }
  365. TIME_MEATURE_START();
  366. PMSM_FOC_Schedule();
  367. TIME_MEATURE_END();
  368. }
  369. //#define ANGLE_TEST
  370. #ifdef ANGLE_TEST
  371. static void _debug_angle(void) {
  372. if (motor.b_start) {
  373. PMSM_FOC_Set_Angle(motor.s_testAngle);
  374. if (++motor.s_testAngle >= 360) {
  375. motor.s_testAngle = 0;
  376. }
  377. }
  378. }
  379. #endif
  380. /*FOC 的部分处理,比如速度环,状态机,转把采集等*/
  381. measure_time_t g_meas_MCTask;
  382. void Sched_MC_mTask(void) {
  383. time_measure_start(&g_meas_MCTask);
  384. u8 runMode = PMSM_FOC_CtrlMode();
  385. #if ANGLE_TEST
  386. _debug_angle();
  387. #endif
  388. PMSM_FOC_Calc_iDC();
  389. if (motor.b_calibrate || (motor.mode == CTRL_MODE_OPEN)) {
  390. return;
  391. }
  392. if ((runMode == CTRL_MODE_TRQ || runMode == CTRL_MODE_SPD) && !PMSM_FOC_MotorLocking()) {
  393. //堵转判断
  394. if (motor.b_runStall) {
  395. if (!mc_throttle_released()) {
  396. return;
  397. }
  398. motor.b_runStall = false; //转把释放,清除堵转标志
  399. }else if ((ABS(PMSM_FOC_GetSpeed()) < 1.0f) && (PMSM_FOC_Get()->out.s_RealIdq.q >= CONFIG_STALL_MAX_CURRENT)){
  400. if (motor.runStall_time == 0) {
  401. motor.runStall_time = get_tick_ms();
  402. }else {
  403. if (get_delta_ms(motor.runStall_time) >= CONFIG_STALL_MAX_TIME) {
  404. motor.b_runStall = true;
  405. motor.runStall_time = 0;
  406. torque_speed_target(runMode, 0.0f);
  407. return;
  408. }
  409. }
  410. }else {
  411. motor.runStall_time = 0;
  412. }
  413. }
  414. if ((runMode != CTRL_MODE_OPEN) || (motor.mode != CTRL_MODE_OPEN)) {
  415. if (motor.mode != CTRL_MODE_OPEN) {
  416. u32 mask;
  417. if (mc_throttle_released() && PMSM_FOC_GetSpeed() == 0.0f) {
  418. mask = cpu_enter_critical();
  419. PMSM_FOC_Stop();
  420. pwm_disable_channel();
  421. cpu_exit_critical(mask);
  422. }else {
  423. mask = cpu_enter_critical();
  424. PMSM_FOC_Start(motor.mode);
  425. pwm_enable_channel();
  426. cpu_exit_critical(mask);
  427. }
  428. }
  429. if (runMode != CTRL_MODE_OPEN) {
  430. eCtrl_Running();
  431. float f_throttle = get_throttle_float();
  432. if (f_throttle != motor.throttle) {
  433. motor.throttle = f_throttle;
  434. torque_speed_target(runMode, f_throttle);
  435. }
  436. PMSM_FOC_Slow_Task();
  437. }
  438. }
  439. }