motor.c 41 KB

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