motor.c 47 KB

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