foc_core.c 7.4 KB

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