foc_core.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "hal/hal.h"
  2. #include "hal/pwm.h"
  3. #include "libs/task.h"
  4. #include "foc_core.h"
  5. #include "foc_api.h"
  6. #include "foc_stm.h"
  7. #include "phase_current.h"
  8. #include "park_clark.h"
  9. #include "hall_sensor.h"
  10. #include "circle_limitation.h"
  11. #include "svpwm.h"
  12. motor_foc_t mFOC = {
  13. .motor_p = {
  14. .poles = 2,
  15. .ld = 0.578477f,
  16. .lq = 5.78477f,
  17. .rs = 1.088f,
  18. .inertia = 3.319367f,
  19. .b_emf = 4.332566f,
  20. },
  21. .PI_id = {
  22. .Kp_gain = 5,
  23. .Ki_gain = 100,
  24. .max_output = MAX_VBUS_VOLTAGE,
  25. .min_output = -MAX_VBUS_VOLTAGE,
  26. },
  27. .PI_iq = {
  28. .Kp_gain = 5,
  29. .Ki_gain = 100,
  30. .max_output = MAX_VBUS_VOLTAGE,
  31. .min_output = -MAX_VBUS_VOLTAGE,
  32. },
  33. .PI_speed = {
  34. .Kp_gain = 5,
  35. .Ki_gain = 100,
  36. .max_output = MAX_CURRENT,
  37. .min_output = -MAX_CURRENT,
  38. },
  39. };
  40. #if 1
  41. static void __inline foc_update_theta(motor_foc_t *foc) {
  42. float angle = 0.0f;
  43. if (foc->override.is_theta) {
  44. angle = foc->override.theta;
  45. }else {
  46. angle = hall_sensor_get_theta();
  47. }
  48. foc->motor_s.angle = angle;
  49. foc->motor_s.theta = degree_2_pi(foc->motor_s.angle);
  50. }
  51. #else
  52. static void __inline foc_update_theta(motor_foc_t *foc) {
  53. static float angle = 0.0f;
  54. static bool first_s = false;
  55. if (!first_s) {
  56. first_s = true;
  57. angle = hall_sensor_get_theta();
  58. }else {
  59. angle += 0.5f;
  60. }
  61. fast_norm_angle(&angle);
  62. foc->motor_s.angle = angle;
  63. foc->motor_s.theta = degree_2_pi(angle);
  64. }
  65. #endif
  66. static void __inline Foc_Current_PI_Contrl(motor_foc_t *foc, dq_t *sampled, dq_t *ref_out) {
  67. if (foc->mode == FOC_MODE_PI_CURRENT || foc->mode == FOC_MODE_PI_FULL) {
  68. ref_out->Vd = pi_control(&foc->PI_id, foc->dq_ref.Id - sampled->Id);
  69. ref_out->Vq = pi_control(&foc->PI_iq, foc->dq_ref.Iq - sampled->Iq);
  70. }else {
  71. ref_out->Vd = foc->dq_ref.Vd;
  72. ref_out->Vq = foc->dq_ref.Vq;
  73. }
  74. if (foc->override.is_vdq) {
  75. ref_out->Vd = foc->override.vdq.Vd;
  76. ref_out->Vq = foc->override.vdq.Vq;
  77. }
  78. foc->dq_v.Vd = ref_out->Vd;
  79. foc->dq_v.Vq = ref_out->Vq;
  80. }
  81. static void __inline DeadTime_Compensation(current_samp_t *c_sample, phase_time_t *time) {
  82. #if 0
  83. /* Dead time compensation */
  84. if ( c_sample->Ia > 0)
  85. {
  86. time->A += TDead;
  87. }
  88. else
  89. {
  90. time->A -= TDead;
  91. }
  92. if ( c_sample->Ib > 0 )
  93. {
  94. time->B += TDead;
  95. }
  96. else
  97. {
  98. time->B -= TDead;
  99. }
  100. if ( c_sample->Ic > 0 )
  101. {
  102. time->C += TDead;
  103. }
  104. else
  105. {
  106. time->C -= TDead;
  107. }
  108. #endif
  109. }
  110. static void __inline Debug_Log(motor_foc_t *foc){
  111. #if 0
  112. static int count;
  113. if (count++ % 10 == 0) {
  114. //printf("$%d %d %d %d %d;",(int)(foc->current_samp.Ia * 1000.0f), (int)(foc->current_samp.Ib * 1000.0f),
  115. // (int)(foc->current_samp.Ic * 1000.0f), (int)foc->sector * 100, (int)foc->motor_s.angle);
  116. printf("$%d;", (int)hall_sensor_get_speed());
  117. }
  118. #endif
  119. }
  120. static void __inline Debug_dq(dq_t *dq){
  121. #if 0
  122. static int count;
  123. if (count++ % 10 == 0) {
  124. printf("$%d %d;",(int)(dq->d * 1000.0f), (int)(dq->q * 1000.0f));
  125. }
  126. #endif
  127. }
  128. #if defined (CCMRAM)
  129. #if defined (__ICCARM__)
  130. #pragma location = ".ccmram"
  131. #elif defined (__CC_ARM)
  132. __attribute__( ( section ( ".ccmram" ) ) )
  133. #endif
  134. #endif
  135. /* FOC 主控制任务 */
  136. void FOC_Fast_Task(motor_foc_t *foc){
  137. current_samp_t *c_sample = &foc->current_samp;
  138. alpha_beta_t sample_ab, pwm_ab;
  139. dq_t sample_dq, v_dq;
  140. phase_time_t phase_time;
  141. u32 sample_point;
  142. /* 更新电角度 */
  143. foc_update_theta(foc);
  144. /* 采集相电流 */
  145. phase_current_sample(c_sample);
  146. /* ABC三相坐标到alpha-beta坐标 */
  147. Clark(c_sample->Ia, c_sample->Ib, c_sample->Ic, &sample_ab);
  148. /* alpha-beta坐标系到D-Q旋转坐标系 */
  149. Park(&sample_ab, foc->motor_s.theta, &sample_dq);
  150. /* 电流环,输出电压给SVPWM */
  151. Foc_Current_PI_Contrl(foc, &sample_dq, &v_dq);
  152. /* 确保电压在6个扇区的内切圆中 */
  153. CirCle_Limitation_Process(&v_dq, foc->vbus, 0.95f);
  154. /* d-q坐标系到alpha-beta坐标系,输出给svpwm */
  155. Rev_Park(&v_dq, foc->motor_s.theta, &pwm_ab);
  156. /* SVPWM,获取三相逆变器的开关时间,用的是pwm1模式,如果是pwm2模式,这个函数需要修改 */
  157. SVM_Get_Phase_Time(&pwm_ab, foc->vbus, FOC_PWM_Half_Period, &phase_time, &foc->sector);
  158. /* 计算三相电流的采样点 */
  159. sample_point = get_phase_sample_point(c_sample, &phase_time, foc->sector);
  160. /* 死区补偿 */
  161. DeadTime_Compensation(c_sample, &phase_time);
  162. /* 更新 TIM1的CCR0-2,生成互补pwm */
  163. PWM_UpdateDuty(phase_time.A, phase_time.B, phase_time.C, sample_point);
  164. Debug_Log(foc);
  165. Debug_dq(&sample_dq);
  166. }
  167. /* 计算电流环的参考输入 */
  168. void Foc_Calc_Current_Ref(motor_foc_t *foc) {
  169. if (foc->mode == FOC_MODE_PI_SPEED || foc->mode == FOC_MODE_PI_FULL){
  170. float speed_ref = ramp_get_target(&foc->speed_ramp);
  171. float speed_feedback = foc_get_speed();
  172. float vq_out = pi_control(&foc->PI_speed, speed_ref - speed_feedback);
  173. foc->dq_ref.Iq = vq_out;
  174. foc->dq_ref.Id = 0.0f; //if MTPA used, d is not 0
  175. }else {
  176. foc->dq_ref.Iq = ramp_get_target(&foc->current_ramp);
  177. foc->dq_ref.Id = 0.0f; //if MTPA used, d is not 0
  178. }
  179. }
  180. void Foc_Speed_Ramp(motor_foc_t *foc){
  181. if (foc->rpm_ref >= 0 && foc->mode != FOC_MODE_PI_CURRENT){
  182. u16 current_rpm = foc_get_speed();
  183. u16 ref_rpm = foc->rpm_ref;
  184. foc->rpm_ref = -1;
  185. if (ref_rpm + 60 < current_rpm){
  186. ramp_set_target(&foc->current_ramp, foc->dq_ref.Iq, speed_to_current(ref_rpm), SPEED_RAMP_DURATION);
  187. ramp_exc(&foc->current_ramp);
  188. foc->mode = FOC_MODE_PI_CURRENT;
  189. }
  190. }
  191. }
  192. void foc_brake_handler(void) {
  193. mFOC.foc_fault = foc_brake_error;
  194. }
  195. void foc_pwm_up_handler(void){
  196. phase_current_adc_triger(&mFOC.current_samp);
  197. }
  198. #if defined (CCMRAM)
  199. #if defined (__ICCARM__)
  200. #pragma location = ".ccmram"
  201. #elif defined (__CC_ARM)
  202. __attribute__( ( section ( ".ccmram" ) ) )
  203. #endif
  204. #endif
  205. void current_sample_handler(void) {
  206. if (mFOC.current_samp.is_calibrating) {
  207. phase_current_offset(&mFOC.current_samp);
  208. }else {
  209. FOC_Fast_Task(&mFOC);
  210. }
  211. }
  212. void foc_slow_task_handler(void) {
  213. FOC_Normal_Task(&mFOC);
  214. }
  215. void foc_pwm_start(bool start) {
  216. if (start == mFOC.mosGate) {
  217. return;
  218. }
  219. if (start) {
  220. PWM_Start();
  221. }else {
  222. PWM_Stop();
  223. }
  224. mFOC.mosGate = start;
  225. }