motor.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "foc/motor/motor.h"
  2. #include "foc/motor/current.h"
  3. #include "foc/foc_config.h"
  4. #include "foc/samples.h"
  5. #include "math/fast_math.h"
  6. #include "bsp/timer_count32.h"
  7. #include "libs/time_measure.h"
  8. #include "bsp/delay.h"
  9. #include "bsp/bsp.h"
  10. #include "bsp/adc.h"
  11. #include "bsp/pwm.h"
  12. #include "foc/commands.h"
  13. #include "libs/logger.h"
  14. #include "bsp/sched_timer.h"
  15. #include "foc/core/e_ctrl.h"
  16. #include "foc/motor/motor_param.h"
  17. static motor_t motor = {
  18. .s_direction = POSITIVE,
  19. };
  20. void mc_init(void) {
  21. adc_init();
  22. pwm_3phase_init();
  23. samples_init();
  24. motor_encoder_init();
  25. foc_command_init();
  26. PMSM_FOC_CoreInit();
  27. sched_timer_enable(SPD_CTRL_MS);
  28. }
  29. motor_t * mc_params(void) {
  30. return &motor;
  31. }
  32. bool mc_start(u8 mode) {
  33. if (motor.b_start) {
  34. return true;
  35. }
  36. if (mode > CTRL_MODE_CURRENT) {
  37. PMSM_FOC_SetErrCode(FOC_Param_Err);
  38. return false;
  39. }
  40. if (PMSM_FOC_GetSpeed() > 10.0f) {
  41. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  42. return false;
  43. }
  44. if (!mc_throttle_released()) {
  45. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  46. return false;
  47. }
  48. eCtrl_init(1000, 2000);
  49. motor_encoder_start(motor.s_direction);
  50. PMSM_FOC_Start(mode);
  51. pwm_turn_on_low_side();
  52. task_udelay(500);
  53. phase_current_start_cali();
  54. pwm_start();
  55. adc_start_convert();
  56. phase_current_wait_cali();
  57. motor.throttle = 0;
  58. motor.b_start = true;
  59. gpio_led2_enable(true);
  60. return true;
  61. }
  62. bool mc_stop(void) {
  63. if (!motor.b_start) {
  64. return true;
  65. }
  66. if (PMSM_FOC_GetSpeed() > 10.0f) {
  67. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  68. return false;
  69. }
  70. if (!mc_throttle_released()) {
  71. PMSM_FOC_SetErrCode(FOC_Throttle_Err);
  72. return false;
  73. }
  74. adc_stop_convert();
  75. pwm_stop();
  76. PMSM_FOC_Stop();
  77. motor.b_start = false;
  78. gpio_led2_enable(false);
  79. return true;
  80. }
  81. void mc_set_spd_torque(s32 target) {
  82. motor.b_ignor_throttle = true;
  83. motor.s_targetFix = target;
  84. }
  85. void mc_use_throttle(void) {
  86. motor.b_ignor_throttle = false;
  87. }
  88. void mc_encoder_calibrate(s16 vd) {
  89. if (PMSM_FOC_Is_Start()) {
  90. return;
  91. }
  92. pwm_turn_on_low_side();
  93. task_udelay(500);
  94. PMSM_FOC_Start(CTRL_MODE_OPEN);
  95. phase_current_start_cali();
  96. pwm_start();
  97. adc_start_convert();
  98. phase_current_wait_cali();
  99. PMSM_FOC_Set_Angle(0);
  100. PMSM_FOC_SetOpenVdq(vd, 0);
  101. delay_ms(3000);
  102. for (int i = 0; i < 50000000; i++) {
  103. for (s16 angle = 0; angle < 360; angle++) {
  104. PMSM_FOC_Set_Angle(angle);
  105. motor_encoder_offset(angle);
  106. delay_us(1000);
  107. }
  108. }
  109. delay_ms(500);
  110. PMSM_FOC_SetOpenVdq(0, 0);
  111. delay_ms(500);
  112. adc_stop_convert();
  113. pwm_stop();
  114. PMSM_FOC_Stop();
  115. motor_encoder_offset_finish();
  116. }
  117. bool mc_lock_motor(bool lock) {
  118. if (lock && (PMSM_FOC_GetSpeed() > 10)) {
  119. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  120. return false;
  121. }
  122. PMSM_FOC_LockMotor(lock); //if mot enabled, foc core will do lock
  123. if (!motor.b_start) {
  124. if (lock) {
  125. pwm_start();
  126. pwm_update_duty(0, 0, 0);
  127. }else {
  128. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  129. pwm_stop();
  130. }
  131. }
  132. return true;
  133. }
  134. bool mc_throttle_released(void) {
  135. return get_throttle_float() < THROTTLE_LOW_VALUE;
  136. }
  137. static float _get_speed_from_throttle(void) {
  138. if (motor.b_ignor_throttle) {
  139. return motor.s_targetFix;
  140. }
  141. float f_throttle = motor.throttle;
  142. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  143. return 0;
  144. }
  145. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  146. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  147. return (THROTTLE_MIN_RPM + PMSM_FOC_GetSpeedLimit() * ration);
  148. }
  149. static float _get_idq_from_throttle(void) {
  150. if (motor.b_ignor_throttle) {
  151. return motor.s_targetFix;
  152. }
  153. float f_throttle = motor.throttle;
  154. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  155. return 0;
  156. }
  157. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  158. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  159. return (THROTTLE_MIN_IDQ + MAX_iDQ * ration);
  160. }
  161. /*do 50 times filter*/
  162. static void brake_timer_handler(shark_timer_t *t) {
  163. int count = 50;
  164. int settimes = 0;
  165. while(count-- >= 0) {
  166. bool b1 = gpio_input_bit_get(GPIOB, GPIO_PIN_4) == SET;
  167. bool b2 = gpio_input_bit_get(GPIOB, GPIO_PIN_5) == SET;
  168. if (b1 && b2) {
  169. settimes++;
  170. }
  171. }
  172. if (settimes == 0) {
  173. PMSM_FOC_Brake(true);
  174. }else if (settimes == 50) {
  175. PMSM_FOC_Brake(false);
  176. }else {
  177. //有干扰,do nothing
  178. }
  179. }
  180. static shark_timer_t _brake_timer = TIMER_INIT(_brake_timer, brake_timer_handler);
  181. void MC_Brake_IRQHandler(void){
  182. shark_timer_post(&_brake_timer, 0);
  183. }
  184. measure_time_t g_meas_timeup = {.intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  185. void TIMER_UP_IRQHandler(void){
  186. //phase_current_adc_triger();
  187. time_measure_start(&g_meas_timeup);
  188. }
  189. measure_time_t g_meas_foc = {.exec_max_time = 20, .intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  190. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  191. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  192. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  193. void ADC_IRQHandler(void) {
  194. if (phase_current_offset()) {//check if is adc offset checked
  195. return;
  196. }
  197. TIME_MEATURE_START();
  198. PMSM_FOC_Schedule();
  199. TIME_MEATURE_END();
  200. }
  201. //#define ANGLE_TEST
  202. #ifdef ANGLE_TEST
  203. static void _debug_angle(void) {
  204. if (motor.b_start) {
  205. PMSM_FOC_Set_Angle(motor.s_testAngle);
  206. if (++motor.s_testAngle >= 360) {
  207. motor.s_testAngle = 0;
  208. }
  209. }
  210. }
  211. #endif
  212. /*FOC 的部分处理,比如速度环,状态机,转把采集等*/
  213. void Sched_MC_mTask(void) {
  214. u8 runMode = PMSM_FOC_CtrlMode();
  215. #if ANGLE_TEST
  216. _debug_angle();
  217. #endif
  218. if (runMode != CTRL_MODE_OPEN) {
  219. eCtrl_Running();
  220. if (get_throttle_float() != motor.throttle) {
  221. motor.throttle = get_throttle_float();
  222. if (runMode == CTRL_MODE_SPD) {
  223. float speed_Ref = _get_speed_from_throttle();
  224. PMSM_FOC_Set_Speed(speed_Ref);
  225. }else if (runMode == CTRL_MODE_TRQ) {
  226. float torque_idq = _get_idq_from_throttle();
  227. PMSM_FOC_Set_Torque(torque_idq);
  228. }
  229. }
  230. PMSM_FOC_idqCalc();
  231. }
  232. }