dbc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /************************************************************************
  2. Project: Welling Motor Control Paltform
  3. Filename: dbc.c
  4. Partner Filename: dbc.h
  5. Description: Dead time compensation of three-phase voltage inverter
  6. Complier: IAR Embedded Workbench for ARM 7.80, IAR Systems.
  7. CPU TYPE : GD32F3x0
  8. *************************************************************************
  9. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  10. All rights reserved.
  11. *************************************************************************
  12. *************************************************************************
  13. Revising History (ECL of this file):
  14. ************************************************************************/
  15. /************************************************************************
  16. Beginning of File, do not put anything above here except notes
  17. Compiler Directives:
  18. *************************************************************************/
  19. #ifndef _DBC_C_
  20. #define _DBC_C_
  21. #endif
  22. /************************************************************************
  23. Included File:
  24. *************************************************************************/
  25. #include "dbc.h"
  26. #include "glbcof.h"
  27. /************************************************************************
  28. Function: dbc_voDBCompCoef;
  29. Description: deadband compensation coefficient initial;
  30. Call by: functions in main loop;
  31. Input Variables: DBC_COF_IN;
  32. Output/Return Variables: DBC_CALC_COF;
  33. Subroutine Call: N/A;
  34. Reference: N/A;
  35. *************************************************************************/
  36. ULONG ulKcoef;
  37. SLONG slPWMPerNs;
  38. SLONG slPosLostVoltNs;
  39. SLONG slNegWinVoltNs;
  40. void dbc_voDBCompCoef(DBC_COF_IN *in, DBC_CALC_COF *out)
  41. {
  42. // ULONG ulKcoef;
  43. // SLONG slPWMPerNs;
  44. // SLONG slPosLostVoltNs;
  45. // SLONG slNegWinVoltNs;
  46. slPWMPerNs = in->ulPWMPerUs * 100;
  47. if (slPWMPerNs < 100)
  48. {
  49. slPWMPerNs = 100;
  50. }
  51. if (in->uwDeadBandTimeNs > 5000)
  52. {
  53. in->uwDeadBandTimeNs = 5000;
  54. }
  55. if (in->uwPosSwOnTimeNs > 3000)
  56. {
  57. in->uwPosSwOnTimeNs = 3000;
  58. }
  59. if (in->uwPosSwOffTimeNs > 3000)
  60. {
  61. in->uwPosSwOffTimeNs = 3000;
  62. }
  63. if (in->uwNegSwOnTimeNs > 3000)
  64. {
  65. in->uwNegSwOnTimeNs = 3000;
  66. }
  67. if (in->uwNegSwOffTimeNs > 3000)
  68. {
  69. in->uwNegSwOffTimeNs = 3000;
  70. }
  71. if (in->uwKcoefVtPerAp > 1000)
  72. {
  73. in->uwKcoefVtPerAp = 1000;
  74. }
  75. if (in->uwIBaseAp > 10000)
  76. {
  77. in->uwIBaseAp = 10000;
  78. }
  79. if (in->uwVBaseVt > 10000)
  80. {
  81. in->uwVBaseVt = 10000;
  82. }
  83. else if (in->uwVBaseVt < 10)
  84. {
  85. in->uwVBaseVt = 10;
  86. }
  87. slPosLostVoltNs = (SLONG)in->uwDeadBandTimeNs + in->uwPosSwOnTimeNs - in->uwPosSwOffTimeNs;
  88. if (slPosLostVoltNs > (slPWMPerNs >> 1))
  89. {
  90. slPosLostVoltNs = (slPWMPerNs >> 1);
  91. }
  92. else if (slPosLostVoltNs < 0)
  93. {
  94. slPosLostVoltNs = 0;
  95. }
  96. out->uwPosLostVoltDuty = (slPosLostVoltNs << 14) / slPWMPerNs; // Q14
  97. slNegWinVoltNs = (SLONG)in->uwDeadBandTimeNs + in->uwNegSwOnTimeNs - in->uwNegSwOffTimeNs;
  98. if (slNegWinVoltNs > (slPWMPerNs >> 1))
  99. {
  100. slNegWinVoltNs = (slPWMPerNs >> 1);
  101. }
  102. else if (slNegWinVoltNs < 0)
  103. {
  104. slNegWinVoltNs = 0;
  105. }
  106. out->uwNegWinVoltDuty = (slNegWinVoltNs << 14) / slPWMPerNs; // Q14
  107. ulKcoef = (((ULONG)in->uwKcoefVtPerAp * in->uwIBaseAp) << 6) / in->uwVBaseVt / 100; // Q6, Deadband compensation slope coefficient
  108. if (ulKcoef > 32767)
  109. {
  110. ulKcoef = 32767;
  111. }
  112. else
  113. {
  114. out->uwKcoef = ulKcoef;
  115. }
  116. }
  117. /************************************************************************
  118. Function: dbc_voDBComp;
  119. Description: voltage composation for deadband;
  120. Call by: functions in TBC;
  121. Input Variables: DBC_CALC_IN, DBC_CALC_COF;
  122. Output/Return Variables: DBC_CALC_OUT;
  123. Subroutine Call: N/A;
  124. Reference: N/A;
  125. *************************************************************************/
  126. void dbc_voDBComp(DBC_CALC_IN *in, DBC_CALC_COF *coef, DBC_CALC_OUT *out)
  127. {
  128. SLONG slUaCompPrePu, slUaCompPu, slUbCompPrePu, slUbCompPu, slUcCompPrePu, slUcCompPu, slPosLostVoltPu, slNegWinVoltPu;
  129. if (in->swWsPu > cof_uw100RpmPu)
  130. {
  131. slPosLostVoltPu = ((SLONG)in->uwVdcPu * coef->uwPosLostVoltDuty >> 14); // Q14=Q14+Q14-Q14
  132. slNegWinVoltPu = ((SLONG)in->uwVdcPu * coef->uwNegWinVoltDuty >> 14); // Q14=Q14+Q14-Q14
  133. slUaCompPrePu = (SLONG)in->swIaPu * coef->uwKcoef >> 6; // Q14=Q14+Q6-Q6
  134. slUbCompPrePu = (SLONG)in->swIbPu * coef->uwKcoef >> 6; // Q14=Q14+Q6-Q6
  135. slUcCompPrePu = (SLONG)in->swIcPu * coef->uwKcoef >> 6; // Q14=Q14+Q6-Q6
  136. if (slPosLostVoltPu > 32767)
  137. {
  138. slPosLostVoltPu = 32767;
  139. }
  140. else if (slPosLostVoltPu < -32768)
  141. {
  142. slPosLostVoltPu = -32768;
  143. }
  144. if (slNegWinVoltPu > 32767)
  145. {
  146. slNegWinVoltPu = 32767;
  147. }
  148. else if (slNegWinVoltPu < -32768)
  149. {
  150. slNegWinVoltPu = -32768;
  151. }
  152. /* Uacomp, Ubcomp, Uccomp limination */
  153. if (slUaCompPrePu > slPosLostVoltPu)
  154. {
  155. slUaCompPu = slPosLostVoltPu;
  156. }
  157. else if (slUaCompPrePu < -slNegWinVoltPu)
  158. {
  159. slUaCompPu = -slNegWinVoltPu;
  160. }
  161. else
  162. {
  163. slUaCompPu = slUaCompPrePu;
  164. }
  165. if (slUbCompPrePu > slPosLostVoltPu)
  166. {
  167. slUbCompPu = slPosLostVoltPu;
  168. }
  169. else if (slUbCompPrePu < -slNegWinVoltPu)
  170. {
  171. slUbCompPu = -slNegWinVoltPu;
  172. }
  173. else
  174. {
  175. slUbCompPu = slUbCompPrePu;
  176. }
  177. if (slUcCompPrePu > slPosLostVoltPu)
  178. {
  179. slUcCompPu = slPosLostVoltPu;
  180. }
  181. else if (slUcCompPrePu < -slNegWinVoltPu)
  182. {
  183. slUcCompPu = -slNegWinVoltPu;
  184. }
  185. else
  186. {
  187. slUcCompPu = slUcCompPrePu;
  188. }
  189. /* Ialfa = 2/3 * ia - 1/3 * ib - 1/3 * ic */
  190. out->swUalphaCompPu = (slUaCompPu * 2 - slUbCompPu - slUcCompPu) * 10923 >> 15; // Q14=Q14+Q15-Q15
  191. /* Ibeta = 1 / sqrt(3) * (ib - ic) */
  192. out->swUbetaCompPu = (slUbCompPu - slUcCompPu) * 18919 >> 15; // Q14=Q14+Q15-Q15
  193. }
  194. else
  195. {
  196. out->swUalphaCompPu = 0; // Q14=Q14+Q15-Q15
  197. out->swUbetaCompPu = 0; // Q14=Q14+Q15-Q15
  198. }
  199. }
  200. /************************************************************************
  201. Function: asr_voSpdPIInit;
  202. Description: deadband compensation initialization;
  203. Call by: functions in main loop;
  204. Input Variables: N/A;
  205. Output/Return Variables: N/A;
  206. Subroutine Call: N/A;
  207. Reference: N/A;
  208. *************************************************************************/
  209. void dbc_voDBCompInit(void)
  210. {
  211. dbc_stDbCompOut.swUalphaCompPu = 0;
  212. dbc_stDbCompOut.swUbetaCompPu = 0;
  213. }
  214. /*************************************************************************
  215. Local Functions (N/A)
  216. *************************************************************************/
  217. /************************************************************************
  218. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  219. All rights reserved.
  220. *************************************************************************/
  221. #ifdef _DBC_C_
  222. #undef _DBC_C_
  223. #endif
  224. /*************************************************************************
  225. End of this File (EOF):
  226. !!!!!!Do not put anything after this part!!!!!!!!!!!
  227. *************************************************************************/