motor.c 51 KB

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