motor.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  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. mot_contrl_set_torque_limit_rttime(&motor.controller, CONFIG_LIMIT_RAMP_TIME);
  585. mot_contrl_set_vel_rttime(&motor.controller, CONFIG_CRUISE_RAMP_TIME);
  586. mc_gear_vmode_changed();
  587. }
  588. cpu_exit_critical(mask);
  589. return false;
  590. }
  591. bool mc_is_epm(void) {
  592. return motor.b_epm;
  593. }
  594. bool mc_is_start(void) {
  595. return (motor.b_start || mot_contrl_is_start(&motor.controller));
  596. }
  597. bool mc_start_epm_move(epm_dir_t dir, bool is_command) {
  598. if (!motor.b_epm || !motor.b_start) {
  599. mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
  600. return false;
  601. }
  602. if ((dir == motor.epm_dir) && (is_command == motor.b_epm_cmd_move)) {
  603. return true;
  604. }
  605. u32 mask = cpu_enter_critical();
  606. if (motor.epm_dir != dir) {
  607. motor.f_epm_vel = 0.0f;
  608. motor.f_epm_trq = 0.0f;
  609. }
  610. motor.epm_dir = dir;
  611. if (dir != EPM_Dir_None) {
  612. motor.b_epm_cmd_move = is_command;
  613. if (!mot_contrl_is_start(&motor.controller)) {
  614. mot_contrl_start(&motor.controller, motor.mode);
  615. mc_gear_vmode_changed();
  616. pwm_enable_channel();
  617. }else if (mot_contrl_is_auto_holdding(&motor.controller)) {
  618. mc_auto_hold(false);
  619. }
  620. if (dir == EPM_Dir_Back) {
  621. mot_contrl_velloop_params(&motor.controller, 0.2f, 7.5f);
  622. }else {
  623. mot_contrl_velloop_params(&motor.controller, 0.2f, 7.5f);
  624. }
  625. mot_contrl_set_torque_limit_rttime(&motor.controller, 2.0);
  626. mot_contrl_set_torque_limit(&motor.controller, motor.f_epm_trq);
  627. mot_contrl_set_vel_rttime(&motor.controller, 1000.0f);
  628. mot_contrl_set_target_vel(&motor.controller, motor.f_epm_vel);
  629. }else {
  630. motor.b_epm_cmd_move = false;
  631. mot_contrl_velloop_params(&motor.controller, mc_conf()->c.pid[PID_Vel_ID].kp, mc_conf()->c.pid[PID_Vel_ID].ki);
  632. mot_contrl_set_target_vel(&motor.controller, 0);
  633. }
  634. cpu_exit_critical(mask);
  635. return true;
  636. }
  637. void mc_set_fan_duty(u8 duty) {
  638. sys_debug("fan duty %d\n", duty);
  639. if (!fan_pwm_is_running() && duty > 0) {
  640. motor.fan[0].start_ts = get_tick_ms();
  641. motor.fan[1].start_ts = get_tick_ms();
  642. shark_timer_post(&_fan_det_timer1, 5000);
  643. shark_timer_post(&_fan_det_timer2, 5000);
  644. }else if (duty == 0) {
  645. shark_timer_cancel(&_fan_det_timer1);
  646. shark_timer_cancel(&_fan_det_timer2);
  647. }
  648. fan_set_duty(duty);
  649. }
  650. bool mc_command_epm_move(epm_dir_t dir) {
  651. return mc_start_epm_move(dir, true);
  652. }
  653. bool mc_throttle_epm_move(epm_dir_t dir) {
  654. return mc_start_epm_move(dir, false);
  655. }
  656. void mc_set_throttle_r(bool use, u8 r) {
  657. motor.u_throttle_ration = r;
  658. motor.b_ignor_throttle = use;
  659. if (motor.b_ignor_throttle) {
  660. mc_clr_critical_error(FOC_CRIT_THRO_Err);
  661. mc_clr_critical_error(FOC_CRIT_THRO2_Err);
  662. }
  663. }
  664. void mc_use_throttle(void) {
  665. motor.b_ignor_throttle = false;
  666. }
  667. void mc_get_running_status(u8 *data) {
  668. data[0] = motor.mode;
  669. data[0] |= (mot_contrl_is_auto_holdding(&motor.controller)?1:0) << 2;
  670. data[0] |= (motor.b_break?1:0) << 3;
  671. data[0] |= (motor.b_cruise?1:0) << 4;
  672. data[0] |= (motor.b_start?1:0) << 5;
  673. data[0] |= (mc_is_epm()?1:0) << 6;
  674. data[0] |= (motor.b_lock_motor) << 7; //motor locked
  675. }
  676. u16 mc_get_running_status2(void) {
  677. u16 data = 0;
  678. data = motor.b_start?1:0;
  679. data |= (motor.n_gear & 0x7) << 1;
  680. data |= (mot_contrl_is_auto_holdding(&motor.controller)?1:0) << 3;
  681. data |= (motor.b_break?1:0) << 4;
  682. data |= (motor.b_cruise?1:0) << 5;
  683. data |= (mc_is_epm()?1:0) << 6;
  684. data |= (motor.b_lock_motor) << 7; //motor locked
  685. data |= (mot_contrl_ebrk_is_running(&motor.controller)?1:0) << 8; //能量回收运行标志
  686. data |= ((motor.n_CritiCalErrMask != 0)?1:0) << 9;
  687. data |= (fan_pwm_is_running()?1:0) << 10; //风扇是否运行
  688. data |= (motor.b_runStall?1:0) << 11; //是否堵转
  689. data |= (mot_contrl_dccurr_is_protected(&motor.controller)?1:0) << 12; //是否欠压限制母线电流
  690. data |= (mot_contrl_torque_is_protected(&motor.controller)?1:0) << 13; //是否高温限扭矩
  691. data |= (etcs_is_running(&motor.controller.etcs)?1:0) << 14; //电子tcs是否正在工作
  692. data |= (throttle_not_released_err()?1:0) << 15;
  693. return data;
  694. }
  695. static float _force_angle = 0.0f;
  696. static int _force_wait = 2000;
  697. /* 开环,强制给定电角度和DQ的电压 */
  698. void mc_force_run_open(s16 vd, s16 vq, bool align) {
  699. if (motor.b_start || motor.b_force_run) {
  700. if (vd == 0 && vq == 0) {
  701. mot_contrl_set_vdq(&motor.controller, 0, 0);
  702. delay_ms(500);
  703. wdog_reload();
  704. adc_stop_convert();
  705. pwm_stop();
  706. mot_contrl_stop(&motor.controller);
  707. pwm_up_enable(true);
  708. motor.b_force_run = false;
  709. motor.b_ignor_throttle = false;
  710. }
  711. return;
  712. }
  713. if (vd == 0 && vq == 0) {
  714. return;
  715. }
  716. motor.b_ignor_throttle = true;
  717. MC_Check_MosVbusThrottle();
  718. if (mc_unsafe_critical_error()) {
  719. mot_contrl_set_error(&motor.controller, FOC_Have_CritiCal_Err);
  720. }
  721. pwm_up_enable(false);
  722. pwm_turn_on_low_side();
  723. task_udelay(500);
  724. mot_contrl_start(&motor.controller, CTRL_MODE_OPEN);
  725. phase_current_offset_calibrate();
  726. pwm_start();
  727. adc_start_convert();
  728. pwm_enable_channel();
  729. phase_current_calibrate_wait();
  730. mot_contrl_set_angle(&motor.controller, 0);
  731. mot_contrl_set_vdq(&motor.controller, (float)vd, 0);
  732. if (align) {
  733. _force_wait = 2000 + 1;
  734. }else {
  735. _force_wait = 2000;
  736. }
  737. motor.b_force_run = true;
  738. }
  739. bool mc_ind_motor_start(bool start) {
  740. if (start == motor.b_ind_start) {
  741. return true;
  742. }
  743. if (start) {
  744. motor.b_ignor_throttle = true;
  745. MC_Check_MosVbusThrottle();
  746. if (mc_unsafe_critical_error() || mot_params_flux_pending()) {
  747. mot_contrl_set_error(&motor.controller, FOC_Have_CritiCal_Err);
  748. return false;
  749. }
  750. pwm_up_enable(false);
  751. pwm_turn_on_low_side();
  752. task_udelay(500);
  753. mot_contrl_start(&motor.controller, CTRL_MODE_OPEN);
  754. phase_current_offset_calibrate();
  755. pwm_start();
  756. adc_start_convert();
  757. pwm_enable_channel();
  758. phase_current_calibrate_wait();
  759. mot_contrl_set_vdq_immediate(&motor.controller, 0, 0);
  760. motor.b_ind_start = start;
  761. }else {
  762. u32 mask = cpu_enter_critical();
  763. motor.b_ind_start = start;
  764. mot_contrl_set_vdq(&motor.controller, 0, 0);
  765. cpu_exit_critical(mask);
  766. delay_us(500);
  767. wdog_reload();
  768. adc_stop_convert();
  769. pwm_stop();
  770. mot_contrl_stop(&motor.controller);
  771. motor.mode = CTRL_MODE_OPEN;
  772. pwm_up_enable(true);
  773. motor.b_ignor_throttle = false;
  774. }
  775. return true;
  776. }
  777. static void _encoder_zero_off_timer_handler(shark_timer_t *t){
  778. if (!motor.b_calibrate) {
  779. return;
  780. }
  781. float enc_off = 0.0f;
  782. float phase = motor_encoder_zero_phase_detect(&enc_off);
  783. mot_contrl_set_vdq(&motor.controller, 0, 0);
  784. delay_ms(50);
  785. adc_stop_convert();
  786. pwm_stop();
  787. mot_contrl_stop(&motor.controller);
  788. _mc_internal_init(CTRL_MODE_OPEN, false);
  789. motor.b_calibrate = false;
  790. }
  791. bool mc_encoder_zero_calibrate(s16 vd) {
  792. if (motor.b_calibrate) {
  793. if (vd == 0) {
  794. encoder_clear_cnt_offset();
  795. shark_timer_cancel(&_encoder_zero_off_timer);
  796. mot_contrl_set_vdq(&motor.controller, 0, 0);
  797. delay_ms(500);
  798. adc_stop_convert();
  799. pwm_stop();
  800. mot_contrl_stop(&motor.controller);
  801. _mc_internal_init(CTRL_MODE_OPEN, false);
  802. motor.b_calibrate = false;
  803. motor.b_ignor_throttle = false;
  804. }
  805. return true;
  806. }
  807. encoder_clear_cnt_offset();
  808. motor.b_ignor_throttle = true;
  809. MC_Check_MosVbusThrottle();
  810. if (mc_unsafe_critical_error()) {
  811. mot_contrl_set_error(&motor.controller, FOC_Have_CritiCal_Err);
  812. return false;
  813. }
  814. _mc_internal_init(CTRL_MODE_OPEN, true);
  815. motor.b_calibrate = true;
  816. pwm_turn_on_low_side();
  817. task_udelay(500);
  818. mot_contrl_start(&motor.controller, CTRL_MODE_OPEN);
  819. phase_current_offset_calibrate();
  820. pwm_start();
  821. adc_start_convert();
  822. pwm_enable_channel();
  823. phase_current_calibrate_wait();
  824. mot_contrl_set_angle(&motor.controller, 0);
  825. mot_contrl_set_vdq(&motor.controller, vd, 0);
  826. shark_timer_post(&_encoder_zero_off_timer, 6*1000);
  827. return true;
  828. }
  829. bool mc_current_sensor_calibrate(float current) {
  830. if (!mc_start(CTRL_MODE_OPEN)) {
  831. return false;
  832. }
  833. phase_current_sensor_start_calibrate(current);
  834. phase_current_calibrate_wait();
  835. return true;
  836. }
  837. bool mc_lock_motor(bool lock) {
  838. if (motor.b_lock_motor == lock) {
  839. return true;
  840. }
  841. int ret = true;
  842. u32 mask = cpu_enter_critical();
  843. if (motor.b_start) {
  844. mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
  845. ret = false;
  846. goto ml_ex_cri;
  847. }
  848. if (lock && (motor_encoder_get_speed() >= CONFIG_LOCK_MOTOR_MIN_RPM)) {
  849. mot_contrl_set_error(&motor.controller, FOC_NowAllowed_With_Speed);
  850. ret = false;
  851. goto ml_ex_cri;
  852. }
  853. motor.b_lock_motor = lock;
  854. if (lock) {
  855. pwm_start();
  856. pwm_update_duty(0, 0, 0);
  857. pwm_enable_channel();
  858. }else {
  859. pwm_stop();
  860. }
  861. ml_ex_cri:
  862. cpu_exit_critical(mask);
  863. return ret;
  864. }
  865. bool mc_auto_hold(bool hold) {
  866. if (motor.b_auto_hold == hold) {
  867. return true;
  868. }
  869. if (!mc_conf()->s.auto_hold) {
  870. mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
  871. return false;
  872. }
  873. if (!motor.b_start) {
  874. mot_contrl_set_error(&motor.controller, FOC_NotAllowed);
  875. return false;
  876. }
  877. if (hold && !mc_throttle_released()) {
  878. mot_contrl_set_error(&motor.controller, FOC_Throttle_Err);
  879. return false;
  880. }
  881. u32 mask = cpu_enter_critical();
  882. motor.b_auto_hold = hold;
  883. if (!mot_contrl_is_start(&motor.controller)) {
  884. mot_contrl_start(&motor.controller, motor.mode);
  885. mot_contrl_set_autohold(&motor.controller, hold);
  886. pwm_enable_channel();
  887. }else {
  888. mot_contrl_set_autohold(&motor.controller, hold);
  889. }
  890. cpu_exit_critical(mask);
  891. return true;
  892. }
  893. bool mc_set_critical_error(u8 err) {
  894. if (mc_critical_err_is_set(err)) {
  895. return false;
  896. }
  897. motor.n_CritiCalErrMask |= (1u << err);
  898. return true;
  899. }
  900. void mc_clr_critical_error(u8 err) {
  901. motor.n_CritiCalErrMask &= ~(1u << err);
  902. }
  903. bool mc_critical_err_is_set(u8 err) {
  904. u32 mask = (1u << err);
  905. return (motor.n_CritiCalErrMask & mask) != 0;
  906. }
  907. u32 mc_get_critical_error(void) {
  908. return motor.n_CritiCalErrMask;
  909. }
  910. bool mc_throttle_released(void) {
  911. if (motor.b_ignor_throttle) {
  912. return motor.u_throttle_ration == 0;
  913. }
  914. return throttle_is_released();
  915. }
  916. static bool mc_is_gpio_mlock(void) {
  917. int count = 50;
  918. int settimes = 0;
  919. while(count-- > 0) {
  920. bool b1 = gpio_motor_locked();
  921. if (b1) {
  922. settimes ++;
  923. }
  924. delay_us(1);
  925. }
  926. if (settimes == 0) {
  927. return false;
  928. }else if (settimes == 50) {
  929. return true;
  930. }
  931. //有干扰,do nothing
  932. return false;
  933. }
  934. static bool _mc_is_hwbrake(void) {
  935. int count = 50;
  936. int settimes = 0;
  937. while(count-- > 0) {
  938. bool b1 = mc_get_gpio_brake() || mc_get_gpio_brake1();
  939. if (b1) {
  940. settimes ++;
  941. }
  942. delay_us(1);
  943. }
  944. if (settimes == 0) {
  945. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  946. return true;
  947. #else
  948. return false;
  949. #endif
  950. }else if (settimes == 50) {
  951. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  952. return false;
  953. #else
  954. return true;
  955. #endif
  956. }
  957. //有干扰,do nothing
  958. motor.n_brake_errors++;
  959. return false;
  960. }
  961. static bool mc_detect_hwbrake(void) {
  962. motor.b_break = _mc_is_hwbrake();
  963. return motor.b_break;
  964. }
  965. static void _fan_det_timer_handler(shark_timer_t *t) {
  966. if (t == &_fan_det_timer1) {
  967. motor.fan[0].rpm = 0;
  968. motor.fan[0].det_ts = 0;
  969. }else {
  970. motor.fan[1].rpm = 0;
  971. motor.fan[1].det_ts = 0;
  972. }
  973. }
  974. void Fan_IRQHandler(int idx) {
  975. fan_t *fan = motor.fan + idx;
  976. u32 pre_ts = fan->det_ts;
  977. u32 delta_ts = get_delta_ms(pre_ts);
  978. fan->det_ts = get_tick_ms();
  979. float rpm = 60.0f * 1000 / (float)delta_ts;
  980. LowPass_Filter(fan->rpm, rpm, 0.1f);
  981. if (idx == 0) {
  982. shark_timer_post(&_fan_det_timer1, 100);
  983. }else {
  984. shark_timer_post(&_fan_det_timer2, 100);
  985. }
  986. }
  987. void MC_Brake_IRQHandler(void) {
  988. mc_detect_hwbrake();
  989. if (!motor.b_start) {
  990. return;
  991. }
  992. if (motor.b_break) {
  993. mc_enable_cruise(false);
  994. mot_contrl_set_hw_brake(&motor.controller, true);
  995. }else {
  996. mot_contrl_set_hw_brake(&motor.controller, false);
  997. }
  998. }
  999. static void _pwm_brake_prot_timer_handler(shark_timer_t *t){
  1000. pwm_brake_enable(true);
  1001. sys_debug("MC protect error\n");
  1002. }
  1003. static void mc_save_err_runtime(void) {
  1004. mc_error.vbus_x10 = (s16)(sample_vbus_raw() * 10.0f);
  1005. mc_error.ibus_x10 = (s16)(sample_ibus_raw() * 10.0f);
  1006. mc_error.vacc_x10 = (s16) (sample_acc_vol_raw() * 10.0f);
  1007. mc_error.id_ref_x10 = (s16)(motor.controller.foc.in.target_id.interpolation * 10.0f);
  1008. mc_error.iq_ref_x10 = (s16)(motor.controller.foc.in.target_iq.interpolation * 10.0f);
  1009. mc_error.id_x10 = (s16)(motor.controller.foc.out.curr_dq.d * 10.0f);
  1010. mc_error.iq_x10 = (s16)(motor.controller.foc.out.curr_dq.q * 10.0f);
  1011. mc_error.vd_x10 = (s16)(motor.controller.foc.out.vol_dq.d * 10.0f);
  1012. mc_error.vq_x10 = (s16)(motor.controller.foc.out.vol_dq.q * 10.0f);
  1013. mc_error.torque_ref_x10 = (s16)(motor.controller.target_torque * 10.0f);
  1014. mc_error.run_mode = motor.controller.mode_running;
  1015. mc_error.rpm = (s16)motor_encoder_get_speed();
  1016. mc_error.b_sensorless = !foc_observer_is_encoder();
  1017. mc_error.b_sensorless_stable = foc_observer_sensorless_stable();
  1018. mc_error.mos_temp = get_mos_temp_raw();
  1019. mc_error.mot_temp = get_motor_temp_raw();
  1020. mc_error.enc_error = motor_encoder_may_error();
  1021. mc_error.sensorless_rpm = (s16)foc_observer_sensorless_speed();
  1022. mc_err_runtime_add(&mc_error);
  1023. }
  1024. void MC_Protect_IRQHandler(void){
  1025. pwm_brake_enable(false);
  1026. shark_timer_post(&_brake_prot_timer, 1000);
  1027. if (!motor.b_start) {
  1028. return;
  1029. }
  1030. mc_set_critical_error(FOC_CRIT_Phase_Err);
  1031. mc_save_err_runtime();
  1032. _mc_internal_init(CTRL_MODE_OPEN, false);
  1033. adc_stop_convert();
  1034. pwm_stop();
  1035. mot_contrl_stop(&motor.controller);
  1036. pwm_up_enable(true);
  1037. }
  1038. void motor_debug(void) {
  1039. if (!mc_unsafe_critical_error()) {
  1040. return;
  1041. }
  1042. 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);
  1043. 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);
  1044. 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);
  1045. }
  1046. static void motor_vbus_crit_low(s16 curr_vbus) {
  1047. static u16 _vbus_e_count = 0;
  1048. if (curr_vbus < motor.s_vbus_hw_min) {
  1049. _vbus_e_count ++;
  1050. if (_vbus_e_count >= 2) {
  1051. if (mot_contrl_is_start(&motor.controller)) {
  1052. pwm_disable_channel();
  1053. mc_save_err_runtime();
  1054. mot_contrl_stop(&motor.controller);
  1055. }
  1056. if (mc_set_critical_error(FOC_CRIT_Vol_HW_Err)) {
  1057. if (mot_contrl_get_speed(&motor.controller) > CONFIG_ZERO_SPEED_RPM) {
  1058. mc_crit_err_add_s16(FOC_CRIT_Vol_HW_Err, curr_vbus);
  1059. }
  1060. }
  1061. }
  1062. }else {
  1063. _vbus_e_count = 0;
  1064. }
  1065. }
  1066. void TIMER_UP_IRQHandler(void){
  1067. if (!motor.b_start && !mot_contrl_is_start(&motor.controller)) {
  1068. motor_encoder_update(false);
  1069. motor_vbus_crit_low((s16)get_vbus_int());
  1070. }
  1071. }
  1072. measure_time_t g_meas_foc = {.exec_max_time = 25, .intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  1073. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  1074. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  1075. #if (CONFIG_ENABLE_IAB_REC==1)
  1076. #define CONFIG_IAB_REC_COUNT 1000
  1077. static s16 ia[CONFIG_IAB_REC_COUNT], ib[CONFIG_IAB_REC_COUNT];
  1078. static int iab_w_count = 0, iab_r_count = 0;
  1079. static bool b_iab_rec = false;
  1080. extern void can_plot2(s16 v1, s16 v2);
  1081. #endif
  1082. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  1083. void ADC_IRQHandler(void) {
  1084. if (phase_current_offset()) {//check if is adc offset checked
  1085. return;
  1086. }
  1087. if (phase_current_sensor_do_calibrate()){
  1088. pwm_update_duty(100, FOC_PWM_Half_Period-100, 100);
  1089. pwm_update_sample(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period+1, PHASE_BC);
  1090. return;
  1091. }
  1092. TIME_MEATURE_START();
  1093. #if (CONFIG_ENABLE_IAB_REC==1)
  1094. if (b_iab_rec && (iab_w_count < CONFIG_IAB_REC_COUNT)) {
  1095. ia[iab_w_count] = (s16)motor.controller.foc.in.curr_abc[0];
  1096. ib[iab_w_count] = (s16)motor.controller.foc.in.curr_abc[1];
  1097. iab_w_count ++;
  1098. }
  1099. #endif
  1100. motor_vbus_crit_low((s16)sample_vbus_raw()); //need fast detect vbus very low, to stop the motor
  1101. float vd, vq;
  1102. if (motor.b_ind_start) {
  1103. mot_params_high_freq_inject();
  1104. vd = motor.controller.foc.out.vol_dq.d;
  1105. vq = motor.controller.foc.out.vol_dq.q;
  1106. }
  1107. if (!mot_contrl_update(&motor.controller)) {/* FOC 角度错误,立即停机 */
  1108. if (mot_contrl_is_start(&motor.controller)) {
  1109. pwm_disable_channel();
  1110. /* 记录错误 */
  1111. if (!foc_observer_is_force_sensorless()) {
  1112. mc_save_err_runtime();
  1113. }
  1114. mot_contrl_stop(&motor.controller);
  1115. g_meas_foc.first = true;
  1116. if (!foc_observer_is_force_sensorless()) {
  1117. if (mc_set_critical_error(FOC_CRIT_Angle_Err)) {
  1118. mc_crit_err_add_s16(FOC_CRIT_Angle_Err, (s16)motor_encoder_get_speed());
  1119. }
  1120. if (motor_encoder_may_error() == ENCODER_PWM_ERR) {
  1121. if (mc_set_critical_error(FOC_CRIT_Encoder_Err)) {
  1122. mc_crit_err_add(FOC_CRIT_Encoder_Err, (s16)enc_pwm_err_ms, enc_delta_err2);
  1123. }
  1124. }else if (motor_encoder_may_error() == ENCODER_AB_ERR) {
  1125. if (mc_set_critical_error(FOC_CRIT_ENC_AB_Err)) {
  1126. mc_crit_err_add(FOC_CRIT_ENC_AB_Err, enc_delta_err1, enc_delta_err2);
  1127. }
  1128. }
  1129. }
  1130. }
  1131. }
  1132. if (motor.b_ind_start) {
  1133. float id = motor.controller.foc.out.curr_dq.d;
  1134. float iq = motor.controller.foc.out.curr_dq.q;
  1135. mot_params_hj_sample_vi(vd, vq, id, iq);
  1136. }
  1137. TIME_MEATURE_END();
  1138. }
  1139. #if (CONFIG_ENABLE_IAB_REC==1)
  1140. static void _iab_plot_timer_handler(shark_timer_t *t) {
  1141. if (!b_iab_rec) {
  1142. return;
  1143. }
  1144. if (iab_r_count < iab_w_count) {
  1145. can_plot2(ia[iab_r_count], ib[iab_r_count]);
  1146. iab_r_count ++;
  1147. shark_timer_post(t, 10);
  1148. }
  1149. }
  1150. static shark_timer_t _iab_plot_timer = TIMER_INIT(_iab_plot_timer, _iab_plot_timer_handler);
  1151. void mc_start_current_rec(bool rec) {
  1152. if (b_iab_rec == rec) {
  1153. return;
  1154. }
  1155. if (!rec) {
  1156. b_iab_rec = false;
  1157. shark_timer_cancel(&_iab_plot_timer);
  1158. return;
  1159. }
  1160. iab_w_count = 0;
  1161. iab_r_count = 0;
  1162. b_iab_rec = true;
  1163. shark_timer_post(&_iab_plot_timer, 100);
  1164. }
  1165. #endif
  1166. static bool mc_run_stall_process(u8 run_mode) {
  1167. if ((run_mode == CTRL_MODE_TRQ || run_mode == CTRL_MODE_SPD) && !mot_contrl_is_auto_holdding(&motor.controller)) {
  1168. //堵转判断
  1169. if (motor.b_runStall) {
  1170. if (!mc_throttle_released()) {
  1171. return true;
  1172. }
  1173. motor.runStall_time = 0;
  1174. motor.b_runStall = false; //转把释放,清除堵转标志
  1175. }else if (mot_contrl_get_current_vector(&motor.controller) >= CONFIG_STALL_MAX_CURRENT){
  1176. if (ABS(mot_contrl_get_speed(&motor.controller)) < 1.0f && (motor.runStall_time == 0)) {
  1177. motor.runStall_time = get_tick_ms();
  1178. motor.runStall_pos = motor_encoder_get_position();
  1179. }
  1180. if (motor.runStall_time > 0) {
  1181. if (get_delta_ms(motor.runStall_time) >= CONFIG_STALL_MAX_TIME) {
  1182. motor.b_runStall = true;
  1183. motor.runStall_time = 0;
  1184. mot_contrl_set_torque(&motor.controller, 0);
  1185. throttle_torque_reset();
  1186. return true;
  1187. }
  1188. if (ABS(motor.runStall_pos - motor_encoder_get_position()) >= 0.2f) {
  1189. motor.runStall_time = 0;
  1190. }
  1191. }
  1192. }else {
  1193. motor.runStall_time = 0;
  1194. }
  1195. }
  1196. return false;
  1197. }
  1198. static void mc_autohold_process(void) {
  1199. if (!mc_conf()->s.auto_hold) {
  1200. if (mot_contrl_is_auto_holdding(&motor.controller)) {
  1201. mc_auto_hold(false);
  1202. }
  1203. return;
  1204. }
  1205. if (mot_contrl_is_auto_holdding(&motor.controller)) {
  1206. if (!mc_throttle_released()) {
  1207. mc_auto_hold(false);
  1208. motor.b_wait_brk_release = false;
  1209. }else if (!motor.b_break && motor.b_wait_brk_release) {
  1210. motor.b_wait_brk_release = false;
  1211. }else if (motor.b_break && !motor.b_wait_brk_release) {
  1212. mc_auto_hold(false);
  1213. }
  1214. }
  1215. if (!mot_contrl_is_auto_holdding(&motor.controller) && motor.b_break && (motor_encoder_get_speed() == 0)) {
  1216. if (motor.n_autohold_time == 0) {
  1217. motor.n_autohold_time = get_tick_ms();
  1218. }else {
  1219. if (get_delta_ms(motor.n_autohold_time) >= CONFIG_AUTOHOLD_DETECT_TIME) {
  1220. if (mc_auto_hold(true)) {
  1221. motor.b_wait_brk_release = true;
  1222. }
  1223. }
  1224. }
  1225. }else {
  1226. motor.n_autohold_time = 0;
  1227. }
  1228. }
  1229. static void mc_process_throttle_epm(void) {
  1230. if (!motor.b_epm_cmd_move) {//通过命令前进后退,不处理转把
  1231. if (mc_throttle_released()) {
  1232. mc_throttle_epm_move(EPM_Dir_None);
  1233. }else {
  1234. mc_throttle_epm_move(EPM_Dir_Forward);
  1235. }
  1236. }
  1237. }
  1238. static void mc_process_epm_move(void) {
  1239. if (!motor.b_epm || (motor.epm_dir == EPM_Dir_None)){
  1240. return;
  1241. }
  1242. float target_vel = mc_conf()->c.max_epm_rpm;
  1243. float target_trq = mc_conf()->c.max_epm_torque;
  1244. if (motor.epm_dir == EPM_Dir_Back) {
  1245. target_vel = -mc_conf()->c.max_epm_back_rpm;
  1246. target_trq = mc_conf()->c.max_epm_back_torque;
  1247. }else if (!motor.b_epm_cmd_move) {
  1248. target_vel = throttle_vol_to_open_ration(throttle_get_signal()) * 2.0f * (float)target_vel;
  1249. }
  1250. motor.f_epm_trq = target_trq;
  1251. motor.f_epm_vel = target_vel;
  1252. mot_contrl_set_torque_limit(&motor.controller, motor.f_epm_trq);
  1253. mot_contrl_set_target_vel(&motor.controller, motor.f_epm_vel);
  1254. }
  1255. static bool mc_process_force_running(void) {
  1256. if (motor.b_calibrate || (motor.mode == CTRL_MODE_OPEN)) {
  1257. if (motor.b_force_run && _force_wait <= 2000) {
  1258. if (_force_wait > 0) {
  1259. --_force_wait;
  1260. }else {
  1261. _force_angle += 1.5f;
  1262. rand_angle(_force_angle);
  1263. mot_contrl_set_angle(&motor.controller, _force_angle);
  1264. }
  1265. }
  1266. return true;
  1267. }
  1268. return false;
  1269. }
  1270. static void mc_process_brake_light(void) {
  1271. bool can_lighting = false;
  1272. 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)) {
  1273. can_lighting = true;
  1274. }
  1275. gpio_brk_light_enable(can_lighting);
  1276. }
  1277. #ifdef CONFIG_CRUISE_ENABLE_ACCL
  1278. static void mc_process_curise(void) {
  1279. static bool can_pause_resume = false;
  1280. if (motor.b_cruise) {
  1281. if (mot_contrl_get_speed(&motor.controller) < CONFIG_CRUISE_EXIT_RPM) {
  1282. mot_contrl_set_cruise(&motor.controller, false);
  1283. return;
  1284. }
  1285. /* 定速巡航模式下,必须转把归位后才能开始通过拧动转把加速 */
  1286. if (mc_throttle_released() && !can_pause_resume) {
  1287. can_pause_resume = true;
  1288. }
  1289. if (!can_pause_resume) {
  1290. return;
  1291. }
  1292. if (mot_contrl_is_cruise_enabled(&motor.controller)) {
  1293. u32 cruise_time = shark_get_seconds() - motor.cruise_time;
  1294. if ((cruise_time >= 3) && motor.cruise_torque == 0.0f) {
  1295. motor.cruise_torque = motor.controller.target_torque;
  1296. }else if ((cruise_time >= 3) && (motor.cruise_torque > 0.0f)){
  1297. float trq_req = get_user_request_torque();
  1298. if (trq_req > motor.cruise_torque * 1.2f) {
  1299. mot_contrl_pause_cruise(&motor.controller); //需要加速,暂停定速巡航
  1300. }
  1301. }
  1302. }else {
  1303. float trq_req = get_user_request_torque();
  1304. if (trq_req <= motor.cruise_torque * 1.1f) {
  1305. mot_contrl_resume_cruise(&motor.controller); //重新开始定速巡航,巡航速度还是前一次定速巡航给的速度
  1306. }
  1307. }
  1308. }else {
  1309. mot_contrl_set_cruise(&motor.controller, false);
  1310. can_pause_resume = false;
  1311. }
  1312. }
  1313. #endif
  1314. #ifndef CONFIG_DQ_STEP_RESPONSE
  1315. static bool mc_can_stop_foc(void) {
  1316. if (mc_critical_need_stop()) {
  1317. return true;
  1318. }
  1319. if (motor.mode == CTRL_MODE_CURRENT) {
  1320. return false;
  1321. }
  1322. if (!motor.b_cruise && !motor.b_epm && motor.mode == CTRL_MODE_SPD) {
  1323. if (motor.s_target_speed != MAX_S16 && motor.s_target_speed != 0) {
  1324. return false;
  1325. }else {
  1326. return true;
  1327. }
  1328. }
  1329. if (mc_throttle_released() && mot_contrl_get_speed(&motor.controller) == 0.0f) {
  1330. if (!mot_contrl_is_auto_holdding(&motor.controller) && motor.epm_dir == EPM_Dir_None) {
  1331. return true;
  1332. }
  1333. }
  1334. /* 启用无感观测器,但是观测器未稳定,关闭输出,滑行 */
  1335. if (!foc_observer_is_encoder() && !foc_observer_sensorless_stable()) {
  1336. return true;
  1337. }
  1338. return false;
  1339. }
  1340. static bool mc_can_restart_foc(void) {
  1341. bool can_start = (!mc_throttle_released() || (motor.epm_dir != EPM_Dir_None)) && (!mc_critical_can_not_run());
  1342. if (!foc_observer_is_encoder() && !foc_observer_sensorless_stable()){
  1343. return false;
  1344. }
  1345. if ((motor.s_target_speed != MAX_S16 && motor.s_target_speed != 0) && (!mc_critical_can_not_run()) && motor.mode == CTRL_MODE_SPD) {
  1346. return true;
  1347. }
  1348. return can_start;
  1349. }
  1350. #endif
  1351. static void mc_motor_runstop(void) {
  1352. u32 mask;
  1353. if (mot_contrl_is_start(&motor.controller) && mc_can_stop_foc()) {
  1354. mask = cpu_enter_critical();
  1355. mot_contrl_stop(&motor.controller);
  1356. pwm_disable_channel();
  1357. g_meas_foc.first = true;
  1358. cpu_exit_critical(mask);
  1359. }
  1360. if (!mot_contrl_is_start(&motor.controller) && mc_can_restart_foc()) {
  1361. mask = cpu_enter_critical();
  1362. mot_contrl_start(&motor.controller, motor.mode);
  1363. mc_gear_vmode_changed();
  1364. throttle_torque_reset();
  1365. pwm_enable_channel();
  1366. g_meas_foc.first = true;
  1367. cpu_exit_critical(mask);
  1368. }
  1369. }
  1370. static void mc_process_throttle_torque(float vol) {
  1371. float torque = throttle_get_torque(&motor.controller, vol);
  1372. if (mc_throttle_released()) {
  1373. #ifdef CONFIG_CRUISE_ENABLE_ACCL
  1374. if (mc_is_cruise_enabled()) {
  1375. return;
  1376. }
  1377. #endif
  1378. if (mot_contrl_energy_recovery(&motor.controller, true)) {
  1379. return;
  1380. }
  1381. }
  1382. if (motor.controller.mode_running == CTRL_MODE_TRQ) {
  1383. throttle_set_torque(&motor.controller, torque);
  1384. }else if (motor.controller.mode_running == CTRL_MODE_SPD) {
  1385. if (!mc_is_cruise_enabled()) {
  1386. float vel_ref = motor.controller.userlim.mot_vel * throttle_get_open_ration_filted();
  1387. mot_contrl_set_target_vel(&motor.controller, vel_ref);
  1388. }
  1389. }else if (motor.controller.mode_running == CTRL_MODE_EBRAKE){
  1390. float vel = mot_contrl_get_speed(&motor.controller);
  1391. float ebrk_trq = motor_get_ebreak_toruqe(vel);
  1392. if (ebrk_trq >= -mot_contrl_get_ebrk_torque(&motor.controller)/2){
  1393. mot_contrl_set_ebrk_time(&motor.controller, 1);
  1394. }
  1395. if (ebrk_trq != 0) {
  1396. mot_contrl_set_torque(&motor.controller, ebrk_trq);
  1397. }
  1398. if ((mot_contrl_get_final_torque(&motor.controller) < 0.0001f && vel < CONFIG_MIN_RPM_EXIT_EBRAKE) ||
  1399. (!mc_throttle_released() || (mc_throttle_released() && (vel == 0.0f)))) {
  1400. mot_contrl_energy_recovery(&motor.controller, false);
  1401. throttle_torque_reset();
  1402. }
  1403. }
  1404. }
  1405. /*FOC 的部分处理,比如速度环,状态机,转把采集等*/
  1406. measure_time_t g_meas_MCTask;
  1407. #define mc_TaskStart time_measure_start(&g_meas_MCTask)
  1408. #define mc_TaskEnd time_measure_end(&g_meas_MCTask)
  1409. void Sched_MC_mTask(void) {
  1410. static int vbus_err_cnt = 0;
  1411. static bool _sensorless_run = false;
  1412. mc_TaskStart;
  1413. adc_vref_filter();
  1414. throttle_detect(motor.b_start);
  1415. F_all_Calc();
  1416. #ifdef CONFIG_CRUISE_ENABLE_ACCL
  1417. mc_process_curise();
  1418. #endif
  1419. u8 runMode = mot_contrl_mode(&motor.controller);
  1420. /*保护功能*/
  1421. u8 limted = mot_contrl_protect(&motor.controller);
  1422. /* 母线电流,实际采集的相电流矢量大小的计算 */
  1423. mot_contrl_calc_current(&motor.controller);
  1424. 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)) {
  1425. vbus_err_cnt ++;
  1426. if (vbus_err_cnt >= 5) {
  1427. if (mc_set_critical_error(FOC_CRIT_IDC_OV)) {
  1428. mc_crit_err_add(FOC_CRIT_IDC_OV, (s16)sample_vbus_raw(), (s16)sample_ibus_raw());
  1429. mc_save_err_runtime();
  1430. }
  1431. }
  1432. }else {
  1433. vbus_err_cnt = 0;
  1434. }
  1435. if (mc_process_force_running()) {
  1436. mc_TaskEnd;
  1437. return;
  1438. }
  1439. bool sensor_less = !foc_observer_is_encoder();
  1440. if (mc_detect_vbus_mode() || (limted == FOC_LIM_CHANGE_L) || (_sensorless_run != sensor_less)) {
  1441. mc_gear_vmode_changed();
  1442. if (sensor_less && foc_observer_sensorless_stable()) {//unstable 记录在ADC中断处理中
  1443. if (motor_encoder_may_error() == ENCODER_PWM_ERR) {
  1444. mc_set_critical_error(FOC_CRIT_Encoder_Err);
  1445. mc_crit_err_add(FOC_CRIT_Encoder_Err, (s16)enc_pwm_err_ms, enc_delta_err2);
  1446. }else if (motor_encoder_may_error() == ENCODER_AB_ERR) {
  1447. mc_set_critical_error(FOC_CRIT_ENC_AB_Err);
  1448. mc_crit_err_add(FOC_CRIT_ENC_AB_Err, enc_delta_err1, enc_delta_err2);
  1449. }
  1450. }
  1451. motor.b_limit_pending = false;
  1452. }else if (limted == FOC_LIM_CHANGE_H) {
  1453. motor.b_limit_pending = true;
  1454. }
  1455. _sensorless_run = sensor_less;
  1456. /* 如果取消高温,欠压等限流需要释放转把后才生效,确保不会突然加速 */
  1457. if (motor.b_limit_pending && mc_throttle_released()) {
  1458. motor.b_limit_pending = false;
  1459. mc_gear_vmode_changed();
  1460. }
  1461. /* 堵转处理 */
  1462. if (mc_run_stall_process(runMode) || (motor.mode == CTRL_MODE_CURRENT)) {
  1463. mot_contrl_slow_task(&motor.controller);
  1464. mc_motor_runstop();
  1465. if (motor.b_ind_start) {
  1466. mot_params_flux_stop();
  1467. }
  1468. mc_TaskEnd;
  1469. return;
  1470. }
  1471. mc_process_brake_light();
  1472. if ((runMode != CTRL_MODE_OPEN) || (motor.mode != CTRL_MODE_OPEN)) {
  1473. #ifndef CONFIG_DQ_STEP_RESPONSE
  1474. mc_autohold_process();
  1475. if (motor.mode != CTRL_MODE_OPEN) {
  1476. mc_motor_runstop();
  1477. }
  1478. if (runMode != CTRL_MODE_OPEN) {
  1479. if (runMode == CTRL_MODE_SPD) {
  1480. if (mc_is_epm()) {
  1481. mc_process_throttle_epm();
  1482. mc_process_epm_move();
  1483. }else if (motor.s_target_speed != MAX_S16) {
  1484. mot_contrl_set_target_vel(&motor.controller, motor.s_target_speed);
  1485. }
  1486. }else {
  1487. float thro = throttle_get_signal();
  1488. if (motor.b_ignor_throttle) {
  1489. float r = (float)motor.u_throttle_ration/100.0f;
  1490. thro = throttle_open_ration_to_vol(r);
  1491. }
  1492. mc_process_throttle_torque(thro);
  1493. }
  1494. mot_contrl_slow_task(&motor.controller);
  1495. }
  1496. #endif
  1497. }
  1498. mc_TaskEnd;
  1499. }