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

NameTypeWhat it is
destSINT INT DINT REALTag the answer is stored in
expressionSINT INT DINT REALTags 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

RungWhat happens
Rung trueWorks out the expression and stores the answer in dest
Rung falseNothing. 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**2 is -4, because the power runs before the minus sign
  • 2**3**2 is 64, because two powers run left to right
  • one decimal anywhere makes the whole expression decimal, so 7/2*1.5 stored in a DINT is 5

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 ONS if you want one.
  • Dividing whole numbers and losing the fraction: 7 / 2 is 3. Store into a REAL, or bring a decimal into the expression, if you need 3.5.
  • Writing a comparison in it. CPT stores a number; CMP decides a rung.
  • Reaching for CPT for a single add. ADD(A, B, Result) reads better than CPT(Result, A + B) when there is only one operation.

On this page