motor.c 47 KB

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