motor.c 29 KB

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