foc_core.c 6.8 KB

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