test_cursample_calib.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include "gtest/gtest.h"
  2. #include <gtest/gtest.h>
  3. #include <stdint.h>
  4. #include <tuple>
  5. #include "scope.h"
  6. #include "test_user.h"
  7. class CurSampleCalibTest : public testing::Test
  8. {
  9. protected:
  10. static void SetUpTestSuite()
  11. {
  12. CodeParaInit();
  13. adc_voSampleInit();
  14. pwm_voInit();
  15. scm_uwSpdFbkLpfAbsPu = 0;
  16. }
  17. virtual void SetUp() override
  18. {}
  19. virtual void TearDown() override
  20. {
  21. adc_voSampleInit();
  22. pwm_voInit();
  23. scm_uwSpdFbkLpfAbsPu = 0;
  24. }
  25. };
  26. class CurSampleCalibTest1 : public CurSampleCalibTest, public testing::WithParamInterface<::std::tuple<double, double, double>>
  27. {};
  28. TEST_P(CurSampleCalibTest1, SingResCalib)
  29. {
  30. int timerPrd = HW_PWM_PERIOD;
  31. int timerCnt = 0, timerCntDir = 0, prdCnt = 0;
  32. double phase, ia, ib, ic, iaRdson, ibRdson, icRdson;
  33. double currentFreqHz = 100; // unit: Hz
  34. double currentMod = get<0>(GetParam()); // unit: A
  35. double rdsonCoef = get<1>(GetParam());
  36. double voltageMod = get<2>(GetParam()); // unit: V
  37. double loopNum = 100000000;
  38. double spdAbsTarget = voltageMod * 32768 * 1000 / M_FLUX_WB / 2 / 3.14 * 1000 / FBASE; // 2600
  39. /* Coef Cal */
  40. adc_voSampleCoef(&adc_stCof);
  41. adc_stCof.uwIaOffset = 2048;
  42. adc_stCof.uwIbOffset = 2048;
  43. adc_stCof.uwIcOffset = 2048;
  44. adc_stCof.uwIdcOffset = 0;
  45. pwm_stGenCoefIn.uwPWMDutyMax = cp_stControlPara.swPWMMaxDuty;
  46. pwm_stGenCoefIn.uwPWM7To5Duty = cp_stControlPara.swPWM7to5Duty;
  47. pwm_stGenCoefIn.uwPWMMinSample1Pu = cp_stControlPara.swPWMMinSampleDuty1;
  48. pwm_stGenCoefIn.uwPWMMinSample2Pu = cp_stControlPara.swPWMMinSampleDuty2;
  49. pwm_stGenCoefIn.uwPWMMinSample3Pu = cp_stControlPara.swPWMMinSampleDuty3;
  50. pwm_stGenCoefIn.uwSampleSteadyPu = cp_stControlPara.swPWMSampleToSteady;
  51. pwm_stGenCoefIn.uwSingelResisSamplePu = cp_stControlPara.swPWMSampleSigR;
  52. pwm_stGenCoefIn.uwOvmNo = cp_stControlPara.swPWMOverMdlMode;
  53. pwm_stGenCoefIn.uwPWMPd = HW_PWM_PERIOD;
  54. pwm_voGenCoef(&pwm_stGenCoefIn, &pwm_stGenCoef);
  55. for (int i = 0; i < loopNum; i++)
  56. {
  57. /* Current */
  58. phase = currentFreqHz * 2 * 3.14 * i / (TIM0CLK_KHZ * 1000);
  59. ia = currentMod * sin(phase);
  60. ib = currentMod * sin(phase + (double)2 / 3 * 3.14);
  61. ic = currentMod * sin(phase + (double)4 / 3 * 3.14);
  62. iaRdson = ia * rdsonCoef;
  63. ibRdson = ib * rdsonCoef;
  64. icRdson = ic * rdsonCoef;
  65. /* Timer: centre-aligned */
  66. if (timerCntDir == 0)
  67. {
  68. timerCnt++;
  69. if (timerCnt == (timerPrd / 2))
  70. {
  71. testTimerIntFlg[TIMER0][TIMER_INT_FLAG_UP] = 1;
  72. timerCntDir = 1;
  73. }
  74. }
  75. else
  76. {
  77. timerCnt--;
  78. if (timerCnt == 0)
  79. {
  80. testTimerIntFlg[TIMER0][TIMER_INT_FLAG_UP] = 1;
  81. timerCntDir = 0;
  82. prdCnt++;
  83. }
  84. }
  85. /* ADC Trigger */
  86. if (timerCntDir == 0)
  87. {
  88. if (prdCnt % 2 == 1)
  89. {
  90. if (timerCnt == pwm_stGenOut.uwRdsonTrig)
  91. {
  92. double iaReg = (double)adc_stCof.uwIaOffset - iaRdson * 100 * 2048 / ADC_IPHASE_CUR_MAX_AP; // Negative direction current
  93. if (iaReg < 0)
  94. {
  95. iaReg = 0;
  96. }
  97. if (iaReg > 4096)
  98. {
  99. iaReg = 4096;
  100. }
  101. double ibReg = (double)adc_stCof.uwIbOffset - ibRdson * 100 * 2048 / ADC_IPHASE_CUR_MAX_AP; // Negative direction current
  102. if (ibReg < 0)
  103. {
  104. ibReg = 0;
  105. }
  106. if (ibReg > 4096)
  107. {
  108. ibReg = 4096;
  109. }
  110. double icReg = (double)adc_stCof.uwIcOffset - icRdson * 100 * 2048 / ADC_IPHASE_CUR_MAX_AP; // Negative direction current
  111. if (icReg < 0)
  112. {
  113. icReg = 0;
  114. }
  115. if (icReg > 4096)
  116. {
  117. icReg = 4096;
  118. }
  119. ADC_IDATA0(ADC0) = iaReg;
  120. ADC_IDATA1(ADC0) = ibReg;
  121. ADC_IDATA2(ADC0) = icReg;
  122. testAdcIntFlg[ADC0][ADC_INT_FLAG_EOIC] = 1;
  123. }
  124. }
  125. else
  126. {
  127. if (timerCnt == pwm_stGenOut.uwSigRTrig)
  128. {
  129. switch (pwm_stGenOut.uwSingelRSampleArea)
  130. {
  131. case SampleA:
  132. ADC_IDATA0(ADC1) = (UWORD)(adc_stCof.uwIdcOffset + abs(ia) * 100 * 4096 / ADC_IDC_CUR_MAX_AP);
  133. if (ADC_IDATA0(ADC1) > 4096)
  134. {
  135. ADC_IDATA0(ADC1) = 4096;
  136. }
  137. break;
  138. case SampleB:
  139. ADC_IDATA0(ADC1) = (UWORD)(adc_stCof.uwIdcOffset + abs(ib) * 100 * 4096 / ADC_IDC_CUR_MAX_AP);
  140. if (ADC_IDATA0(ADC1) > 4096)
  141. {
  142. ADC_IDATA0(ADC1) = 4096;
  143. }
  144. break;
  145. case SampleC:
  146. ADC_IDATA0(ADC1) = (UWORD)(adc_stCof.uwIdcOffset + abs(ic) * 100 * 4096 / ADC_IDC_CUR_MAX_AP);
  147. if (ADC_IDATA0(ADC1) > 4096)
  148. {
  149. ADC_IDATA0(ADC1) = 4096;
  150. }
  151. break;
  152. default:
  153. break;
  154. }
  155. testAdcIntFlg[ADC1][ADC_INT_FLAG_EOIC] = 1;
  156. }
  157. }
  158. }
  159. /* ADC Interrupt */
  160. if (testAdcIntFlg[ADC0][ADC_INT_FLAG_EOIC] == 1)
  161. {
  162. adc_uwRdsonUReg = ADC_IDATA0(ADC0);
  163. adc_uwRdsonVReg = ADC_IDATA1(ADC0);
  164. adc_uwRdsonWReg = ADC_IDATA2(ADC0);
  165. testAdcIntFlg[ADC0][ADC_INT_FLAG_EOIC] = 0;
  166. }
  167. if (testAdcIntFlg[ADC1][ADC_INT_FLAG_EOIC] == 1)
  168. {
  169. adc_uwADDMAPhase1 = ADC_IDATA0(ADC1);
  170. testAdcIntFlg[ADC1][ADC_INT_FLAG_EOIC] = 0;
  171. }
  172. /* Timer Interrupt */
  173. if (testTimerIntFlg[TIMER0][TIMER_INT_FLAG_UP] == 1)
  174. {
  175. if ((timerCntDir == 1) && (prdCnt % 2 == 1))
  176. {
  177. /* Speed Mock */
  178. if(scm_uwSpdFbkLpfAbsPu < spdAbsTarget)
  179. {
  180. scm_uwSpdFbkLpfAbsPu += 20; ///< 转速需模拟实际从0上升,否则采样校准系数会计算为0
  181. }
  182. else
  183. {
  184. scm_uwSpdFbkLpfAbsPu = spdAbsTarget;
  185. }
  186. /* Sample and Calibration */
  187. adc_voSampleUp(&adc_stCof, &adc_stUpOut);
  188. adc_voSampleDown(&adc_stCof, &adc_stDownOut);
  189. adc_voSRCalibration(&adc_stCof, &adc_stUpOut, &adc_stDownOut);
  190. /* ADC Trigger Cal */
  191. pwm_stGenIn.swUalphaPu = (SWORD)(voltageMod * 10 * sin(phase + (double)3.14 / 36) * 16384 / VBASE); // Q14
  192. pwm_stGenIn.swUbetaPu = (SWORD)(voltageMod * 10 * cos(phase + (double)3.14 / 36) * 16384 / VBASE); // Q14
  193. pwm_stGenIn.uwVdcPu = (UWORD)(36 * 10 * 16384 / VBASE); // Q14
  194. pwm_voGen(&pwm_stGenIn, &pwm_stGenCoef, &pwm_stGenOut);
  195. /* Scope */
  196. //UdpScope::Send(0, adc_stDownOut.slSampIcPu, adc_stUpOut.swCalibIcPu, adc_stDownOut.swIcPu);
  197. //UdpScope::Send(0, scm_uwSpdFbkLpfAbsPu, pwm_stGenOut.blSampleCalibFlag);
  198. /* Determine whether the unit test pass */
  199. if (prdCnt > (loopNum / HW_PWM_PERIOD - 5))
  200. {
  201. if (!adc_stCof.blCalibCalFlag)
  202. {
  203. if(adc_stDownOut.ulISamplePeakPu > 32767)
  204. {
  205. EXPECT_NEAR((double)adc_stDownOut.slSampIaPu * 780 / 1024, adc_stDownOut.swIaPu, 10); ///< 系数为立即数,程序中更改后测试用例需要随之更改
  206. EXPECT_NEAR((double)adc_stDownOut.slSampIbPu * 780 / 1024, adc_stDownOut.swIbPu, 10);
  207. EXPECT_NEAR((double)adc_stDownOut.slSampIcPu * 780 / 1024, adc_stDownOut.swIcPu, 10);
  208. }
  209. // else if(adc_stDownOut.ulISamplePeakPu > 25800)
  210. // {
  211. // EXPECT_NEAR((double)adc_stDownOut.slSampIaPu * 1024 / 1024, adc_stDownOut.swIaPu, 10); ///< 系数为立即数,程序中更改后测试用例需要随之更改
  212. // EXPECT_NEAR((double)adc_stDownOut.slSampIbPu * 1024 / 1024, adc_stDownOut.swIbPu, 10);
  213. // EXPECT_NEAR((double)adc_stDownOut.slSampIcPu * 1024 / 1024, adc_stDownOut.swIcPu, 10);
  214. // }
  215. else
  216. {
  217. EXPECT_NEAR((double)adc_stDownOut.slSampIaPu * 1024 / 1024, adc_stDownOut.swIaPu, 10); ///< 系数为立即数,程序中更改后测试用例需要随之更改
  218. EXPECT_NEAR((double)adc_stDownOut.slSampIbPu * 1024 / 1024, adc_stDownOut.swIbPu, 10);
  219. EXPECT_NEAR((double)adc_stDownOut.slSampIcPu * 1024 / 1024, adc_stDownOut.swIcPu, 10);
  220. }
  221. }
  222. else
  223. {
  224. if (1024 / rdsonCoef < adc_stCof.uwCalibcoefMin)
  225. {
  226. if (currentMod * rdsonCoef * 100 >= ADC_IPHASE_CUR_MAX_AP)
  227. {
  228. EXPECT_NEAR((double)adc_stDownOut.slSampIaPu * 780 / 1024, adc_stDownOut.swIaPu, 50);
  229. EXPECT_NEAR((double)adc_stDownOut.slSampIbPu * 780 / 1024, adc_stDownOut.swIbPu, 50);
  230. EXPECT_NEAR((double)adc_stDownOut.slSampIcPu * 780 / 1024, adc_stDownOut.swIcPu, 50);
  231. }
  232. else
  233. {
  234. EXPECT_NEAR((double)adc_stDownOut.slSampIaPu * adc_stCof.uwCalibcoefMin / 1024, adc_stDownOut.swIaPu, 10);
  235. EXPECT_NEAR((double)adc_stDownOut.slSampIbPu * adc_stCof.uwCalibcoefMin / 1024, adc_stDownOut.swIbPu, 10);
  236. EXPECT_NEAR((double)adc_stDownOut.slSampIcPu * adc_stCof.uwCalibcoefMin / 1024, adc_stDownOut.swIcPu, 10);
  237. }
  238. }
  239. else if (1024 / rdsonCoef > adc_stCof.uwCalibcoefMax)
  240. {
  241. EXPECT_NEAR((double)adc_stDownOut.slSampIaPu * adc_stCof.uwCalibcoefMax / 1024, adc_stDownOut.swIaPu, 10);
  242. EXPECT_NEAR((double)adc_stDownOut.slSampIbPu * adc_stCof.uwCalibcoefMax / 1024, adc_stDownOut.swIbPu, 10);
  243. EXPECT_NEAR((double)adc_stDownOut.slSampIcPu * adc_stCof.uwCalibcoefMax / 1024, adc_stDownOut.swIcPu, 10);
  244. }
  245. else
  246. {
  247. if (currentMod < 20)
  248. {
  249. EXPECT_NEAR(ia * 100 * 16384 / IBASE, adc_stDownOut.swIaPu, 0.5 * 100 * 16384 / IBASE);
  250. EXPECT_NEAR(ib * 100 * 16384 / IBASE, adc_stDownOut.swIbPu, 0.5 * 100 * 16384 / IBASE);
  251. EXPECT_NEAR(ic * 100 * 16384 / IBASE, adc_stDownOut.swIcPu, 0.5 * 100 * 16384 / IBASE);
  252. }
  253. else
  254. {
  255. EXPECT_NEAR(ia * 100 * 16384 / IBASE, adc_stDownOut.swIaPu, 1.1 * 100 * 16384 / IBASE);
  256. EXPECT_NEAR(ib * 100 * 16384 / IBASE, adc_stDownOut.swIbPu, 1.1 * 100 * 16384 / IBASE);
  257. EXPECT_NEAR(ic * 100 * 16384 / IBASE, adc_stDownOut.swIcPu, 1.1 * 100 * 16384 / IBASE);
  258. }
  259. }
  260. }
  261. }
  262. }
  263. testTimerIntFlg[TIMER0][TIMER_INT_FLAG_UP] = 0;
  264. }
  265. }
  266. }
  267. INSTANTIATE_TEST_SUITE_P(DiffCurMod, CurSampleCalibTest1,
  268. ::testing::Combine(::testing::Values(5, 19, 39, 55), ::testing::Values(0.4, 0.8, 2.1, 6), ::testing::Values(1, 2, 10)));