motor.c 49 KB

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