motor.c 51 KB

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