tdc_itf.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include <stdint.h>
  2. #include "board.h"
  3. #include "tdc_tof.h"
  4. #include "bill.h"
  5. #include "check.h"
  6. #include "param.h"
  7. #include "alarm.h"
  8. #include "stat.h"
  9. // resistence(ohm) from 0 dgree to 55 dgree
  10. static const uint16_t NTC50KOhmX10Ary[] ={
  11. 2749, 2633, 2521, 2415, 2314, 2218, 2127, 2040,
  12. 1956, 1877, 1802, 1729, 1661, 1595, 1532, 1472,
  13. 1415, 1360, 1308, 1258, 1210, 1164, 1120, 1079,
  14. 1038, 1000, 963, 928, 894, 862, 831, 801,
  15. 773, 745, 719, 694, 670, 648, 624, 603,
  16. 583, 563, 544, 526, 508, 491, 475, 459,
  17. 444, 430, 416, 403, 390, 377, 365, 354
  18. };
  19. // tangent slope from 0 dgree to 55 dgree
  20. static const float NTC50KSlpAry[] ={
  21. -0.00855432f, -0.00898473f, -0.00944287f, -0.00991080f, -0.01041667f, -0.01091703f, -0.01146789f, -0.01201923f,
  22. -0.01261034f, -0.01322751f, -0.01386963f, -0.01451379f, -0.01522070f, -0.01592357f, -0.01666667f, -0.01748252f,
  23. -0.01824818f, -0.01912046f, -0.02000000f, -0.02096436f, -0.02178649f, -0.02283105f, -0.02392344f, -0.02493766f,
  24. -0.02604167f, -0.02717391f, -0.02840909f, -0.02967359f, -0.03095975f, -0.03225806f, -0.03355705f, -0.03521127f,
  25. -0.03649635f, -0.03816794f, -0.03968254f, -0.04149378f, -0.04310345f, -0.04484305f, -0.04672897f, -0.04878049f,
  26. -0.05076142f, -0.05263158f, -0.05494505f, -0.05681818f, -0.05952381f, -0.06172840f, -0.06410256f, -0.06666667f,
  27. -0.06944444f, -0.07194245f, -0.07462687f, -0.07751938f, -0.08064516f, -0.08333333f, -0.08771930f, -0.00282486f
  28. };
  29. #define NTC50K_SUM (sizeof(NTC50KOhmX10Ary)/sizeof(uint16_t))
  30. // start temperature of each row(no end temp)
  31. //modified by yuewei 20260409 start
  32. //static const float RowTemp_Ary[] = {
  33. // 5.0f, 15.0f, 25.0f, 35.0f, 45.0f
  34. //};
  35. /*
  36. static const float RowTemp_Ary[] = {
  37. 10.0f, 15.0f, 25.0f
  38. };
  39. */
  40. //modified by yuewei 20260520 start
  41. #if 1
  42. //#define ROW_NUM (sizeof(RowTemp_Ary)/sizeof(float))
  43. //#define COL_NUM (12)
  44. const uint16_t Quad_Cell_NUM = 28;
  45. //Quad_Cell_t Quad_Cell_Ary[ROW_NUM*COL_NUM] =
  46. Quad_Cell_t Quad_Cell_Ary[28] =
  47. {
  48. //温度补偿系数,总共8个温度点
  49. //格式:{温度, {常数:m, 系数:k}}
  50. //温度:5C -> 50C
  51. {5.000000, {0.000000f,0.952426f}},
  52. {10.000000,{0.000000f,0.966416f}},
  53. {25.000000,{0.000000f,1.000000f}},
  54. {30.000000,{0.000000f,1.008647f}},
  55. {35.000000,{0.000000f,1.016146f}},
  56. {40.000000,{0.000000f,1.022568f}},
  57. {45.000000,{0.000000f,1.027991f}},
  58. {50.000000,{0.000000f,1.032488f}},
  59. //流速等级补偿系数,总共20个流速点
  60. //{流速等级, {常数:m, 系数:k}},注意:流速等级如果设置0,则表示没有流量点需要修正
  61. //第1~10个流速点
  62. {0.000000,{0.000000f,0.000000f}},
  63. {0.000000,{0.000000f,0.000000f}},
  64. {0.000000,{0.000000f,0.000000f}},
  65. {0.000000,{0.000000f,0.000000f}},
  66. {0.000000,{0.000000f,0.000000f}},
  67. {0.000000,{0.000000f,0.000000f}},
  68. {0.000000,{0.000000f,0.000000f}},
  69. {0.000000,{0.000000f,0.000000f}},
  70. {0.000000,{0.000000f,0.000000f}},
  71. {0.000000,{0.000000f,0.000000f}},
  72. //第11~20个流速点
  73. {0.000000,{0.000000f,0.000000f}},
  74. {0.000000,{0.000000f,0.000000f}},
  75. {0.000000,{0.000000f,0.000000f}},
  76. {0.000000,{0.000000f,0.000000f}},
  77. {0.000000,{0.000000f,0.000000f}},
  78. {0.000000,{0.000000f,0.000000f}},
  79. {0.000000,{0.000000f,0.000000f}},
  80. {0.000000,{0.000000f,0.000000f}},
  81. {0.000000,{0.000000f,0.000000f}},
  82. {0.000000,{0.000000f,0.000000f}}
  83. };
  84. #endif
  85. //modified by yuewei 20260520 end
  86. double FlowVol = 0.0f;
  87. double FwCumFlow;
  88. double BwCumFlow;
  89. double FlowTestCoe = 1.0f;
  90. float BwFlowThres = (-1.0f) * (Q1 * 1.6f * 1000.0f * 1000.0f / 3600.0f); //less -Q2
  91. tdc_itf_t TdcItf = {
  92. .bw_flow_flag = 0,
  93. .ch_bit_map = 3,//yw 0->3, 3->15
  94. .blank_flag = 0,
  95. .pipe_coe = 0.1168745, //modified by yuewei 20260610 2.288299->0.1168745
  96. .path_skew = {0.000f, 0.000f, 0.000f, 0.000f},
  97. .ch_num = 1,
  98. .mux_ch_no = {0, 1, 2, 3},
  99. .mux_switch = 1,
  100. .ntc_num = NTC50K_SUM,
  101. .ntc_ohm_ary = NTC50KOhmX10Ary,
  102. .ntc_slp_ary = NTC50KSlpAry,
  103. //modified by yuewei 20260520 start
  104. //.row_num = ROW_NUM,
  105. //.col_num = COL_NUM,
  106. //.row_temp_ary = RowTemp_Ary,
  107. //modified by yuewei 20260520 end
  108. .quad_cell_ary = Quad_Cell_Ary,
  109. .set_blank = set_pipe_blank,
  110. .clr_blank = clr_pipe_blank,
  111. .flow_vol = tdc_flow_itf,
  112. .check_flow = SetCheckFlow
  113. };
  114. float var_dtof_buf[180];
  115. uint32_t test_var_flag = 0;
  116. uint32_t test_var_cnt = 0;
  117. #define VAR_TEST_BUF_SIZE (sizeof(var_dtof_buf) / sizeof(var_dtof_buf[0]))
  118. void test_dtof_var(void) {
  119. double var = 0;
  120. double avg = 0;
  121. int i;
  122. int cnt = VAR_TEST_BUF_SIZE;
  123. if (test_var_flag == 0) {
  124. return;
  125. }
  126. var_dtof_buf[test_var_cnt % VAR_TEST_BUF_SIZE] = get_diff_tof();
  127. test_var_cnt++;
  128. if (test_var_cnt > cnt) {
  129. test_var_flag = 0;
  130. test_var_cnt = 0;
  131. for (i = 0; i < cnt; i++) {
  132. avg += var_dtof_buf[i];
  133. }
  134. avg /= cnt;
  135. for (i = 0; i < cnt; i++) {
  136. var += (var_dtof_buf[i] - avg) * (var_dtof_buf[i] - avg);
  137. }
  138. var /= cnt - 1;
  139. printf("\nThe average of %ds dtof is %lfps\n", cnt, avg * 1000);
  140. printf("The var of %ds dtof is %lf(unit: 10ps)\n", cnt, var * 10000);
  141. }
  142. }
  143. void tdc_flow_itf(double flow_vol, float tmp_float) {
  144. FlowVol = flow_vol;
  145. //add by yuewei 20260519
  146. float rtc_interrupt_time_seconds = get_rtc_interrupt_time();
  147. #if defined(CHECK_CORRECT_ENABLE)
  148. float FlowCorrFactor = CheckFlowCorrectFactor(WaterTemp, (uint16_t)tmp_float);
  149. FlowVol *= FlowCorrFactor; // unit:cm^3/s = ml/s
  150. #endif
  151. //20260429
  152. //add by yuewei 20260327 start
  153. //static double last_vol = 0.0f;
  154. //FlowVol = last_vol * (1.0f - VOL_FILTER_ALPHA) + FlowVol * VOL_FILTER_ALPHA;
  155. //last_vol = FlowVol;
  156. //add by yuewei 20260327 end
  157. //LOG("vol = %.6f\n",FlowVol*0.000001f * 3600);
  158. //20260429
  159. /*
  160. if (FlowVol > BwFlowThres) {
  161. CumFlow += FlowVol/2;
  162. TdcItf.bw_flow_flag = 0;
  163. } else {
  164. TdcItf.bw_flow_flag = 1;
  165. }
  166. if (FlowVol >= 0) {
  167. FwCumFlow += FlowVol/2;
  168. SetBillVol(FlowVol, 0);
  169. } else {
  170. BwCumFlow += (0 - FlowVol/2);
  171. SetBillVol(0, (0 - FlowVol));
  172. }
  173. */
  174. CumFlow += FlowVol * rtc_interrupt_time_seconds;
  175. if (FlowVol >= 0)
  176. {
  177. TdcItf.bw_flow_flag = 0;
  178. FwCumFlow += FlowVol * rtc_interrupt_time_seconds;
  179. SetBillVol(FlowVol, 0);
  180. }
  181. else
  182. {
  183. TdcItf.bw_flow_flag = 1;
  184. BwCumFlow += (0 - FlowVol * rtc_interrupt_time_seconds);
  185. SetBillVol(0, (0 - FlowVol));
  186. }
  187. //modified by yuewei 20260319 start
  188. //SetBillCumFlow(CumFlow, FwCumFlow, BwCumFlow);
  189. SetCheckFlow(CumFlow, tmp_float, WaterTemp);
  190. SetStatFlow(FlowVol);
  191. //modified by yuewei 20260319 end
  192. }
  193. void set_pipe_blank(uint8_t ch) {
  194. if ((TdcItf.blank_flag & (0x00000001 << ch)) == 0) {
  195. Alarm(ERR_CH0 + ch, 1, ch);
  196. }
  197. if (TdcItf.blank_flag == (TdcItf.ch_bit_map & ~(0x00000001 << ch))) {
  198. Alarm(ERR_BLANK_PIPE, 1, TdcItf.ch_bit_map);
  199. }
  200. TdcItf.blank_flag |= (0x00000001 << ch);
  201. }
  202. void clr_pipe_blank(uint8_t ch) {
  203. if ((TdcItf.blank_flag & (0x00000001 << ch)) != 0) {
  204. Alarm(ERR_CH0 + ch, 0, ch);
  205. }
  206. if (TdcItf.blank_flag == (0x00000001 << ch)) {
  207. Alarm(ERR_BLANK_PIPE, 0, 0);
  208. }
  209. TdcItf.blank_flag &= ~(0x00000001 << ch);
  210. }