OAProfessor

Deep OA Product Insight

User Tools

Site Tools


dp_fct_expressions

The dp_fct config for a data point element is a slick way to create calculated tags without scripting.

The most common use case for this is to average some sensor values to get an overall value. Let's take this example: you have a chilled tank with 3 sensor values for the temperature. You really want the overall tank temperature, but over time the tank will have different temps at the sensors. A good way to get a better feel for the tank overall temperature is the average of the temps: tank temp = (sensor1 + sensor2 + sensor3) / 3.0

when using the dp_fct: you would build it like this:

Here is another real-world example where I had to take two 16bit words and combine them into a new 32bit word. This is an old school technique where you want to bit shift the upper word and OR them together. Here is that example:

(p1 << 16) | p2

These are simple expressions using mathematical equations. What if you want to make a more complex statement? Here is another use case: If the tank is too low, the sensor1 value will be near to room temperature, ignore that one.

if(Sensor1 > 60)
  TankTemp = (sensor2 + sensor3) / 2.0;
else
  TankTemp = (sensor1 + sensor2 + sensor3) / 3.0;

Well, this takes a different kind of expression. For hard-core programmers, this is simple, but for the casual OA programmer, it is not easy. Here is the syntax for an if-then-else statement:

TankTemp = Sensor1 > 60 ? (sensor2 + sensor3) / 2.0 : (sensor1 + sensor2 + sensor3) / 3.0

The ? operator tells the parser that this is the IF part. The : operator tells the parser the ELSE part follows.

Using the syntax for the dp_fct:

p1>60?(p2+p3)/2.0:(p1+p2+p3)/3.0

Another feature of the dp_fct is to call a library function. This is pretty advanced and allows for more complex logic. Be careful, this script is running in the EVENT manager, don't do things that are “waiting functions” like system() and complex dpQuery sql.

To use a function, you will still need to setup the p1, p2, etc. as these become the library variables passed into the function. In the function field, just put the function name and parameters like: tableLookup(p1, p2). The function must return a value which is the same type as the DPE where this dp_fct is configured.

The trick to all this is to setup the system to recognize your functions in the library. I recommend to make a special lib .ctl file for just dp_fct so that it is easier to load. You will need to put a couple of lines in the config file:

[event]
loadCtrlLibs = "dpfct.ctl"

A function might look like this:

float tableLookup(anytype p1, anytype p2)
{
  float fRet;
  // table lookup with arrays, mapping, etc. where fRet = something
  return(fRet);
}
dp_fct_expressions.txt · Last modified: 2023/04/19 18:52 by toddmalone

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki