motor.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 "foc/commands.h"
  14. #include "libs/logger.h"
  15. #include "bsp/sched_timer.h"
  16. #include "foc/core/e_ctrl.h"
  17. #include "foc/samples.h"
  18. #include "foc/motor/motor_param.h"
  19. #include "foc/core/torque.h"
  20. #include "app/nv_storage.h"
  21. static bool mc_is_hwbrake(void);
  22. static void _pwm_brake_timer_handler(shark_timer_t *);
  23. static shark_timer_t _brake_timer = TIMER_INIT(_brake_timer, _pwm_brake_timer_handler);
  24. static motor_t motor = {
  25. .s_direction = POSITIVE,
  26. };
  27. static void mc_gpio_init(void) {
  28. #ifdef GPIO_BRAKE_IN_GROUP
  29. rcu_periph_clock_enable(GPIO_BRAKE_IN_RCU);
  30. gpio_init(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_MODE, GPIO_OSPEED_50MHZ, GPIO_BRAKE_IN_PIN);
  31. gpio_exti_source_select(GPIO_BRAKE_EXIT_SRC_GROUP, GPIO_BRAKE_EXIT_SRC_PIN);
  32. exti_init(GPIO_BRAKE_EXTI, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
  33. nvic_irq_enable(GPIO_BRAKE_IRQ, EBREAK_IRQ_PRIORITY, 0U);
  34. exti_interrupt_flag_clear(GPIO_BRAKE_EXTI);
  35. exti_interrupt_enable(GPIO_BRAKE_EXTI);
  36. #endif
  37. }
  38. static void MC_Mos_Check_error(void) {
  39. int count = 1000;
  40. gpio_phase_u_detect(true);
  41. while(count-- >= 0) {
  42. task_udelay(20);
  43. sample_uvw_phase();
  44. }
  45. float abc[3];
  46. get_phase_vols(abc);
  47. gpio_phase_u_detect(false);
  48. if (abc[0] > 10 || abc[1] > 10 || abc[2] > 10) {
  49. PMSM_FOC_SetCriticalError(FOC_CRIT_H_MOS_Err);
  50. }else if (abc[0] < 0.001f){
  51. PMSM_FOC_SetCriticalError(FOC_CRIT_L_MOS_Err);
  52. }else if ((abc[0] > 0.5f) && (abc[1] < 0.001f || abc[2] < 0.001f)) {
  53. PMSM_FOC_SetCriticalError(FOC_CRIT_Phase_Conn_Err);
  54. }
  55. sys_debug("vol %f, %f, %f\n", abc[0], abc[1], abc[2]);
  56. }
  57. static u32 _self_check_task(void *p) {
  58. if (ENC_Check_error()) {
  59. err_add_record(FOC_CRIT_Encoder_Err, 0);
  60. PMSM_FOC_SetCriticalError(FOC_CRIT_Encoder_Err);
  61. }
  62. return 0;
  63. }
  64. void mc_init(void) {
  65. adc_init();
  66. pwm_3phase_init();
  67. samples_init();
  68. motor_encoder_init();
  69. foc_command_init();
  70. PMSM_FOC_CoreInit();
  71. mc_gpio_init();
  72. MC_Mos_Check_error();
  73. sched_timer_enable(SPD_CTRL_MS);
  74. shark_task_create(_self_check_task, NULL);
  75. }
  76. motor_t * mc_params(void) {
  77. return &motor;
  78. }
  79. bool mc_start(u8 mode) {
  80. if (motor.b_start) {
  81. return true;
  82. }
  83. MC_Mos_Check_error();
  84. if (PMSM_FOC_GetCriticalError() != 0) {
  85. PMSM_FOC_SetErrCode(FOC_Have_CritiCal_Err);
  86. return false;
  87. }
  88. if (mode > CTRL_MODE_CURRENT) {
  89. PMSM_FOC_SetErrCode(FOC_Param_Err);
  90. return false;
  91. }
  92. if (PMSM_FOC_GetSpeed() > 10.0f) {
  93. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  94. return false;
  95. }
  96. if (!mc_throttle_released()) {
  97. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  98. return false;
  99. }
  100. motor.mode = mode;
  101. eCtrl_init(1000, 3000);
  102. motor_encoder_start(motor.s_direction);
  103. PMSM_FOC_Start(mode);
  104. pwm_turn_on_low_side();
  105. delay_ms(100);
  106. phase_current_offset_calibrate();
  107. pwm_start();
  108. adc_start_convert();
  109. phase_current_calibrate_wait();
  110. motor.throttle = 0;
  111. motor.b_start = true;
  112. if (phase_curr_offset_check()) {
  113. PMSM_FOC_SetCriticalError(FOC_CRIT_CURR_OFF_Err);
  114. mc_stop();
  115. return false;
  116. }
  117. if (mc_is_hwbrake()) {
  118. PMSM_FOC_Brake(true);
  119. }
  120. gpio_beep(1000);
  121. return true;
  122. }
  123. bool mc_stop(void) {
  124. if (!motor.b_start) {
  125. return true;
  126. }
  127. if (PMSM_FOC_GetSpeed() > 10.0f) {
  128. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  129. sys_debug("speed error\n");
  130. return false;
  131. }
  132. if (!mc_throttle_released()) {
  133. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  134. sys_debug("throttle error\n");
  135. return false;
  136. }
  137. motor.mode = CTRL_MODE_OPEN;
  138. adc_stop_convert();
  139. pwm_stop();
  140. PMSM_FOC_Stop();
  141. motor.b_start = false;
  142. gpio_led2_enable(false);
  143. return true;
  144. }
  145. bool mc_set_foc_mode(u8 mode) {
  146. if (mode == motor.mode) {
  147. return true;
  148. }
  149. if (!motor.b_start) {
  150. return false;
  151. }
  152. u32 mask = cpu_enter_critical();
  153. bool ret = false;
  154. if (PMSM_FOC_SetCtrlMode(mode)) {
  155. motor.mode = mode;
  156. if (mode == CTRL_MODE_OPEN) {
  157. PMSM_FOC_Start(motor.mode);
  158. pwm_enable_channel();
  159. }
  160. ret = true;
  161. }
  162. cpu_exit_critical(mask);
  163. return ret;
  164. }
  165. void mc_set_spd_torque(s32 target) {
  166. motor.b_ignor_throttle = true;
  167. motor.s_targetFix = target;
  168. }
  169. void mc_use_throttle(void) {
  170. motor.b_ignor_throttle = false;
  171. }
  172. void mc_encoder_off_calibrate(s16 vd) {
  173. if (PMSM_FOC_Is_Start()) {
  174. return;
  175. }
  176. motor.b_calibrate = true;
  177. pwm_turn_on_low_side();
  178. task_udelay(500);
  179. PMSM_FOC_Start(CTRL_MODE_OPEN);
  180. phase_current_offset_calibrate();
  181. pwm_start();
  182. adc_start_convert();
  183. phase_current_calibrate_wait();
  184. PMSM_FOC_Set_Angle(0);
  185. PMSM_FOC_SetOpenVdq(vd, 0);
  186. delay_ms(2000);
  187. motor_encoder_set_direction(POSITIVE);
  188. for (int i = 0; i < 100000; i++) {
  189. for (float angle = 0; angle < 360; angle++) {
  190. PMSM_FOC_Set_Angle(angle);
  191. delay_ms(2);
  192. if (i > 10) {
  193. motor_encoder_offset(angle);
  194. }
  195. }
  196. wdog_reload();
  197. if (motor_encoder_offset_is_finish()) {
  198. break;
  199. }
  200. }
  201. motor_encoder_set_direction(NEGATIVE);
  202. delay_ms(100);
  203. for (int i = 0; i < 100000; i++) {
  204. for (float angle = 360; angle > 0; angle--) {
  205. PMSM_FOC_Set_Angle(angle);
  206. delay_ms(2);
  207. if (i > 10) {
  208. motor_encoder_offset(angle);
  209. }
  210. }
  211. wdog_reload();
  212. if (motor_encoder_offset_is_finish()) {
  213. break;
  214. }
  215. }
  216. delay_ms(500);
  217. PMSM_FOC_SetOpenVdq(0, 0);
  218. delay_ms(500);
  219. wdog_reload();
  220. adc_stop_convert();
  221. pwm_stop();
  222. PMSM_FOC_Stop();
  223. motor.b_calibrate = false;
  224. }
  225. bool mc_encoder_zero_calibrate(s16 vd) {
  226. if (PMSM_FOC_Is_Start()) {
  227. return false;
  228. }
  229. motor.b_calibrate = true;
  230. pwm_turn_on_low_side();
  231. task_udelay(500);
  232. PMSM_FOC_Start(CTRL_MODE_OPEN);
  233. phase_current_offset_calibrate();
  234. pwm_start();
  235. adc_start_convert();
  236. phase_current_calibrate_wait();
  237. PMSM_FOC_Set_Angle(0);
  238. PMSM_FOC_SetOpenVdq(vd, 0);
  239. delay_ms(2000);
  240. float phase = motor_encoder_zero_phase_detect();
  241. delay_ms(500);
  242. PMSM_FOC_SetOpenVdq(0, 0);
  243. delay_ms(500);
  244. adc_stop_convert();
  245. pwm_stop();
  246. PMSM_FOC_Stop();
  247. motor.b_calibrate = false;
  248. if (phase != INVALID_ANGLE) {
  249. nv_save_angle_offset(phase);
  250. return true;
  251. }
  252. return false;
  253. }
  254. bool mc_current_sensor_calibrate(float current) {
  255. if (!mc_start(CTRL_MODE_OPEN)) {
  256. return false;
  257. }
  258. phase_current_sensor_start_calibrate(current);
  259. phase_current_calibrate_wait();
  260. return true;
  261. }
  262. bool mc_lock_motor(bool lock) {
  263. if (lock && (PMSM_FOC_GetSpeed() > 10)) {
  264. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  265. return false;
  266. }
  267. PMSM_FOC_LockMotor(lock); //if mot enabled, foc core will do lock
  268. if (!motor.b_start) {
  269. if (lock) {
  270. pwm_start();
  271. pwm_update_duty(0, 0, 0);
  272. }else {
  273. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  274. pwm_stop();
  275. }
  276. }
  277. return true;
  278. }
  279. bool mc_throttle_released(void) {
  280. return get_throttle_float() < CONFIG_THROTTLE_LOW_VALUE;
  281. }
  282. static bool mc_is_hwbrake(void) {
  283. #ifdef GPIO_BRAKE_IN_GROUP
  284. int count = 50;
  285. int settimes = 0;
  286. while(count-- > 0) {
  287. bool b1 = gpio_input_bit_get(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_PIN) == SET;
  288. if (b1) {
  289. settimes ++;
  290. }
  291. delay_us(1);
  292. }
  293. if (settimes == 0) {
  294. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  295. return true;
  296. #else
  297. return false;
  298. #endif
  299. }else if (settimes == 50) {
  300. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  301. return false;
  302. #else
  303. return true;
  304. #endif
  305. }else {
  306. //有干扰,do nothing
  307. motor.n_brake_errors++;
  308. return false;
  309. }
  310. #else
  311. return false;
  312. #endif
  313. }
  314. void MC_Brake_IRQHandler(void) {
  315. if (!motor.b_start) {
  316. return;
  317. }
  318. if (mc_is_hwbrake()) {
  319. PMSM_FOC_Brake(true);
  320. }else {
  321. PMSM_FOC_Brake(false);
  322. }
  323. }
  324. static void _pwm_brake_timer_handler(shark_timer_t *t){
  325. pwm_brake_enable(true);
  326. }
  327. void MC_Protect_IRQHandler(void){
  328. pwm_brake_enable(false);
  329. shark_timer_post(&_brake_timer, 1000);
  330. if (!motor.b_start) {
  331. return;
  332. }
  333. PMSM_FOC_SetCriticalError(FOC_CRIT_Phase_Err);
  334. }
  335. measure_time_t g_meas_timeup = {.intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  336. void TIMER_UP_IRQHandler(void){
  337. //phase_current_adc_triger();
  338. time_measure_start(&g_meas_timeup);
  339. }
  340. measure_time_t g_meas_foc = {.exec_max_time = 20, .intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  341. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  342. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  343. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  344. void ADC_IRQHandler(void) {
  345. if (phase_current_offset()) {//check if is adc offset checked
  346. return;
  347. }
  348. if (phase_current_sensor_do_calibrate()){
  349. pwm_update_duty(100, FOC_PWM_Half_Period-100, 100);
  350. pwm_update_sample(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period+1, PHASE_BC);
  351. return;
  352. }
  353. TIME_MEATURE_START();
  354. PMSM_FOC_Schedule();
  355. TIME_MEATURE_END();
  356. }
  357. //#define ANGLE_TEST
  358. #ifdef ANGLE_TEST
  359. static void _debug_angle(void) {
  360. if (motor.b_start) {
  361. PMSM_FOC_Set_Angle(motor.s_testAngle);
  362. if (++motor.s_testAngle >= 360) {
  363. motor.s_testAngle = 0;
  364. }
  365. }
  366. }
  367. #endif
  368. /*FOC 的部分处理,比如速度环,状态机,转把采集等*/
  369. measure_time_t g_meas_MCTask;
  370. void Sched_MC_mTask(void) {
  371. time_measure_start(&g_meas_MCTask);
  372. u8 runMode = PMSM_FOC_CtrlMode();
  373. #if ANGLE_TEST
  374. _debug_angle();
  375. #endif
  376. PMSM_FOC_Calc_iDC();
  377. if (motor.b_calibrate || (motor.mode == CTRL_MODE_OPEN)) {
  378. return;
  379. }
  380. if ((runMode != CTRL_MODE_OPEN) || (motor.mode != CTRL_MODE_OPEN)) {
  381. if (motor.mode != CTRL_MODE_OPEN) {
  382. u32 mask;
  383. if (mc_throttle_released() && PMSM_FOC_GetSpeed() == 0.0f) {
  384. mask = cpu_enter_critical();
  385. PMSM_FOC_Stop();
  386. pwm_disable_channel();
  387. cpu_exit_critical(mask);
  388. }else {
  389. mask = cpu_enter_critical();
  390. PMSM_FOC_Start(motor.mode);
  391. pwm_enable_channel();
  392. cpu_exit_critical(mask);
  393. }
  394. }
  395. if (runMode != CTRL_MODE_OPEN) {
  396. eCtrl_Running();
  397. float f_throttle = get_throttle_float();
  398. if (f_throttle != motor.throttle) {
  399. motor.throttle = f_throttle;
  400. torque_speed_target(runMode, f_throttle);
  401. }
  402. PMSM_FOC_idqCalc();
  403. }
  404. }
  405. }