Here we wil show :
- | how to save last 5 values of some variable
|
- | how to view these stored values to user
|
- | statistical processing of these values, computing of average value
|
First, we need to create global array of size 5, we will call it g_history:
|
This array should be the same type as the watched item, in our case it is LONG. To save the values of this item into g_history array, the following script is used:
function OnItemRead_MartinPC_enterprises_enterprises_15_0_0_4(enterprises_15_0_0_4)
|
{
|
// save last 5 inputs into g_history
|
var i;
|
|
// shift left values in g_history
|
for ( i=0; i<4; i++ )
|
{
|
g_history[i] = g_history[i+1];
|
}
|
|
// set new top value
|
g_history[4] = enterprises_15_0_0_4;
|
|
return enterprises_15_0_0_4;
|
}
|
|
function OnItemWrite_MartinPC_enterprises_enterprises_15_0_0_4(enterprises_15_0_0_4)
|
{
|
return enterprises_15_0_0_4;
|
}
|
The script to view these 5 values :
// function shows last 5 values of some input
|
// these values are saved in g_history global array
|
function OnItemRead_LastValues()
|
{
|
var RetVal = "";
|
var i;
|
|
// show all values in []
|
for ( i=0; i<5; i++ )
|
{
|
RetVal += "[" + g_history[i] + "]";
|
}
|
|
return RetVal;
|
}
|
And the computing of average value :
// compute the average of values in g_history
|
function OnItemRead_Average()
|
{
|
var RetVal = "";
|
var avg=0.0;
|
var i=0;
|
|
// compute sum of the values
|
for ( i=0; i<5; i++ )
|
{
|
avg += g_history[i];
|
}
|
|
// divide the sum with the number of elements
|
avg /= 5;
|
RetVal = "Average = " + avg;
|
|
return RetVal;
|
}
|
Send feedback on this topic.
Copyright © 2004-2013, SAE - Automation, s.r.o. (Ltd.), All rights reserved.