foc_core.c 6.3 KB

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