Back to tutorials
PLC Instructions

PLC Programming Computational Mathematical Instructions – CPT | Compute

By
Vladimir Romanov
Table of Contents

Introduction

The CPT, also known as the compute, is an instruction which has extensive capabilities when it comes to mathematical and logic functions in ladder logic. This instruction allows the user to input a complex string of instructions composed of computational, operational and comparison functions and computes the result which is stored in the specified register. The instruction will work with operands used in the based instructions as well as many others.

The CPT instruction has many advantages as well as drawbacks. Based on personal experience, you should only use this instruction if absolutely necessary and the same could not be achieved with several basic instructions. In other words, you can compute an ADD instruction through the CPT, but you shouldn’t.

The drawbacks come from the fact that it’s not easy to see what’s going on with the logic within this particular instruction. Multiple operands interact with each other which leads to a confusing execution and inability to troubleshoot or tweak when needed.

Example & Usage of CPT

Here’s a real-world scenario of a CPT instruction:

  1. A CompactLogix 1769-:24ER-QB1B Allen Bradley PLC is used to control a process.
  2. In Rung 0, a CPT instruction is used to compute a mathematical instruction utilizing System_DINT[0], System_DINT[1], System_DINT[2], System_DINT[3] and System_DINT[4] registers.
  3. The result of the computation is set to be stored in the “Dest” register which is specified to be System_DINT[5].
  4. Since the mathematical equation is defined as System_DINT[0] + System_DINT[1] – System_DINT[2] * System_DINT[3] / System_DINT[4] and the registers are set to 12, 32, 54, 4 and 31 respectively, the result evaluates to 38.
  5. In Rung 1, a CPT instruction is used to compute a mathematical instruction utilizing System_REAL[0] and System_REAL[1] registers.
  6. The result of the computation is set to be stored in the “Dest” register which is specified to be System_REAL[2].
  7. Since the mathematical equation is defined as COS(System_REAL[0] )* SIN(System_REAL[1]) and the registers are set to 23 and 42 respectively, the result evaluates to 0.48835304.
  8. In Rung 2 a CPT instruction is used to compute an average of three registers: System_DINT[6], System_DINT[1] and System_DINT[2].
  9. The result of the computation is set to be stored in the “Dest” register which is specified to be System_DINT[9].
  10. Since the mathematical equation is defined as (System_DINT[6] + System_DINT[1] + System_DINT[2]) / 3 and the registers are set to 100, 50 and 200 respectively, the result evaluates to 116.

Programming example in RSLogix 5000:

CPTInstructionRSLogixStudio5000

Outcome:

The CPT instruction is a powerful mathematical tool which can be used for complex computations, instructional combinations & for special functions such as the ones found in trigonometry. This instruction allows one to quickly combine a set of mathematical instruction through a traditional formula input which we’re all used to as opposed to ADD Instructions, MUL Instructions, etc.

It may not be obvious from the example above, but a clear drawback of the CPT instruction is the fact that you can’t easily troubleshoot it. The function does not break down the different operands and requires one to go into controller scoped tags in order to see what the values are set to.

The general rule of thumb I’d recommend when using this instruction is that you should only use it if you can’t accomplish the same operation through 3 basic instructions. In other words, if you can use ADD -> MUL -> SUB, use them instead of the CPT.

Data Types Allowed for CPT

The CPT can be used to compare two values of identical types or mismatching types such as float and integer. You may also specify a boolean register which is frequently used for logical evaluations.

  • Integer – You may specify each operand to be of “Integer” type.
  • Float – You may specify each operand to be of “Float” type.
  • Boolean – You may specify each operand to be of “Boolean” type

Video Tutorial

Back to tutorials

Related Tutorials

Advanced

PowerFlex 525 Parameter, Input and Output Programming Tutorial in RSLogix Studio 5000

The PowerFlex 525 series of Variable Frequency Drives from Allen Bradley is highly utilized and respected within the automation field. The drives are compact, versatile and easy to use. We’ve written an extensive tutorial on how to get started with these drives in a previous tutorial; it covers how to initialize the drive, connect it to an EtherNet/IP based PLC and how to set the right parameters. In this tutorial, we’re looking to expand on the concepts and demonstrate a way to access and utilize the multiple Input and Output options available on the drive. The reason you may want to utilize the IO is to save money on purchasing external components, integrate instrumentation into your drive and save floor space, wiring and cost. We’ve seen the need to integrate closely related sensors into a PowerFlex 525 application; this would be the right way to do it.

By
Vladimir Romanov
Advanced

Publishing MQTT Data Using Opto 22 groov EPIC PAC

MQTT is a communication protocol that has taken off in the IIoT community. It’s a light-weight, efficient protocol that works through a publisher / broker / subscriber model. It creates an easy way for field devices to communicate and retrieve data from a single location. In this article, we’ll go over MQTT and dive into an example of how you can publish data using a groov EPIC PAC and retrieve the data using a MQTT client.

By
Vladimir Romanov
Intermediate

HMI Design - Best Practices for Effective HMI Screens

HMI Design is the practice of building HMI screens that are intuitive to the end user, pleasing to the eye and are efficient to operate. As control systems within manufacturing are migrated from traditional push button designs to primarily operated from HMI displays, HMI Design has become trivial to any new installation.Although there is a wide range of HMI based systems, the basic principles of good design remain the same. Different industries may dictate different patterns of layout for their systems, but the good practices remain.

By
Vladimir Romanov