motor.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. return true;
  97. }
  98. bool mc_stop(void) {
  99. if (!motor.b_start) {
  100. return true;
  101. }
  102. if (PMSM_FOC_GetSpeed() > 10.0f) {
  103. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  104. return false;
  105. }
  106. if (!mc_throttle_released()) {
  107. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  108. return false;
  109. }
  110. motor.mode = CTRL_MODE_OPEN;
  111. adc_stop_convert();
  112. pwm_stop();
  113. PMSM_FOC_Stop();
  114. motor.b_start = false;
  115. gpio_led2_enable(false);
  116. return true;
  117. }
  118. void mc_set_spd_torque(s32 target) {
  119. motor.b_ignor_throttle = true;
  120. motor.s_targetFix = target;
  121. }
  122. void mc_use_throttle(void) {
  123. motor.b_ignor_throttle = false;
  124. }
  125. void mc_encoder_off_calibrate(s16 vd) {
  126. if (PMSM_FOC_Is_Start()) {
  127. return;
  128. }
  129. pwm_turn_on_low_side();
  130. task_udelay(500);
  131. PMSM_FOC_Start(CTRL_MODE_OPEN);
  132. phase_current_offset_calibrate();
  133. pwm_start();
  134. adc_start_convert();
  135. phase_current_calibrate_wait();
  136. PMSM_FOC_Set_Angle(0);
  137. PMSM_FOC_SetOpenVdq(vd, 0);
  138. delay_ms(2000);
  139. motor_encoder_set_direction(POSITIVE);
  140. for (int i = 0; i < 1000000; i++) {
  141. for (float angle = 0; angle < 360; angle++) {
  142. PMSM_FOC_Set_Angle(angle);
  143. delay_ms(2);
  144. motor_encoder_offset(angle);
  145. }
  146. if (motor_encoder_offset_is_finish()) {
  147. break;
  148. }
  149. }
  150. delay_ms(500);
  151. motor_encoder_set_direction(NEGATIVE);
  152. for (int i = 0; i < 1000000; i++) {
  153. for (float angle = 359; angle >= 0; angle--) {
  154. PMSM_FOC_Set_Angle(angle);
  155. delay_ms(2);
  156. motor_encoder_offset(angle);
  157. }
  158. if (motor_encoder_offset_is_finish()) {
  159. break;
  160. }
  161. }
  162. delay_ms(500);
  163. PMSM_FOC_SetOpenVdq(0, 0);
  164. delay_ms(500);
  165. adc_stop_convert();
  166. pwm_stop();
  167. PMSM_FOC_Stop();
  168. }
  169. bool mc_encoder_zero_calibrate(s16 vd) {
  170. if (PMSM_FOC_Is_Start()) {
  171. return false;
  172. }
  173. pwm_turn_on_low_side();
  174. task_udelay(500);
  175. PMSM_FOC_Start(CTRL_MODE_OPEN);
  176. phase_current_offset_calibrate();
  177. pwm_start();
  178. adc_start_convert();
  179. phase_current_calibrate_wait();
  180. PMSM_FOC_Set_Angle(0);
  181. PMSM_FOC_SetOpenVdq(vd, 0);
  182. delay_ms(2000);
  183. float phase = motor_encoder_zero_phase_detect();
  184. delay_ms(500);
  185. PMSM_FOC_SetOpenVdq(0, 0);
  186. delay_ms(500);
  187. adc_stop_convert();
  188. pwm_stop();
  189. PMSM_FOC_Stop();
  190. if (phase != INVALID_ANGLE) {
  191. nv_save_angle_offset(phase);
  192. return true;
  193. }
  194. return false;
  195. }
  196. bool mc_current_sensor_calibrate(float current) {
  197. if (!mc_start(CTRL_MODE_OPEN)) {
  198. return false;
  199. }
  200. phase_current_sensor_start_calibrate(current);
  201. phase_current_calibrate_wait();
  202. return true;
  203. }
  204. bool mc_lock_motor(bool lock) {
  205. if (lock && (PMSM_FOC_GetSpeed() > 10)) {
  206. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  207. return false;
  208. }
  209. PMSM_FOC_LockMotor(lock); //if mot enabled, foc core will do lock
  210. if (!motor.b_start) {
  211. if (lock) {
  212. pwm_start();
  213. pwm_update_duty(0, 0, 0);
  214. }else {
  215. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  216. pwm_stop();
  217. }
  218. }
  219. return true;
  220. }
  221. bool mc_throttle_released(void) {
  222. return get_throttle_float() < THROTTLE_LOW_VALUE;
  223. }
  224. static bool mc_is_hwbrake(void) {
  225. #ifdef GPIO_BRAKE_IN_GROUP
  226. int count = 50;
  227. int settimes = 0;
  228. while(count-- > 0) {
  229. bool b1 = gpio_input_bit_get(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_PIN) == SET;
  230. if (b1) {
  231. settimes ++;
  232. }
  233. delay_us(1);
  234. }
  235. if (settimes == 0) {
  236. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  237. return true;
  238. #else
  239. return false;
  240. #endif
  241. }else if (settimes == 50) {
  242. #if GPIO_BREAK_MODE==GPIO_LOW_BRK_MODE
  243. return false;
  244. #else
  245. return true;
  246. #endif
  247. }else {
  248. //有干扰,do nothing
  249. motor.n_brake_errors++;
  250. return false;
  251. }
  252. #else
  253. return false;
  254. #endif
  255. }
  256. void MC_Brake_IRQHandler(void) {
  257. if (!motor.b_start) {
  258. return;
  259. }
  260. if (mc_is_hwbrake()) {
  261. PMSM_FOC_Brake(true);
  262. }else {
  263. PMSM_FOC_Brake(false);
  264. }
  265. }
  266. void MC_Protect_IRQHandler(void){
  267. if (!motor.b_start) {
  268. return;
  269. }
  270. PMSM_FOC_Stop(); //三相50%占空比输出,防止mos过压击穿
  271. pwm_start();
  272. adc_start_convert();
  273. }
  274. measure_time_t g_meas_timeup = {.intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  275. void TIMER_UP_IRQHandler(void){
  276. //phase_current_adc_triger();
  277. time_measure_start(&g_meas_timeup);
  278. }
  279. measure_time_t g_meas_foc = {.exec_max_time = 20, .intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  280. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  281. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  282. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  283. void ADC_IRQHandler(void) {
  284. if (phase_current_offset()) {//check if is adc offset checked
  285. return;
  286. }
  287. if (phase_current_sensor_do_calibrate()){
  288. pwm_update_duty(100, FOC_PWM_Half_Period-100, 100);
  289. pwm_update_sample(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period+1, PHASE_BC);
  290. return;
  291. }
  292. TIME_MEATURE_START();
  293. PMSM_FOC_Schedule();
  294. TIME_MEATURE_END();
  295. }
  296. //#define ANGLE_TEST
  297. #ifdef ANGLE_TEST
  298. static void _debug_angle(void) {
  299. if (motor.b_start) {
  300. PMSM_FOC_Set_Angle(motor.s_testAngle);
  301. if (++motor.s_testAngle >= 360) {
  302. motor.s_testAngle = 0;
  303. }
  304. }
  305. }
  306. #endif
  307. /*FOC 的部分处理,比如速度环,状态机,转把采集等*/
  308. measure_time_t g_meas_MCTask;
  309. void Sched_MC_mTask(void) {
  310. time_measure_start(&g_meas_MCTask);
  311. u8 runMode = PMSM_FOC_CtrlMode();
  312. #if ANGLE_TEST
  313. _debug_angle();
  314. #endif
  315. if ((runMode != CTRL_MODE_OPEN) || (motor.mode != CTRL_MODE_OPEN)) {
  316. if (motor.mode != CTRL_MODE_OPEN) {
  317. u32 mask;
  318. if (mc_throttle_released() && PMSM_FOC_GetSpeed() == 0.0f) {
  319. mask = cpu_enter_critical();
  320. PMSM_FOC_Stop();
  321. pwm_disable_channel();
  322. cpu_exit_critical(mask);
  323. }else {
  324. mask = cpu_enter_critical();
  325. PMSM_FOC_Start(motor.mode);
  326. pwm_enable_channel();
  327. cpu_exit_critical(mask);
  328. }
  329. }
  330. if (runMode != CTRL_MODE_OPEN) {
  331. eCtrl_Running();
  332. float f_throttle = get_throttle_float();
  333. if (f_throttle != motor.throttle) {
  334. motor.throttle = f_throttle;
  335. torque_speed_target(runMode, f_throttle);
  336. }
  337. PMSM_FOC_idqCalc();
  338. }
  339. }
  340. }