KSquare Utilities
FlowMeterTracker.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 #include <stdlib.h>
3 #include <memory.h>
4 
5 #include <string>
6 #include <iostream>
7 #include <fstream>
8 #include <vector>
9 #include <map>
10 
11 #include "MemoryDebug.h"
12 using namespace std;
13 
14 
15 #include "GlobalGoalKeeper.h"
16 #include "KKBaseTypes.h"
17 #include "OSservices.h"
18 #include "KKStr.h"
19 using namespace KKB;
20 
21 #include "Variables.h"
22 
23 #include "FlowMeterTracker.h"
24 using namespace KKLSC;
25 
26 
27 
28 
30  flowMeterPresent (false),
31  flowRateDefault (1.0f),
32  flowRateRatioDefault (1.0f),
33  history (NULL),
34  historyTableSize (-1),
35  historyLastIdxAdded (-1),
36  historyOldestIdx (-1),
37  imagingWidthMeters (0.0635f),
38  imagingWidthPixels (2048),
39  scanRate (0.0f),
40  ticsPerMeter (10.0f)
41 {
42 }
43 
44 
45 
47  flowMeterPresent (entry.flowMeterPresent),
48  flowRateDefault (entry.flowRateDefault),
49  flowRateRatioDefault (entry.flowRateRatioDefault),
50  history (NULL),
51  historyTableSize (entry.historyTableSize),
52  historyLastIdxAdded (entry.historyLastIdxAdded),
53  historyOldestIdx (entry.historyOldestIdx),
54  imagingWidthMeters (entry.imagingWidthMeters),
55  imagingWidthPixels (entry.imagingWidthPixels),
56  scanRate (entry.scanRate),
57  ticsPerMeter (entry.ticsPerMeter)
58 {
59  history = new Entry[historyTableSize];
60  for (kkint32 x = 0; x < historyTableSize; ++x)
61  history[x] = entry.history[x];
62 }
63 
64 
65 
66 
68 {
69  delete history;
70  history = NULL;
71 }
72 
73 
74 void FlowMeterTracker::Initialize (bool _flowMeterPresent,
75  float _flowRateRatioDefault,
76  kkint32 _historyTableSize,
77  float _scanRate,
78  float _imagingWidthMeters,
79  kkint32 _imagingWidthPixels,
80  float _ticsPerMeter
81  )
82 {
83  flowMeterPresent = _flowMeterPresent;
84  flowRateRatioDefault = _flowRateRatioDefault;
85  scanRate = _scanRate;
86  imagingWidthMeters = _imagingWidthMeters;
87  imagingWidthPixels = _imagingWidthPixels;
88  ticsPerMeter = _ticsPerMeter;
89 
90  historyTableSize = _historyTableSize;
91  delete history;
92  history = new Entry[historyTableSize];
93  historyLastIdxAdded = -1;
94  historyOldestIdx = -1;
95 
96  flowRateDefault = ComputeFlowRateFromFlowRateRatio (flowRateRatioDefault);
97 }
98 
99 
100 
102 {
103  kkint32 mem = sizeof (*this);
104  if (historyTableSize > 0)
105  mem += sizeof (Entry) * historyTableSize;
106  return mem;
107 }
108 
109 
110 
111 void FlowMeterTracker::FlowRateRatioDefaultChanged (float _flowRateRatioDefault)
112 {
113  flowRateRatioDefault = _flowRateRatioDefault;
114  flowRateDefault = ComputeFlowRateFromFlowRateRatio (flowRateRatioDefault);
115 }
116 
117 
118 
119 void FlowMeterTracker::ScanRateChanged (float _newScanRate)
120 {
121  scanRate = _newScanRate;
122  flowRateDefault = ComputeFlowRateFromFlowRateRatio (flowRateRatioDefault);
123 }
124 
125 
126 
127 
128 void FlowMeterTracker::AddEntry (kkuint32 _scanLineNum,
129  kkuint32 _counterValue
130  )
131 {
132  if (historyLastIdxAdded < 0)
133  {
134  historyLastIdxAdded = 0;
135  historyOldestIdx = 0;
136  }
137  else
138  {
139  ++historyLastIdxAdded;
140  if (historyLastIdxAdded >= historyTableSize)
141  historyLastIdxAdded = 0;
142  if (historyLastIdxAdded == historyOldestIdx)
143  {
144  ++historyOldestIdx;
145  if (historyOldestIdx >= historyTableSize)
146  historyOldestIdx = 0;
147  }
148  }
149  history[historyLastIdxAdded].scanLineNum = _scanLineNum;
150  history[historyLastIdxAdded].counterValue = _counterValue;
151 } /* AddEntry */
152 
153 
154 
155 FlowMeterTracker::Entry::Entry ():
156  scanLineNum (0),
157  counterValue (0)
158 {
159 }
160 
161 
162 
164 {
165  if ((historyLastIdxAdded == historyOldestIdx) || (ticsPerMeter == 0.0f) || (scanRate == 0.0f))
166  return flowRateDefault;
167 
168  kkint32 prevIdx = historyLastIdxAdded - 1;
169  if (prevIdx < 0)
170  prevIdx = historyTableSize - 1;
171 
172  EntryPtr lastPtr = history + historyLastIdxAdded;
173  EntryPtr prevPtr = history + prevIdx;
174 
175  kkint32 tics = lastPtr->counterValue - prevPtr->counterValue;
176  kkint32 scanLines = lastPtr->scanLineNum - prevPtr->scanLineNum;
177 
178  float meters = tics / ticsPerMeter;
179  float secs = scanLines / scanRate;
180 
181  return meters / secs;
182 } /* FlowRateInstantaneous */
183 
184 
185 
186 
188 {
189  if ((historyLastIdxAdded == historyOldestIdx) || (ticsPerMeter == 0.0f) || (scanRate == 0.0f))
190  return flowRateDefault;
191 
192  EntryPtr lastPtr = history + historyLastIdxAdded;
193  EntryPtr prevPtr = history + historyOldestIdx;
194 
195  kkint32 tics = lastPtr->counterValue - prevPtr->counterValue;
196  kkint32 scanLines = lastPtr->scanLineNum - prevPtr->scanLineNum;
197 
198  if ((tics == 0) || (scanLines == 0))
199  return 0.0f;
200 
201  float meters = tics / ticsPerMeter;
202  float secs = scanLines / scanRate;
203 
204  return meters / secs;
205 } /* FlowRateTrend */
206 
207 
208 
209 
211  float& flowRateRatio
212  )
213 {
214  if (!flowMeterPresent)
215  {
216  flowRate = flowRateDefault;
217  flowRateRatio = flowRateRatioDefault;
218  return;
219  }
220 
221  flowRate = FlowRateInstantaneous ();
222  flowRateRatio = ComputeFlowRateRatioFromFlowRate (flowRate);
223 
224  return;
225 }
226 
227 
228 void FlowMeterTracker::GetFlowRateTrend (float& flowRate,
229  float& flowRateRatio
230  )
231 {
232  if ((!flowMeterPresent) || (this->historyLastIdxAdded < 0) || (historyLastIdxAdded == this->historyOldestIdx))
233  {
234  flowRate = flowRateDefault;
235  flowRateRatio = flowRateRatioDefault;
236  return;
237  }
238 
239  if (!flowMeterPresent)
240  {
241  flowRate = flowRateDefault;
242  flowRateRatio = flowRateRatioDefault;
243  return;
244  }
245 
246  flowRate = FlowRateTrend ();
247  flowRateRatio = ComputeFlowRateRatioFromFlowRate (flowRate);
248 
249  return;
250 }
251 
252 
253 
255 {
256  if ((_flowRateRatio == 0.0f) || (imagingWidthPixels == 0.0f))
257  return flowRateDefault;
258 
259  //float flowRate = (imagingWidthMeters * scanRate) / (_flowRateRatio * imagingWidthPixels);
260  float flowRate = (_flowRateRatio * scanRate * imagingWidthMeters) / imagingWidthPixels;
261 
262  return flowRate;
263 }
264 
265 
266 
268 {
269  if ((_flowRate == 0.0f) || (imagingWidthPixels == 0.0f))
270  return flowRateRatioDefault;
271 
272  //float flowRateRatio = (imagingWidthMeters * scanRate) / (_flowRate * imagingWidthPixels);
273  float flowRateRatio = (_flowRate * imagingWidthPixels) / (imagingWidthMeters * scanRate);
274 
275  return flowRateRatio;
276 }
__int32 kkint32
Definition: KKBaseTypes.h:88
float ComputeFlowRateFromFlowRateRatio(float _flowRateRatio)
FlowMeterTracker(const FlowMeterTracker &entry)
void Initialize(bool _flowMeterPresent, float _flowRateRatioDefault, kkint32 _historyTableSize, float _scanRate, float _imagingWidthMeters, kkint32 _imagingWidthPixels, float _ticsPerMeter)
void GetFlowRateTrend(float &flowRate, float &flowRateRatio)
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
KKTHread * KKTHreadPtr
void GetFlowRateInstantaneous(float &flowRate, float &flowRateRatio)
float ComputeFlowRateRatioFromFlowRate(float _flowRate)
Class that keeps track the Flow-Meter-Counter values by scan-lines; using these values it will comput...
Contains Classes that are specific to Cameras physical characteristics.
void ScanRateChanged(float _newScanRate)
void AddEntry(kkuint32 _scanLineNum, kkuint32 _counterValue)
void FlowRateRatioDefaultChanged(float _flowRateRatioDefault)