motor.c 48 KB

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