foc_core.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "os/co_task.h"
  2. #include "bsp/pwm.h"
  3. #include "bsp/adc.h"
  4. #include "foc/core/foc_core.h"
  5. #include "foc/core/foc_api.h"
  6. #include "foc/core/foc_fsm.h"
  7. #include "foc/motor/current.h"
  8. #include "foc/core/park_clark.h"
  9. #include "foc/motor/hall.h"
  10. #include "foc/motor/ntc.h"
  11. #include "foc/motor/motor.h"
  12. #include "foc/core/circle_limitation.h"
  13. #include "foc/samples.h"
  14. #include "foc/core/svpwm.h"
  15. #include "bsp/timer_count32.h"
  16. #include "libs/logger.h"
  17. motor_foc_t g_foc = {
  18. .id_controller = {
  19. .Kp_gain = 9,
  20. .Ki_gain = 1071,
  21. .max_output = MAX_VBUS_VOLTAGE,
  22. .min_output = -MAX_VBUS_VOLTAGE,
  23. },
  24. .iq_controller = {
  25. .Kp_gain = 10,
  26. .Ki_gain = 1080,
  27. .max_output = MAX_VBUS_VOLTAGE,
  28. .min_output = -MAX_VBUS_VOLTAGE,
  29. },
  30. .speed_controller = {
  31. .Kp_gain = 1,
  32. .Ki_gain = 200,
  33. .max_output = MAX_CURRENT,
  34. .min_output = -MAX_CURRENT,
  35. },
  36. };
  37. static void foc_sample_task(void*);
  38. void foc_core_init(void) {
  39. samples_init();
  40. ntc_sensor_init();
  41. co_task_create(foc_sample_task, NULL, 256);
  42. }
  43. #if 1
  44. static void __inline foc_update_theta(motor_foc_t *foc) {
  45. foc->motor_stat.angle = hall_sensor_get_theta();
  46. foc->motor_stat.theta = degree_2_pi(foc->motor_stat.angle);
  47. }
  48. #else
  49. static void __inline foc_update_theta(motor_foc_t *foc) {
  50. static float angle = 0.0f;
  51. static bool first_s = false;
  52. if (!first_s) {
  53. first_s = true;
  54. angle = hall_sensor_get_theta();
  55. }else {
  56. angle += 0.5f;
  57. }
  58. fast_norm_angle(&angle);
  59. foc->motor_s.angle = angle;
  60. foc->motor_s.theta = degree_2_pi(angle);
  61. }
  62. #endif
  63. static void __inline foc_calc_voltage(motor_foc_t *foc, dq_t *sampled, dq_t *ref_out) {
  64. //float vd = pi_control(&foc->PI_id, foc->dq_ref.Id - sampled->Id);
  65. //float vq = pi_control(&foc->PI_iq, foc->dq_ref.Iq - sampled->Iq);
  66. if (foc->mode == FOC_MODE_CURRENT_LOOP || foc->mode == FOC_MODE_CLOSE_LOOP) {
  67. ref_out->Vd = pi_control(&foc->id_controller, foc->dq_command.Id - sampled->Id);
  68. ref_out->Vq = pi_control(&foc->iq_controller, foc->dq_command.Iq - sampled->Iq);
  69. //printf("vd = %f, vq = %f\n", vd, vq);
  70. }else {
  71. ref_out->Vd = foc->dq_command.Vd;
  72. ref_out->Vq = foc->dq_command.Vq;
  73. }
  74. foc->dq_last.Vd = ref_out->Vd;
  75. foc->dq_last.Vq = ref_out->Vq;
  76. }
  77. static void __inline deadtime_compensation(current_samp_t *c_sample, phase_time_t *time) {
  78. #if 0
  79. /* Dead time compensation */
  80. if ( c_sample->Ia > 0)
  81. {
  82. time->A -= TDead;
  83. }
  84. else
  85. {
  86. time->A += TDead;
  87. }
  88. if ( c_sample->Ib > 0 )
  89. {
  90. time->B -= TDead;
  91. }
  92. else
  93. {
  94. time->B += TDead;
  95. }
  96. if ( c_sample->Ic > 0 )
  97. {
  98. time->C -= TDead;
  99. }
  100. else
  101. {
  102. time->C += TDead;
  103. }
  104. #endif
  105. }
  106. static void __inline Debug_Log(motor_foc_t *foc){
  107. #if 0
  108. static int count;
  109. if (count++ % 10 == 0) {
  110. //printf("$%d %d %d %d %d;",(int)(foc->current_samp.Ia * 1000.0f), (int)(foc->current_samp.Ib * 1000.0f),
  111. // (int)(foc->current_samp.Ic * 1000.0f), (int)foc->sector * 100, (int)foc->motor_s.angle);
  112. printf("$%d;", (int)hall_sensor_get_speed());
  113. }
  114. #endif
  115. }
  116. static void __inline Debug_dq(dq_t *dq){
  117. #if 0
  118. static int count;
  119. if (count++ % 10 == 0) {
  120. printf("$%d %d;",(int)(dq->d * 1000.0f), (int)(dq->q * 1000.0f));
  121. }
  122. #endif
  123. }
  124. /* FOC 主控制任务 */
  125. void do_motor_foc(motor_foc_t *foc){
  126. current_samp_t *c_sample = &foc->current_samp;
  127. alpha_beta_t sample_ab, pwm_ab;
  128. dq_t sample_dq, v_dq;
  129. pwm_clear_updata();
  130. /* 更新电角度 */
  131. foc_update_theta(foc);
  132. /* 采集相电流 */
  133. phase_current_sample(c_sample);
  134. /* ABC三相坐标到alpha-beta坐标 */
  135. do_clark(c_sample->Ia, c_sample->Ib, c_sample->Ic, &sample_ab);
  136. /* alpha-beta坐标系到D-Q旋转坐标系 */
  137. do_park(&sample_ab, foc->motor_stat.theta, &sample_dq);
  138. /* 电流环,输出电压给SVPWM */
  139. foc_calc_voltage(foc, &sample_dq, &v_dq);
  140. /* 确保电压在6个扇区的内切圆中 */
  141. circle_limitation(&v_dq, foc->vbus, 0.96f);
  142. /* d-q坐标系到alpha-beta坐标系,输出给svpwm */
  143. Rev_Park(&v_dq, foc->motor_stat.theta, &pwm_ab);
  144. /* SVPWM,获取三相逆变器的开关时间,用的是pwm1模式,如果是pwm2模式,这个函数需要修改 */
  145. svpwm_get_phase_time(&pwm_ab, foc->vbus, FOC_PWM_Half_Period, &c_sample->time, &foc->sector);
  146. /* 计算三相电流的采样点 */
  147. get_phase_sample_point(c_sample, foc->sector);
  148. /* 死区补偿 */
  149. deadtime_compensation(c_sample, &c_sample->time);
  150. /* 更新 TIM1的CCR0-2,生成互补pwm, 相电流更新采样点 */
  151. motor_update_duty(c_sample->time.A, c_sample->time.B, c_sample->time.C, c_sample->time.A_next, c_sample->time.B_next, c_sample->time.C_next);
  152. /* 更新采样点 */
  153. motor_update_sample(c_sample->time.Samp_p1, c_sample->time.Samp_p2, c_sample->sector);
  154. Debug_Log(foc);
  155. Debug_dq(&sample_dq);
  156. }
  157. /* 计算电流环的参考输入 */
  158. void foc_calc_current(motor_foc_t *foc) {
  159. float speed_ref = ramp_get_target(&foc->speed_ramp);
  160. float speed_feedback = foc_get_speed();
  161. float vq_out = pi_control(&foc->speed_controller, speed_ref - speed_feedback);
  162. if (foc->mode == FOC_MODE_SPEED_LOOP || foc->mode == FOC_MODE_CLOSE_LOOP){
  163. foc->dq_command.Iq = vq_out;
  164. foc->dq_command.Id = 0.0f; //if MTPA used, d is not 0
  165. }else {
  166. foc->dq_command.Iq = ramp_get_target(&foc->current_ramp);
  167. foc->dq_command.Id = 0.0f; //if MTPA used, d is not 0
  168. }
  169. }
  170. void foc_speed_ramp(motor_foc_t *foc){
  171. if (foc->speed_command.speed >= 0 && foc->mode != FOC_MODE_OPEN_LOOP){
  172. u16 current_rpm = foc_get_speed();
  173. u16 ref_rpm = foc->speed_command.speed;
  174. foc->speed_command.speed = -1;
  175. if (ref_rpm + RPM_FOR_CLOSE_LOOP < current_rpm){
  176. foc_set_voltage_ramp(speed_to_voltage(ref_rpm));
  177. foc->mode = FOC_MODE_OPEN_LOOP;
  178. }else {
  179. foc_set_speed_ramp(ref_rpm);
  180. }
  181. }
  182. }
  183. void foc_brake_handler(bool brake) {
  184. g_foc.is_brake_in = brake;
  185. }
  186. void foc_pwm_up_handler(void){
  187. phase_current_adc_triger(&g_foc.current_samp);
  188. }
  189. measure_time_t g_meas_foc = {.exec_max_time = 15,};
  190. /*ADC 电流采集中断,调用FOC的核心处理函数*/
  191. void mc_phase_current_irq(void) {
  192. if (g_foc.current_samp.is_calibrating_offset) {
  193. phase_current_offset(&g_foc.current_samp);
  194. return;
  195. }
  196. time_measure_start(&g_meas_foc);
  197. do_motor_foc(&g_foc);
  198. time_measure_end(&g_meas_foc);
  199. if (g_meas_foc.intval_time < 62 || g_meas_foc.intval_time > 62) {
  200. //log_chan_value(2, g_meas_foc.intval_time);
  201. }
  202. }
  203. void foc_pwm_start(bool start) {
  204. if (start == g_foc.mosfec_gate) {
  205. return;
  206. }
  207. if (start) {
  208. pwm_start();
  209. }else {
  210. pwm_stop();
  211. }
  212. g_foc.mosfec_gate = start;
  213. }
  214. /*10ms 定时任务,主要处理foc状态机(里面包含速度环)*/
  215. void foc_normal_task(void) {
  216. foc_fsm(&g_foc);
  217. }
  218. static void foc_sample_task(void *args){
  219. // u64 ts = co_task_sys64_ts();
  220. while(1) {
  221. ntc_sensor_sample();
  222. g_foc.vbus = get_vbus_sample();
  223. wdog_reload();
  224. co_task_yield();
  225. }
  226. }