motor.c 5.8 KB

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