motor.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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/bsp_driver.h"
  8. #include "libs/time_measure.h"
  9. #include "foc/commands.h"
  10. #include "libs/logger.h"
  11. #include "foc/samples.h"
  12. #include "foc/motor/motor_param.h"
  13. #include "foc/core/foc_observer.h"
  14. #include "foc/core/F_Calc.h"
  15. #include "foc/core/etcs.h"
  16. #include "app/nv_storage.h"
  17. #include "app/factory.h"
  18. #include "foc/motor/mot_params_ind.h"
  19. #include "foc/motor/throttle.h"
  20. #include "foc/limit.h"
  21. extern u32 enc_pwm_err_ms;
  22. extern s16 enc_delta_err1, enc_delta_err2;
  23. static bool mc_detect_hwbrake(void);
  24. static bool mc_is_gpio_mlock(void);
  25. static void _pwm_brake_prot_timer_handler(shark_timer_t *);
  26. static shark_timer_t _brake_prot_timer = TIMER_INIT(_brake_prot_timer, _pwm_brake_prot_timer_handler);
  27. static void _fan_det_timer_handler(shark_timer_t *);
  28. static shark_timer_t _fan_det_timer1 = TIMER_INIT(_fan_det_timer1, _fan_det_timer_handler);
  29. static shark_timer_t _fan_det_timer2 = TIMER_INIT(_fan_det_timer2, _fan_det_timer_handler);
  30. static void _encoder_zero_off_timer_handler(shark_timer_t *);
  31. static shark_timer_t _encoder_zero_off_timer = TIMER_INIT(_encoder_zero_off_timer, _encoder_zero_off_timer_handler);
  32. static void _led_off_timer_handler(shark_timer_t *);
  33. static shark_timer_t _led_off_timer = TIMER_INIT(_led_off_timer, _led_off_timer_handler);
  34. motor_t motor = {
  35. .s_direction = POSITIVE,
  36. .n_gear = 0,
  37. .b_high_vol_mode = false,
  38. .mode = CTRL_MODE_OPEN,
  39. .mos_lim = 0,
  40. .motor_lim = 0,
  41. .b_ind_start = false,
  42. .s_target_speed = MAX_S16,
  43. .s_force_torque = MAX_S16,
  44. .u_set.idc_lim = IDC_USER_LIMIT_NONE,
  45. .u_set.rpm_lim = RPM_USER_LIMIT_NONE,
  46. .u_set.ebrk_lvl = 0,
  47. .u_set.n_brkShutPower = CONFIG_Settings_BrkShutPower,
  48. .u_set.b_tcs = CONFIG_Settings_TcsEnable,
  49. };
  50. /* 无感运行的挡位,限制速度,母线电流,最大扭矩 */
  51. static gear_t sensorless_gear = {
  52. .max_speed = CONFIG_SENSORLESS_MAX_SPEED,
  53. .max_torque = CONFIG_SENSORLESS_MAX_TORQUE,
  54. .max_idc = CONFIG_SENSORLESS_MAX_IDC,
  55. .zero_accl = 1500,
  56. .accl_time = 1500,
  57. .torque = {100, 80, 60, 50, 40, 30, 0, 0, 0, 0},
  58. };
  59. static runtime_node_t mc_error;
  60. static void MC_Check_MosVbusThrottle(void) {
  61. int count = 1000;
  62. float ibus_adc = 0;
  63. float vref_adc = 0;
  64. float vref_5v_adc = 0;
  65. gpio_phase_u_detect(true);
  66. while(count-- > 0) {
  67. task_udelay(20);
  68. sample_uvw_phase();
  69. sample_throttle();
  70. sample_vbus();
  71. vref_adc += adc_get_vref();
  72. vref_5v_adc += adc_get_5v_ref();
  73. }
  74. adc_set_vref_calc(vref_adc/1000.0f);
  75. adc_set_5vref_calc(vref_5v_adc/1000.0f);
  76. sys_debug("5v ref %f\n", vref_5v_adc/1000.0f);
  77. count = 50;
  78. while(count-- >0) {
  79. task_udelay(300);
  80. ibus_adc += adc_get_ibus();
  81. }
  82. u16 offset = ((float)ibus_adc)/50.0f;
  83. sys_debug("ibus offset %d\n", offset);
  84. sample_ibus_offset(offset);
  85. if (factory_is_running()) {
  86. return;
  87. }
  88. gpio_phase_u_detect(false);
  89. float abc[3];
  90. get_phase_vols_filtered(abc);
  91. int vbus_vol = get_vbus_int();
  92. if (vbus_vol > mc_conf()->c.max_dc_vol) {
  93. mc_set_critical_error(FOC_CRIT_OV_Vol_Err);
  94. }
  95. if (vbus_vol <= mc_conf()->c.min_dc_vol) {
  96. mc_set_critical_error(FOC_CRIT_UN_Vol_Err);
  97. }
  98. vbus_vol = get_acc_vol();
  99. sys_debug("acc vol %d\n", vbus_vol);
  100. if (vbus_vol >= mc_conf()->c.max_dc_vol) {
  101. mc_set_critical_error(FOC_CRIT_ACC_OV_Err);
  102. }
  103. if (vbus_vol <= mc_conf()->c.min_dc_vol) {
  104. mc_set_critical_error(FOC_CRIT_ACC_Un_Err);
  105. }
  106. if (throttle_is_all_error()) {
  107. if (!motor.b_ignor_throttle) {
  108. mc_set_critical_error(FOC_CRIT_THRO_Err);
  109. mc_set_critical_error(FOC_CRIT_THRO2_Err);
  110. }
  111. }
  112. if (abc[0] > vbus_vol/2 || abc[1] > vbus_vol/2 || abc[2] > vbus_vol/2) {
  113. mc_set_critical_error(FOC_CRIT_H_MOS_Err);
  114. }else if (abc[0] < 0.001f){
  115. mc_set_critical_error(FOC_CRIT_L_MOS_Err);
  116. }else if ((abc[0] > 0.5f) && (abc[1] < 0.001f || abc[2] < 0.001f)) {
  117. mc_set_critical_error(FOC_CRIT_Phase_Conn_Err);
  118. }
  119. sys_debug("phase vol %f, %f, %f\n", abc[0], abc[1], abc[2]);
  120. }
  121. static int hw_brk_err_cnt = 0;
  122. static int hw_brk_no_err_cnt = 0;
  123. static u32 _self_check_task(void *p) {
  124. if (factory_is_running()) {
  125. return 1000;
  126. }
  127. if (get_tick_ms() < 500) { //启动500ms内检测刹车状态是否正常,应该是没有刹车
  128. if (mc_detect_hwbrake() && hw_brk_err_cnt++ >= 60) {
  129. mc_set_critical_error(FOC_CRIT_BRK_Err);
  130. }
  131. }else {
  132. if (mc_critical_err_is_set(FOC_CRIT_BRK_Err) && !mc_detect_hwbrake()) {
  133. if (hw_brk_no_err_cnt++ >= 100) {
  134. mc_clr_critical_error(FOC_CRIT_BRK_Err);
  135. }
  136. }else {
  137. hw_brk_no_err_cnt = 0;
  138. }
  139. }
  140. if (ENC_Check_error()) {
  141. mc_crit_err_add_s16(FOC_CRIT_Encoder_Err, -1);
  142. mc_set_critical_error(FOC_CRIT_Encoder_Err);
  143. }
  144. if (get_tick_ms() < 5000) { //启动后5s内检测锁电机线
  145. if (mc_is_gpio_mlock()) {
  146. mc_lock_motor(true);
  147. }
  148. }
  149. if (motor.b_lock_motor) {
  150. if (!mc_is_gpio_mlock()) {
  151. mc_lock_motor(false);
  152. }
  153. }
  154. if (!motor.b_ignor_throttle) {
  155. if (throttle_is_all_error()) {
  156. mc_set_critical_error(FOC_CRIT_THRO_Err);
  157. mc_set_critical_error(FOC_CRIT_THRO2_Err);
  158. }else if (throttle1_is_error()) {
  159. mc_set_critical_error(FOC_CRIT_THRO_Err);
  160. }else if (throttle2_is_error()) {
  161. mc_set_critical_error(FOC_CRIT_THRO2_Err);
  162. }
  163. }
  164. if (fan_pwm_is_running()) {
  165. #ifdef GPIO_FAN1_IN_GROUP
  166. if ((get_delta_ms(motor.fan[0].start_ts) >= 5000) && (motor.fan[0].rpm == 0)) {
  167. mc_set_critical_error(FOC_CRIT_Fan_Err);
  168. }else if (motor.fan[0].rpm > 0) {
  169. mc_clr_critical_error(FOC_CRIT_Fan_Err);
  170. }
  171. #endif
  172. }
  173. return 5;
  174. }
  175. static bool mc_detect_vbus_mode(void) {
  176. #ifdef CONFIG_FORCE_HIGH_VOL_MODE
  177. motor.b_high_vol_mode = true;
  178. return false;
  179. #else
  180. bool is_96mode = motor.b_high_vol_mode;
  181. motor.b_high_vol_mode = get_vbus_int() >= CONFIG_HIGH_VOL_MODE_MIN_VOL;
  182. return (is_96mode != motor.b_high_vol_mode);
  183. #endif
  184. }
  185. static void _mc_internal_init(u8 mode, bool start) {
  186. motor.mode = mode;
  187. motor.throttle = 0;
  188. motor.b_start = start;
  189. motor.b_runStall = false;
  190. motor.runStall_time = 0;
  191. motor.b_epm = false;
  192. motor.b_epm_cmd_move = false;
  193. motor.epm_dir = EPM_Dir_None;
  194. motor.n_autohold_time = 0;
  195. motor.b_auto_hold = 0;
  196. motor.b_break = false;
  197. motor.b_wait_brk_release = false;
  198. motor.b_force_run = false;
  199. motor.b_cruise = false;
  200. motor.b_limit_pending = false;
  201. motor.f_epm_trq = 0;
  202. motor.f_epm_vel = 0;
  203. motor.s_vbus_hw_min = mc_conf()->c.min_dc_vol;
  204. }
  205. static void _led_off_timer_handler(shark_timer_t *t) {
  206. gpio_led_enable(false);
  207. }
  208. static void mc_gear_mode_set(void) {
  209. gear_t *gears = mc_gear_conf();
  210. if (gears != &sensorless_gear) {
  211. sensorless_gear.max_torque = gears->max_torque;
  212. }else { //slowly changed
  213. line_ramp_set_time(&motor.controller.vel_lim, CONFIG_SENSORLESS_RAMP_TIME);
  214. line_ramp_set_time(&motor.controller.dc_curr_lim, CONFIG_SENSORLESS_RAMP_TIME);
  215. }
  216. mot_contrl_set_vel_limit(&motor.controller, (float)min(gears->max_speed, motor.u_set.rpm_lim));
  217. mot_contrl_set_dccurr_limit(&motor.controller, (float)min(gears->max_idc, motor.u_set.idc_lim));
  218. mot_contrl_set_torque_limit(&motor.controller, (float)gears->max_torque);
  219. }
  220. void mc_init(void) {
  221. fan_pwm_init();
  222. adc_init();
  223. pwm_3phase_init();
  224. samples_init();
  225. motor_encoder_init();
  226. foc_command_init();
  227. throttle_init();
  228. mc_detect_vbus_mode();
  229. mot_contrl_init(&motor.controller);
  230. mc_gpio_init();
  231. MC_Check_MosVbusThrottle();
  232. sched_timer_enable(CONFIG_SPD_CTRL_US);
  233. shark_task_create(_self_check_task, NULL);
  234. pwm_up_enable(true);
  235. gpio_led_enable(true);
  236. shark_timer_post(&_led_off_timer, 5000);
  237. }
  238. motor_t * get_motor(void) {
  239. return &motor;
  240. }
  241. gear_t *mc_gear_conf_by_gear(u8 n_gear) {
  242. gear_t *gears;
  243. if (!foc_observer_is_encoder()) { //无感模式,受限运行
  244. return &sensorless_gear;
  245. }
  246. if (motor.b_high_vol_mode) {
  247. gears = &mc_conf()->g_n[0];
  248. }else {
  249. gears = &mc_conf()->g_l[0];
  250. }
  251. return &gears[n_gear];
  252. }
  253. gear_t *mc_gear_conf(void) {
  254. return mc_gear_conf_by_gear(motor.n_gear);
  255. }
  256. static __INLINE float gear_rpm_2_torque(u8 torque, s16 max) {
  257. return (float)torque/100.0f * max;
  258. }
  259. float mc_gear_max_torque(s16 vel, u8 gear_n) {
  260. gear_t *gear = mc_gear_conf_by_gear(gear_n);
  261. if (gear == NULL) {
  262. return 0;
  263. }
  264. vel = ABS(vel);
  265. if (vel <= 1000) {
  266. return gear_rpm_2_torque(gear->torque[0], gear->max_torque);
  267. }
  268. int vel_idx = vel / 1000;
  269. if (vel_idx >= CONFIG_GEAR_SPEED_TRQ_NUM -1 ) {
  270. return gear_rpm_2_torque(gear->torque[CONFIG_GEAR_SPEED_TRQ_NUM-1], gear->max_torque);
  271. }
  272. float torque_1 = gear_rpm_2_torque(gear->torque[vel_idx-1], gear->max_torque);
  273. float min_rpm = vel_idx * 1000;
  274. float torque_2 = gear_rpm_2_torque(gear->torque[vel_idx], gear->max_torque);
  275. float max_rpm = min_rpm + 1000;
  276. return f_map((float)vel, min_rpm, max_rpm, torque_1, torque_2);
  277. }
  278. /* 必须立即停机 */
  279. bool mc_critical_need_stop(void) {
  280. u32 mask = FOC_Cri_Err_Mask(FOC_CRIT_IDC_OV) | FOC_Cri_Err_Mask(FOC_CRIT_Angle_Err) | FOC_Cri_Err_Mask(FOC_CRIT_Vol_HW_Err);
  281. u32 err = motor.n_CritiCalErrMask & mask;
  282. return (err != 0);
  283. }
  284. bool mc_critical_can_not_run(void) {
  285. u32 mask = FOC_Cri_Err_Mask(FOC_CRIT_MOTOR_TEMP_Lim) | FOC_Cri_Err_Mask(FOC_CRIT_MOS_TEMP_Lim);
  286. u32 err = motor.n_CritiCalErrMask & mask;
  287. bool crit_err = (err != 0) || mc_critical_need_stop();
  288. if (!motor.b_ignor_throttle) {
  289. crit_err = crit_err || throttle_is_all_error();
  290. }
  291. return crit_err;
  292. }
  293. bool mc_unsafe_critical_error(void) {
  294. u32 err = motor.n_CritiCalErrMask & (~(FOC_Cri_Err_Mask(FOC_CRIT_Fan_Err)));
  295. #ifdef CONFIG_DQ_STEP_RESPONSE
  296. sys_debug("err=0x%x\n", err);
  297. err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_Encoder_Err)));
  298. err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO_Err)));
  299. err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO2_Err)));
  300. sys_debug("err=0x%x\n", err);
  301. #else
  302. if (!throttle_is_all_error()) {//不是两个转把都有问题,忽略出问题的转把错误
  303. if (throttle1_is_error()) {
  304. err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO_Err)));
  305. }else if (throttle2_is_error()) {
  306. err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO2_Err)));
  307. }
  308. }
  309. #endif
  310. if (motor.b_ignor_throttle) {
  311. err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO_Err)));
  312. err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO2_Err)));
  313. }
  314. return (err != 0);
  315. }
  316. bool mc_start(u8 mode) {
  317. if (motor.b_start) {
  318. return true;
  319. }
  320. #ifdef CONFIG_DQ_STEP_RESPONSE
  321. mode = CTRL_MODE_CURRENT;
  322. #endif
  323. motor.s_force_torque = MAX_S16;
  324. mc_detect_vbus_mode();
  325. etcs_enable(&motor.controller.etcs, motor.u_set.b_tcs);
  326. if (motor.b_lock_motor) {
  327. mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
  328. return false;
  329. }
  330. MC_Check_MosVbusThrottle();
  331. if (mc_unsafe_critical_error()) {
  332. mot_contrl_set_error(&motor.controller, FOC_Have_CritiCal_Err);
  333. return false;
  334. }
  335. if (mode > CTRL_MODE_CURRENT) {
  336. mot_contrl_set_error(&motor.controller, FOC_Param_Err);
  337. return false;
  338. }
  339. if (mot_contrl_get_speed(&motor.controller) > CONFIG_ZERO_SPEED_RPM) {
  340. mot_contrl_set_error(&motor.controller, FOC_NowAllowed_With_Speed);
  341. return false;
  342. }
  343. if (!mc_throttle_released()) {
  344. mot_contrl_set_error(&motor.controller, FOC_Throttle_Err);
  345. return false;
  346. }
  347. pwm_up_enable(false);
  348. u32 mask = cpu_enter_critical();
  349. _mc_internal_init(mode, true);
  350. throttle_torque_reset();
  351. motor_encoder_start(true);
  352. mot_contrl_start(&motor.controller, mode);
  353. mot_contrl_set_torque_ramp_time(&motor.controller, mc_gear_conf()->zero_accl, mc_conf()->c.thro_dec_time);
  354. mc_set_ebrk_level(motor.u_set.ebrk_lvl);
  355. mc_gear_mode_set();
  356. cpu_exit_critical(mask);
  357. pwm_turn_on_low_side();
  358. delay_ms(10);
  359. phase_current_offset_calibrate();
  360. pwm_start();
  361. delay_us(10); //wait for ebrake error
  362. if (mc_unsafe_critical_error()) {
  363. mc_stop();
  364. return false;
  365. }
  366. adc_start_convert();
  367. phase_current_calibrate_wait();
  368. if (phase_curr_offset_check()) {
  369. mot_contrl_set_error(&motor.controller, FOC_CRIT_CURR_OFF_Err);
  370. mc_stop();
  371. return false;
  372. }
  373. if (mc_detect_hwbrake()) {
  374. mot_contrl_set_hw_brake(&motor.controller, true);
  375. }
  376. return true;
  377. }
  378. bool mc_stop(void) {
  379. if (!motor.b_start) {
  380. return true;
  381. }
  382. if (motor.b_lock_motor) {
  383. mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
  384. return false;
  385. }
  386. if (mot_contrl_get_speed(&motor.controller) > CONFIG_ZERO_SPEED_RPM) {
  387. mot_contrl_set_error(&motor.controller, FOC_NowAllowed_With_Speed);
  388. return false;
  389. }
  390. if (!mc_throttle_released()) {
  391. mot_contrl_set_error(&motor.controller, FOC_Throttle_Err);
  392. return false;
  393. }
  394. u32 mask = cpu_enter_critical();
  395. _mc_internal_init(CTRL_MODE_OPEN, false);
  396. adc_stop_convert();
  397. pwm_stop();
  398. mot_contrl_stop(&motor.controller);
  399. motor_encoder_start(false);
  400. pwm_up_enable(true);
  401. cpu_exit_critical(mask);
  402. return true;
  403. }
  404. void mc_set_mos_lim_level(u8 l) {
  405. if (motor.mos_lim != l) {
  406. mc_crit_err_add(FOC_EV_MOS_Limit_L, l, get_mos_temp_raw());
  407. }
  408. motor.mos_lim = l;
  409. }
  410. void mc_set_motor_lim_level(u8 l) {
  411. if (motor.motor_lim != l) {
  412. mc_crit_err_add(FOC_EV_MOT_Limit_L, l, get_motor_temp_raw());
  413. }
  414. motor.motor_lim = l;
  415. }
  416. bool mc_set_gear(u8 gear) {
  417. if (gear >= CONFIG_MAX_GEARS) {
  418. mot_contrl_set_error(&motor.controller, FOC_Param_Err);
  419. return false;
  420. }
  421. if (motor.n_gear != gear) {
  422. #if TURBO_GEAR_AUTO_EXIT_TIME>0
  423. if (gear == TURBO_GEAR) {
  424. if (motor.mos_lim != 0 || motor.motor_lim != 0) {
  425. mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
  426. return false;
  427. }
  428. if (get_motor_temp_raw() >= TURBO_MIN_MOTOR_TEMP || get_mos_temp_raw() >= TURBO_MIN_MOS_TEMP) {
  429. mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
  430. return false;
  431. }
  432. motor.gear_last = motor.n_gear;
  433. motor.turbo_time = get_tick_ms();
  434. }
  435. #endif
  436. u32 mask = cpu_enter_critical();
  437. motor.n_gear = gear;
  438. mc_gear_mode_set();
  439. cpu_exit_critical(mask);
  440. }
  441. return true;
  442. }
  443. static void mc_turbo_auto_exit(void) {
  444. #if TURBO_GEAR_AUTO_EXIT_TIME>0
  445. if (motor.n_gear != TURBO_GEAR) {
  446. return;
  447. }
  448. if (motor.mos_lim != 0 || motor.motor_lim != 0) {
  449. mc_set_gear(motor.gear_last);
  450. return;
  451. }
  452. u32 delta_time = get_delta_ms(motor.turbo_time);
  453. if (delta_time >= TURBO_GEAR_AUTO_EXIT_TIME) {
  454. motor.turbo_remain_sec = 0;
  455. mc_set_gear(motor.gear_last);
  456. return;
  457. }else {
  458. motor.turbo_remain_sec = (TURBO_GEAR_AUTO_EXIT_TIME - delta_time) / 1000;
  459. }
  460. #endif
  461. }
  462. u8 mc_get_gear(void) {
  463. if (motor.n_gear == 3){
  464. return 0;
  465. }
  466. return motor.n_gear + 1;
  467. }
  468. u8 mc_get_internal_gear(void) {
  469. return motor.n_gear;
  470. }
  471. bool mc_hwbrk_can_shutpower(void) {
  472. if (motor.u_set.n_brkShutPower != MAX_U8) {
  473. return (motor.u_set.n_brkShutPower != 0);
  474. }
  475. return (mc_conf()->s.brk_shut_power != 0);
  476. }
  477. bool mc_enable_cruise(bool enable) {
  478. if (enable == motor.b_cruise) {
  479. return true;
  480. }
  481. if (mot_contrl_set_cruise(&motor.controller, enable)) {
  482. motor.b_cruise = enable;
  483. motor.cruise_time = enable?shark_get_seconds():0;
  484. motor.cruise_torque = 0.0f;
  485. return true;
  486. }
  487. return false;
  488. }
  489. bool mc_set_target_vel(s16 vel) {
  490. if (motor.mode != CTRL_MODE_SPD || mc_is_epm()) {
  491. return false;
  492. }
  493. motor.s_target_speed = vel;
  494. return true;
  495. }
  496. bool mc_set_force_torque(s16 torque) {
  497. if (motor.mode != CTRL_MODE_TRQ) {
  498. return false;
  499. }
  500. if (torque == MAX_S16) {
  501. motor.s_force_torque = MAX_S16;
  502. }else {
  503. motor.s_force_torque = fclamp(torque, -mc_gear_conf()->max_torque, mc_gear_conf()->max_torque);
  504. }
  505. return true;
  506. }
  507. bool mc_is_cruise_enabled(void) {
  508. return motor.b_cruise;
  509. }
  510. bool mc_set_cruise_speed(bool rpm_abs, float target_rpm) {
  511. bool ret;
  512. if (rpm_abs) {
  513. ret = mot_contrl_set_cruise_speed(&motor.controller, target_rpm);
  514. }else {
  515. ret = mot_contrl_set_cruise_speed(&motor.controller, mot_contrl_get_speed(&motor.controller) + target_rpm);
  516. }
  517. if (ret) {
  518. motor.cruise_time = shark_get_seconds();
  519. motor.cruise_torque = 0.0f;
  520. }
  521. return ret;
  522. }
  523. void mc_set_idc_limit(s16 limit) {
  524. if ((limit == motor.u_set.idc_lim) || (limit < 0)) {
  525. return;
  526. }
  527. motor.u_set.idc_lim = limit;
  528. mc_gear_mode_set();
  529. }
  530. void mc_set_rpm_limit(s16 limit) {
  531. if ((limit == motor.u_set.rpm_lim) || (limit < 0)) {
  532. return;
  533. }
  534. motor.u_set.rpm_lim = limit;
  535. mc_gear_mode_set();
  536. }
  537. bool mc_set_ebrk_level(u8 level) {
  538. if (level < CONFIG_EBRK_LVL_NUM) {
  539. motor.u_set.ebrk_lvl = level;
  540. mot_contrl_set_ebrk_time(&motor.controller, mc_get_ebrk_time());
  541. mot_contrl_set_ebrk_torquer(&motor.controller, mc_get_ebrk_torque());
  542. return true;
  543. }
  544. return false;
  545. }
  546. void mc_enable_brkshutpower(u8 shut) {
  547. motor.u_set.n_brkShutPower = shut;
  548. }
  549. void mc_enable_tcs(bool enable) {
  550. motor.u_set.b_tcs = enable;
  551. etcs_enable(&motor.controller.etcs, enable);
  552. }
  553. s16 mc_get_ebrk_torque(void) {
  554. if (motor.u_set.ebrk_lvl < CONFIG_EBRK_LVL_NUM) {
  555. return min(mc_conf()->e_r[motor.u_set.ebrk_lvl].torque, mc_conf()->c.max_ebrk_torque);
  556. }
  557. return 0;
  558. }
  559. u16 mc_get_ebrk_time(void) {
  560. if (motor.u_set.ebrk_lvl < CONFIG_EBRK_LVL_NUM) {
  561. return (u16)mc_conf()->e_r[motor.u_set.ebrk_lvl].time;
  562. }
  563. return 0xFFFF;
  564. }
  565. bool mc_set_foc_mode(u8 mode) {
  566. if (mode == motor.mode) {
  567. return true;
  568. }
  569. if (!motor.b_start && !motor.b_ind_start) {
  570. return false;
  571. }
  572. if (mc_critical_can_not_run()) {
  573. return false;
  574. }
  575. if ((mode == CTRL_MODE_OPEN) && (ABS(mot_contrl_get_speed(&motor.controller)) > CONFIG_ZERO_SPEED_RPM)) {
  576. mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
  577. return false;
  578. }
  579. u32 mask = cpu_enter_critical();
  580. bool ret = false;
  581. if (mot_contrl_request_mode(&motor.controller, mode)) {
  582. motor.mode = mode;
  583. if ((mode == CTRL_MODE_OPEN || mode == CTRL_MODE_CURRENT) && !mot_contrl_is_start(&motor.controller)) {
  584. mot_contrl_start(&motor.controller, motor.mode);
  585. pwm_enable_channel();
  586. }
  587. ret = true;
  588. }
  589. cpu_exit_critical(mask);
  590. return ret;
  591. }
  592. bool mc_start_epm(bool epm) {
  593. if (motor.b_epm == epm) {
  594. return true;
  595. }
  596. if (!motor.b_start) {
  597. mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
  598. return false;
  599. }
  600. if (motor_encoder_get_speed() > CONFIG_ZERO_SPEED_RPM) {
  601. mot_contrl_set_error(&motor.controller, FOC_NowAllowed_With_Speed);
  602. return false;
  603. }
  604. if (!mc_throttle_released()) {
  605. mot_contrl_set_error(&motor.controller, FOC_Throttle_Err);
  606. return false;
  607. }
  608. u32 mask = cpu_enter_critical();
  609. motor.b_epm = epm;
  610. motor.f_epm_vel = 0.0f;
  611. motor.f_epm_trq = 0.0f;
  612. motor_encoder_band_epm(epm);
  613. if (epm) {
  614. mot_contrl_set_target_vel(&motor.controller, 0);
  615. motor.mode = CTRL_MODE_SPD;
  616. motor.epm_dir = EPM_Dir_None;
  617. mot_contrl_set_torque_limit(&motor.controller, mc_conf()->c.max_epm_torque);
  618. mot_contrl_request_mode(&motor.controller, CTRL_MODE_SPD);
  619. }else {
  620. motor.epm_dir = EPM_Dir_None;
  621. motor.mode = CTRL_MODE_TRQ;
  622. motor.b_epm_cmd_move = false;
  623. mot_contrl_request_mode(&motor.controller, CTRL_MODE_TRQ);
  624. mot_contrl_set_torque_limit_rttime(&motor.controller, CONFIG_LIMIT_RAMP_TIME);
  625. mot_contrl_set_vel_rttime(&motor.controller, CONFIG_CRUISE_RAMP_TIME);
  626. mc_gear_mode_set();
  627. }
  628. cpu_exit_critical(mask);
  629. return false;
  630. }
  631. bool mc_is_epm(void) {
  632. return motor.b_epm;
  633. }
  634. bool mc_is_start(void) {
  635. return (motor.b_start || mot_contrl_is_start(&motor.controller));
  636. }
  637. bool mc_start_epm_move(epm_dir_t dir, bool is_command) {
  638. if (!motor.b_epm || !motor.b_start) {
  639. mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
  640. return false;
  641. }
  642. if ((dir == motor.epm_dir) && (is_command == motor.b_epm_cmd_move)) {
  643. return true;
  644. }
  645. u32 mask = cpu_enter_critical();
  646. if (motor.epm_dir != dir) {
  647. motor.f_epm_vel = 0.0f;
  648. motor.f_epm_trq = 0.0f;
  649. }
  650. motor.epm_dir = dir;
  651. if (dir != EPM_Dir_None) {
  652. motor.b_epm_cmd_move = is_command;
  653. if (!mot_contrl_is_start(&motor.controller)) {
  654. mot_contrl_start(&motor.controller, motor.mode);
  655. mc_gear_mode_set();
  656. pwm_enable_channel();
  657. }else if (mot_contrl_is_auto_holdding(&motor.controller)) {
  658. mc_auto_hold(false);
  659. }
  660. if (dir == EPM_Dir_Back) {
  661. mot_contrl_velloop_params(&motor.controller, 0.2f, 7.5f);
  662. }else {
  663. mot_contrl_velloop_params(&motor.controller, 0.2f, 7.5f);
  664. }
  665. mot_contrl_set_torque_limit_rttime(&motor.controller, 2.0);
  666. mot_contrl_set_torque_limit(&motor.controller, motor.f_epm_trq);
  667. mot_contrl_set_vel_rttime(&motor.controller, 1000.0f);
  668. mot_contrl_set_target_vel(&motor.controller, motor.f_epm_vel);
  669. }else {
  670. motor.b_epm_cmd_move = false;
  671. mot_contrl_velloop_params(&motor.controller, mc_conf()->c.pid[PID_Vel_ID].kp, mc_conf()->c.pid[PID_Vel_ID].ki);
  672. mot_contrl_set_target_vel(&motor.controller, 0);
  673. }
  674. cpu_exit_critical(mask);
  675. return true;
  676. }
  677. void mc_set_fan_duty(u8 duty) {
  678. sys_debug("fan duty %d\n", duty);
  679. if (!fan_pwm_is_running() && duty > 0) {
  680. motor.fan[0].start_ts = get_tick_ms();
  681. motor.fan[1].start_ts = get_tick_ms();
  682. shark_timer_post(&_fan_det_timer1, 5000);
  683. shark_timer_post(&_fan_det_timer2, 5000);
  684. }else if (duty == 0) {
  685. shark_timer_cancel(&_fan_det_timer1);
  686. shark_timer_cancel(&_fan_det_timer2);
  687. }
  688. fan_set_duty(duty);
  689. }
  690. bool mc_command_epm_move(epm_dir_t dir) {
  691. return mc_start_epm_move(dir, true);
  692. }
  693. bool mc_throttle_epm_move(epm_dir_t dir) {
  694. return mc_start_epm_move(dir, false);
  695. }
  696. void mc_set_throttle_r(bool use, u8 r) {
  697. motor.u_throttle_ration = r;
  698. motor.b_ignor_throttle = use;
  699. if (motor.b_ignor_throttle) {
  700. mc_clr_critical_error(FOC_CRIT_THRO_Err);
  701. mc_clr_critical_error(FOC_CRIT_THRO2_Err);
  702. }
  703. }
  704. void mc_use_throttle(void) {
  705. motor.b_ignor_throttle = false;
  706. }
  707. void mc_get_running_status(u8 *data) {
  708. data[0] = motor.mode;
  709. data[0] |= (mot_contrl_is_auto_holdding(&motor.controller)?1:0) << 2;
  710. data[0] |= (motor.b_break?1:0) << 3;
  711. data[0] |= (motor.b_cruise?1:0) << 4;
  712. data[0] |= (motor.b_start?1:0) << 5;
  713. data[0] |= (mc_is_epm()?1:0) << 6;
  714. data[0] |= (motor.b_lock_motor) << 7; //motor locked
  715. }
  716. u16 mc_get_running_status2(void) {
  717. u16 data = 0;
  718. data = motor.b_start?1:0;
  719. data |= (motor.n_gear & 0x7) << 1;
  720. data |= (mot_contrl_is_auto_holdding(&motor.controller)?1:0) << 3;
  721. data |= (motor.b_break?1:0) << 4;
  722. data |= (motor.b_cruise?1:0) << 5;
  723. data |= (mc_is_epm()?1:0) << 6;
  724. data |= (motor.b_lock_motor) << 7; //motor locked
  725. data |= (mot_contrl_ebrk_is_running(&motor.controller)?1:0) << 8; //能量回收运行标志
  726. data |= ((motor.n_CritiCalErrMask != 0)?1:0) << 9;
  727. data |= (fan_pwm_is_running()?1:0) << 10; //风扇是否运行
  728. data |= (motor.b_runStall?1:0) << 11; //是否堵转
  729. data |= (mot_contrl_dccurr_is_protected(&motor.controller)?1:0) << 12; //是否欠压限制母线电流
  730. data |= (mot_contrl_torque_is_protected(&motor.controller)?1:0) << 13; //是否高温限扭矩
  731. data |= (etcs_is_running(&motor.controller.etcs)?1:0) << 14; //电子tcs是否正在工作
  732. data |= (throttle_not_released_err()?1:0) << 15;
  733. return data;
  734. }
  735. static float _force_angle = 0.0f;
  736. static int _force_wait = 2000;
  737. /* 开环,强制给定电角度和DQ的电压 */
  738. void mc_force_run_open(s16 vd, s16 vq, bool align) {
  739. if (motor.b_start || motor.b_force_run) {
  740. if (vd == 0 && vq == 0) {
  741. mot_contrl_set_vdq(&motor.controller, 0, 0);
  742. delay_ms(500);
  743. wdog_reload();
  744. adc_stop_convert();
  745. pwm_stop();
  746. mot_contrl_stop(&motor.controller);
  747. pwm_up_enable(true);
  748. motor.b_force_run = false;
  749. motor.b_ignor_throttle = false;
  750. }
  751. return;
  752. }
  753. if (vd == 0 && vq == 0) {
  754. return;
  755. }
  756. motor.b_ignor_throttle = true;
  757. MC_Check_MosVbusThrottle();
  758. if (mc_unsafe_critical_error()) {
  759. mot_contrl_set_error(&motor.controller, FOC_Have_CritiCal_Err);
  760. }
  761. pwm_up_enable(false);
  762. pwm_turn_on_low_side();
  763. task_udelay(500);
  764. mot_contrl_start(&motor.controller, CTRL_MODE_OPEN);
  765. phase_current_offset_calibrate();
  766. pwm_start();
  767. adc_start_convert();
  768. pwm_enable_channel();
  769. phase_current_calibrate_wait();
  770. mot_contrl_set_angle(&motor.controller, 0);
  771. mot_contrl_set_vdq(&motor.controller, (float)vd, 0);
  772. if (align) {
  773. _force_wait = 2000 + 1;
  774. }else {
  775. _force_wait = 2000;
  776. }
  777. motor.b_force_run = true;
  778. }
  779. bool mc_ind_motor_start(bool start) {
  780. if (start == motor.b_ind_start) {
  781. return true;
  782. }
  783. if (start) {
  784. motor.b_ignor_throttle = true;
  785. MC_Check_MosVbusThrottle();
  786. if (mc_unsafe_critical_error() || mot_params_flux_pending()) {
  787. mot_contrl_set_error(&motor.controller, FOC_Have_CritiCal_Err);
  788. return false;
  789. }
  790. pwm_up_enable(false);
  791. pwm_turn_on_low_side();
  792. task_udelay(500);
  793. mot_contrl_start(&motor.controller, CTRL_MODE_OPEN);
  794. phase_current_offset_calibrate();
  795. pwm_start();
  796. adc_start_convert();
  797. pwm_enable_channel();
  798. phase_current_calibrate_wait();
  799. mot_contrl_set_vdq_immediate(&motor.controller, 0, 0);
  800. motor.b_ind_start = start;
  801. }else {
  802. u32 mask = cpu_enter_critical();
  803. motor.b_ind_start = start;
  804. mot_contrl_set_vdq(&motor.controller, 0, 0);
  805. cpu_exit_critical(mask);
  806. delay_us(500);
  807. wdog_reload();
  808. adc_stop_convert();
  809. pwm_stop();
  810. mot_contrl_stop(&motor.controller);
  811. motor.mode = CTRL_MODE_OPEN;
  812. pwm_up_enable(true);
  813. motor.b_ignor_throttle = false;
  814. }
  815. return true;
  816. }
  817. static void _encoder_zero_off_timer_handler(shark_timer_t *t){
  818. if (!motor.b_calibrate) {
  819. return;
  820. }
  821. float enc_off = 0.0f;
  822. float phase = motor_encoder_zero_phase_detect(&enc_off);
  823. mot_contrl_set_vdq(&motor.controller, 0, 0);
  824. delay_ms(50);
  825. adc_stop_convert();
  826. pwm_stop();
  827. mot_contrl_stop(&motor.controller);
  828. _mc_internal_init(CTRL_MODE_OPEN, false);
  829. motor.b_calibrate = false;
  830. }
  831. bool mc_encoder_zero_calibrate(s16 vd) {
  832. if (motor.b_calibrate) {
  833. if (vd == 0) {
  834. encoder_clear_cnt_offset();
  835. shark_timer_cancel(&_encoder_zero_off_timer);
  836. mot_contrl_set_vdq(&motor.controller, 0, 0);
  837. delay_ms(500);
  838. adc_stop_convert();
  839. pwm_stop();
  840. mot_contrl_stop(&motor.controller);
  841. _mc_internal_init(CTRL_MODE_OPEN, false);
  842. motor.b_calibrate = false;
  843. motor.b_ignor_throttle = false;
  844. }
  845. return true;
  846. }
  847. encoder_clear_cnt_offset();
  848. motor.b_ignor_throttle = true;
  849. MC_Check_MosVbusThrottle();
  850. if (mc_unsafe_critical_error()) {
  851. mot_contrl_set_error(&motor.controller, FOC_Have_CritiCal_Err);
  852. return false;
  853. }
  854. _mc_internal_init(CTRL_MODE_OPEN, true);
  855. motor.b_calibrate = true;
  856. pwm_turn_on_low_side();
  857. task_udelay(500);
  858. mot_contrl_start(&motor.controller, CTRL_MODE_OPEN);
  859. phase_current_offset_calibrate();
  860. pwm_start();
  861. adc_start_convert();
  862. pwm_enable_channel();
  863. phase_current_calibrate_wait();
  864. mot_contrl_set_angle(&motor.controller, 0);
  865. mot_contrl_set_vdq(&motor.controller, vd, 0);
  866. shark_timer_post(&_encoder_zero_off_timer, 6*1000);
  867. return true;
  868. }
  869. bool mc_current_sensor_calibrate(float current) {
  870. if (!mc_start(CTRL_MODE_OPEN)) {
  871. return false;
  872. }
  873. phase_current_sensor_start_calibrate(current);
  874. phase_current_calibrate_wait();
  875. return true;
  876. }
  877. bool mc_lock_motor(bool lock) {
  878. if (motor.b_lock_motor == lock) {
  879. return true;
  880. }
  881. int ret = true;
  882. u32 mask = cpu_enter_critical();
  883. if (motor.b_start) {
  884. mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
  885. ret = false;
  886. goto ml_ex_cri;
  887. }
  888. if (lock && (motor_encoder_get_speed() >= CONFIG_LOCK_MOTOR_MIN_RPM)) {
  889. mot_contrl_set_error(&motor.controller, FOC_NowAllowed_With_Speed);
  890. ret = false;
  891. goto ml_ex_cri;
  892. }
  893. motor.b_lock_motor = lock;
  894. if (lock) {
  895. pwm_start();
  896. pwm_update_duty(0, 0, 0);
  897. pwm_enable_channel();
  898. }else {
  899. pwm_stop();
  900. }
  901. ml_ex_cri:
  902. cpu_exit_critical(mask);
  903. return ret;
  904. }
  905. bool mc_auto_hold(bool hold) {
  906. if (motor.b_auto_hold == hold) {
  907. return true;
  908. }
  909. if (!mc_conf()->s.auto_hold) {
  910. mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
  911. return false;
  912. }
  913. if (!motor.b_start) {
  914. mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
  915. return false;
  916. }
  917. if (hold && !mc_throttle_released()) {
  918. mot_contrl_set_error(&motor.controller, FOC_Throttle_Err);
  919. return false;
  920. }
  921. u32 mask = cpu_enter_critical();
  922. motor.b_auto_hold = hold;
  923. if (!mot_contrl_is_start(&motor.controller)) {
  924. mot_contrl_start(&motor.controller, motor.mode);
  925. mot_contrl_set_autohold(&motor.controller, hold);
  926. pwm_enable_channel();
  927. }else {
  928. mot_contrl_set_autohold(&motor.controller, hold);
  929. }
  930. cpu_exit_critical(mask);
  931. return true;
  932. }
  933. bool mc_set_critical_error(u8 err) {
  934. if (mc_critical_err_is_set(err)) {
  935. return false;
  936. }
  937. motor.n_CritiCalErrMask |= (1u << err);
  938. return true;
  939. }
  940. void mc_clr_critical_error(u8 err) {
  941. motor.n_CritiCalErrMask &= ~(1u << err);
  942. }
  943. bool mc_critical_err_is_set(u8 err) {
  944. u32 mask = (1u << err);
  945. return (motor.n_CritiCalErrMask & mask) != 0;
  946. }
  947. u32 mc_get_critical_error(void) {
  948. return motor.n_CritiCalErrMask;
  949. }
  950. bool mc_throttle_released(void) {
  951. if (motor.s_force_torque != MAX_S16) {
  952. return motor.s_force_torque == 0;
  953. }
  954. if (motor.b_ignor_throttle) {
  955. return motor.u_throttle_ration == 0;
  956. }
  957. return throttle_is_released();
  958. }
  959. static bool mc_is_gpio_mlock(void) {
  960. int count = 50;
  961. int settimes = 0;
  962. while(count-- > 0) {
  963. bool b1 = gpio_motor_locked();
  964. if (b1) {
  965. settimes ++;
  966. }
  967. delay_us(1);
  968. }
  969. if (settimes == 0) {
  970. return false;
  971. }else if (settimes == 50) {
  972. return true;
  973. }
  974. //有干扰,do nothing
  975. return false;
  976. }
  977. static bool _mc_is_hwbrake(void) {
  978. int count = 50;
  979. int settimes = 0;
  980. while(count-- > 0) {
  981. bool b1 = mc_get_gpio_brake() || mc_get_gpio_brake1();
  982. if (b1) {
  983. settimes ++;
  984. }
  985. delay_us(1);
  986. }
  987. if (settimes == 0) {
  988. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  989. return true;
  990. #else
  991. return false;
  992. #endif
  993. }else if (settimes == 50) {
  994. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  995. return false;
  996. #else
  997. return true;
  998. #endif
  999. }
  1000. //有干扰,do nothing
  1001. motor.n_brake_errors++;
  1002. return false;
  1003. }
  1004. static bool mc_detect_hwbrake(void) {
  1005. motor.b_break = _mc_is_hwbrake();
  1006. return motor.b_break;
  1007. }
  1008. static void _fan_det_timer_handler(shark_timer_t *t) {
  1009. if (t == &_fan_det_timer1) {
  1010. motor.fan[0].rpm = 0;
  1011. motor.fan[0].det_ts = 0;
  1012. }else {
  1013. motor.fan[1].rpm = 0;
  1014. motor.fan[1].det_ts = 0;
  1015. }
  1016. }
  1017. void Fan_IRQHandler(int idx) {
  1018. fan_t *fan = motor.fan + idx;
  1019. u32 pre_ts = fan->det_ts;
  1020. u32 delta_ts = get_delta_ms(pre_ts);
  1021. fan->det_ts = get_tick_ms();
  1022. float rpm = 60.0f * 1000 / (float)delta_ts;
  1023. LowPass_Filter(fan->rpm, rpm, 0.1f);
  1024. if (idx == 0) {
  1025. shark_timer_post(&_fan_det_timer1, 100);
  1026. }else {
  1027. shark_timer_post(&_fan_det_timer2, 100);
  1028. }
  1029. }
  1030. void MC_Brake_IRQHandler(void) {
  1031. mc_detect_hwbrake();
  1032. if (!motor.b_start) {
  1033. return;
  1034. }
  1035. if (motor.b_break) {
  1036. mc_enable_cruise(false);
  1037. mot_contrl_set_hw_brake(&motor.controller, true);
  1038. }else {
  1039. mot_contrl_set_hw_brake(&motor.controller, false);
  1040. }
  1041. }
  1042. static void _pwm_brake_prot_timer_handler(shark_timer_t *t){
  1043. pwm_brake_enable(true);
  1044. sys_debug("MC protect error\n");
  1045. }
  1046. static void mc_save_err_runtime(void) {
  1047. mc_error.vbus_x10 = (s16)(sample_vbus_raw() * 10.0f);
  1048. mc_error.ibus_x10 = (s16)(sample_ibus_raw() * 10.0f);
  1049. mc_error.vacc_x10 = (s16) (sample_acc_vol_raw() * 10.0f);
  1050. mc_error.id_ref_x10 = (s16)(motor.controller.foc.in.target_id.interpolation * 10.0f);
  1051. mc_error.iq_ref_x10 = (s16)(motor.controller.foc.in.target_iq.interpolation * 10.0f);
  1052. mc_error.id_x10 = (s16)(motor.controller.foc.out.curr_dq.d * 10.0f);
  1053. mc_error.iq_x10 = (s16)(motor.controller.foc.out.curr_dq.q * 10.0f);
  1054. mc_error.vd_x10 = (s16)(motor.controller.foc.out.vol_dq.d * 10.0f);
  1055. mc_error.vq_x10 = (s16)(motor.controller.foc.out.vol_dq.q * 10.0f);
  1056. mc_error.torque_ref_x10 = (s16)(motor.controller.target_torque * 10.0f);
  1057. mc_error.run_mode = motor.controller.mode_running;
  1058. mc_error.rpm = (s16)motor_encoder_get_speed();
  1059. mc_error.b_sensorless = !foc_observer_is_encoder();
  1060. mc_error.b_sensorless_stable = foc_observer_sensorless_stable();
  1061. mc_error.mos_temp = get_mos_temp_raw();
  1062. mc_error.mot_temp = get_motor_temp_raw();
  1063. mc_error.enc_error = motor_encoder_may_error();
  1064. mc_error.sensorless_rpm = (s16)foc_observer_sensorless_speed();
  1065. mc_err_runtime_add(&mc_error);
  1066. }
  1067. void MC_Protect_IRQHandler(void){
  1068. pwm_brake_enable(false);
  1069. shark_timer_post(&_brake_prot_timer, 1000);
  1070. if (!motor.b_start) {
  1071. return;
  1072. }
  1073. mc_set_critical_error(FOC_CRIT_Phase_Err);
  1074. mc_save_err_runtime();
  1075. _mc_internal_init(CTRL_MODE_OPEN, false);
  1076. adc_stop_convert();
  1077. pwm_stop();
  1078. mot_contrl_stop(&motor.controller);
  1079. pwm_up_enable(true);
  1080. }
  1081. void motor_debug(void) {
  1082. if (!mc_unsafe_critical_error()) {
  1083. return;
  1084. }
  1085. sys_debug("err1: %f, %f, %f, %d\n", (float)mc_error.vbus_x10/10.0f, (float)mc_error.id_ref_x10/10.0f, (float)mc_error.iq_ref_x10/10.0f, mc_error.run_mode);
  1086. sys_debug("err2: %f, %f, %f, %f\n", (float)mc_error.id_x10/10.0f, (float)mc_error.iq_x10/10.0f, (float)mc_error.vd_x10/10.0f, (float)mc_error.vq_x10/10.0f);
  1087. sys_debug("err3: %f, %d, %d, %d, %d\n", (float)mc_error.ibus_x10/10.0f, mc_error.sensorless_rpm, mc_error.mos_temp, mc_error.mot_temp, mc_error.enc_error);
  1088. }
  1089. static void motor_vbus_crit_low(s16 curr_vbus) {
  1090. static u16 _vbus_e_count = 0;
  1091. if (curr_vbus < motor.s_vbus_hw_min) {
  1092. _vbus_e_count ++;
  1093. if (_vbus_e_count >= 2) {
  1094. if (mot_contrl_is_start(&motor.controller)) {
  1095. pwm_disable_channel();
  1096. mc_save_err_runtime();
  1097. mot_contrl_stop(&motor.controller);
  1098. }
  1099. if (mc_set_critical_error(FOC_CRIT_Vol_HW_Err)) {
  1100. if (mot_contrl_get_speed(&motor.controller) > CONFIG_ZERO_SPEED_RPM) {
  1101. mc_crit_err_add_s16(FOC_CRIT_Vol_HW_Err, curr_vbus);
  1102. }
  1103. }
  1104. }
  1105. }else {
  1106. _vbus_e_count = 0;
  1107. }
  1108. }
  1109. void TIMER_UP_IRQHandler(void){
  1110. if (!motor.b_start && !mot_contrl_is_start(&motor.controller)) {
  1111. motor_encoder_update(false);
  1112. motor_vbus_crit_low((s16)get_vbus_int());
  1113. }
  1114. }
  1115. measure_time_t g_meas_foc = {.exec_max_time = 25, .intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  1116. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  1117. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  1118. #if (CONFIG_ENABLE_IAB_REC==1)
  1119. #define CONFIG_IAB_REC_COUNT 1000
  1120. static s16 ia[CONFIG_IAB_REC_COUNT], ib[CONFIG_IAB_REC_COUNT];
  1121. static int iab_w_count = 0, iab_r_count = 0;
  1122. static bool b_iab_rec = false;
  1123. extern void can_plot2(s16 v1, s16 v2);
  1124. #endif
  1125. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  1126. void ADC_IRQHandler(void) {
  1127. if (phase_current_offset()) {//check if is adc offset checked
  1128. return;
  1129. }
  1130. if (phase_current_sensor_do_calibrate()){
  1131. pwm_update_duty(100, FOC_PWM_Half_Period-100, 100);
  1132. pwm_update_sample(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period+1, PHASE_BC);
  1133. return;
  1134. }
  1135. TIME_MEATURE_START();
  1136. #if (CONFIG_ENABLE_IAB_REC==1)
  1137. if (b_iab_rec && (iab_w_count < CONFIG_IAB_REC_COUNT)) {
  1138. ia[iab_w_count] = (s16)motor.controller.foc.in.curr_abc[0];
  1139. ib[iab_w_count] = (s16)motor.controller.foc.in.curr_abc[1];
  1140. iab_w_count ++;
  1141. }
  1142. #endif
  1143. motor_vbus_crit_low((s16)sample_vbus_raw()); //need fast detect vbus very low, to stop the motor
  1144. float vd, vq;
  1145. if (motor.b_ind_start) {
  1146. mot_params_high_freq_inject();
  1147. vd = motor.controller.foc.out.vol_dq.d;
  1148. vq = motor.controller.foc.out.vol_dq.q;
  1149. }
  1150. if (!mot_contrl_update(&motor.controller)) {/* FOC 角度错误,立即停机 */
  1151. if (mot_contrl_is_start(&motor.controller)) {
  1152. pwm_disable_channel();
  1153. /* 记录错误 */
  1154. if (!foc_observer_is_force_sensorless()) {
  1155. mc_save_err_runtime();
  1156. }
  1157. mot_contrl_stop(&motor.controller);
  1158. g_meas_foc.first = true;
  1159. if (!foc_observer_is_force_sensorless()) {
  1160. if (mc_set_critical_error(FOC_CRIT_Angle_Err)) {
  1161. mc_crit_err_add_s16(FOC_CRIT_Angle_Err, (s16)motor_encoder_get_speed());
  1162. }
  1163. if (motor_encoder_may_error() == ENCODER_PWM_ERR) {
  1164. if (mc_set_critical_error(FOC_CRIT_Encoder_Err)) {
  1165. mc_crit_err_add(FOC_CRIT_Encoder_Err, (s16)enc_pwm_err_ms, enc_delta_err2);
  1166. }
  1167. }else if (motor_encoder_may_error() == ENCODER_AB_ERR) {
  1168. if (mc_set_critical_error(FOC_CRIT_ENC_AB_Err)) {
  1169. mc_crit_err_add(FOC_CRIT_ENC_AB_Err, enc_delta_err1, enc_delta_err2);
  1170. }
  1171. }
  1172. }
  1173. }
  1174. }
  1175. if (motor.b_ind_start) {
  1176. float id = motor.controller.foc.out.curr_dq.d;
  1177. float iq = motor.controller.foc.out.curr_dq.q;
  1178. mot_params_hj_sample_vi(vd, vq, id, iq);
  1179. }
  1180. TIME_MEATURE_END();
  1181. }
  1182. #if (CONFIG_ENABLE_IAB_REC==1)
  1183. static void _iab_plot_timer_handler(shark_timer_t *t) {
  1184. if (!b_iab_rec) {
  1185. return;
  1186. }
  1187. if (iab_r_count < iab_w_count) {
  1188. can_plot2(ia[iab_r_count], ib[iab_r_count]);
  1189. iab_r_count ++;
  1190. shark_timer_post(t, 10);
  1191. }
  1192. }
  1193. static shark_timer_t _iab_plot_timer = TIMER_INIT(_iab_plot_timer, _iab_plot_timer_handler);
  1194. void mc_start_current_rec(bool rec) {
  1195. if (b_iab_rec == rec) {
  1196. return;
  1197. }
  1198. if (!rec) {
  1199. b_iab_rec = false;
  1200. shark_timer_cancel(&_iab_plot_timer);
  1201. return;
  1202. }
  1203. iab_w_count = 0;
  1204. iab_r_count = 0;
  1205. b_iab_rec = true;
  1206. shark_timer_post(&_iab_plot_timer, 100);
  1207. }
  1208. #endif
  1209. static bool mc_run_stall_process(u8 run_mode) {
  1210. if ((run_mode == CTRL_MODE_TRQ || run_mode == CTRL_MODE_SPD) && !mot_contrl_is_auto_holdding(&motor.controller)) {
  1211. //堵转判断
  1212. if (motor.b_runStall) {
  1213. if (!mc_throttle_released()) {
  1214. return true;
  1215. }
  1216. motor.runStall_time = 0;
  1217. motor.b_runStall = false; //转把释放,清除堵转标志
  1218. }else if (mot_contrl_get_current_vector(&motor.controller) >= CONFIG_STALL_MAX_CURRENT){
  1219. if (ABS(mot_contrl_get_speed(&motor.controller)) < 1.0f && (motor.runStall_time == 0)) {
  1220. motor.runStall_time = get_tick_ms();
  1221. motor.runStall_pos = motor_encoder_get_position();
  1222. }
  1223. if (motor.runStall_time > 0) {
  1224. if (get_delta_ms(motor.runStall_time) >= CONFIG_STALL_MAX_TIME) {
  1225. motor.b_runStall = true;
  1226. motor.runStall_time = 0;
  1227. mot_contrl_set_torque(&motor.controller, 0);
  1228. throttle_torque_reset();
  1229. return true;
  1230. }
  1231. if (ABS(motor.runStall_pos - motor_encoder_get_position()) >= 0.2f) {
  1232. motor.runStall_time = 0;
  1233. }
  1234. }
  1235. }else {
  1236. motor.runStall_time = 0;
  1237. }
  1238. }
  1239. return false;
  1240. }
  1241. static void mc_autohold_process(void) __attribute__((unused));
  1242. static void mc_autohold_process(void) {
  1243. if (!mc_conf()->s.auto_hold) {
  1244. if (mot_contrl_is_auto_holdding(&motor.controller)) {
  1245. mc_auto_hold(false);
  1246. }
  1247. return;
  1248. }
  1249. if (mot_contrl_is_auto_holdding(&motor.controller)) {
  1250. if (!mc_throttle_released()) {
  1251. mc_auto_hold(false);
  1252. motor.b_wait_brk_release = false;
  1253. }else if (!motor.b_break && motor.b_wait_brk_release) {
  1254. motor.b_wait_brk_release = false;
  1255. }else if (motor.b_break && !motor.b_wait_brk_release) {
  1256. mc_auto_hold(false);
  1257. }
  1258. }
  1259. if (!mot_contrl_is_auto_holdding(&motor.controller) && motor.b_break && (motor_encoder_get_speed() == 0)) {
  1260. if (motor.n_autohold_time == 0) {
  1261. motor.n_autohold_time = get_tick_ms();
  1262. }else {
  1263. if (get_delta_ms(motor.n_autohold_time) >= CONFIG_AUTOHOLD_DETECT_TIME) {
  1264. if (mc_auto_hold(true)) {
  1265. motor.b_wait_brk_release = true;
  1266. }
  1267. }
  1268. }
  1269. }else {
  1270. motor.n_autohold_time = 0;
  1271. }
  1272. }
  1273. static void mc_process_throttle_epm(void) __attribute__((unused));
  1274. static void mc_process_throttle_epm(void) {
  1275. if (!motor.b_epm_cmd_move) {//通过命令前进后退,不处理转把
  1276. if (mc_throttle_released()) {
  1277. mc_throttle_epm_move(EPM_Dir_None);
  1278. }else {
  1279. mc_throttle_epm_move(EPM_Dir_Forward);
  1280. }
  1281. }
  1282. }
  1283. static void mc_process_epm_move(void) __attribute__((unused));
  1284. static void mc_process_epm_move(void) {
  1285. if (!motor.b_epm || (motor.epm_dir == EPM_Dir_None)){
  1286. return;
  1287. }
  1288. float target_vel = mc_conf()->c.max_epm_rpm;
  1289. float target_trq = mc_conf()->c.max_epm_torque;
  1290. if (motor.epm_dir == EPM_Dir_Back) {
  1291. target_vel = -mc_conf()->c.max_epm_back_rpm;
  1292. target_trq = mc_conf()->c.max_epm_back_torque;
  1293. }else if (!motor.b_epm_cmd_move) {
  1294. target_vel = throttle_vol_to_opening(throttle_get_signal()) * 2.0f * (float)target_vel;
  1295. }
  1296. motor.f_epm_trq = target_trq;
  1297. motor.f_epm_vel = target_vel;
  1298. mot_contrl_set_torque_limit(&motor.controller, motor.f_epm_trq);
  1299. mot_contrl_set_target_vel(&motor.controller, motor.f_epm_vel);
  1300. }
  1301. static bool mc_process_force_running(void) {
  1302. if (motor.b_calibrate || (motor.mode == CTRL_MODE_OPEN)) {
  1303. if (motor.b_force_run && _force_wait <= 2000) {
  1304. if (_force_wait > 0) {
  1305. --_force_wait;
  1306. }else {
  1307. _force_angle += 1.5f;
  1308. rand_angle(_force_angle);
  1309. mot_contrl_set_angle(&motor.controller, _force_angle);
  1310. }
  1311. }
  1312. return true;
  1313. }
  1314. return false;
  1315. }
  1316. static void mc_process_brake_light(void) {
  1317. bool can_lighting = false;
  1318. if (motor.b_break || motor.b_auto_hold || mot_contrl_ebrk_is_running(&motor.controller) || ((!mc_critical_can_not_run()) && motor_encoder_get_speed() > 2000)) {
  1319. can_lighting = true;
  1320. }
  1321. gpio_brk_light_enable(can_lighting);
  1322. }
  1323. #ifdef CONFIG_CRUISE_ENABLE_ACCL
  1324. static void mc_process_curise(void) {
  1325. static bool can_pause_resume = false;
  1326. if (motor.b_cruise) {
  1327. if (mot_contrl_get_speed(&motor.controller) < CONFIG_CRUISE_EXIT_RPM) {
  1328. mot_contrl_set_cruise(&motor.controller, false);
  1329. return;
  1330. }
  1331. /* 定速巡航模式下,必须转把归位后才能开始通过拧动转把加速 */
  1332. if (mc_throttle_released() && !can_pause_resume) {
  1333. can_pause_resume = true;
  1334. }
  1335. if (!can_pause_resume) {
  1336. return;
  1337. }
  1338. if (mot_contrl_is_cruise_enabled(&motor.controller)) {
  1339. u32 cruise_time = shark_get_seconds() - motor.cruise_time;
  1340. if ((cruise_time >= 3) && motor.cruise_torque == 0.0f) {
  1341. motor.cruise_torque = motor.controller.target_torque;
  1342. }else if ((cruise_time >= 3) && (motor.cruise_torque > 0.0f)){
  1343. float trq_req = get_user_request_torque();
  1344. if (trq_req > motor.cruise_torque * 1.2f) {
  1345. mot_contrl_pause_cruise(&motor.controller); //需要加速,暂停定速巡航
  1346. }
  1347. }
  1348. }else {
  1349. float trq_req = get_user_request_torque();
  1350. if (trq_req <= motor.cruise_torque * 1.1f) {
  1351. mot_contrl_resume_cruise(&motor.controller); //重新开始定速巡航,巡航速度还是前一次定速巡航给的速度
  1352. }
  1353. }
  1354. }else {
  1355. mot_contrl_set_cruise(&motor.controller, false);
  1356. can_pause_resume = false;
  1357. }
  1358. }
  1359. #endif
  1360. static bool mc_can_stop_foc(void) {
  1361. if (mc_critical_need_stop()) {
  1362. return true;
  1363. }
  1364. if (motor.mode == CTRL_MODE_CURRENT) {
  1365. return false;
  1366. }
  1367. if (!motor.b_cruise && !motor.b_epm && motor.mode == CTRL_MODE_SPD) {
  1368. if (motor.s_target_speed != MAX_S16 && motor.s_target_speed != 0) {
  1369. return false;
  1370. }else {
  1371. return true;
  1372. }
  1373. }
  1374. if (mc_throttle_released() && mot_contrl_get_speed(&motor.controller) == 0.0f) {
  1375. if (!mot_contrl_is_auto_holdding(&motor.controller) && motor.epm_dir == EPM_Dir_None) {
  1376. return true;
  1377. }
  1378. }
  1379. /* 启用无感观测器,但是观测器未稳定,关闭输出,滑行 */
  1380. if (!foc_observer_is_encoder() && !foc_observer_sensorless_stable()) {
  1381. return true;
  1382. }
  1383. return false;
  1384. }
  1385. static bool mc_can_restart_foc(void) {
  1386. bool can_start = (!mc_throttle_released() || (motor.epm_dir != EPM_Dir_None)) && (!mc_critical_can_not_run());
  1387. if (!foc_observer_is_encoder() && !foc_observer_sensorless_stable()){
  1388. return false;
  1389. }
  1390. if ((motor.s_target_speed != MAX_S16 && motor.s_target_speed != 0) && (!mc_critical_can_not_run()) && motor.mode == CTRL_MODE_SPD) {
  1391. return true;
  1392. }
  1393. return can_start;
  1394. }
  1395. static void mc_motor_runstop(void) __attribute__((unused));
  1396. static void mc_motor_runstop(void) {
  1397. u32 mask;
  1398. if (mot_contrl_is_start(&motor.controller) && mc_can_stop_foc()) {
  1399. mask = cpu_enter_critical();
  1400. mot_contrl_stop(&motor.controller);
  1401. pwm_disable_channel();
  1402. g_meas_foc.first = true;
  1403. cpu_exit_critical(mask);
  1404. }
  1405. if (!mot_contrl_is_start(&motor.controller) && mc_can_restart_foc()) {
  1406. mask = cpu_enter_critical();
  1407. mot_contrl_start(&motor.controller, motor.mode);
  1408. mc_gear_mode_set();
  1409. throttle_torque_reset();
  1410. pwm_enable_channel();
  1411. g_meas_foc.first = true;
  1412. cpu_exit_critical(mask);
  1413. }
  1414. }
  1415. static void mc_process_throttle_torque(float vol) __attribute__((unused));
  1416. static void mc_process_throttle_torque(float vol) {
  1417. float torque = throttle_get_torque(&motor.controller, vol);
  1418. if (mc_throttle_released()) {
  1419. #ifdef CONFIG_CRUISE_ENABLE_ACCL
  1420. if (mc_is_cruise_enabled()) {
  1421. return;
  1422. }
  1423. #endif
  1424. if (mot_contrl_energy_recovery(&motor.controller, true)) {
  1425. return;
  1426. }
  1427. }
  1428. if (motor.controller.mode_running == CTRL_MODE_TRQ) {
  1429. throttle_set_torque(&motor.controller, torque);
  1430. }else if (motor.controller.mode_running == CTRL_MODE_SPD) {
  1431. if (!mc_is_cruise_enabled()) {
  1432. float vel_ref = motor.controller.userlim.mot_vel * throttle_get_open_ration_filted();
  1433. mot_contrl_set_target_vel(&motor.controller, vel_ref);
  1434. }
  1435. }else if (motor.controller.mode_running == CTRL_MODE_EBRAKE){
  1436. float vel = mot_contrl_get_speed(&motor.controller);
  1437. float ebrk_trq = motor_get_ebreak_toruqe(vel);
  1438. if (ebrk_trq >= -mot_contrl_get_ebrk_torque(&motor.controller)/2){
  1439. mot_contrl_set_ebrk_time(&motor.controller, 1);
  1440. }
  1441. if (ebrk_trq != 0) {
  1442. mot_contrl_set_torque(&motor.controller, ebrk_trq);
  1443. }
  1444. if ((mot_contrl_get_final_torque(&motor.controller) < 0.0001f && vel < CONFIG_MIN_RPM_EXIT_EBRAKE) ||
  1445. (!mc_throttle_released() || (mc_throttle_released() && (vel == 0.0f)))) {
  1446. mot_contrl_energy_recovery(&motor.controller, false);
  1447. throttle_torque_reset();
  1448. }
  1449. }
  1450. }
  1451. /*FOC 的部分处理,比如速度环,状态机,转把采集等*/
  1452. measure_time_t g_meas_MCTask = {.exec_max_time = 100, .intval_max_time = 1000, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  1453. #define mc_TaskStart time_measure_start(&g_meas_MCTask)
  1454. #define mc_TaskEnd time_measure_end(&g_meas_MCTask)
  1455. void Sched_MC_mTask(void) {
  1456. static int vbus_err_cnt = 0;
  1457. static bool _sensorless_run = false;
  1458. mc_TaskStart;
  1459. adc_vref_filter();
  1460. throttle_detect(motor.b_start);
  1461. F_all_Calc();
  1462. #ifdef CONFIG_CRUISE_ENABLE_ACCL
  1463. mc_process_curise();
  1464. #endif
  1465. u8 runMode = mot_contrl_mode(&motor.controller);
  1466. /*保护功能*/
  1467. u8 limted = mot_contrl_protect(&motor.controller);
  1468. /* 母线电流,实际采集的相电流矢量大小的计算 */
  1469. mot_contrl_calc_current(&motor.controller);
  1470. if (factory_is_running()) {
  1471. motor.n_CritiCalErrMask = 0;
  1472. return;
  1473. }
  1474. if ((mot_contrl_get_dc_current(&motor.controller) > (CONFIG_HW_MAX_DC_CURRENT * 1.1f)) || (mot_contrl_get_dc_current(&motor.controller) < CONFIG_HW_MAX_CHRG_CURRENT)) {
  1475. vbus_err_cnt ++;
  1476. if (vbus_err_cnt >= 5) {
  1477. if (mc_set_critical_error(FOC_CRIT_IDC_OV)) {
  1478. mc_crit_err_add(FOC_CRIT_IDC_OV, (s16)sample_vbus_raw(), (s16)sample_ibus_raw());
  1479. mc_save_err_runtime();
  1480. }
  1481. }
  1482. }else {
  1483. vbus_err_cnt = 0;
  1484. }
  1485. if (mc_process_force_running()) {
  1486. mc_TaskEnd;
  1487. return;
  1488. }
  1489. bool sensor_less = !foc_observer_is_encoder();
  1490. if (mc_detect_vbus_mode() || (limted == FOC_LIM_CHANGE_L) || (_sensorless_run != sensor_less)) {
  1491. mc_gear_mode_set();
  1492. if (sensor_less && foc_observer_sensorless_stable()) {//unstable 记录在ADC中断处理中
  1493. if (motor_encoder_may_error() == ENCODER_PWM_ERR) {
  1494. mc_set_critical_error(FOC_CRIT_Encoder_Err);
  1495. mc_crit_err_add(FOC_CRIT_Encoder_Err, (s16)enc_pwm_err_ms, enc_delta_err2);
  1496. }else if (motor_encoder_may_error() == ENCODER_AB_ERR) {
  1497. mc_set_critical_error(FOC_CRIT_ENC_AB_Err);
  1498. mc_crit_err_add(FOC_CRIT_ENC_AB_Err, enc_delta_err1, enc_delta_err2);
  1499. }
  1500. }
  1501. motor.b_limit_pending = false;
  1502. }else if (limted == FOC_LIM_CHANGE_H) {
  1503. motor.b_limit_pending = true;
  1504. }
  1505. _sensorless_run = sensor_less;
  1506. /* 如果取消高温,欠压等限流需要释放转把后才生效,确保不会突然加速 */
  1507. if (motor.b_limit_pending && mc_throttle_released()) {
  1508. motor.b_limit_pending = false;
  1509. mc_gear_mode_set();
  1510. }
  1511. /* 堵转处理 */
  1512. if (mc_run_stall_process(runMode) || (motor.mode == CTRL_MODE_CURRENT)) {
  1513. #ifndef CONFIG_DQ_STEP_RESPONSE
  1514. mot_contrl_slow_task(&motor.controller);
  1515. mc_motor_runstop();
  1516. #endif
  1517. if (motor.b_ind_start) {
  1518. mot_params_flux_stop();
  1519. }
  1520. mc_TaskEnd;
  1521. return;
  1522. }
  1523. mc_process_brake_light();
  1524. if ((runMode != CTRL_MODE_OPEN) || (motor.mode != CTRL_MODE_OPEN)) {
  1525. #ifndef CONFIG_DQ_STEP_RESPONSE
  1526. mc_autohold_process();
  1527. mc_turbo_auto_exit();
  1528. if (motor.mode != CTRL_MODE_OPEN) {
  1529. mc_motor_runstop();
  1530. }
  1531. if (runMode != CTRL_MODE_OPEN) {
  1532. if (runMode == CTRL_MODE_SPD) {
  1533. if (mc_is_epm()) {
  1534. mc_process_throttle_epm();
  1535. mc_process_epm_move();
  1536. }else if (motor.s_target_speed != MAX_S16) {
  1537. mot_contrl_set_target_vel(&motor.controller, motor.s_target_speed);
  1538. }
  1539. }else {
  1540. if (motor.s_force_torque != MAX_S16) {
  1541. mot_contrl_set_force_torque(&motor.controller, motor.s_force_torque);
  1542. }else {
  1543. float thro = throttle_get_signal();
  1544. if (motor.b_ignor_throttle) {
  1545. float r = (float)motor.u_throttle_ration/100.0f;
  1546. thro = throttle_opening_to_vol(r);
  1547. }
  1548. mc_process_throttle_torque(thro);
  1549. }
  1550. }
  1551. mot_contrl_slow_task(&motor.controller);
  1552. }
  1553. #endif
  1554. }
  1555. mc_TaskEnd;
  1556. }