foc_core.c 6.2 KB

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