motor.c 28 KB

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