motor.c 7.1 KB

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