motor.c 28 KB

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