motor.c 9.7 KB

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