Arithmetic expressions <expr>
The usual calculation rules apply to handling arithmetic expressions:
- Order-of-operations rule
- the parenthesis rule, whereby square brackets "[ ]" must be used. Round parenthesis "(...)” are for comments.
Parameters are often used in arithmetic expressions. The notation of parameters is:
- P followed by an integer, e.g. P12.
Example of an arithmetic expression:
P5 = [[sin[R1*30.00] + P2] / P5]
macros (strings) may be assigned to arithmetic expressions and parts of them.
A macro name leads to a macro content which is analysed. Recursive handling is also possible.
Macro names must be placed in quotation marks. When decoded, the notation is case-sensitive (uppercase/lowercase).
Nested strings are identified by a preceding '\' before the double quotation marks. Make sure that complete nesting levels are always grouped in a macro, i.e. adding ´[´ at the start and ´]´ at the end of macro content should have no effect on the result of the mathematical expression.
Programing Example
Nested macros
Correct:
N10 "STRING1" = "COS[\"STRING2\"]"
N20 "STRING2" = "5 * 12"
N30 "STRING3" = "SIN[89.5 + \"STRING1\"]"
N40 X[-2 * "STRING1" + "STRING2" + "STRING3"] (Move to X60)
M30
Wrong:
Only complete nesting levels may be compiled
in the string.
N10 "STRING1" = "COS["
N20 "STRING2" = "90]"
N30 "STRING3" = " \"STRING1\" \"STRING2\" "
Macros defined in the NC program are valid program global.
Section "Macros” describes how to program macros outside mathematic expressions.
Overview of all available calculation operations:
Basic types of calculation:
Addition | + | P1 = P2 + P3 + 0.357 |
Subtraction | - | P1 = P2 - 0.031 |
Multiplication | * | P1 = P2 * [P3 + 0.5] |
Division | / | P1 = P2 * P3 / [P5 + P6] |
Exponential calculation | ** | P1 = 2**P3 (2 to the power P3) |
Modulo calculation | MOD | P1 = 11 MOD 3 (-> 2) |
Numerical functions:
Absolute value formation | ABS [..] | P1 = ABS [P2 - P4] |
Squaring | SQR [..] | P1 = SQR [P2] + SQR [P3] |
Square root | SQRT [..] | P1 = SQRT [SQR[P2]+SQR[P3]] |
e function | EXP [..] | P1 = EXP [P2 * P4] |
Natural logarithm | LN [..] | P1 = LN [P2] + LN [P3] |
To the power of ten | DEXP [..] | P1 = DEXP [P2] |
Common logarithm | LOG [..] | P1 = LOG [P2] |
Attention
In the case of LN, LOG and SQRT the argument must always be greater than 0!
Bit operators:
AND operation | & | P1 = P2 & P3 |
OR operation | | | P1 = P2 | P3 |
Exclusive OR | ^ | P1 = P2 ^ P3 |
Complement | INV[..] | P1 = INV[P2] |
Attention
The operands may be any positive mathematical expression or number within the range 0 ... 2^32-1 (UNS32). Negative expressions or numerals are not allowed. Floating point numbers are converted into integers.
The result of a bit operation is always within the range of 0... 2^32-1 (UNS32).
Logic operators:
AND operation | && / AND | P1 = P2 && P3 P1 = P2 AND P3 |
OR operation | || / OR | P1 = P2 || P3 P1 = P2 OR P3 |
Exclusive OR operation | XOR | P1 = P2 XOR P3 |
NOT operation | NOT[..] | P1 = NOT[P2] P1 = NOT[1] (P1 = 0) P1 = NOT[0.5] (P1 = 0) P1 = NOT[0.49] (P1 = 1) P1 = NOT[0] (P1 = 1) |
Attention
Operands may be any positive mathematical expression or numeral. Negative expressions or numerals are not allowed.
A floating point number is evaluated as TRUE (1) if its value is > or = 0.5.
Comparison operators:
Loops (see Section Statements for influencing NC program flow) require comparison operators. Verification can be conducted as follows:
Equality | == | $IF P1 == 5 |
Inequality | != | $IF P1 != 5 |
Greater than or equal to | >= | $IF P1 >= 10 |
Less than or equal to | <= | $IF P1 <= 10 |
Less than | < | $IF P1 < 10 |
Greater than | > | $IF P1 > 10 |
Operator priorities:
The priorities of available operators are listed in descending order. 10 is the highest and 1 is the lowest priority.
Priority | Operator | Description |
10 | ** | Power |
9 | *, / | Multiplication, division |
8 | +, - | Addition, subtraction |
7 | & | Bitwise AND |
6 | ^ | Bitwise exclusive OR |
5 | | | Bitwise OR |
4 | <=, >=, ==, <, >, != | Comparison operators |
3 | &&, AND | Logic AND |
2 | XOR | Logic exclusive OR |
1 | ||, OR | Logic OR |
Possible truth values are:
True | TRUE | $IF V.A.MERF.X == TRUE |
Not true | FALSE | $WHILE V.G.WZ[2].OK == FALSE |
Attention
Processing truth values:
For TRUE, the value 1 is used in the controller.
For FALSE, the value 0 is used in the controller.
Trigonometric functions (angles specified in degrees):
Sine | SIN [..] | P1 = SIN [P2] |
Cosine | COS [..] | P1 = COS [P2] |
Tangent | TAN [..] | P1 = TAN [P2] |
Cotangent | COT [..] | P1 = COT [P2] |
Arcsine | ASIN [..] | P1 = ASIN [P2] |
Arccosine | ACOS [..] | P1 = ACOS [P2] |
Arctangent | ATAN [..] | P1 = ATAN [P2] |
Arctangent with | ATAN2 [y,x] | P1 = ATAN2[100,100] (-> result is 45°) |
Arc cotangent | ACOT [..] | P1 = ACOT [P2] |
Attention
With the numerical functions ASIN and ACOS, the argument must always be between -1 and +1.
For the numerical function TAN, the argument should not assume the values -90, 90, 270 ... degrees.
For the numerical function COT, the argument should not assume the values... -180, 0, 180 ... degrees.
The numerical function ATAN2 results in x!=0 for the angle of a position relative to the X axis in the correct quadrant.
Special case: For ATAN2[0.0] (x = 0 and y = 0), the result is always 0.
Transformation functions:
Remove | INT [..] | P1 = INT [123.567] (P1 = 123) |
Remove | FRACT [..] | P1 = FRACT [123.567] |
Round up to integer | ROUND [..] | P1 = ROUND [77.5] (P1 = 78) P1 = ROUND [45.4] (P1 = 45) |
Round up | CEIL [..] | P1 = CEIL [8.3] (P1 = 9) |
Round down | FLOOR [..] | P1 = FLOOR [8.7] (P1 = 8) |
Constants:
3.141592654 (π) | PI | P2 = 2*PI (P2 = 6.283185307) |
Special functions:
Check for existence of variables (V.P., V.L., V.S., V.E.) / parameters / M/H functions / macros |
EXIST [<variable/ parameter/ M function/ H function/ macro>] |
$IF EXIST[V.P.MYVAR] == TRUE $IF EXIST[P1] != TRUE $IF EXIST[M55] == TRUE $IF EXIST[H20] == TRUE $IF EXIST["Macro1"] == TRUE |
Determine the size of an array Dimension of variables (V.P., V.L.,V.S., V.E.) / parameters |
SIZEOF [<array_name>, <dimension>]
or for 1. Dim. SIZEOF [<array_name>] |
$IF SIZEOF [V.P.MYARRAY,2] == 3 P1 = SIZEOF[P10,2] SIZEOF always results in -1 for non-existent array dimensions and for variable that are not arrays. |
Determine smaller value | MIN [x,y] |
P1 = MIN [P2, P3] |
Determine greater value | MAX [x,y] |
P1 = MAX [P2, P3] |
larger value sign | SIGN [..] |
P1 = SIGN [P2] results in positive values: 1 negative values: -1 Zero: 0 |
Encryption functions:
These functions are used to encrypt and decrypt strings. The related key is user-definable. Strings may contain important data that require protection by encryption.
Encrypted data can then be saved to file with #MSG SAVE, for example, or supplied to the PLC by V.E. variables.
Encrypt string | ENCRYPT ["Key", "String" ] |
Decrypt string | DECRYPT ["Key", "Decrypted string" ] |
The product of ENCRYPT or DECRYPT is assigned to a string variable. In this case, note the following:
- With ENCRYPT, the string variable must at least be double the length of the string to be encrypted.
- With DECRYPT, the string variable must at least be half the length of the string to be decrypted.
Programing Example
Encrypt string and save to file:
N10 V.E.encrypted = ENCRYPT[ "Key", "String to be encrypted" ]
N20 #MSG SAVE ["Encrypted text = %s", V.E.encrypted ]
..
Decrypt encrypted string and output as message:
N10 V.E.decrypted = DECRYPT[ "Key", V.E.encrypted ]
N20 #MSG ["%s", V.E.decrypted ]
..
M30