motor.c 45 KB

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