Scenario 2: Track inventory and production with packing line counters
- Last UpdatedMar 18, 2021
- 4 minute read
You can use the Counter retrieval feature to calculate production rates and totals. For example, you could answer questions like:
-
How many screw tops have we used in the last week?
-
How many products have we produced since Monday?
What is Counter Retrieval?
With Counter retrieval, you can easily calculate the amount of increase in a tag value (and therefore the number of items produced) over time. For example, you might have an integer counter that keeps track of how many cartons were produced. The counter has an indicator like this:
[ 9 ][ 9 ][ 9 ][ 9 ]
Some counters are reset manually -- for example, at the end of a shift before restocking. Others may never be reset, and instead will simply rollover, for example from 9999 to 0. Calculating totals accurately depends on:
-
Whether the counter has rolled over
-
Whether the counter gets reset manually
About Rollover Values
The rollover value is defined in the AVEVA Historian for each tag.
The next value after the highest value that can be physically shown by the counter is called the rollover value. For example, the rollover value for a four-digit integer counter is 10,000. When the counter reaches the 9,999th value, the counter rolls back to 0. Therefore, a counter value of 9,900 at one time and a value of 100 at a later time means that you have produced 200 units during that period, even though the counter value has dropped by 9,800 (9,900 minus 100). Counter retrieval allows you to handle this situation and calculate the correct value.
Similarly, if you had a PLC register that is a 2-byte unsigned integer, the rollover value would be 65,536.
For each cycle, the counter retrieval mode shows the increase in that counter during the cycle, including rollovers.
Counter retrieval is a true cyclic mode. It returns one row for each tag in the query for each cycle. The number of cycles is based on the specified resolution or cycle count.
The counter algorithm is applied only to analog and discrete tags. For integer analog tags, the result will be an integer returned as a float data type. For a real analog tag, the rollover value and the result may be real values and can include fractional values. For discrete tags, the rollover value is assumed to be 2. If a query contains tags of other types, then no rows are returned for those tags.
Resetting a Counter
If you have a counter that normally gets reset manually before it rolls over, you must set the rollover value for the tag to 0 so that the count is simply how much change occurred since the manual reset.
For example, assume that you have the following data values for five consecutive cycle boundaries, and that the value 0 occurs as the first value within the last cycle:
100, 110, 117, 123, 3
If you set the rollover value to 0, the counter retrieval mode assumes that the 0 following the value 123 represents a manual reset, and returns a value of 3 for the last cycle, which is assumed to be the count after the manual reset. The value 0 itself does not contribute 1 to the counter value in this case.
If the rollover value is instead set to 200, then the counter retrieval mode assumes that the value 0 represents a normal rollover, and a count of 80 is calculated and returned (200 - 123 + 3). In this case, the value 0 contributes 1 to the counter value, and that is the change from the value 199 to the value 200.
Handling Exceptions
To handle reversals instead of treating them as rollovers, you can use a counter deadband. For more information, see Counter Retrieval - Using a Counter Deadband in the AVEVA Historian Retrieval Guide.
For details on how counter retrieval handles "gaps" in data and how to compensate for that in your queries, see Counter Retrieval and Counter Retrieval - How It Works in the AVEVA Historian Retrieval Guide.
Example: Counting Items with Counter Retrieval
To use the counter mode, set the following parameter in your query:
wwRetrievalMode = 'Counter'
In the following example, the rollover value for the TotalProduced.Counter tag is set to 0. In a time span of 3 hours, the tag increments from 0 to 9999 twice. The following query returns the an hourly total count within the queried time span. The QualityDetail of 212 indicates that a counter rollover occurred during the reported time range.
SELECT DateTime, TagName, Value, Quality, QualityDetail AS QD
FROM History
WHERE TagName like 'TotalProduced.Counter'
AND DateTime >= '2015-08-17 13:00'
AND DateTime < '2015-08-17 16:00'
AND wwResolution=3600000
AND wwRetrievalMode='counter'
AND wwQualityRule='optimistic'
AND wwTimeStampRule='start'
The results are:
|
DateTime |
TagName |
Value |
Quality |
QD |
|
2015-08-17 13:00:00.0000000 |
TotalProduced.Counter |
4460 |
0 |
212 |
|
2015-08-17 14:00:00.0000000 |
TotalProduced.Counter |
4544 |
0 |
64 |
|
2015-08-17 15:00:00.0000000 |
TotalProduced.Counter |
4481 |
0 |
212 |