motor.c 48 KB

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