motor.c 45 KB

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