Retain Variables
What is a retain variable?
A retain variable is a PLC "Programmable Logic Controller" A Programmable Logic Controller, PLC, or Programmable Controller is a digital computer used for automation of industrial processes, such as control of machinery on factory assembly lines. Used to synchronize the flow of inputs from (physical) sensors and events with the flow of outputs to actuators and events variable which:
- is non-volatile: stored in non-volatile controller memory (AKD PDMM or PCMM). When using KAS Simulator the retain variables are stored in a normal disk file.
- is known by all programs (when its content is changed, the change is propagated to all equations in which this variable is used)
- normally does not contain real-time critical data.
-
-
- Retain variables are saved when you stop the PLC. The next time the PLC is started the retained values are used. Additionally, retains are auto-saved every two seconds. This applies to both the AKD PDMM and PCMM models.
- Variables stored in non-volatile memory that have changed will be updated every two seconds. Unchanged variables are not updated.
Such a variable is used to store application specific data, like for instance to count a cutting-edge cycle in order to stop for its blade replacement after a specific number of iterations.
- On an application “Start”, KAS initializes the retain variables with the value stored in the NVRAM "Non-volatile random access memory" NVRAM is the general name used to describe any type of random access memory which does not lose its information when power is turned off. This is in contrast to the most common forms of random access memory today, which both require continual power in order to maintain their data. NVRAM is a subgroup of the more general class of non-volatile memory types, the difference being that NVRAM devices offer random access, like hard disks. The best-known form of NVRAM memory today is flash memory.
- On an application “Cold Start”, KAS initializes the retain variables with their default value.
- If the declaration of the retain variables is different between the non-volatile memory and the project, the retain variables are also reset to their default value.
Two parameters are checked to identify if the declaration changed:
- The number of variables of each type
- The length of a STRING type variables
-
-
The non-volatile memory size is hardware dependent. If the size of the retained variables is larger than the non-volatile storage space, an error will be logged and the data will not be stored in non-volatile memory. See NVRAM for more information.
For the KAS Runtime In computer science, runtime (or run-time) describes the operation of a computer program, the duration of its execution, from beginning to termination (compare compile time). Within KAS, runtime also refers to the virtual machine that manage the program written in a computer language while it is running Simulator, the retained variables are saved in a file in your project repository.
-
-
The following actions will reset retained value(s) to their Init value(s):
- Changing the type of a retain variable
- Changing the length of a string retain variable
- Changing the size of an array variable
- Changing any element of a structure variable
-
-
Retained variables should not be used as the output of Function A function calculates a result according to the current value of its inputs. A function has no internal data and is not linked to declared instances. Blocks. Doing so may cause problems with retaining the value if the FB is executed with the Enable input off. The output structure can cause it to be conditionally updated to zero or the old value.
A work-around solution is to use a
copy (1 gain)
function to selectively enable updating the retain variable from an intermediate variable, which is the output of a function block.Work-around exampleIn the following image
ReadActualPositionX
is continually reading position feedback from an axis, and the copy block is saving a specific value to a retain variable.
is the intermediate variable andActualPositionX
SavedPositionX
is the retain variable.
Copy a Retained Variable Value to Init Value
Once determined, retained variable values can be saved to the variable's initial value. This allows for the KAS project to more easily be re-used for other, similar machines. In the following example we will determine a retain variable's value and save the value to the retain variable's initial value.
- Run the program to determine the retain variable's value, which is FALSE.
- Right-click on the variable and select Copy variable value to init value.
- We see that the Init value field is now "FALSE".
Limitations
The copy function has several limitations
- The menu item is only enabled under these conditions:
- The project must be compiled and running, and the KAS IDE "Integrated development environment" An integrated development environment is a type of computer software that assists computer programmers in developing software. IDEs normally consist of a source code editor, a compiler and/or interpreter, build-automation tools, and a debugger is connected to the controller.
- One or more retain variables must be selected (an individual variable or a group).
- The program must be recompiled and downloaded for this to take effect.
- Arrays and structures are not supported.