CPT — Compute
CPT (Compute) works out a whole expression in one box and stores the answer in a tag. Covers operator order, the REAL promotion rule, and why it is not a one-shot.
CPT
Dest
dest
Expression
expression
Works out an expression and stores the answer in a tag. Runs on every scan the rung is true, not once per rising edge.
Everything CPT does you could also do by chaining MUL, DIV and ADD boxes through
scratch tags. CPT lets you write it on one line instead:
CPT(Result, (Speed * 60) / PulsesPerRev)Operands
| Name | Type | What it is |
|---|---|---|
dest | SINT INT DINT REAL | Tag the answer is stored in |
expression | SINT INT DINT REAL | Tags and typed-in values joined by operators |
The destination has to be a tag — a number or another expression cannot be written to. A BOOL
destination is not allowed either: use CMP when the answer is a yes or no.
How It Works
| Rung | What happens |
|---|---|
| Rung true | Works out the expression and stores the answer in dest |
| Rung false | Nothing. dest keeps the value it had |
CPT is not a one-shot. Hold the rung true for ten scans and it works the expression out ten
times and stores ten times. If you want it to run once per button press, put a
ONS in front of it.
What you can write in the expression
+ - * / MOD, brackets, a negative sign, ** to raise to a power, the bitwise
operators AND OR XOR NOT, and the functions ABS SQRT TRUNC LN LOG DEG RAD
SIN COS TAN ASIN ACOS ATAN ATAN2.
You can read an element of an array (Samples[2], and the index can be an expression of its own
like Samples[i + 1]), and a member of a structure (Tmr.ACC).
You cannot write a comparison — Count > 5 — because a comparison answers yes or no and
CPT has to store a number. Put those in a CMP box.
The full operator order, the whole-expression decimal rule and the operators that do not behave the way maths does are on the Expressions in Ladder page. The short version:
-2**2is-4, because the power runs before the minus sign2**3**2is64, because two powers run left to right- one decimal anywhere makes the whole expression decimal, so
7/2*1.5stored in aDINTis5
Example — Motor RPM
A sensor gives pulses per second and the motor gives a fixed number of pulses per revolution. Turn that into RPM.
0
In_Running
CPT
Dest
Sts_MotorRpm
Expression
(In_PulsesPerSec * 60) / Cfg_PulsesPerRev
In_PulsesPerSec is 250 and Cfg_PulsesPerRev is 100, so the answer is 15000 / 100 = 150 RPM.
Watch the order. Writing In_PulsesPerSec / Cfg_PulsesPerRev * 60 would work out 250/100 first,
and because both are whole numbers that is 2, not 2.5 — giving 120 RPM instead of 150. Multiply
before you divide when you are working in whole numbers.
Common Mistakes
- Expecting it to run once per press. It runs every scan the rung is true. Add a
ONSif you want one. - Dividing whole numbers and losing the fraction:
7 / 2is3. Store into aREAL, or bring a decimal into the expression, if you need3.5. - Writing a comparison in it.
CPTstores a number;CMPdecides a rung. - Reaching for
CPTfor a single add.ADD(A, B, Result)reads better thanCPT(Result, A + B)when there is only one operation.
Related
- CMP — the same expressions, used to decide a rung
- Expressions in Ladder — operator order, decimals, and the traps
- Math instructions — one operation per box
Math — ADD / SUB / MUL / DIV / MOD
Reference for Ladder Logic math instructions in rungs.dev — ADD, SUB, MUL, DIV, MOD — performing arithmetic on two sources and writing the result to a destination tag.
ABS / SQRT / NEG — One-Input Math
Reference for the one-input Ladder Logic math instructions in rungs.dev — ABS (absolute value), SQRT (square root), and NEG (negate). Each reads one Source and writes one Dest.