motor.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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 "bsp/fan_pwm.h"
  14. #include "foc/commands.h"
  15. #include "libs/logger.h"
  16. #include "bsp/sched_timer.h"
  17. #include "foc/core/e_ctrl.h"
  18. #include "foc/samples.h"
  19. #include "foc/motor/motor_param.h"
  20. #include "foc/core/foc_observer.h"
  21. #include "foc/core/thro_torque.h"
  22. #include "app/nv_storage.h"
  23. //#include "foc/core/torque.h"
  24. #include "foc/limit.h"
  25. #ifdef CONFIG_DQ_STEP_RESPONSE
  26. extern float target_d;
  27. extern float target_q;
  28. #endif
  29. static bool mc_is_hwbrake(void);
  30. static bool mc_is_gpio_mlock(void);
  31. static void _pwm_brake_prot_timer_handler(shark_timer_t *);
  32. static shark_timer_t _brake_prot_timer = TIMER_INIT(_brake_prot_timer, _pwm_brake_prot_timer_handler);
  33. static void _autohold_beep_timer_handler(shark_timer_t *);
  34. static shark_timer_t _autohold_beep_timer = TIMER_INIT(_autohold_beep_timer, _autohold_beep_timer_handler);
  35. static void _fan_det_timer_handler(shark_timer_t *);
  36. static shark_timer_t _fan_det_timer1 = TIMER_INIT(_fan_det_timer1, _fan_det_timer_handler);
  37. static shark_timer_t _fan_det_timer2 = TIMER_INIT(_fan_det_timer2, _fan_det_timer_handler);
  38. static void _encoder_zero_off_timer_handler(shark_timer_t *);
  39. static shark_timer_t _encoder_zero_off_timer = TIMER_INIT(_encoder_zero_off_timer, _encoder_zero_off_timer_handler);
  40. static void _led_off_timer_handler(shark_timer_t *);
  41. static shark_timer_t _led_off_timer = TIMER_INIT(_led_off_timer, _led_off_timer_handler);
  42. static motor_t motor = {
  43. .s_direction = POSITIVE,
  44. .n_gear = 0,
  45. .b_is96Mode = false,
  46. .mode = CTRL_MODE_OPEN,
  47. };
  48. static void MC_Check_MosVbusThrottle(void) {
  49. int count = 1000;
  50. float ibus_adc = 0;
  51. float vref_adc = 0;
  52. float vref_5v_adc = 0;
  53. gpio_phase_u_detect(true);
  54. while(count-- > 0) {
  55. task_udelay(20);
  56. sample_uvw_phase();
  57. sample_throttle();
  58. sample_vbus();
  59. vref_adc += adc_get_vref();
  60. vref_5v_adc += adc_get_5v_ref();
  61. }
  62. adc_set_vref_calc(vref_adc/1000.0f);
  63. adc_set_5vref_calc(vref_5v_adc/1000.0f);
  64. sys_debug("5v ref %f\n", vref_5v_adc/1000.0f);
  65. count = 50;
  66. while(count-- >0) {
  67. task_udelay(300);
  68. ibus_adc += adc_get_ibus();
  69. }
  70. u16 offset = ((float)ibus_adc)/50.0f;
  71. sys_debug("ibus offset %d\n", offset);
  72. sample_ibus_offset(offset);
  73. gpio_phase_u_detect(false);
  74. float abc[3];
  75. get_phase_vols(abc);
  76. int vbus_vol = get_vbus_int();
  77. if (vbus_vol > nv_get_foc_params()->s_maxDCVol) {
  78. mc_set_critical_error(FOC_CRIT_OV_Vol_Err);
  79. }
  80. if (vbus_vol <= nv_get_foc_params()->s_minDCVol) {
  81. mc_set_critical_error(FOC_CRIT_UN_Vol_Err);
  82. }
  83. vbus_vol = get_acc_vol();
  84. sys_debug("acc vol %d\n", vbus_vol);
  85. if (vbus_vol >= nv_get_foc_params()->s_maxDCVol) {
  86. mc_set_critical_error(FOC_CRIT_ACC_OV_Err);
  87. }
  88. if (vbus_vol <= nv_get_foc_params()->s_minDCVol) {
  89. mc_set_critical_error(FOC_CRIT_ACC_Un_Err);
  90. }
  91. if ((get_throttle_float() < nv_get_foc_params()->f_minThroVol) || (get_throttle_float() > nv_get_foc_params()->f_maxThroVol)) {
  92. if (!motor.b_ignor_throttle) {
  93. mc_set_critical_error(FOC_CRIT_THRO_Err);
  94. }
  95. }
  96. if (abc[0] > vbus_vol/2 || abc[1] > vbus_vol/2 || abc[2] > vbus_vol/2) {
  97. mc_set_critical_error(FOC_CRIT_H_MOS_Err);
  98. }else if (abc[0] < 0.001f){
  99. mc_set_critical_error(FOC_CRIT_L_MOS_Err);
  100. }else if ((abc[0] > 0.5f) && (abc[1] < 0.001f || abc[2] < 0.001f)) {
  101. mc_set_critical_error(FOC_CRIT_Phase_Conn_Err);
  102. }
  103. sys_debug("phase vol %f, %f, %f\n", abc[0], abc[1], abc[2]);
  104. }
  105. static u32 _self_check_task(void *p) {
  106. if (ENC_Check_error()) {
  107. err_add_record(FOC_CRIT_Encoder_Err, 0);
  108. mc_set_critical_error(FOC_CRIT_Encoder_Err);
  109. }
  110. if (get_tick_ms() < 5000) { //启动后5s内检测锁电机线
  111. if (mc_is_gpio_mlock()) {
  112. mc_lock_motor(true);
  113. }
  114. }
  115. if (motor.b_lock_motor) {
  116. if (!mc_is_gpio_mlock()) {
  117. mc_lock_motor(false);
  118. }
  119. }
  120. if (fan_pwm_is_running()) {
  121. if ((get_delta_ms(motor.fan[0].start_ts) >= 5000) && (motor.fan[0].rpm == 0)) {
  122. mc_set_critical_error(FOC_CRIT_Fan1_Err);
  123. }else if (motor.fan[0].rpm > 0) {
  124. mc_clr_critical_error(FOC_CRIT_Fan1_Err);
  125. }
  126. if ((get_delta_ms(motor.fan[1].start_ts) >= 5000) && (motor.fan[1].rpm == 0)) {
  127. mc_set_critical_error(FOC_CRIT_Fan2_Err);
  128. }else if (motor.fan[1].rpm > 0) {
  129. mc_clr_critical_error(FOC_CRIT_Fan2_Err);
  130. }
  131. }
  132. return 5;
  133. }
  134. static bool mc_detect_vbus_mode(void) {
  135. #ifdef CONFIG_FORCE_96V_MODE
  136. motor.b_is96Mode = true;
  137. return false;
  138. #else
  139. bool is_96mode = motor.b_is96Mode;
  140. motor.b_is96Mode = get_vbus_int() >= CONFIG_96V_MODE_VOL;
  141. return (is_96mode != motor.b_is96Mode);
  142. #endif
  143. }
  144. static void _mc_internal_init(u8 mode, bool start) {
  145. motor.mode = mode;
  146. motor.throttle = 0;
  147. motor.b_start = start;
  148. motor.b_runStall = false;
  149. motor.runStall_time = 0;
  150. motor.b_epm = false;
  151. motor.b_epm_cmd_move = false;
  152. motor.epm_dir = EPM_Dir_None;
  153. motor.n_autohold_time = 0;
  154. motor.b_auto_hold = 0;
  155. motor.b_break = false;
  156. motor.b_wait_brk_release = false;
  157. motor.b_force_run = false;
  158. motor.b_cruise = false;
  159. }
  160. static void _led_off_timer_handler(shark_timer_t *t) {
  161. gpio_led_enable(false);
  162. }
  163. static void mc_gear_vmode_changed(void) {
  164. mc_gear_t *gears;
  165. if (motor.b_is96Mode) {
  166. gears = &nv_get_gear_configs()->gears_96[0];
  167. }else {
  168. gears = &nv_get_gear_configs()->gears_48[0];
  169. }
  170. //sys_debug("limit %d-%d-%d, mode = %s\n", gears[motor.n_gear].u_maxRPM, gears[motor.n_gear].u_maxIdc, gears[motor.n_gear].u_maxTorque, motor.b_is96Mode?"96V":"48V");
  171. PMSM_FOC_SpeedLimit(gears[motor.n_gear].u_maxRPM);
  172. PMSM_FOC_DCCurrLimit(gears[motor.n_gear].u_maxIdc);
  173. PMSM_FOC_TorqueLimit(gears[motor.n_gear].u_maxTorque);
  174. }
  175. void mc_init(void) {
  176. fan_pwm_init();
  177. adc_init();
  178. pwm_3phase_init();
  179. samples_init();
  180. motor_encoder_init();
  181. foc_command_init();
  182. thro_torque_init();
  183. mc_detect_vbus_mode();
  184. PMSM_FOC_CoreInit();
  185. eCtrl_init(nv_get_foc_params()->n_acc_time, nv_get_foc_params()->n_dec_time);
  186. mc_gpio_init();
  187. limter_set_under_voltage(nv_get_foc_params()->s_minDCVol);
  188. MC_Check_MosVbusThrottle();
  189. sched_timer_enable(CONFIG_SPD_CTRL_US);
  190. shark_task_create(_self_check_task, NULL);
  191. pwm_up_enable(true);
  192. gpio_led_enable(true);
  193. shark_timer_post(&_led_off_timer, 5000);
  194. }
  195. motor_t * mc_params(void) {
  196. return &motor;
  197. }
  198. void mc_need_update(void) {
  199. motor.b_updated = true;
  200. }
  201. bool mc_unsafe_critical_error(void) {
  202. u32 err = motor.n_CritiCalErrMask & (~(FOC_Cri_Err_Mask(FOC_CRIT_Fan1_Err)));
  203. err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_Fan2_Err)));
  204. #ifdef CONFIG_DQ_STEP_RESPONSE
  205. sys_debug("err=0x%x\n", err);
  206. err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_Encoder_Err)));
  207. err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO_Err)));
  208. sys_debug("err=0x%x\n", err);
  209. #endif
  210. if (motor.b_ignor_throttle) {
  211. err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO_Err)));
  212. }
  213. return (err != 0);
  214. }
  215. bool mc_start(u8 mode) {
  216. if (motor.b_start) {
  217. return true;
  218. }
  219. #ifdef CONFIG_DQ_STEP_RESPONSE
  220. mode = CTRL_MODE_CURRENT;
  221. target_d = 0.0f;
  222. target_q = 0.0f;
  223. #endif
  224. mc_detect_vbus_mode();
  225. if (motor.b_lock_motor) {
  226. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  227. return false;
  228. }
  229. MC_Check_MosVbusThrottle();
  230. if (mc_unsafe_critical_error()) {
  231. PMSM_FOC_SetErrCode(FOC_Have_CritiCal_Err);
  232. return false;
  233. }
  234. if (mode > CTRL_MODE_CURRENT) {
  235. PMSM_FOC_SetErrCode(FOC_Param_Err);
  236. return false;
  237. }
  238. if (motor_encoder_get_speed() > CONFIG_ZERO_SPEED_RPM) {
  239. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  240. return false;
  241. }
  242. if (!mc_throttle_released()) {
  243. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  244. return false;
  245. }
  246. pwm_up_enable(false);
  247. _mc_internal_init(mode, true);
  248. #if 0
  249. torque_reset();
  250. #else
  251. thro_torque_reset();
  252. #endif
  253. eCtrl_init(nv_get_foc_params()->n_acc_time, nv_get_foc_params()->n_dec_time);
  254. motor_encoder_start(true);
  255. PMSM_FOC_Start(mode);
  256. PMSM_FOC_RT_LimInit();
  257. pwm_turn_on_low_side();
  258. delay_ms(10);
  259. phase_current_offset_calibrate();
  260. pwm_start();
  261. delay_us(10); //wait for ebrake error
  262. if (mc_unsafe_critical_error()) {
  263. mc_stop();
  264. return false;
  265. }
  266. adc_start_convert();
  267. phase_current_calibrate_wait();
  268. if (phase_curr_offset_check()) {
  269. mc_set_critical_error(FOC_CRIT_CURR_OFF_Err);
  270. mc_stop();
  271. return false;
  272. }
  273. if (mc_is_hwbrake()) {
  274. PMSM_FOC_Brake(true);
  275. }
  276. gpio_beep(200);
  277. return true;
  278. }
  279. bool mc_stop(void) {
  280. if (!motor.b_start) {
  281. return true;
  282. }
  283. if (motor.b_lock_motor) {
  284. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  285. return false;
  286. }
  287. if (motor_encoder_get_speed() > CONFIG_ZERO_SPEED_RPM) {
  288. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  289. return false;
  290. }
  291. if (!mc_throttle_released()) {
  292. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  293. return false;
  294. }
  295. u32 mask = cpu_enter_critical();
  296. _mc_internal_init(CTRL_MODE_OPEN, false);
  297. adc_stop_convert();
  298. pwm_stop();
  299. PMSM_FOC_Stop();
  300. motor_encoder_start(false);
  301. pwm_up_enable(true);
  302. cpu_exit_critical(mask);
  303. return true;
  304. }
  305. bool mc_set_gear(u8 gear) {
  306. if (gear >= CONFIG_MAX_GEAR_NUM) {
  307. PMSM_FOC_SetErrCode(FOC_Param_Err);
  308. return false;
  309. }
  310. if (motor.n_gear != gear) {
  311. motor.n_gear = gear;
  312. u32 mask = cpu_enter_critical();
  313. mc_gear_vmode_changed();
  314. cpu_exit_critical(mask);
  315. }
  316. return true;
  317. }
  318. u8 mc_get_gear(void) {
  319. if (motor.n_gear == 3){
  320. return 0;
  321. }
  322. return motor.n_gear + 1;
  323. }
  324. bool mc_enable_cruise(bool enable) {
  325. if (enable == motor.b_cruise) {
  326. return true;
  327. }
  328. if (PMSM_FOC_EnableCruise(enable)) {
  329. motor.b_cruise = enable;
  330. motor.cruise_time = enable?shark_get_seconds():0;
  331. motor.cruise_torque = 0.0f;
  332. return true;
  333. }
  334. return false;
  335. }
  336. bool mc_is_cruise_enabled(void) {
  337. return motor.b_cruise;
  338. }
  339. bool mc_set_cruise_speed(bool rpm_abs, float target_rpm) {
  340. bool ret;
  341. if (rpm_abs) {
  342. ret = PMSM_FOC_Set_CruiseSpeed(target_rpm);
  343. }else {
  344. ret = PMSM_FOC_Set_CruiseSpeed(PMSM_FOC_GetSpeed() + target_rpm);
  345. }
  346. if (ret) {
  347. motor.cruise_time = shark_get_seconds();
  348. motor.cruise_torque = 0.0f;
  349. }
  350. return ret;
  351. }
  352. bool mc_set_foc_mode(u8 mode) {
  353. if (mode == motor.mode) {
  354. return true;
  355. }
  356. if (!motor.b_start) {
  357. return false;
  358. }
  359. u32 mask = cpu_enter_critical();
  360. bool ret = false;
  361. if (PMSM_FOC_SetCtrlMode(mode)) {
  362. motor.mode = mode;
  363. if (mode == CTRL_MODE_OPEN || mode == CTRL_MODE_CURRENT) {
  364. PMSM_FOC_Start(motor.mode);
  365. pwm_enable_channel();
  366. }
  367. ret = true;
  368. }
  369. cpu_exit_critical(mask);
  370. return ret;
  371. }
  372. bool mc_start_epm(bool epm) {
  373. if (motor.b_epm == epm) {
  374. return true;
  375. }
  376. if (!motor.b_start) {
  377. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  378. return false;
  379. }
  380. if (motor_encoder_get_speed() > CONFIG_ZERO_SPEED_RPM) {
  381. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  382. return false;
  383. }
  384. if (!mc_throttle_released()) {
  385. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  386. return false;
  387. }
  388. u32 mask = cpu_enter_critical();
  389. motor.b_epm = epm;
  390. if (epm) {
  391. eCtrl_set_TgtSpeed(0);
  392. motor.mode = CTRL_MODE_SPD;
  393. PMSM_FOC_TorqueLimit(nv_get_foc_params()->s_maxEpmTorqueLim);
  394. PMSM_FOC_SetCtrlMode(CTRL_MODE_SPD);
  395. }else {
  396. motor.epm_dir = EPM_Dir_None;
  397. motor.mode = CTRL_MODE_TRQ;
  398. motor.b_epm_cmd_move = false;
  399. PMSM_FOC_SetCtrlMode(CTRL_MODE_TRQ);
  400. mc_gear_vmode_changed();
  401. }
  402. cpu_exit_critical(mask);
  403. return false;
  404. }
  405. bool mc_is_epm(void) {
  406. return motor.b_epm;
  407. }
  408. bool mc_is_start(void) {
  409. return (motor.b_start || PMSM_FOC_Is_Start());
  410. }
  411. bool mc_start_epm_move(EPM_Dir_t dir, bool is_command) {
  412. if (!motor.b_epm || !motor.b_start) {
  413. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  414. return false;
  415. }
  416. if ((dir == motor.epm_dir) && (is_command == motor.b_epm_cmd_move)) {
  417. return true;
  418. }
  419. u32 mask = cpu_enter_critical();
  420. motor.epm_dir = dir;
  421. if (dir != EPM_Dir_None) {
  422. motor.b_epm_cmd_move = is_command;
  423. if (!PMSM_FOC_Is_Start()) {
  424. PMSM_FOC_Start(motor.mode);
  425. pwm_enable_channel();
  426. }
  427. float rpm = nv_get_foc_params()->s_maxEpmRPM;
  428. if (dir == EPM_Dir_Back) {
  429. rpm = -rpm;
  430. }
  431. sys_debug("rpm %f\n", rpm);
  432. PMSM_FOC_Set_Speed(rpm);
  433. }else {
  434. motor.b_epm_cmd_move = false;
  435. PMSM_FOC_Set_Speed(0);
  436. }
  437. cpu_exit_critical(mask);
  438. return true;
  439. }
  440. void mc_set_fan_duty(u8 duty) {
  441. sys_debug("fan duty %d\n", duty);
  442. if (!fan_pwm_is_running() && duty > 0) {
  443. motor.fan[0].start_ts = get_tick_ms();
  444. motor.fan[1].start_ts = get_tick_ms();
  445. shark_timer_post(&_fan_det_timer1, 5000);
  446. shark_timer_post(&_fan_det_timer2, 5000);
  447. }else if (duty == 0) {
  448. shark_timer_cancel(&_fan_det_timer1);
  449. shark_timer_cancel(&_fan_det_timer2);
  450. }
  451. fan_set_duty(duty);
  452. }
  453. bool mc_command_epm_move(EPM_Dir_t dir) {
  454. return mc_start_epm_move(dir, true);
  455. }
  456. bool mc_throttle_epm_move(EPM_Dir_t dir) {
  457. return mc_start_epm_move(dir, false);
  458. }
  459. void mc_set_throttle_r(bool use, u8 r) {
  460. motor.u_throttle_ration = r;
  461. motor.b_ignor_throttle = use;
  462. if (motor.b_ignor_throttle) {
  463. mc_clr_critical_error(FOC_CRIT_THRO_Err);
  464. }
  465. }
  466. void mc_use_throttle(void) {
  467. motor.b_ignor_throttle = false;
  468. }
  469. void mc_get_running_status(u8 *data) {
  470. data[0] = motor.mode;
  471. data[0] |= (PMSM_FOC_AutoHoldding()?1:0) << 2;
  472. data[0] |= (motor.b_break?1:0) << 3;
  473. data[0] |= (PMSM_FOC_Get()->in.b_cruiseEna?1:0) << 4;
  474. data[0] |= (motor.b_start?1:0) << 5;
  475. data[0] |= (mc_is_epm()?1:0) << 6;
  476. data[0] |= (motor.b_lock_motor) << 7; //motor locked
  477. }
  478. u16 mc_get_running_status2(void) {
  479. u16 data = 0;
  480. data = motor.n_gear;
  481. data |= (PMSM_FOC_AutoHoldding()?1:0) << 2;
  482. data |= (motor.b_break?1:0) << 3;
  483. data |= (PMSM_FOC_Get()->in.b_cruiseEna?1:0) << 4;
  484. data |= (motor.b_start?1:0) << 5;
  485. data |= (mc_is_epm()?1:0) << 6;
  486. data |= (motor.b_lock_motor) << 7; //motor locked
  487. data |= (eCtrl_is_eBrk_Running()?1:0) << 8; //能量回收运行标志
  488. data |= ((motor.n_CritiCalErrMask != 0)?1:0) << 9;
  489. data |= (fan_pwm_is_running()?1:0) << 10; //风扇是否运行
  490. data |= (motor.b_runStall?1:0) << 11; //是否堵转
  491. data |= (PMSM_FOC_iDC_is_Limited()?1:0) << 12; //是否欠压限制母线电流
  492. data |= (PMSM_FOC_Torque_is_Limited()?1:0) << 13; //是否高温限扭矩
  493. data |= (motor.b_is96Mode?1:0) << 14; //是否高压模式
  494. return data;
  495. }
  496. static float _force_angle = 0.0f;
  497. static int _force_wait = 2000;
  498. /* 开环,强制给定电角度和DQ的电压 */
  499. void mc_force_run_open(s16 vd, s16 vq) {
  500. if (motor.b_start || motor.b_force_run) {
  501. if (vd == 0 && vq == 0) {
  502. PMSM_FOC_SetOpenVdq(0, 0);
  503. delay_ms(500);
  504. wdog_reload();
  505. adc_stop_convert();
  506. pwm_stop();
  507. PMSM_FOC_Stop();
  508. pwm_up_enable(true);
  509. motor.b_force_run = false;
  510. motor.b_ignor_throttle = false;
  511. }
  512. return;
  513. }
  514. if (vd == 0 && vq == 0) {
  515. return;
  516. }
  517. motor.b_ignor_throttle = true;
  518. MC_Check_MosVbusThrottle();
  519. if (mc_unsafe_critical_error()) {
  520. PMSM_FOC_SetErrCode(FOC_Have_CritiCal_Err);
  521. }
  522. pwm_up_enable(false);
  523. pwm_turn_on_low_side();
  524. task_udelay(500);
  525. PMSM_FOC_Start(CTRL_MODE_OPEN);
  526. phase_current_offset_calibrate();
  527. pwm_start();
  528. adc_start_convert();
  529. pwm_enable_channel();
  530. phase_current_calibrate_wait();
  531. PMSM_FOC_Set_Angle(0);
  532. PMSM_FOC_SetOpenVdq(vd, 0);
  533. _force_wait = 2000;
  534. motor.b_force_run = true;
  535. }
  536. void mc_encoder_off_calibrate(s16 vd) {
  537. if (motor.b_start || motor.b_calibrate) {
  538. return;
  539. }
  540. motor.b_calibrate = true;
  541. pwm_up_enable(false);
  542. pwm_turn_on_low_side();
  543. task_udelay(500);
  544. PMSM_FOC_Start(CTRL_MODE_OPEN);
  545. phase_current_offset_calibrate();
  546. pwm_start();
  547. adc_start_convert();
  548. phase_current_calibrate_wait();
  549. PMSM_FOC_Set_Angle(0);
  550. PMSM_FOC_SetOpenVdq(vd, 0);
  551. delay_ms(2000);
  552. motor_encoder_set_direction(POSITIVE);
  553. for (int i = 0; i < 200; i++) {
  554. for (float angle = 0; angle < 360; angle++) {
  555. PMSM_FOC_Set_Angle(angle);
  556. delay_ms(1);
  557. if (i > 20) {
  558. motor_encoder_offset(angle);
  559. }
  560. }
  561. wdog_reload();
  562. if (motor_encoder_offset_is_finish()) {
  563. break;
  564. }
  565. }
  566. motor_encoder_set_direction(NEGATIVE);
  567. delay_ms(100);
  568. for (int i = 0; i < 200; i++) {
  569. for (float angle = 360; angle > 0; angle--) {
  570. PMSM_FOC_Set_Angle(angle);
  571. delay_ms(1);
  572. if (i > 10) {
  573. motor_encoder_offset(angle);
  574. }
  575. }
  576. wdog_reload();
  577. if (motor_encoder_offset_is_finish()) {
  578. break;
  579. }
  580. }
  581. delay_ms(500);
  582. PMSM_FOC_SetOpenVdq(0, 0);
  583. delay_ms(500);
  584. wdog_reload();
  585. adc_stop_convert();
  586. pwm_stop();
  587. PMSM_FOC_Stop();
  588. pwm_up_enable(true);
  589. motor_encoder_data_upload();
  590. motor.b_calibrate = false;
  591. }
  592. static void _encoder_zero_off_timer_handler(shark_timer_t *t){
  593. if (!motor.b_calibrate) {
  594. return;
  595. }
  596. float enc_off = 0.0f;
  597. float phase = motor_encoder_zero_phase_detect(&enc_off);
  598. PMSM_FOC_SetOpenVdq(0, 0);
  599. delay_ms(50);
  600. adc_stop_convert();
  601. pwm_stop();
  602. PMSM_FOC_Stop();
  603. _mc_internal_init(CTRL_MODE_OPEN, false);
  604. motor.b_calibrate = false;
  605. if (phase != INVALID_ANGLE) {
  606. nv_save_angle_offset(phase);
  607. }
  608. }
  609. bool mc_encoder_zero_calibrate(s16 vd) {
  610. if (motor.b_calibrate) {
  611. if (vd == 0) {
  612. encoder_clear_cnt_offset();
  613. shark_timer_cancel(&_encoder_zero_off_timer);
  614. PMSM_FOC_SetOpenVdq(0, 0);
  615. delay_ms(500);
  616. adc_stop_convert();
  617. pwm_stop();
  618. PMSM_FOC_Stop();
  619. _mc_internal_init(CTRL_MODE_OPEN, false);
  620. motor.b_calibrate = false;
  621. motor.b_ignor_throttle = false;
  622. }
  623. return true;
  624. }
  625. encoder_clear_cnt_offset();
  626. motor.b_ignor_throttle = true;
  627. MC_Check_MosVbusThrottle();
  628. if (mc_unsafe_critical_error()) {
  629. PMSM_FOC_SetErrCode(FOC_Have_CritiCal_Err);
  630. return false;
  631. }
  632. _mc_internal_init(CTRL_MODE_OPEN, true);
  633. motor.b_calibrate = true;
  634. pwm_turn_on_low_side();
  635. task_udelay(500);
  636. PMSM_FOC_Start(CTRL_MODE_OPEN);
  637. phase_current_offset_calibrate();
  638. pwm_start();
  639. adc_start_convert();
  640. pwm_enable_channel();
  641. phase_current_calibrate_wait();
  642. PMSM_FOC_Set_Angle(0);
  643. PMSM_FOC_SetOpenVdq(vd, 0);
  644. shark_timer_post(&_encoder_zero_off_timer, 6*1000);
  645. return true;
  646. }
  647. bool mc_current_sensor_calibrate(float current) {
  648. if (!mc_start(CTRL_MODE_OPEN)) {
  649. return false;
  650. }
  651. phase_current_sensor_start_calibrate(current);
  652. phase_current_calibrate_wait();
  653. return true;
  654. }
  655. bool mc_lock_motor(bool lock) {
  656. if (motor.b_lock_motor == lock) {
  657. return true;
  658. }
  659. int ret = true;
  660. u32 mask = cpu_enter_critical();
  661. if (motor.b_start) {
  662. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  663. ret = false;
  664. goto ml_ex_cri;
  665. }
  666. if (lock && (motor_encoder_get_speed() > CONFIG_ZERO_SPEED_RPM)) {
  667. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  668. ret = false;
  669. goto ml_ex_cri;
  670. }
  671. motor.b_lock_motor = lock;
  672. if (lock) {
  673. pwm_start();
  674. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  675. pwm_enable_channel();
  676. }else {
  677. pwm_stop();
  678. }
  679. ml_ex_cri:
  680. cpu_exit_critical(mask);
  681. return ret;
  682. }
  683. bool mc_auto_hold(bool hold) {
  684. if (motor.b_auto_hold == hold) {
  685. return true;
  686. }
  687. if (nv_get_foc_params()->n_autoHold == 0) {
  688. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  689. return false;
  690. }
  691. if (!motor.b_start) {
  692. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  693. return false;
  694. }
  695. if (hold && !mc_throttle_released()) {
  696. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  697. return false;
  698. }
  699. u32 mask = cpu_enter_critical();
  700. motor.b_auto_hold = hold;
  701. if (!PMSM_FOC_Is_Start()) {
  702. PMSM_FOC_Start(motor.mode);
  703. PMSM_FOC_AutoHold(hold);
  704. pwm_enable_channel();
  705. }else {
  706. PMSM_FOC_AutoHold(hold);
  707. }
  708. cpu_exit_critical(mask);
  709. return true;
  710. }
  711. void mc_set_critical_error(u8 err) {
  712. motor.n_CritiCalErrMask |= (1u << err);
  713. }
  714. void mc_clr_critical_error(u8 err) {
  715. motor.n_CritiCalErrMask &= ~(1u << err);
  716. }
  717. u32 mc_get_critical_error(void) {
  718. return motor.n_CritiCalErrMask;
  719. }
  720. bool mc_throttle_released(void) {
  721. if (motor.b_ignor_throttle) {
  722. return motor.u_throttle_ration == 0;
  723. }
  724. return get_throttle_float() <= nv_get_foc_params()->n_startThroVol;
  725. }
  726. static bool mc_is_gpio_mlock(void) {
  727. int count = 50;
  728. int settimes = 0;
  729. while(count-- > 0) {
  730. bool b1 = gpio_motor_locked();
  731. if (b1) {
  732. settimes ++;
  733. }
  734. delay_us(1);
  735. }
  736. if (settimes == 0) {
  737. return false;
  738. }else if (settimes == 50) {
  739. return true;
  740. }
  741. //有干扰,do nothing
  742. return false;
  743. }
  744. static bool mc_is_hwbrake(void) {
  745. int count = 50;
  746. int settimes = 0;
  747. while(count-- > 0) {
  748. bool b1 = mc_get_gpio_brake() && mc_get_gpio_brake1();
  749. if (b1) {
  750. settimes ++;
  751. }
  752. delay_us(1);
  753. }
  754. if (settimes == 0) {
  755. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  756. return true;
  757. #else
  758. return false;
  759. #endif
  760. }else if (settimes == 50) {
  761. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  762. return false;
  763. #else
  764. return true;
  765. #endif
  766. }
  767. //有干扰,do nothing
  768. motor.n_brake_errors++;
  769. return false;
  770. }
  771. static void _fan_det_timer_handler(shark_timer_t *t) {
  772. if (t == &_fan_det_timer1) {
  773. motor.fan[0].rpm = 0;
  774. motor.fan[0].det_ts = 0;
  775. }else {
  776. motor.fan[1].rpm = 0;
  777. motor.fan[1].det_ts = 0;
  778. }
  779. }
  780. void Fan_IRQHandler(int idx) {
  781. fan_t *fan = motor.fan + idx;
  782. u32 pre_ts = fan->det_ts;
  783. u32 delta_ts = get_delta_ms(pre_ts);
  784. fan->det_ts = get_tick_ms();
  785. float rpm = 60.0f * 1000 / (float)delta_ts;
  786. LowPass_Filter(fan->rpm, rpm, 0.1f);
  787. if (idx == 0) {
  788. shark_timer_post(&_fan_det_timer1, 100);
  789. }else {
  790. shark_timer_post(&_fan_det_timer2, 100);
  791. }
  792. }
  793. void MC_Brake_IRQHandler(void) {
  794. if (mc_is_hwbrake()) {
  795. motor.b_break = true;
  796. }else {
  797. motor.b_break = false;
  798. }
  799. if (!motor.b_start) {
  800. return;
  801. }
  802. if (motor.b_break) {
  803. PMSM_FOC_Brake(true);
  804. }else {
  805. PMSM_FOC_Brake(false);
  806. }
  807. }
  808. static void _pwm_brake_prot_timer_handler(shark_timer_t *t){
  809. pwm_brake_enable(true);
  810. sys_debug("MC protect error\n");
  811. }
  812. void MC_Protect_IRQHandler(void){
  813. pwm_brake_enable(false);
  814. shark_timer_post(&_brake_prot_timer, 1000);
  815. if (!motor.b_start) {
  816. return;
  817. }
  818. mc_set_critical_error(FOC_CRIT_Phase_Err);
  819. _mc_internal_init(CTRL_MODE_OPEN, false);
  820. adc_stop_convert();
  821. pwm_stop();
  822. PMSM_FOC_Stop();
  823. pwm_up_enable(true);
  824. }
  825. void TIMER_UP_IRQHandler(void){
  826. if (!motor.b_start && !PMSM_FOC_Is_Start()) {
  827. motor_encoder_update();
  828. }
  829. }
  830. measure_time_t g_meas_foc = {.exec_max_time = 20, .intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  831. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  832. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  833. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  834. void ADC_IRQHandler(void) {
  835. if (phase_current_offset()) {//check if is adc offset checked
  836. return;
  837. }
  838. if (phase_current_sensor_do_calibrate()){
  839. pwm_update_duty(100, FOC_PWM_Half_Period-100, 100);
  840. pwm_update_sample(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period+1, PHASE_BC);
  841. return;
  842. }
  843. TIME_MEATURE_START();
  844. PMSM_FOC_Schedule();
  845. TIME_MEATURE_END();
  846. }
  847. #ifndef CONFIG_DQ_STEP_RESPONSE
  848. static bool mc_can_stop_foc(void) {
  849. if (mc_throttle_released() && PMSM_FOC_GetSpeed() == 0.0f) {
  850. if (!PMSM_FOC_AutoHoldding() && motor.epm_dir == EPM_Dir_None) {
  851. return true;
  852. }
  853. }
  854. /* 启用无感观测器,同时速度低于观测器允许的最小速度,关闭输出,滑行 */
  855. if (!foc_observer_is_encoder() && PMSM_FOC_GetSpeed() < CONFIG_SMO_MIN_SPEED) {
  856. return true;
  857. }
  858. return false;
  859. }
  860. static bool mc_can_restart_foc(void) {
  861. return (!PMSM_FOC_Is_Start()) && (!mc_throttle_released()) && (!mc_unsafe_critical_error());
  862. }
  863. #endif
  864. static bool mc_run_stall_process(u8 run_mode) {
  865. if ((run_mode == CTRL_MODE_TRQ || run_mode == CTRL_MODE_SPD) && !PMSM_FOC_AutoHoldding()) {
  866. //堵转判断
  867. if (motor.b_runStall) {
  868. if (!mc_throttle_released()) {
  869. return true;
  870. }
  871. motor.runStall_time = 0;
  872. motor.b_runStall = false; //转把释放,清除堵转标志
  873. }else if (PMSM_FOC_Get_Real_dqVector() >= CONFIG_STALL_MAX_CURRENT){
  874. if (ABS(PMSM_FOC_GetSpeed()) < 1.0f && (motor.runStall_time == 0)) {
  875. motor.runStall_time = get_tick_ms();
  876. motor.runStall_pos = motor_encoder_get_position();
  877. }
  878. if (motor.runStall_time > 0) {
  879. if (get_delta_ms(motor.runStall_time) >= CONFIG_STALL_MAX_TIME) {
  880. motor.b_runStall = true;
  881. motor.runStall_time = 0;
  882. PMSM_FOC_Set_Torque(0);
  883. #if 0
  884. torque_reset();
  885. #else
  886. thro_torque_reset();
  887. #endif
  888. return true;
  889. }
  890. if (ABS(motor.runStall_pos - motor_encoder_get_position()) >= 0.2f) {
  891. motor.runStall_time = 0;
  892. }
  893. }
  894. }else {
  895. motor.runStall_time = 0;
  896. }
  897. }
  898. return false;
  899. }
  900. static void _autohold_beep_timer_handler(shark_timer_t *t) {
  901. gpio_beep(60);
  902. }
  903. static void mc_autohold_process(void) {
  904. if (nv_get_foc_params()->n_autoHold == 0) {
  905. if (PMSM_FOC_AutoHoldding()) {
  906. mc_auto_hold(false);
  907. }
  908. return;
  909. }
  910. if (PMSM_FOC_AutoHoldding()) {
  911. if (!mc_throttle_released()) {
  912. mc_auto_hold(false);
  913. motor.b_wait_brk_release = false;
  914. }else if (!motor.b_break && motor.b_wait_brk_release) {
  915. motor.b_wait_brk_release = false;
  916. }else if (motor.b_break && !motor.b_wait_brk_release) {
  917. mc_auto_hold(false);
  918. }
  919. }
  920. if (!PMSM_FOC_AutoHoldding() && motor.b_break && (motor_encoder_get_speed() < CONFIG_ZERO_SPEED_RPM)) {
  921. if (motor.n_autohold_time == 0) {
  922. motor.n_autohold_time = get_tick_ms();
  923. }else {
  924. if (get_delta_ms(motor.n_autohold_time) >= CONFIG_AUTOHOLD_DETECT_TIME) {
  925. if (mc_auto_hold(true)) {
  926. motor.b_wait_brk_release = true;
  927. shark_timer_post(&_autohold_beep_timer, 0);
  928. }
  929. }
  930. }
  931. }else {
  932. motor.n_autohold_time = 0;
  933. }
  934. }
  935. static void mc_process_throttle_epm(void) {
  936. if (!motor.b_epm_cmd_move) {//通过命令前进后退,不处理转把
  937. if (mc_throttle_released()) {
  938. mc_throttle_epm_move(EPM_Dir_None);
  939. }else {
  940. mc_throttle_epm_move(EPM_Dir_Forward);
  941. }
  942. }
  943. }
  944. static bool mc_process_force_running(void) {
  945. if (motor.b_calibrate || (motor.mode == CTRL_MODE_OPEN)) {
  946. if (motor.b_force_run) {
  947. if (_force_wait > 0) {
  948. --_force_wait;
  949. }else {
  950. _force_angle += 1.0f;
  951. rand_angle(_force_angle);
  952. PMSM_FOC_Set_Angle(_force_angle);
  953. }
  954. }
  955. return true;
  956. }
  957. return false;
  958. }
  959. #ifdef CONFIG_CRUISE_ENABLE_ACCL
  960. static void mc_process_curise(void) {
  961. static bool can_pause_resume = false;
  962. if (motor.b_cruise) {
  963. if (PMSM_FOC_GetSpeed() < CONFIG_MIN_CRUISE_RPM) {
  964. mc_enable_cruise(false);
  965. return;
  966. }
  967. /* 定速巡航模式下,必须转把归位后才能开始通过拧动转把加速 */
  968. if (mc_throttle_released() && !can_pause_resume) {
  969. can_pause_resume = true;
  970. }
  971. if (!can_pause_resume) {
  972. return;
  973. }
  974. if (PMSM_FOC_Is_CruiseEnabled()) {
  975. u32 cruise_time = shark_get_seconds() - motor.cruise_time;
  976. if ((cruise_time >= 3) && motor.cruise_torque == 0.0f) {
  977. motor.cruise_torque = PMSM_FOC_Get()->in.s_targetTorque;
  978. }else if ((cruise_time >= 3) && (motor.cruise_torque > 0.0f)){
  979. #if 0
  980. float trq_req = get_thro_request_torque();
  981. #else
  982. float trq_req = get_user_request_torque();
  983. #endif
  984. if (trq_req > motor.cruise_torque * 1.2f) {
  985. PMSM_FOC_PauseCruise(); //需要加速,暂停定速巡航
  986. }
  987. }
  988. }else {
  989. #if 0
  990. float trq_req = get_thro_request_torque();
  991. #else
  992. float trq_req = get_user_request_torque();
  993. #endif
  994. if (trq_req <= motor.cruise_torque * 1.1f) {
  995. PMSM_FOC_ResumeCruise(); //重新开始定速巡航,巡航速度还是前一次定速巡航给的速度
  996. motor.cruise_time = shark_get_seconds();
  997. }
  998. }
  999. }else {
  1000. can_pause_resume = false;
  1001. }
  1002. }
  1003. #endif
  1004. /*FOC 的部分处理,比如速度环,状态机,转把采集等*/
  1005. measure_time_t g_meas_MCTask;
  1006. #define mc_TaskStart time_measure_start(&g_meas_MCTask)
  1007. #define mc_TaskEnd time_measure_end(&g_meas_MCTask)
  1008. void Sched_MC_mTask(void) {
  1009. mc_TaskStart;
  1010. adc_vref_filter();
  1011. #ifdef CONFIG_CRUISE_ENABLE_ACCL
  1012. mc_process_curise();
  1013. #endif
  1014. u8 runMode = PMSM_FOC_CtrlMode();
  1015. /*保护功能*/
  1016. bool limted = PMSM_FOC_RunTime_Limit();
  1017. /* 母线电流,实际采集的相电流矢量大小的计算 */
  1018. PMSM_FOC_Calc_Current();
  1019. if (mc_process_force_running()) {
  1020. mc_TaskEnd;
  1021. return;
  1022. }
  1023. if (mc_detect_vbus_mode() || limted) {
  1024. mc_gear_vmode_changed();
  1025. }
  1026. /* 堵转处理 */
  1027. if (mc_run_stall_process(runMode) || (runMode == CTRL_MODE_CURRENT)) {
  1028. eCtrl_Running();
  1029. PMSM_FOC_Slow_Task();
  1030. mc_TaskEnd;
  1031. return;
  1032. }
  1033. if ((runMode != CTRL_MODE_OPEN) || (motor.mode != CTRL_MODE_OPEN)) {
  1034. #ifndef CONFIG_DQ_STEP_RESPONSE
  1035. mc_autohold_process();
  1036. if (motor.mode != CTRL_MODE_OPEN) {
  1037. u32 mask;
  1038. if (mc_can_stop_foc()) {
  1039. if (PMSM_FOC_Is_Start()) {
  1040. mask = cpu_enter_critical();
  1041. PMSM_FOC_Stop();
  1042. pwm_disable_channel();
  1043. cpu_exit_critical(mask);
  1044. }
  1045. }
  1046. if (mc_can_restart_foc()) {
  1047. mask = cpu_enter_critical();
  1048. PMSM_FOC_Start(motor.mode);
  1049. mc_gear_vmode_changed();
  1050. #if 0
  1051. torque_reset();
  1052. #else
  1053. thro_torque_reset();
  1054. #endif
  1055. pwm_enable_channel();
  1056. cpu_exit_critical(mask);
  1057. }
  1058. }
  1059. if (runMode != CTRL_MODE_OPEN) {
  1060. eCtrl_Running();
  1061. if ((runMode == CTRL_MODE_SPD) && mc_is_epm()) {
  1062. mc_process_throttle_epm();
  1063. }else {
  1064. #if 0
  1065. if (motor.b_ignor_throttle) { //使用软件设置的油门开度
  1066. float r = (float)motor.u_throttle_ration/100.0f;
  1067. request_torque(r);
  1068. }else {
  1069. throttle_process(runMode, get_throttle_float());
  1070. }
  1071. #else
  1072. float thro = get_throttle_float();
  1073. if (motor.b_ignor_throttle) {
  1074. float r = (float)motor.u_throttle_ration/100.0f;
  1075. thro = thro_ration_to_voltage(r);
  1076. }
  1077. thro_torque_process(runMode, thro);
  1078. #endif
  1079. }
  1080. PMSM_FOC_Slow_Task();
  1081. }
  1082. #endif
  1083. }
  1084. mc_TaskEnd;
  1085. }