0:02
Good morning everyone, thank you for joining us on this webinar on Global Variables vs Retained Variables.
0:09
If you have a question at any point you can put it in and we’ll get to the end. We’ll just go straight into the presentation.
0:13
Hello and welcome to today’s webinar. Today we will look at Global Variables vs Retained Variables in Cscape 10.
0:22
We will begin this week’s webinar by defining what are Global Variables and Retained Variables.
0:29
Then, we will discuss when you would use one over the other.
0:34
Next, we will explore how they behave in different circumstances, for example, at power-up, at download time, and so on.
0:44
To finish, we will have a demonstration in Cscape 10.
0:48
As always, there will be a Q &A session at the end of this webinar.
0:53
Before we dive into variables, we are going to talk about Horner programming languages.
1:00
Unlike our competitors, Horner has continued to support register-based advanced ladder programming in our controllers.
1:07
However, on this channel, we most often use either a variable-based advanced ladder, or one of the IEC languages.
1:17
All of these options are available to you with Horner controllers.
1:21
But the focus of today’s discussion will be on variable-based advanced ladder.
1:28
Remember, global variables and retained variables are features of any variable-based language.
1:36
What are global variables and retained variables in Cscape 10?
1:42
These are the two different classifications of variables in Horner variable-based languages.
1:49
Explained simply, retained variable values are retained through a power cycle or stop-run transition, whereas global variables are not.
2:00
However, there is much more to discuss beyond this.
2:04
Both variable types are configured in the program variable window in Cscape 10.
2:11
When would you use global variables?
2:13
If you do not require the variable value to persist through a power cycle or stop run transition, you can use a global variable.
2:24
In a typical logic program, most variables will be global variables.
2:29
We will now look at a few example use cases for global variables.
2:34
In our first example, a variable which is directly mapped to a system bit or system register has to be a global variable.
2:43
In the image on the right, we can see our Program Variables window in Cscape 10, with a list of global variables mapped to different system bits and system registers.
2:56
The value of your variables will be set by the controller at the top of every scan, so there is no need to make this variable retentive.
3:07
Similarly, we can use global variables for our physical digital or analog inputs.
3:14
The value of the variable will be set by the controller at the top of every scan to match the current real-world input value, so there is no need to make this variable retentive.
3:26
This also applies to a variable which is tied to a physical digital or analogue output, but only if you want the output to default to off or its minimum on powerup.
3:42
There are some exceptions to this rule, but we will discuss these later in the webinar.
3:47
Another example is a variable which is always initialised to the same value on powerup.
3:53
In this scenario, you will likely have initialization logic in your program to set the desired value on the first scan.
4:04
You can do this in logic by adding a first scan contact and a move block as seen in the image on the top right.
4:14
Alternatively, you can set the variable’s initialization value, or init value, in the program variable window.
4:25
As we can see in the bottom right, the valve position will initialize on an integer value of 50.
4:32
If you have a variable whose value is constantly being calculated in logic, you would also use a global variable.
4:42
An example of this can be seen on the right.
4:45
Here, we are calculating the temperature of an oven, and the math block continuously calculating.
4:53
In this case, it is perfectly fine for the output variable to be assigned to a global variable.
5:01
Finally, we’re going to discuss timers. Most often, we will be dealing with non-retentive or global timers.
5:09
As a timekeeping tool, there is no need to make your timers retentive, since your Timers will be interrupted by a power cycle or stop run transition, and have to reset regardless.
5:24
It is unlikely that you would need to retain a timer’s value through a power cycle.
5:30
So in nearly all cases, we will use a global variable to store our timer’s value.
5:37
Next, let’s look at retained variables.
5:40
A retained variable should be used when you need the value of a variable to persist through a power cycle or stop run transition.
5:51
Usually your project will have fewer retained variables than global variables.
5:57
Let’s once again go through some examples.
6:00
First let’s start with a common example, that being set points.
6:05
A setpoint is a target value for your variable.
6:11
This could be used to control all types of parameters, including temperature, position, pressure, and so on.
6:19
To ensure our setpoint value persists through a power cycle or stop run transition, we use a retained variable.
6:28
There are some exceptions to this, but for most applications we would use a retained variable.
6:37
Next, let’s discuss PID loops.
6:41
Each PID loop has 16 internal variables, as well as any extra assigned variables, as we can see in the diagram on the right.
6:51
Each PID loop can have variables that store set point values, coefficients for tuning the loop, manual flags, and more.
7:04
These variables have strict values that need to be preserved through a power cycle or stop run transition.
7:12
To ensure they don’t reset to zero, we set each of these variables to be retained variables.
7:20
Next, let’s look at counters.
7:25
Counters are a common feature of most programs.
7:28
On the right, we have an example of a simple counter setup that’s tracking machine runtime.
7:35
First, we have a counter that counts a tick every hour, which feeds into a second counter that counts hours for machine hours.
7:45
For this example to work correctly, both of our counter values must be stored in retained variables.
7:53
Next, we are going to look at a less common scenario, where we need a digital or analog output valve to be retained.
8:02
In the example on the right, we have a position that we want the valve to start at after a power cycle or stop run transition.
8:10
there are two steps we need to take in order to accomplish this.
8:16
First, we need to assign the variable as retained, and then we must map it to the real-world IOPoint.
8:23
Then, in the hardware configuration menu, we have to ensure that the particular output is set to hold last value.
8:32
We will explore this in more depth in the demonstrations portion of our webinar shortly.
8:38
Finally, we have a variable which has a value that is not continuously being calculated or set.
8:46
In our example on the right, we have a variable called batchMaxTemp.
8:53
We want this variable to keep track of our batch’s maximum temperature, but the batch is not always active.
9:02
So, in the case of a power cycle or stop run transition, we need to ensure that the maximum temperature value is not lost.
9:12
For this reason, we would make BatchMaxTemp a retained variable.
9:18
Now we are going to discuss how both global and retained variables behave on download.
9:25
By default, all global variables are manually cleared, meaning they are set to 0 on every power cycle or stop run transition.
9:37
If an initialization, or init, value is set in the program variable window, the global variable will be set to that value instead.
9:48
The behavior of global variables can be changed in the compiler settings, but this is not recommended.
9:56
We will show you this in the demonstration portion, but this should only be used in specific scenarios.
10:03
How do retained variables behave on download?
10:06
By default, all retained variables maintain their previous values, and newly created retained variables start with a value of 0.
10:17
However, we can decide to clear all retained variables, only if desired, however.
10:26
Clearing variable values on download is the proper behavior if downloading a new program with all new variables.
10:35
Now we will begin the demonstrations portion of our webinar. Here we are in Cscape 10.
10:44
Let’s begin in the program variables window in the top left.
10:49
This will open a window at the bottom of our Cscape program.
10:53
And down here, we can see the lists for our global and retained variables.
10:59
Let’s first look at our global variables.
11:02
The first several entries are system variables that are mapped to system bits and system registers.
11:09
These are automatically created by Cscape when we open a new project.
11:16
The first entries in this list are mapped to real-world IOPoins.
11:22
The variables we have created for our demonstration start here.
11:27
The Program Variables window will list the variables in the order they were created.
11:32
We can see that the first of these are all mapped to real-world IOPoins.
11:39
Remember, since these are all global variables, they will initialize to a value of 0.
11:45
Except for this variable, ValvePosition, which has its init value set to an integer of 50.
11:55
In this case, ValvePosition will initialize with a value of 50.
12:01
Next, let’s look at our retained variables.
12:05
By default, there will be no retained variables in your program.
12:10
All the ones you see here were added manually for this demonstration.
12:17
As you can see, only one of these retained variables is mapped to a register, and it is mapped to a real-world IOPoint.
12:28
We want the valve to start out in the same position it was in when the machine powered down.
12:34
Since this variable is tied to a real-world IOPoint, We first have to navigate to Hardware Configuration at the top.
12:44
From here, click on Local I.O.
12:46
Navigate through these menus to Analog Output and ensure that the Hold Last Value checkbox is ticked.
12:55
Now the retained variable will work as intended for our real-world I.O. point.
13:02
Next, let’s take a closer look at these variables and how they are working at runtime.
13:09
If we navigate to the row of icons at the top of Cscape 10, we can open our variable inspector window.
13:17
Here we can see some of our variables.
13:21
The machine run variable is set to off, so our machine is currently turned off.
13:28
For our demonstration, we can simulate turning the machine on by changing this value to a 1.
13:35
Now we have our valve position variable which is a retained variable as we have shown earlier.
13:43
If we change the value of this variable we can expect that it will reset to its init value of 50 once the machine goes through a power cycle or stop run transition.
13:57
We can test this by turning our program off then back on.
14:06
And if we return to the variable inspector window, we can now see that valve position has indeed returned to 50.
14:15
Next, let’s look at what happens to our variables on a download.
14:19
For today’s demonstration, these are the key options to focus on.
14:23
We can choose to either keep current values for all existing retained variables or clear all retained variables.
14:32
The clear all retained Variables’ option would set all retained variables to 0.
14:40
This could be used if we were downloading a new program to overwrite an old program, or if you were downloading a new program onto a new controller with a demo program pre-installed.
14:53
While it may be useful for a first download, on subsequent downloads we want our retained variables to keep their values.
15:00
So instead, we select Keep Current Values for all existing retained variables.
15:07
This option is selected by default, as it is standard practice for most operations.
15:13
Finally, we are going to show you how to change the default settings for global variables.
15:19
This should only be needed in rare, specific situations, but we will demonstrate it nonetheless.
15:26
Navigate to the wave icon in the top left corner of Cscape.
15:31
Click on the settings, and then in compiler options, we can see all the available options for how global variables behave.
15:41
You can select any of these options should you require them.
15:47
And that concludes our webinar for today.
15:50
Thank you so much for listening, and the Q &A session will begin shortly.
15:59
Okay, a nice straightforward one for you this morning.
16:02
Next week we will be following on with automatic screen capture for machine documentation.
16:09
So if you’d like to sign up for that you would have got a newsletter firstly with the link or else you can go to the webinar page of the website which also has the registration link for the webinar.
16:22
Okay, I don’t see any questions then so on that note I think we can leave it there.
16:25
Thank you all and we’ll see you next time.







