motor.c 45 KB

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