motor.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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/timer_count32.h"
  8. #include "libs/time_measure.h"
  9. #include "bsp/delay.h"
  10. #include "bsp/bsp.h"
  11. #include "bsp/adc.h"
  12. #include "bsp/pwm.h"
  13. #include "bsp/fan_pwm.h"
  14. #include "foc/commands.h"
  15. #include "libs/logger.h"
  16. #include "bsp/sched_timer.h"
  17. #include "foc/core/e_ctrl.h"
  18. #include "foc/samples.h"
  19. #include "foc/motor/motor_param.h"
  20. #include "foc/core/foc_observer.h"
  21. #include "foc/core/thro_torque.h"
  22. #include "app/nv_storage.h"
  23. //#include "foc/core/torque.h"
  24. #include "foc/limit.h"
  25. #ifdef CONFIG_DQ_STEP_RESPONSE
  26. extern float target_d;
  27. extern float target_q;
  28. #endif
  29. static bool mc_is_hwbrake(void);
  30. static bool mc_is_gpio_mlock(void);
  31. static void _pwm_brake_prot_timer_handler(shark_timer_t *);
  32. static shark_timer_t _brake_prot_timer = TIMER_INIT(_brake_prot_timer, _pwm_brake_prot_timer_handler);
  33. static void _autohold_beep_timer_handler(shark_timer_t *);
  34. static shark_timer_t _autohold_beep_timer = TIMER_INIT(_autohold_beep_timer, _autohold_beep_timer_handler);
  35. static void _fan_det_timer_handler(shark_timer_t *);
  36. static shark_timer_t _fan_det_timer1 = TIMER_INIT(_fan_det_timer1, _fan_det_timer_handler);
  37. static shark_timer_t _fan_det_timer2 = TIMER_INIT(_fan_det_timer2, _fan_det_timer_handler);
  38. static void _encoder_zero_off_timer_handler(shark_timer_t *);
  39. static shark_timer_t _encoder_zero_off_timer = TIMER_INIT(_encoder_zero_off_timer, _encoder_zero_off_timer_handler);
  40. static void _led_off_timer_handler(shark_timer_t *);
  41. static shark_timer_t _led_off_timer = TIMER_INIT(_led_off_timer, _led_off_timer_handler);
  42. static motor_t motor = {
  43. .s_direction = POSITIVE,
  44. .n_gear = 0,
  45. .b_is96Mode = false,
  46. .mode = CTRL_MODE_OPEN,
  47. };
  48. static void MC_Check_MosVbusThrottle(void) {
  49. int count = 1000;
  50. float ibus_adc = 0;
  51. float vref_adc = 0;
  52. float vref_5v_adc = 0;
  53. gpio_phase_u_detect(true);
  54. while(count-- > 0) {
  55. task_udelay(20);
  56. sample_uvw_phase();
  57. sample_throttle();
  58. sample_vbus();
  59. vref_adc += adc_get_vref();
  60. vref_5v_adc += adc_get_5v_ref();
  61. }
  62. adc_set_vref_calc(vref_adc/1000.0f);
  63. adc_set_5vref_calc(vref_5v_adc/1000.0f);
  64. sys_debug("5v ref %f\n", vref_5v_adc/1000.0f);
  65. count = 50;
  66. while(count-- >0) {
  67. task_udelay(300);
  68. ibus_adc += adc_get_ibus();
  69. }
  70. u16 offset = ((float)ibus_adc)/50.0f;
  71. sys_debug("ibus offset %d\n", offset);
  72. sample_ibus_offset(offset);
  73. gpio_phase_u_detect(false);
  74. float abc[3];
  75. get_phase_vols(abc);
  76. int vbus_vol = get_vbus_int();
  77. if (vbus_vol > nv_get_foc_params()->s_maxDCVol) {
  78. mc_set_critical_error(FOC_CRIT_OV_Vol_Err);
  79. }
  80. if (vbus_vol <= nv_get_foc_params()->s_minDCVol) {
  81. mc_set_critical_error(FOC_CRIT_UN_Vol_Err);
  82. }
  83. vbus_vol = get_acc_vol();
  84. sys_debug("acc vol %d\n", vbus_vol);
  85. if (vbus_vol >= nv_get_foc_params()->s_maxDCVol) {
  86. mc_set_critical_error(FOC_CRIT_ACC_OV_Err);
  87. }
  88. if (vbus_vol <= nv_get_foc_params()->s_minDCVol) {
  89. mc_set_critical_error(FOC_CRIT_ACC_Un_Err);
  90. }
  91. if ((get_throttle_float() < nv_get_foc_params()->f_minThroVol) || (get_throttle_float() > nv_get_foc_params()->f_maxThroVol)) {
  92. if (!motor.b_ignor_throttle) {
  93. mc_set_critical_error(FOC_CRIT_THRO_Err);
  94. }
  95. }
  96. if (abc[0] > vbus_vol/2 || abc[1] > vbus_vol/2 || abc[2] > vbus_vol/2) {
  97. mc_set_critical_error(FOC_CRIT_H_MOS_Err);
  98. }else if (abc[0] < 0.001f){
  99. mc_set_critical_error(FOC_CRIT_L_MOS_Err);
  100. }else if ((abc[0] > 0.5f) && (abc[1] < 0.001f || abc[2] < 0.001f)) {
  101. mc_set_critical_error(FOC_CRIT_Phase_Conn_Err);
  102. }
  103. sys_debug("phase vol %f, %f, %f\n", abc[0], abc[1], abc[2]);
  104. }
  105. static u32 _self_check_task(void *p) {
  106. if (ENC_Check_error()) {
  107. err_add_record(FOC_CRIT_Encoder_Err, 0);
  108. mc_set_critical_error(FOC_CRIT_Encoder_Err);
  109. }
  110. if (get_tick_ms() < 5000) { //启动后5s内检测锁电机线
  111. if (mc_is_gpio_mlock()) {
  112. mc_lock_motor(true);
  113. }
  114. }
  115. if (motor.b_lock_motor) {
  116. if (!mc_is_gpio_mlock()) {
  117. mc_lock_motor(false);
  118. }
  119. }
  120. if (fan_pwm_is_running()) {
  121. if ((get_delta_ms(motor.fan[0].start_ts) >= 5000) && (motor.fan[0].rpm == 0)) {
  122. mc_set_critical_error(FOC_CRIT_Fan1_Err);
  123. }else if (motor.fan[0].rpm > 0) {
  124. mc_clr_critical_error(FOC_CRIT_Fan1_Err);
  125. }
  126. if ((get_delta_ms(motor.fan[1].start_ts) >= 5000) && (motor.fan[1].rpm == 0)) {
  127. mc_set_critical_error(FOC_CRIT_Fan2_Err);
  128. }else if (motor.fan[1].rpm > 0) {
  129. mc_clr_critical_error(FOC_CRIT_Fan2_Err);
  130. }
  131. }
  132. return 5;
  133. }
  134. static bool mc_detect_vbus_mode(void) {
  135. #ifdef CONFIG_FORCE_96V_MODE
  136. motor.b_is96Mode = true;
  137. return false;
  138. #else
  139. bool is_96mode = motor.b_is96Mode;
  140. motor.b_is96Mode = get_vbus_int() >= CONFIG_96V_MODE_VOL;
  141. return (is_96mode != motor.b_is96Mode);
  142. #endif
  143. }
  144. static void _mc_internal_init(u8 mode, bool start) {
  145. motor.mode = mode;
  146. motor.throttle = 0;
  147. motor.b_start = start;
  148. motor.b_runStall = false;
  149. motor.runStall_time = 0;
  150. motor.b_epm = false;
  151. motor.b_epm_cmd_move = false;
  152. motor.epm_dir = EPM_Dir_None;
  153. motor.n_autohold_time = 0;
  154. motor.b_auto_hold = 0;
  155. motor.b_break = false;
  156. motor.b_wait_brk_release = false;
  157. motor.b_force_run = false;
  158. motor.b_cruise = false;
  159. }
  160. static void _led_off_timer_handler(shark_timer_t *t) {
  161. gpio_led_enable(false);
  162. }
  163. static void mc_gear_vmode_changed(void) {
  164. mc_gear_t *gears;
  165. if (motor.b_is96Mode) {
  166. gears = &nv_get_gear_configs()->gears_96[0];
  167. }else {
  168. gears = &nv_get_gear_configs()->gears_48[0];
  169. }
  170. //sys_debug("limit %d-%d-%d, mode = %s\n", gears[motor.n_gear].u_maxRPM, gears[motor.n_gear].u_maxIdc, gears[motor.n_gear].u_maxTorque, motor.b_is96Mode?"96V":"48V");
  171. PMSM_FOC_SpeedLimit(gears[motor.n_gear].u_maxRPM);
  172. PMSM_FOC_DCCurrLimit(gears[motor.n_gear].u_maxIdc);
  173. PMSM_FOC_TorqueLimit(gears[motor.n_gear].u_maxTorque);
  174. }
  175. static s16 mc_get_gear_idc_limit(void) {
  176. if (motor.b_is96Mode) {
  177. return nv_get_gear_configs()->gears_96[motor.n_gear].u_maxIdc;
  178. }else {
  179. return nv_get_gear_configs()->gears_48[motor.n_gear].u_maxIdc;
  180. }
  181. }
  182. void mc_init(void) {
  183. fan_pwm_init();
  184. adc_init();
  185. pwm_3phase_init();
  186. samples_init();
  187. motor_encoder_init();
  188. foc_command_init();
  189. thro_torque_init();
  190. mc_detect_vbus_mode();
  191. PMSM_FOC_CoreInit();
  192. eCtrl_init(nv_get_foc_params()->n_acc_time, nv_get_foc_params()->n_dec_time);
  193. mc_gpio_init();
  194. limter_set_under_voltage(nv_get_foc_params()->s_minDCVol);
  195. MC_Check_MosVbusThrottle();
  196. sched_timer_enable(CONFIG_SPD_CTRL_US);
  197. shark_task_create(_self_check_task, NULL);
  198. pwm_up_enable(true);
  199. gpio_led_enable(true);
  200. shark_timer_post(&_led_off_timer, 5000);
  201. }
  202. motor_t * mc_params(void) {
  203. return &motor;
  204. }
  205. void mc_need_update(void) {
  206. motor.b_updated = true;
  207. }
  208. bool mc_unsafe_critical_error(void) {
  209. u32 err = motor.n_CritiCalErrMask & (~(FOC_Cri_Err_Mask(FOC_CRIT_Fan1_Err)));
  210. err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_Fan2_Err)));
  211. #ifdef CONFIG_DQ_STEP_RESPONSE
  212. sys_debug("err=0x%x\n", err);
  213. err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_Encoder_Err)));
  214. err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO_Err)));
  215. sys_debug("err=0x%x\n", err);
  216. #endif
  217. if (motor.b_ignor_throttle) {
  218. err = err & (~(FOC_Cri_Err_Mask(FOC_CRIT_THRO_Err)));
  219. }
  220. return (err != 0);
  221. }
  222. bool mc_start(u8 mode) {
  223. if (motor.b_start) {
  224. return true;
  225. }
  226. #ifdef CONFIG_DQ_STEP_RESPONSE
  227. mode = CTRL_MODE_CURRENT;
  228. target_d = 0.0f;
  229. target_q = 0.0f;
  230. #endif
  231. mc_detect_vbus_mode();
  232. if (motor.b_lock_motor) {
  233. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  234. return false;
  235. }
  236. MC_Check_MosVbusThrottle();
  237. if (mc_unsafe_critical_error()) {
  238. PMSM_FOC_SetErrCode(FOC_Have_CritiCal_Err);
  239. return false;
  240. }
  241. if (mode > CTRL_MODE_CURRENT) {
  242. PMSM_FOC_SetErrCode(FOC_Param_Err);
  243. return false;
  244. }
  245. if (motor_encoder_get_speed() > CONFIG_ZERO_SPEED_RPM) {
  246. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  247. return false;
  248. }
  249. if (!mc_throttle_released()) {
  250. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  251. return false;
  252. }
  253. pwm_up_enable(false);
  254. _mc_internal_init(mode, true);
  255. #if 0
  256. torque_reset();
  257. #else
  258. thro_torque_reset();
  259. #endif
  260. eCtrl_init(nv_get_foc_params()->n_acc_time, nv_get_foc_params()->n_dec_time);
  261. motor_encoder_start(true);
  262. PMSM_FOC_Start(mode);
  263. PMSM_FOC_RT_LimInit();
  264. pwm_turn_on_low_side();
  265. delay_ms(10);
  266. phase_current_offset_calibrate();
  267. pwm_start();
  268. delay_us(10); //wait for ebrake error
  269. if (mc_unsafe_critical_error()) {
  270. mc_stop();
  271. return false;
  272. }
  273. adc_start_convert();
  274. phase_current_calibrate_wait();
  275. if (phase_curr_offset_check()) {
  276. mc_set_critical_error(FOC_CRIT_CURR_OFF_Err);
  277. mc_stop();
  278. return false;
  279. }
  280. if (mc_is_hwbrake()) {
  281. PMSM_FOC_Brake(true);
  282. }
  283. gpio_beep(200);
  284. return true;
  285. }
  286. bool mc_stop(void) {
  287. if (!motor.b_start) {
  288. return true;
  289. }
  290. if (motor.b_lock_motor) {
  291. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  292. return false;
  293. }
  294. if (motor_encoder_get_speed() > CONFIG_ZERO_SPEED_RPM) {
  295. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  296. return false;
  297. }
  298. if (!mc_throttle_released()) {
  299. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  300. return false;
  301. }
  302. u32 mask = cpu_enter_critical();
  303. _mc_internal_init(CTRL_MODE_OPEN, false);
  304. adc_stop_convert();
  305. pwm_stop();
  306. PMSM_FOC_Stop();
  307. motor_encoder_start(false);
  308. pwm_up_enable(true);
  309. cpu_exit_critical(mask);
  310. return true;
  311. }
  312. bool mc_set_gear(u8 gear) {
  313. if (gear >= CONFIG_MAX_GEAR_NUM) {
  314. PMSM_FOC_SetErrCode(FOC_Param_Err);
  315. return false;
  316. }
  317. if (motor.n_gear != gear) {
  318. motor.n_gear = gear;
  319. u32 mask = cpu_enter_critical();
  320. mc_gear_vmode_changed();
  321. cpu_exit_critical(mask);
  322. }
  323. return true;
  324. }
  325. u8 mc_get_gear(void) {
  326. if (motor.n_gear == 3){
  327. return 0;
  328. }
  329. return motor.n_gear + 1;
  330. }
  331. bool mc_enable_cruise(bool enable) {
  332. if (enable == motor.b_cruise) {
  333. return true;
  334. }
  335. if (PMSM_FOC_EnableCruise(enable)) {
  336. motor.b_cruise = enable;
  337. motor.cruise_time = enable?shark_get_seconds():0;
  338. motor.cruise_torque = 0.0f;
  339. return true;
  340. }
  341. return false;
  342. }
  343. bool mc_is_cruise_enabled(void) {
  344. return motor.b_cruise;
  345. }
  346. bool mc_set_cruise_speed(bool rpm_abs, float target_rpm) {
  347. bool ret;
  348. if (rpm_abs) {
  349. ret = PMSM_FOC_Set_CruiseSpeed(target_rpm);
  350. }else {
  351. ret = PMSM_FOC_Set_CruiseSpeed(PMSM_FOC_GetSpeed() + target_rpm);
  352. }
  353. if (ret) {
  354. motor.cruise_time = shark_get_seconds();
  355. motor.cruise_torque = 0.0f;
  356. }
  357. return ret;
  358. }
  359. void mc_set_idc_limit(s16 limit) {
  360. s16 g_limit = mc_get_gear_idc_limit();
  361. limit = min(g_limit, limit);
  362. PMSM_FOC_DCCurrLimit(limit);
  363. }
  364. bool mc_set_foc_mode(u8 mode) {
  365. if (mode == motor.mode) {
  366. return true;
  367. }
  368. if (!motor.b_start) {
  369. return false;
  370. }
  371. u32 mask = cpu_enter_critical();
  372. bool ret = false;
  373. if (PMSM_FOC_SetCtrlMode(mode)) {
  374. motor.mode = mode;
  375. if (mode == CTRL_MODE_OPEN || mode == CTRL_MODE_CURRENT) {
  376. PMSM_FOC_Start(motor.mode);
  377. pwm_enable_channel();
  378. }
  379. ret = true;
  380. }
  381. cpu_exit_critical(mask);
  382. return ret;
  383. }
  384. bool mc_start_epm(bool epm) {
  385. if (motor.b_epm == epm) {
  386. return true;
  387. }
  388. if (!motor.b_start) {
  389. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  390. return false;
  391. }
  392. if (motor_encoder_get_speed() > CONFIG_ZERO_SPEED_RPM) {
  393. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  394. return false;
  395. }
  396. if (!mc_throttle_released()) {
  397. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  398. return false;
  399. }
  400. u32 mask = cpu_enter_critical();
  401. motor.b_epm = epm;
  402. if (epm) {
  403. eCtrl_set_TgtSpeed(0);
  404. motor.mode = CTRL_MODE_SPD;
  405. PMSM_FOC_TorqueLimit(nv_get_foc_params()->s_maxEpmTorqueLim);
  406. PMSM_FOC_SetCtrlMode(CTRL_MODE_SPD);
  407. }else {
  408. motor.epm_dir = EPM_Dir_None;
  409. motor.mode = CTRL_MODE_TRQ;
  410. motor.b_epm_cmd_move = false;
  411. PMSM_FOC_SetCtrlMode(CTRL_MODE_TRQ);
  412. mc_gear_vmode_changed();
  413. }
  414. cpu_exit_critical(mask);
  415. return false;
  416. }
  417. bool mc_is_epm(void) {
  418. return motor.b_epm;
  419. }
  420. bool mc_is_start(void) {
  421. return (motor.b_start || PMSM_FOC_Is_Start());
  422. }
  423. bool mc_start_epm_move(EPM_Dir_t dir, bool is_command) {
  424. if (!motor.b_epm || !motor.b_start) {
  425. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  426. return false;
  427. }
  428. if ((dir == motor.epm_dir) && (is_command == motor.b_epm_cmd_move)) {
  429. return true;
  430. }
  431. u32 mask = cpu_enter_critical();
  432. motor.epm_dir = dir;
  433. if (dir != EPM_Dir_None) {
  434. motor.b_epm_cmd_move = is_command;
  435. if (!PMSM_FOC_Is_Start()) {
  436. PMSM_FOC_Start(motor.mode);
  437. pwm_enable_channel();
  438. }
  439. float rpm = nv_get_foc_params()->s_maxEpmRPM;
  440. if (dir == EPM_Dir_Back) {
  441. rpm = -rpm;
  442. }
  443. sys_debug("rpm %f\n", rpm);
  444. PMSM_FOC_Set_Speed(rpm);
  445. }else {
  446. motor.b_epm_cmd_move = false;
  447. PMSM_FOC_Set_Speed(0);
  448. }
  449. cpu_exit_critical(mask);
  450. return true;
  451. }
  452. void mc_set_fan_duty(u8 duty) {
  453. sys_debug("fan duty %d\n", duty);
  454. if (!fan_pwm_is_running() && duty > 0) {
  455. motor.fan[0].start_ts = get_tick_ms();
  456. motor.fan[1].start_ts = get_tick_ms();
  457. shark_timer_post(&_fan_det_timer1, 5000);
  458. shark_timer_post(&_fan_det_timer2, 5000);
  459. }else if (duty == 0) {
  460. shark_timer_cancel(&_fan_det_timer1);
  461. shark_timer_cancel(&_fan_det_timer2);
  462. }
  463. fan_set_duty(duty);
  464. }
  465. bool mc_command_epm_move(EPM_Dir_t dir) {
  466. return mc_start_epm_move(dir, true);
  467. }
  468. bool mc_throttle_epm_move(EPM_Dir_t dir) {
  469. return mc_start_epm_move(dir, false);
  470. }
  471. void mc_set_throttle_r(bool use, u8 r) {
  472. motor.u_throttle_ration = r;
  473. motor.b_ignor_throttle = use;
  474. if (motor.b_ignor_throttle) {
  475. mc_clr_critical_error(FOC_CRIT_THRO_Err);
  476. }
  477. }
  478. void mc_use_throttle(void) {
  479. motor.b_ignor_throttle = false;
  480. }
  481. void mc_get_running_status(u8 *data) {
  482. data[0] = motor.mode;
  483. data[0] |= (PMSM_FOC_AutoHoldding()?1:0) << 2;
  484. data[0] |= (motor.b_break?1:0) << 3;
  485. data[0] |= (PMSM_FOC_Get()->in.b_cruiseEna?1:0) << 4;
  486. data[0] |= (motor.b_start?1:0) << 5;
  487. data[0] |= (mc_is_epm()?1:0) << 6;
  488. data[0] |= (motor.b_lock_motor) << 7; //motor locked
  489. }
  490. u16 mc_get_running_status2(void) {
  491. u16 data = 0;
  492. data = motor.n_gear;
  493. data |= (PMSM_FOC_AutoHoldding()?1:0) << 2;
  494. data |= (motor.b_break?1:0) << 3;
  495. data |= (PMSM_FOC_Get()->in.b_cruiseEna?1:0) << 4;
  496. data |= (motor.b_start?1:0) << 5;
  497. data |= (mc_is_epm()?1:0) << 6;
  498. data |= (motor.b_lock_motor) << 7; //motor locked
  499. data |= (eCtrl_is_eBrk_Running()?1:0) << 8; //能量回收运行标志
  500. data |= ((motor.n_CritiCalErrMask != 0)?1:0) << 9;
  501. data |= (fan_pwm_is_running()?1:0) << 10; //风扇是否运行
  502. data |= (motor.b_runStall?1:0) << 11; //是否堵转
  503. data |= (PMSM_FOC_iDC_is_Limited()?1:0) << 12; //是否欠压限制母线电流
  504. data |= (PMSM_FOC_Torque_is_Limited()?1:0) << 13; //是否高温限扭矩
  505. data |= (motor.b_is96Mode?1:0) << 14; //是否高压模式
  506. return data;
  507. }
  508. static float _force_angle = 0.0f;
  509. static int _force_wait = 2000;
  510. /* 开环,强制给定电角度和DQ的电压 */
  511. void mc_force_run_open(s16 vd, s16 vq) {
  512. if (motor.b_start || motor.b_force_run) {
  513. if (vd == 0 && vq == 0) {
  514. PMSM_FOC_SetOpenVdq(0, 0);
  515. delay_ms(500);
  516. wdog_reload();
  517. adc_stop_convert();
  518. pwm_stop();
  519. PMSM_FOC_Stop();
  520. pwm_up_enable(true);
  521. motor.b_force_run = false;
  522. motor.b_ignor_throttle = false;
  523. }
  524. return;
  525. }
  526. if (vd == 0 && vq == 0) {
  527. return;
  528. }
  529. motor.b_ignor_throttle = true;
  530. MC_Check_MosVbusThrottle();
  531. if (mc_unsafe_critical_error()) {
  532. PMSM_FOC_SetErrCode(FOC_Have_CritiCal_Err);
  533. }
  534. pwm_up_enable(false);
  535. pwm_turn_on_low_side();
  536. task_udelay(500);
  537. PMSM_FOC_Start(CTRL_MODE_OPEN);
  538. phase_current_offset_calibrate();
  539. pwm_start();
  540. adc_start_convert();
  541. pwm_enable_channel();
  542. phase_current_calibrate_wait();
  543. PMSM_FOC_Set_Angle(0);
  544. PMSM_FOC_SetOpenVdq(vd, 0);
  545. _force_wait = 2000;
  546. motor.b_force_run = true;
  547. }
  548. void mc_encoder_off_calibrate(s16 vd) {
  549. if (motor.b_start || motor.b_calibrate) {
  550. return;
  551. }
  552. motor.b_calibrate = true;
  553. pwm_up_enable(false);
  554. pwm_turn_on_low_side();
  555. task_udelay(500);
  556. PMSM_FOC_Start(CTRL_MODE_OPEN);
  557. phase_current_offset_calibrate();
  558. pwm_start();
  559. adc_start_convert();
  560. phase_current_calibrate_wait();
  561. PMSM_FOC_Set_Angle(0);
  562. PMSM_FOC_SetOpenVdq(vd, 0);
  563. delay_ms(2000);
  564. motor_encoder_set_direction(POSITIVE);
  565. for (int i = 0; i < 200; i++) {
  566. for (float angle = 0; angle < 360; angle++) {
  567. PMSM_FOC_Set_Angle(angle);
  568. delay_ms(1);
  569. if (i > 20) {
  570. motor_encoder_offset(angle);
  571. }
  572. }
  573. wdog_reload();
  574. if (motor_encoder_offset_is_finish()) {
  575. break;
  576. }
  577. }
  578. motor_encoder_set_direction(NEGATIVE);
  579. delay_ms(100);
  580. for (int i = 0; i < 200; i++) {
  581. for (float angle = 360; angle > 0; angle--) {
  582. PMSM_FOC_Set_Angle(angle);
  583. delay_ms(1);
  584. if (i > 10) {
  585. motor_encoder_offset(angle);
  586. }
  587. }
  588. wdog_reload();
  589. if (motor_encoder_offset_is_finish()) {
  590. break;
  591. }
  592. }
  593. delay_ms(500);
  594. PMSM_FOC_SetOpenVdq(0, 0);
  595. delay_ms(500);
  596. wdog_reload();
  597. adc_stop_convert();
  598. pwm_stop();
  599. PMSM_FOC_Stop();
  600. pwm_up_enable(true);
  601. motor_encoder_data_upload();
  602. motor.b_calibrate = false;
  603. }
  604. static void _encoder_zero_off_timer_handler(shark_timer_t *t){
  605. if (!motor.b_calibrate) {
  606. return;
  607. }
  608. float enc_off = 0.0f;
  609. float phase = motor_encoder_zero_phase_detect(&enc_off);
  610. PMSM_FOC_SetOpenVdq(0, 0);
  611. delay_ms(50);
  612. adc_stop_convert();
  613. pwm_stop();
  614. PMSM_FOC_Stop();
  615. _mc_internal_init(CTRL_MODE_OPEN, false);
  616. motor.b_calibrate = false;
  617. if (phase != INVALID_ANGLE) {
  618. nv_save_angle_offset(phase);
  619. }
  620. }
  621. bool mc_encoder_zero_calibrate(s16 vd) {
  622. if (motor.b_calibrate) {
  623. if (vd == 0) {
  624. encoder_clear_cnt_offset();
  625. shark_timer_cancel(&_encoder_zero_off_timer);
  626. PMSM_FOC_SetOpenVdq(0, 0);
  627. delay_ms(500);
  628. adc_stop_convert();
  629. pwm_stop();
  630. PMSM_FOC_Stop();
  631. _mc_internal_init(CTRL_MODE_OPEN, false);
  632. motor.b_calibrate = false;
  633. motor.b_ignor_throttle = false;
  634. }
  635. return true;
  636. }
  637. encoder_clear_cnt_offset();
  638. motor.b_ignor_throttle = true;
  639. MC_Check_MosVbusThrottle();
  640. if (mc_unsafe_critical_error()) {
  641. PMSM_FOC_SetErrCode(FOC_Have_CritiCal_Err);
  642. return false;
  643. }
  644. _mc_internal_init(CTRL_MODE_OPEN, true);
  645. motor.b_calibrate = true;
  646. pwm_turn_on_low_side();
  647. task_udelay(500);
  648. PMSM_FOC_Start(CTRL_MODE_OPEN);
  649. phase_current_offset_calibrate();
  650. pwm_start();
  651. adc_start_convert();
  652. pwm_enable_channel();
  653. phase_current_calibrate_wait();
  654. PMSM_FOC_Set_Angle(0);
  655. PMSM_FOC_SetOpenVdq(vd, 0);
  656. shark_timer_post(&_encoder_zero_off_timer, 6*1000);
  657. return true;
  658. }
  659. bool mc_current_sensor_calibrate(float current) {
  660. if (!mc_start(CTRL_MODE_OPEN)) {
  661. return false;
  662. }
  663. phase_current_sensor_start_calibrate(current);
  664. phase_current_calibrate_wait();
  665. return true;
  666. }
  667. bool mc_lock_motor(bool lock) {
  668. if (motor.b_lock_motor == lock) {
  669. return true;
  670. }
  671. int ret = true;
  672. u32 mask = cpu_enter_critical();
  673. if (motor.b_start) {
  674. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  675. ret = false;
  676. goto ml_ex_cri;
  677. }
  678. if (lock && (motor_encoder_get_speed() > CONFIG_ZERO_SPEED_RPM)) {
  679. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  680. ret = false;
  681. goto ml_ex_cri;
  682. }
  683. motor.b_lock_motor = lock;
  684. if (lock) {
  685. pwm_start();
  686. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  687. pwm_enable_channel();
  688. }else {
  689. pwm_stop();
  690. }
  691. ml_ex_cri:
  692. cpu_exit_critical(mask);
  693. return ret;
  694. }
  695. bool mc_auto_hold(bool hold) {
  696. if (motor.b_auto_hold == hold) {
  697. return true;
  698. }
  699. if (nv_get_foc_params()->n_autoHold == 0) {
  700. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  701. return false;
  702. }
  703. if (!motor.b_start) {
  704. PMSM_FOC_SetErrCode(FOC_NotAllowed);
  705. return false;
  706. }
  707. if (hold && !mc_throttle_released()) {
  708. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  709. return false;
  710. }
  711. u32 mask = cpu_enter_critical();
  712. motor.b_auto_hold = hold;
  713. if (!PMSM_FOC_Is_Start()) {
  714. PMSM_FOC_Start(motor.mode);
  715. PMSM_FOC_AutoHold(hold);
  716. pwm_enable_channel();
  717. }else {
  718. PMSM_FOC_AutoHold(hold);
  719. }
  720. cpu_exit_critical(mask);
  721. return true;
  722. }
  723. void mc_set_critical_error(u8 err) {
  724. motor.n_CritiCalErrMask |= (1u << err);
  725. }
  726. void mc_clr_critical_error(u8 err) {
  727. motor.n_CritiCalErrMask &= ~(1u << err);
  728. }
  729. u32 mc_get_critical_error(void) {
  730. return motor.n_CritiCalErrMask;
  731. }
  732. bool mc_throttle_released(void) {
  733. if (motor.b_ignor_throttle) {
  734. return motor.u_throttle_ration == 0;
  735. }
  736. return get_throttle_float() <= nv_get_foc_params()->n_startThroVol;
  737. }
  738. static bool mc_is_gpio_mlock(void) {
  739. int count = 50;
  740. int settimes = 0;
  741. while(count-- > 0) {
  742. bool b1 = gpio_motor_locked();
  743. if (b1) {
  744. settimes ++;
  745. }
  746. delay_us(1);
  747. }
  748. if (settimes == 0) {
  749. return false;
  750. }else if (settimes == 50) {
  751. return true;
  752. }
  753. //有干扰,do nothing
  754. return false;
  755. }
  756. static bool mc_is_hwbrake(void) {
  757. int count = 50;
  758. int settimes = 0;
  759. while(count-- > 0) {
  760. bool b1 = mc_get_gpio_brake() && mc_get_gpio_brake1();
  761. if (b1) {
  762. settimes ++;
  763. }
  764. delay_us(1);
  765. }
  766. if (settimes == 0) {
  767. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  768. return true;
  769. #else
  770. return false;
  771. #endif
  772. }else if (settimes == 50) {
  773. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  774. return false;
  775. #else
  776. return true;
  777. #endif
  778. }
  779. //有干扰,do nothing
  780. motor.n_brake_errors++;
  781. return false;
  782. }
  783. static void _fan_det_timer_handler(shark_timer_t *t) {
  784. if (t == &_fan_det_timer1) {
  785. motor.fan[0].rpm = 0;
  786. motor.fan[0].det_ts = 0;
  787. }else {
  788. motor.fan[1].rpm = 0;
  789. motor.fan[1].det_ts = 0;
  790. }
  791. }
  792. void Fan_IRQHandler(int idx) {
  793. fan_t *fan = motor.fan + idx;
  794. u32 pre_ts = fan->det_ts;
  795. u32 delta_ts = get_delta_ms(pre_ts);
  796. fan->det_ts = get_tick_ms();
  797. float rpm = 60.0f * 1000 / (float)delta_ts;
  798. LowPass_Filter(fan->rpm, rpm, 0.1f);
  799. if (idx == 0) {
  800. shark_timer_post(&_fan_det_timer1, 100);
  801. }else {
  802. shark_timer_post(&_fan_det_timer2, 100);
  803. }
  804. }
  805. void MC_Brake_IRQHandler(void) {
  806. if (mc_is_hwbrake()) {
  807. motor.b_break = true;
  808. }else {
  809. motor.b_break = false;
  810. }
  811. if (!motor.b_start) {
  812. return;
  813. }
  814. if (motor.b_break) {
  815. PMSM_FOC_Brake(true);
  816. }else {
  817. PMSM_FOC_Brake(false);
  818. }
  819. }
  820. static void _pwm_brake_prot_timer_handler(shark_timer_t *t){
  821. pwm_brake_enable(true);
  822. sys_debug("MC protect error\n");
  823. }
  824. void MC_Protect_IRQHandler(void){
  825. pwm_brake_enable(false);
  826. shark_timer_post(&_brake_prot_timer, 1000);
  827. if (!motor.b_start) {
  828. return;
  829. }
  830. mc_set_critical_error(FOC_CRIT_Phase_Err);
  831. _mc_internal_init(CTRL_MODE_OPEN, false);
  832. adc_stop_convert();
  833. pwm_stop();
  834. PMSM_FOC_Stop();
  835. pwm_up_enable(true);
  836. }
  837. void TIMER_UP_IRQHandler(void){
  838. if (!motor.b_start && !PMSM_FOC_Is_Start()) {
  839. motor_encoder_update();
  840. }
  841. }
  842. measure_time_t g_meas_foc = {.exec_max_time = 20, .intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  843. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  844. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  845. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  846. void ADC_IRQHandler(void) {
  847. if (phase_current_offset()) {//check if is adc offset checked
  848. return;
  849. }
  850. if (phase_current_sensor_do_calibrate()){
  851. pwm_update_duty(100, FOC_PWM_Half_Period-100, 100);
  852. pwm_update_sample(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period+1, PHASE_BC);
  853. return;
  854. }
  855. TIME_MEATURE_START();
  856. PMSM_FOC_Schedule();
  857. TIME_MEATURE_END();
  858. }
  859. #ifndef CONFIG_DQ_STEP_RESPONSE
  860. static bool mc_can_stop_foc(void) {
  861. if (mc_throttle_released() && PMSM_FOC_GetSpeed() == 0.0f) {
  862. if (!PMSM_FOC_AutoHoldding() && motor.epm_dir == EPM_Dir_None) {
  863. return true;
  864. }
  865. }
  866. /* 启用无感观测器,同时速度低于观测器允许的最小速度,关闭输出,滑行 */
  867. if (!foc_observer_is_encoder() && PMSM_FOC_GetSpeed() < CONFIG_SMO_MIN_SPEED) {
  868. return true;
  869. }
  870. return false;
  871. }
  872. static bool mc_can_restart_foc(void) {
  873. return (!PMSM_FOC_Is_Start()) && (!mc_throttle_released()) && (!mc_unsafe_critical_error());
  874. }
  875. #endif
  876. static bool mc_run_stall_process(u8 run_mode) {
  877. if ((run_mode == CTRL_MODE_TRQ || run_mode == CTRL_MODE_SPD) && !PMSM_FOC_AutoHoldding()) {
  878. //堵转判断
  879. if (motor.b_runStall) {
  880. if (!mc_throttle_released()) {
  881. return true;
  882. }
  883. motor.runStall_time = 0;
  884. motor.b_runStall = false; //转把释放,清除堵转标志
  885. }else if (PMSM_FOC_Get_Real_dqVector() >= CONFIG_STALL_MAX_CURRENT){
  886. if (ABS(PMSM_FOC_GetSpeed()) < 1.0f && (motor.runStall_time == 0)) {
  887. motor.runStall_time = get_tick_ms();
  888. motor.runStall_pos = motor_encoder_get_position();
  889. }
  890. if (motor.runStall_time > 0) {
  891. if (get_delta_ms(motor.runStall_time) >= CONFIG_STALL_MAX_TIME) {
  892. motor.b_runStall = true;
  893. motor.runStall_time = 0;
  894. PMSM_FOC_Set_Torque(0);
  895. #if 0
  896. torque_reset();
  897. #else
  898. thro_torque_reset();
  899. #endif
  900. return true;
  901. }
  902. if (ABS(motor.runStall_pos - motor_encoder_get_position()) >= 0.2f) {
  903. motor.runStall_time = 0;
  904. }
  905. }
  906. }else {
  907. motor.runStall_time = 0;
  908. }
  909. }
  910. return false;
  911. }
  912. static void _autohold_beep_timer_handler(shark_timer_t *t) {
  913. gpio_beep(60);
  914. }
  915. static void mc_autohold_process(void) {
  916. if (nv_get_foc_params()->n_autoHold == 0) {
  917. if (PMSM_FOC_AutoHoldding()) {
  918. mc_auto_hold(false);
  919. }
  920. return;
  921. }
  922. if (PMSM_FOC_AutoHoldding()) {
  923. if (!mc_throttle_released()) {
  924. mc_auto_hold(false);
  925. motor.b_wait_brk_release = false;
  926. }else if (!motor.b_break && motor.b_wait_brk_release) {
  927. motor.b_wait_brk_release = false;
  928. }else if (motor.b_break && !motor.b_wait_brk_release) {
  929. mc_auto_hold(false);
  930. }
  931. }
  932. if (!PMSM_FOC_AutoHoldding() && motor.b_break && (motor_encoder_get_speed() < CONFIG_ZERO_SPEED_RPM)) {
  933. if (motor.n_autohold_time == 0) {
  934. motor.n_autohold_time = get_tick_ms();
  935. }else {
  936. if (get_delta_ms(motor.n_autohold_time) >= CONFIG_AUTOHOLD_DETECT_TIME) {
  937. if (mc_auto_hold(true)) {
  938. motor.b_wait_brk_release = true;
  939. shark_timer_post(&_autohold_beep_timer, 0);
  940. }
  941. }
  942. }
  943. }else {
  944. motor.n_autohold_time = 0;
  945. }
  946. }
  947. static void mc_process_throttle_epm(void) {
  948. if (!motor.b_epm_cmd_move) {//通过命令前进后退,不处理转把
  949. if (mc_throttle_released()) {
  950. mc_throttle_epm_move(EPM_Dir_None);
  951. }else {
  952. mc_throttle_epm_move(EPM_Dir_Forward);
  953. }
  954. }
  955. }
  956. static bool mc_process_force_running(void) {
  957. if (motor.b_calibrate || (motor.mode == CTRL_MODE_OPEN)) {
  958. if (motor.b_force_run) {
  959. if (_force_wait > 0) {
  960. --_force_wait;
  961. }else {
  962. _force_angle += 1.0f;
  963. rand_angle(_force_angle);
  964. PMSM_FOC_Set_Angle(_force_angle);
  965. }
  966. }
  967. return true;
  968. }
  969. return false;
  970. }
  971. #ifdef CONFIG_CRUISE_ENABLE_ACCL
  972. static void mc_process_curise(void) {
  973. static bool can_pause_resume = false;
  974. if (motor.b_cruise) {
  975. if (PMSM_FOC_GetSpeed() < CONFIG_MIN_CRUISE_RPM) {
  976. mc_enable_cruise(false);
  977. return;
  978. }
  979. /* 定速巡航模式下,必须转把归位后才能开始通过拧动转把加速 */
  980. if (mc_throttle_released() && !can_pause_resume) {
  981. can_pause_resume = true;
  982. }
  983. if (!can_pause_resume) {
  984. return;
  985. }
  986. if (PMSM_FOC_Is_CruiseEnabled()) {
  987. u32 cruise_time = shark_get_seconds() - motor.cruise_time;
  988. if ((cruise_time >= 3) && motor.cruise_torque == 0.0f) {
  989. motor.cruise_torque = PMSM_FOC_Get()->in.s_targetTorque;
  990. }else if ((cruise_time >= 3) && (motor.cruise_torque > 0.0f)){
  991. #if 0
  992. float trq_req = get_thro_request_torque();
  993. #else
  994. float trq_req = get_user_request_torque();
  995. #endif
  996. if (trq_req > motor.cruise_torque * 1.2f) {
  997. PMSM_FOC_PauseCruise(); //需要加速,暂停定速巡航
  998. }
  999. }
  1000. }else {
  1001. #if 0
  1002. float trq_req = get_thro_request_torque();
  1003. #else
  1004. float trq_req = get_user_request_torque();
  1005. #endif
  1006. if (trq_req <= motor.cruise_torque * 1.1f) {
  1007. PMSM_FOC_ResumeCruise(); //重新开始定速巡航,巡航速度还是前一次定速巡航给的速度
  1008. motor.cruise_time = shark_get_seconds();
  1009. }
  1010. }
  1011. }else {
  1012. can_pause_resume = false;
  1013. }
  1014. }
  1015. #endif
  1016. /*FOC 的部分处理,比如速度环,状态机,转把采集等*/
  1017. measure_time_t g_meas_MCTask;
  1018. #define mc_TaskStart time_measure_start(&g_meas_MCTask)
  1019. #define mc_TaskEnd time_measure_end(&g_meas_MCTask)
  1020. void Sched_MC_mTask(void) {
  1021. mc_TaskStart;
  1022. adc_vref_filter();
  1023. #ifdef CONFIG_CRUISE_ENABLE_ACCL
  1024. mc_process_curise();
  1025. #endif
  1026. u8 runMode = PMSM_FOC_CtrlMode();
  1027. /*保护功能*/
  1028. bool limted = PMSM_FOC_RunTime_Limit();
  1029. /* 母线电流,实际采集的相电流矢量大小的计算 */
  1030. PMSM_FOC_Calc_Current();
  1031. if (mc_process_force_running()) {
  1032. mc_TaskEnd;
  1033. return;
  1034. }
  1035. if (mc_detect_vbus_mode() || limted) {
  1036. mc_gear_vmode_changed();
  1037. }
  1038. /* 堵转处理 */
  1039. if (mc_run_stall_process(runMode) || (runMode == CTRL_MODE_CURRENT)) {
  1040. eCtrl_Running();
  1041. PMSM_FOC_Slow_Task();
  1042. mc_TaskEnd;
  1043. return;
  1044. }
  1045. if ((runMode != CTRL_MODE_OPEN) || (motor.mode != CTRL_MODE_OPEN)) {
  1046. #ifndef CONFIG_DQ_STEP_RESPONSE
  1047. mc_autohold_process();
  1048. if (motor.mode != CTRL_MODE_OPEN) {
  1049. u32 mask;
  1050. if (mc_can_stop_foc()) {
  1051. if (PMSM_FOC_Is_Start()) {
  1052. mask = cpu_enter_critical();
  1053. PMSM_FOC_Stop();
  1054. pwm_disable_channel();
  1055. cpu_exit_critical(mask);
  1056. }
  1057. }
  1058. if (mc_can_restart_foc()) {
  1059. mask = cpu_enter_critical();
  1060. PMSM_FOC_Start(motor.mode);
  1061. mc_gear_vmode_changed();
  1062. #if 0
  1063. torque_reset();
  1064. #else
  1065. thro_torque_reset();
  1066. #endif
  1067. pwm_enable_channel();
  1068. cpu_exit_critical(mask);
  1069. }
  1070. }
  1071. if (runMode != CTRL_MODE_OPEN) {
  1072. eCtrl_Running();
  1073. if ((runMode == CTRL_MODE_SPD) && mc_is_epm()) {
  1074. mc_process_throttle_epm();
  1075. }else {
  1076. #if 0
  1077. if (motor.b_ignor_throttle) { //使用软件设置的油门开度
  1078. float r = (float)motor.u_throttle_ration/100.0f;
  1079. request_torque(r);
  1080. }else {
  1081. throttle_process(runMode, get_throttle_float());
  1082. }
  1083. #else
  1084. float thro = get_throttle_float();
  1085. if (motor.b_ignor_throttle) {
  1086. float r = (float)motor.u_throttle_ration/100.0f;
  1087. thro = thro_ration_to_voltage(r);
  1088. }
  1089. thro_torque_process(runMode, thro);
  1090. #endif
  1091. }
  1092. PMSM_FOC_Slow_Task();
  1093. }
  1094. #endif
  1095. }
  1096. mc_TaskEnd;
  1097. }