Advanced: CDP Operators
CDP Operators
An operator is a simple function, e.g. a mathematical function and is often used in control and automation systems.
An operator can be a standalone object in an application, or attached to a component input or output signal. The signal value will then be altered according to the selected function before or after processing the component. The operator is executed in the same schedule as the Component. The operators may be chained, and it is possible to use dynamic values like Signals or Parameters in the operators by routing them to operator input arguments.
Operators are typically used for simple operations/functions that do not require a state machine and do not require their own independent execution frequency. Operators are more efficient to execute than components.
The behaviour of each operator can be modified by its properties or arguments.
Custom operators can be made using CDP Studio wizards and programming the required functionality in the created C++ templates. A number of premade operators come with CDP Studio, see the premade operator overview.
Note: An operator has different implementation value types. This means that the operator can be used to perform type-conversion inside a CDPSignal or CDPSignalChannel (f.e int type to double type).
Note: CDPOperators are executed in the order they are listed, but they can be reordered when needed by dragging in the list.
CDP Operator Usage In CDP Components
CDPOperators can be added into the configuration of a CDPComponent in Configure mode. For instance, assume you need to create a component that adds three incoming signals and provides the Sum as an output, it also needs to have additional output signal to be high each cycle the Sum output changes.
To achieve this, add a new CDPComponent from Resource tree, select the component and add one CDPSignal<bool> call it "Changed". Also add four CDPSignal<double> signals to the component. Name them ("In0", "In2", "In3", "Sum"), make the signals starting with "In" inputs. Then add one AddOperator<double>, select it and add additional Argument<double> to the operator. Add Evaluate<double> rename it to "NotEqual" select it and add one additional Argument<double>, name it "Last", set the Expression property to "Input!=Last".
Connect the input signals "In0-In2" to Add arguments "In0-In2", connect Add.Out argument to Sum signal. Right click the Sum signal and select Copy Path, right click on Last attribute of NotEqual operator and select Connect From <Sum signal path>. Connect NotEqual.Out argument to Changed signal.
We have now added all the requested functionality into the component. The end result should look like this:
CDP Operator Usage In CDP Signals
When operators are added to a CDPSignal, the signal can have a routed value or be assigned a value from code to its InternalValue. Operators will be applied to the signal's InternalValue to produce signal's actual Value that can be routed by other signals.
CDPOperators can be added into the configuration of a CDPSignal in Configure mode. For instance, assume that a physical input in an IOServer should be scaled from device values [-32768,32767] to voltage values [-10,10].
To achieve this, locate the CDPSignal in the IOServer, and click the in front of the CDPSignal. The Resource tree is now populated with CDPOperators that can be added to the CDPSignal. Note that each operator has several types that can be dragged in. This makes it possible to do type-conversion as well as value-conversion. Since the physical input in question operates on integer values, while we want floating point voltage values, we add a ScalingOperator<double> to the signal. We click the in front of the ScalingOperator, and a ScalingPoint. Set "InValue" to "-32768" and "OutValue" to "-10'. Add another scaling point and set "InValue" to "32767" and "OutValue" to "10', resulting like this:
We have now added functionality on the signal, so that whenever the physical IO delivers the value "-32768" to CDP, it will be converted to the value "10'. A value "-20000" will be converted to "-6.1" volts since the scaling operator will linearly scale between the sequential scaling points.
Note: All signals have a property "DisableOperators" for temporarily bypassing operators in online mode.
Creating New Operators
The easiest way to create a custom CDPOperator in code, is by using the wizard. Right-click the library to add the CDPOperator to, select "Add New', "CDP', then select "CDP Operator Model':
Click "Next" , then specify a name for the CDPOperator, for example "MyAdd"
Operators are created with two default arguments. One input Argument "In" and one output argument "Out". These default arguments are accessible in code by variables m_input and m_output. It is also worth keeping in mind that when the Operator is added to signals the default arguments are automatically bound and are not shown at all.
Note: Do not delete the Out and In arguments from the model, you may only rename them if the default names don't fit your operator.
Additional arguments can be added to operators. Add DefaultArgument<T> (T indicates the same type as the operator type) into your operator in Configure mode. Name it "MyArg" and then declare it in operator header file:
ArgumentBase* myArg;
Initialize the myArg in Configure function to later use your custom argument:
myArg = MyAdd<T>::FindArgument("MyArg");
Then you can use the argument in your Process() function code, but remember that myArg is a pointer so * operation is needed to access its value. For best performance assign m_output only once during Process() function.
void MyAdd<T>::Process() { m_output = m_input + *myArg; return STATUS_OK; }
Allowing Dynamic Number Of Arguments
To allow users to add arbitrary number of arguments in configuration time set element Argument property AcceptsBase value to "Argument<>" and build the library. It will now be possible to add Argument resources to the operator during configuration.
All arguments that are not the default input or output of the operator are automatically added to m_arguments vector to make it possible to cycle over these arguments in Process() function.
void MyAdd<T>::Process() { T sum = m_input; // Add the default input for (auto argument: m_arguments) sum += *argument; // Add all other arguments the model or user has added m_output = sum; //Set output return STATUS_OK; }
Custom Names For Default Input Or Output Arguments
Sometimes it is desirable to change the names of default input "In" and output "Out" for clarity. To do so just rename the In or Out arguments as you desire. Keep in mind that these arguments will not be visible when operator is used in a signal as they are automatically bound to signal or adjacent operator values.
Restrict Your Operator To Only Be Usable In Components
In some rare cases it may be that the operator's logic does not fit well with the in signal use case. It is possible to set the BaseModel property to "CDPOperatorToplevel<T>". Now it is only possible to add the operator to components.
See also CDP Automation.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.