motor.c 51 KB

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