motor.c 9.2 KB

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