motor.c 5.3 KB

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