Expressions are the primary means to perform calculations and logic. Expressions are found in Calculation Events, User Event Conditions, Activity Programs, and other applications in Powervision.
What is an Expression?
Expressions are mathematical or logical statements used to calculate and set values of a variable or evaluate true/false conditions using logic, arithmetic operations, binary operations, or combinations of the three.
Examples:
Variable1 + Variable2: Sum two variables
If(Variable1 = 10, Variable1 = 10, Variable1 +1): Increment a variable if its value is less than 10.
Variable1 > 500: Conditional evaluation returns true if variable is greater than 500
Where are Expressions Used?
- Activity Program: Decision blocks and Set Variable blocks use expressions. When the flow diagram encounters either one in the sequential flow their respective expressions will be evaluated.
- Calculation Event: Calculation Events are a container for an expression belonging to a variable. When a calculation event is fired from an event, for example from a user events action list, the expression will be evaluated and the variable set to its result. A variable may have more than one calculation event, each performing a unique calculation.
- User Event Condition: Conditional logic using expressions can be created for User Events. The condition can be called from a event and if it evaluates to true then the Event will fire all of its actions.
- State Machine Transitions: Expressions can be used to allow transitions from one state to the next only if the condition is met. The transitions Condition will be called by a Event and if it evaluates to true then the transition will fire its actions if it has any and move to the next state.
- Pre-Defined Curves: Pre-defined curves may use expressions to define the input characteristics of a analog input rather than a table of data points. This expression will get evaluated automatically at the sample rate of the I/O Definition it is tied to.
When do Expressions Get Evaluated?
Expression will be evaluated when the object it is a member of gets called by an Event. Events can be a User Event, a timer expiring, a button press, a variable calling it Fire on Change method, etc... See Topic Tutorial on Events for more information.
Creating Expressions
Each of the Objects in the list above will have an expression editor window that is common amongst all of the objects allowing you to create an expression. Built in reference information in a tabbed layout makes it easy to see what features are available.
Conventions:
- Comments: Use two forward slashes on each line you wish to create a comment.
- Color Code: Green = Comments, Blue = Function, Black = Operators and Variables, Red = Constants.
- Syntax: Use standard arithmetic operation conventions using Infix Notation to create an expression. The subject of the formula (variable to the left of the = sign) need not be declared, the object owning the expression is the subject, and the result will automatically be passed on to that object. These are treated as single line expressions so there is no terminating character like a semicolon needed at the end of the line. Note: the single line may occupy more than one physical line in the editor if it is a lengthy expression with nested logic to make it more readable.
Features:
- Math Operators: The standard set of math operators can be typed or selected from the list at the bottom of the window. This serves as a reference as to what symbols can be used.
- Variables: Any system variable can be used in the expression. A complete list is available in the Variables tab. To jump through the list type the first letter of the variable. To select the variable double click on it. Note that variables with spaces in the name use the Underscore symbol to mark the space character.
- Enumerations: You may also use the name of a enumerated value rather than trying to remember its numeric value. These will be found in the Enumeration Values tab. The numeric value of the name will be substituted when the expression is evaluated.
- Functions: A whole list of Arithmetic, logic, and binary operations are available with descriptions in the Functions tab. Take special note of Trig functions that may use/return units in radians rather than degrees.
Testing your Expression
- The built in Test Evaluation makes it easy to verify the expression works as intended.
- Each input variable will have a data slider associated with it to set its test value.
- The variables used must have their simulation min/max and step size attributes set to a range of values easy to use during the test.
- The result of the expression is given at the bottom of the test window.
Examples:
- Set Variable to a Constant
- Set Variable to an Enumeration
- Simple Arithmetic
- Toggle a Variable between two values
- Set Variable Conditionally using isValid
- Nested Logic
- Increment a Counter and Wrap Around Back to 0
Comments
0 comments
Please sign in to leave a comment.