motor.c 7.7 KB

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