motor.c 28 KB

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