tdc_itf.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 = 30;
  45. //Quad_Cell_t Quad_Cell_Ary[ROW_NUM*COL_NUM] =
  46. Quad_Cell_t Quad_Cell_Ary[30] =
  47. {
  48. //温度补偿系数,总共10个温度点
  49. //格式:{温度, {常数:m, 系数:k}}
  50. //温度:5C -> 50C
  51. //介质:纯水,Marczak公式理论声速归一化(25℃=1497m/s,k=1.0)
  52. {5.000000, {0.000000f,0.952900f}},
  53. {10.000000,{0.000000f,0.967000f}},
  54. {15.000000,{0.000000f,0.979300f}},
  55. {20.000000,{0.000000f,0.990400f}},
  56. {25.000000,{0.000000f,1.000000f}},
  57. {30.000000,{0.000000f,1.008300f}},
  58. {35.000000,{0.000000f,1.015500f}},
  59. {40.000000,{0.000000f,1.021700f}},
  60. {45.000000,{0.000000f,1.026500f}},
  61. {50.000000,{0.000000f,1.030600f}},
  62. //流速等级补偿系数,总共20个流速点
  63. //{流速等级, {常数:m, 系数:k}},注意:流速等级如果设置0,则表示没有流量点需要修正
  64. //第1~10个流速点
  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. {0.000000,{0.000000f,0.000000f}},
  73. {0.000000,{0.000000f,0.000000f}},
  74. {0.000000,{0.000000f,0.000000f}},
  75. //第11~20个流速点
  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. {0.000000,{0.000000f,0.000000f}},
  84. {0.000000,{0.000000f,0.000000f}},
  85. {0.000000,{0.000000f,0.000000f}}
  86. };
  87. #endif
  88. //modified by yuewei 20260520 end
  89. double FlowVol = 0.0f;
  90. double FwCumFlow;
  91. double BwCumFlow;
  92. double FlowTestCoe = 1.0f;
  93. float BwFlowThres = (-1.0f) * (Q1 * 1.6f * 1000.0f * 1000.0f / 3600.0f); //less -Q2
  94. tdc_itf_t TdcItf = {
  95. .bw_flow_flag = 0,
  96. .ch_bit_map = 3,//yw 0->3, 3->15
  97. .blank_flag = 0,
  98. .pipe_coe = 0.1168745, //modified by yuewei 20260610 2.288299->0.1168745
  99. .path_skew = {0.000f, 0.000f, 0.000f, 0.000f},
  100. .ch_num = 1,
  101. .mux_ch_no = {0, 1, 2, 3},
  102. .mux_switch = 1,
  103. .ntc_num = NTC50K_SUM,
  104. .ntc_ohm_ary = NTC50KOhmX10Ary,
  105. .ntc_slp_ary = NTC50KSlpAry,
  106. //modified by yuewei 20260520 start
  107. //.row_num = ROW_NUM,
  108. //.col_num = COL_NUM,
  109. //.row_temp_ary = RowTemp_Ary,
  110. //modified by yuewei 20260520 end
  111. .quad_cell_ary = Quad_Cell_Ary,
  112. .set_blank = set_pipe_blank,
  113. .clr_blank = clr_pipe_blank,
  114. .flow_vol = tdc_flow_itf,
  115. .check_flow = SetCheckFlow
  116. };
  117. float var_dtof_buf[180];
  118. uint32_t test_var_flag = 0;
  119. uint32_t test_var_cnt = 0;
  120. #define VAR_TEST_BUF_SIZE (sizeof(var_dtof_buf) / sizeof(var_dtof_buf[0]))
  121. void test_dtof_var(void) {
  122. double var = 0;
  123. double avg = 0;
  124. int i;
  125. int cnt = VAR_TEST_BUF_SIZE;
  126. if (test_var_flag == 0) {
  127. return;
  128. }
  129. var_dtof_buf[test_var_cnt % VAR_TEST_BUF_SIZE] = get_diff_tof();
  130. test_var_cnt++;
  131. if (test_var_cnt > cnt) {
  132. test_var_flag = 0;
  133. test_var_cnt = 0;
  134. for (i = 0; i < cnt; i++) {
  135. avg += var_dtof_buf[i];
  136. }
  137. avg /= cnt;
  138. for (i = 0; i < cnt; i++) {
  139. var += (var_dtof_buf[i] - avg) * (var_dtof_buf[i] - avg);
  140. }
  141. var /= cnt - 1;
  142. printf("\nThe average of %ds dtof is %lfps\n", cnt, avg * 1000);
  143. printf("The var of %ds dtof is %lf(unit: 10ps)\n", cnt, var * 10000);
  144. }
  145. }
  146. void tdc_flow_itf(double flow_vol, float tmp_float) {
  147. FlowVol = flow_vol;
  148. //add by yuewei 20260519
  149. float rtc_interrupt_time_seconds = get_rtc_interrupt_time();
  150. #if defined(CHECK_CORRECT_ENABLE)
  151. //modified by yuewei 20260914 start
  152. //float FlowCorrFactor = CheckFlowCorrectFactor(WaterTemp, (uint16_t)tmp_float);
  153. float FlowCorrFactor = CheckFlowCorrectFactor(WaterTemp, (uint16_t)fabs(tmp_float));
  154. //modified by yuewei 20260914 end
  155. FlowVol *= FlowCorrFactor; // unit:cm^3/s = ml/s
  156. #endif
  157. //20260429
  158. //add by yuewei 20260327 start
  159. //static double last_vol = 0.0f;
  160. //FlowVol = last_vol * (1.0f - VOL_FILTER_ALPHA) + FlowVol * VOL_FILTER_ALPHA;
  161. //last_vol = FlowVol;
  162. //add by yuewei 20260327 end
  163. //LOG("vol = %.6f\n",FlowVol*0.000001f * 3600);
  164. //20260429
  165. /*
  166. if (FlowVol > BwFlowThres) {
  167. CumFlow += FlowVol/2;
  168. TdcItf.bw_flow_flag = 0;
  169. } else {
  170. TdcItf.bw_flow_flag = 1;
  171. }
  172. if (FlowVol >= 0) {
  173. FwCumFlow += FlowVol/2;
  174. SetBillVol(FlowVol, 0);
  175. } else {
  176. BwCumFlow += (0 - FlowVol/2);
  177. SetBillVol(0, (0 - FlowVol));
  178. }
  179. */
  180. //float f_tdc_sample_times_per_second = TDC_SAMPLE_TIMES_PER_SECOND == 0 ? 1 : TDC_SAMPLE_TIMES_PER_SECOND;//deal TDC_SAMPLE_TIMES_PER_SECOND = 0
  181. CumFlow += FlowVol * rtc_interrupt_time_seconds;
  182. if (FlowVol >= 0)
  183. {
  184. TdcItf.bw_flow_flag = 0;
  185. FwCumFlow += FlowVol * rtc_interrupt_time_seconds;
  186. SetBillVol(FlowVol, 0);
  187. }
  188. else
  189. {
  190. TdcItf.bw_flow_flag = 1;
  191. BwCumFlow += (0 - FlowVol * rtc_interrupt_time_seconds);
  192. SetBillVol(0, (0 - FlowVol));
  193. }
  194. //modified by yuewei 20260319 start
  195. //SetBillCumFlow(CumFlow, FwCumFlow, BwCumFlow);
  196. SetCheckFlow(CumFlow, tmp_float, WaterTemp);
  197. SetStatFlow(FlowVol);
  198. //modified by yuewei 20260319 end
  199. }
  200. void set_pipe_blank(uint8_t ch) {
  201. if ((TdcItf.blank_flag & (0x00000001 << ch)) == 0) {
  202. Alarm(ERR_CH0 + ch, 1, ch);
  203. }
  204. if (TdcItf.blank_flag == (TdcItf.ch_bit_map & ~(0x00000001 << ch))) {
  205. Alarm(ERR_BLANK_PIPE, 1, TdcItf.ch_bit_map);
  206. }
  207. TdcItf.blank_flag |= (0x00000001 << ch);
  208. }
  209. void clr_pipe_blank(uint8_t ch) {
  210. if ((TdcItf.blank_flag & (0x00000001 << ch)) != 0) {
  211. Alarm(ERR_CH0 + ch, 0, ch);
  212. }
  213. if (TdcItf.blank_flag == (0x00000001 << ch)) {
  214. Alarm(ERR_BLANK_PIPE, 0, 0);
  215. }
  216. TdcItf.blank_flag &= ~(0x00000001 << ch);
  217. }