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(float f_throttle) {
  138. if (motor.b_ignor_throttle) {
  139. return motor.s_targetFix;
  140. }
  141. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  142. return 0;
  143. }
  144. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  145. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  146. return (PMSM_FOC_GetSpeedLimit() * ration);
  147. }
  148. static float _get_torque_from_throttle(float f_throttle) {
  149. if (motor.b_ignor_throttle) {
  150. return motor.s_targetFix;
  151. }
  152. if (f_throttle <= (THROTTLE_LOW_VALUE)) {
  153. return 0;
  154. }
  155. float delta = f_throttle - (THROTTLE_LOW_VALUE);
  156. float ration = delta / (THROTTLE_MAX_VALUE - THROTTLE_LOW_VALUE);
  157. return (MAX_TORQUE * ration);
  158. }
  159. /*do 50 times filter*/
  160. static void brake_timer_handler(shark_timer_t *t) {
  161. int count = 50;
  162. int settimes = 0;
  163. while(count-- >= 0) {
  164. bool b1 = gpio_input_bit_get(GPIOB, GPIO_PIN_4) == SET;
  165. bool b2 = gpio_input_bit_get(GPIOB, GPIO_PIN_5) == SET;
  166. if (b1 && b2) {
  167. settimes++;
  168. }
  169. }
  170. if (settimes == 0) {
  171. PMSM_FOC_Brake(true);
  172. }else if (settimes == 50) {
  173. PMSM_FOC_Brake(false);
  174. }else {
  175. //有干扰,do nothing
  176. }
  177. }
  178. static shark_timer_t _brake_timer = TIMER_INIT(_brake_timer, brake_timer_handler);
  179. void MC_Brake_IRQHandler(void){
  180. shark_timer_post(&_brake_timer, 0);
  181. }
  182. measure_time_t g_meas_timeup = {.intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  183. void TIMER_UP_IRQHandler(void){
  184. //phase_current_adc_triger();
  185. time_measure_start(&g_meas_timeup);
  186. }
  187. measure_time_t g_meas_foc = {.exec_max_time = 20, .intval_max_time = 62, .intval_low_err = 0, .intval_hi_err = 0, .first = true,};
  188. #define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
  189. #define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
  190. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  191. void ADC_IRQHandler(void) {
  192. if (phase_current_offset()) {//check if is adc offset checked
  193. return;
  194. }
  195. TIME_MEATURE_START();
  196. PMSM_FOC_Schedule();
  197. TIME_MEATURE_END();
  198. }
  199. //#define ANGLE_TEST
  200. #ifdef ANGLE_TEST
  201. static void _debug_angle(void) {
  202. if (motor.b_start) {
  203. PMSM_FOC_Set_Angle(motor.s_testAngle);
  204. if (++motor.s_testAngle >= 360) {
  205. motor.s_testAngle = 0;
  206. }
  207. }
  208. }
  209. #endif
  210. /*FOC 的部分处理,比如速度环,状态机,转把采集等*/
  211. void Sched_MC_mTask(void) {
  212. u8 runMode = PMSM_FOC_CtrlMode();
  213. #if ANGLE_TEST
  214. _debug_angle();
  215. #endif
  216. if (runMode != CTRL_MODE_OPEN) {
  217. eCtrl_Running();
  218. float f_throttle = get_throttle_float();
  219. if (f_throttle != motor.throttle) {
  220. motor.throttle = f_throttle;
  221. if (runMode == CTRL_MODE_SPD) {
  222. float speed_Ref = _get_speed_from_throttle(f_throttle);
  223. PMSM_FOC_Set_Speed(speed_Ref);
  224. }else if (runMode == CTRL_MODE_TRQ) {
  225. float torque = _get_torque_from_throttle(f_throttle);
  226. PMSM_FOC_Set_Torque(torque);
  227. }
  228. }
  229. PMSM_FOC_idqCalc();
  230. }
  231. }