motor.c 8.3 KB

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