2D and 3D array in Studio 5000

prakasha

Member
Join Date
Dec 2022
Location
Davangere, Karnataka, India
Posts
75
Hi All,

I am unable understand how 2D and 3D array works and in which Consequences these array will used in the program specially in studio 5000.

Please explain with example in a simple way.

Thank you...
 
Imagine an excel spreadsheet, you have say 2 or 3 columns and a number of rows this is an array.
So a two dimensional array
is:
My_Array[0,0] row 0 column 0
My_Array[0,1] row 0 column 1
My_Array[0,2] row 0 column 2
My_Array[1,0] row 1 column 0
My_Array[1,1] Row 1 column 1
My_Array[1,2] row 1, column 2
This is a two dimensional array it contains 6 locations so when a tag is created you put the type of variable like so My_Array[0..2,0..2] of integer
You can create as many rows or columns (within some limits) as you like
When using within a program you address the array locations either by a number or indirectly with other tags, like My_Array[pointer1,pointer2]
so if pointer1 was 4 & pointer 2 was 1 the variable it referenced
would be My_Array[4,1]
see pic of an array of 9,9 here you can see I'm moving the contents of Array[1,2] to D200 remember, arrays start at 0,0.
You can also create an array of a structure for example if you have some device that had 5 variables (lets say a VFD but these contain different types like integer, float & bits)
then for example:
The structure would be
Speed integer
Fow_In Float
Flow_Out Float
Running bit
Error bit
then an array My_Motor[0..10] of My_Structure
so instead of a 2 dimensional array of one type i.e. an integer it contains
10 groups of variables of a type of structure
so then to reference say the first array containing the information is
My_Motor[0].Speed or My_Motor[0].Flow_In & so on.

For the second motor then it would be
My_Motor[1].Speed or My_Motor[1].Flow
It means that if you have a number of motors then you can reference them
see screenshots

Structure.png 10_10 array.png
 
I have never used 2D and 3D array in the program which I have been associated. I wanted to know how it work and what condition it will be used. I went through the manual but i didn't get much of it.

Parky thank you very much for your explanation I will start to work with it..
 
Picture using Microsoft Excel, where you can only have 1, 2 or 3 columns. Maybe you are only needing an alarm value stored in an array. A simple one "column" array would work.

Maybe you need an array where you are storing an alarm number and the number of times that it occurred, you could use a 2D array for that.

Or, maybe you need an array of X,Y,Z values, 3D array could be handy.

I prefer not to use them. People tend to get confused by them. I need to run this test again to make sure I'm not crazy, but they seemed to be much less efficient if you ever need to sort them. If I need to keep track of something like my 3D example, I'll make a UDT that contains members X,Y,Z and make an array of my UDT instead.
 
Arrays have many uses, it enables you to indirectly address variables by using a pointer. for example if you wished to store say an hours worth of a temperature reading done every minute, then by using an array you can use the pointer like below (not in PLC language more like basic, or perhps ST but you should get the idea).
If My_Pointer > 59 then My_Pointer = 0 //if the pointer is beyond the array bounds then set it to 0
IF Minute_Pulse then
My_Array[My_Pointer] = Main_Temperature // every minute store the temperature
INCREMENT My_Pointer // add 1 to the pointer
END_IF

So every minute the temperature is stored in the array & the pointer is incremented to point to the next element in the array.
As posted earlier, for something like many motors or valves you may create a standard function where the variables for that valve or motor are passed to the function block ( In AB language "AOI"), this could be done discretely by passing each parameter though the in/out parameters, however, using an array of structures there only needs to be one variable i.e. Motor[x] this contains all the parameters in a structure for that motor.
Or perhaps you want to store a batch process results in an array.
Lets say a batch process i.e. making soup, you want to store the last 10 results,
Each batch contains the following:
Batch Start Time
Final Weight
Batch End Time.
So we have 3 results & we need 10 of these to display on the HMI screen
So we have two arrays the first one is the one used to gather the data.
Batch_Results = Array[0..2] of a structure
Then we have another array, but this time we have one that is multi dimensional i.e. Stored_Batches = Array[0..10,0..2] of a structure
So effectively we have within one array 10 off batch_Results
Every time a batch is completed, the values in the current Batch_results is completed, these are moved into the Stored_Batches array (a little more complicated as you would have to shift them down the array).
So you end up with the following

10:22:53, 1023.5, 11:45:34
12:02:23, 1027.1, 13:00:31
13:21:50, 1022.5, 14:27:24
14:56:34, 1024.5, 15:45:31
15:22:51, 1023.1, 16:45:15
16:56:53, 1026.5, 17:55:32
As Dmroeder posted,multidimensional arrays can be a pain to someone who was trying to faultfind, but once working can save a lot of code typing.
 
I wanted to know ... [in] what condition it will be used.

Quick-start

The same way a 1D array is an array of single (0D) values, the simplest way to think about a 2D array is as an array of equal- and fixed-sized 1D arrays, and a 3D array as an array of 2D arrays. More generally, an ND array can be thought of as an array of [N-1]D arrays.

TL;DR
The High-level Concepts
[N.B. skim this section to get an idea of the meanings of the terms, then look at the examples below, then come back to these concepts]

Multi-dimensional Data Structures are models
Almost all computer programs implement a model of a process in the real world. The data structures of that program store the data of the model. If the real world process has data that are multi-dimensional, then it may make sense to store model data in a multi-dimensional data structure. That is why multi-dimensional data structures have been implemented in most programming languages.
Model Data
Each datum in a multi-dimensional data structure typically has the same data type (e.g. integer, real, string, UDT) as all other data in that structure, and each datum represents the same physical quantity of the model. For example, a single datum could represent a numerical scalar, such as a temperature, or a brightness, or a time, or a product presence/absence, or a product size, or a product type, or a product quanity; a single datum could also represent some combination of such scalar values combined into a UDT.
Model Dimensions
Each of the model's dimensions in a 2D or 3D data structure is (typically) independent of (i.e. orthogonal to) the other dimensions; depending on the model, that orthogonality may be spatial (right-left vs. up-down; East-West vs. North-South), or that orthogonality may conceptual (spatial vs. time).
Model Data Location - Index Tuples
Each datum stored in a multi-dimensional data structure has a unique memory location in that structure, and each unique memory location is identified by a unique pair (2D) or triplet (3D) or tuple (N-D) of indices (index values), one index for each dimension. An index value represents a real-world parameter structure other than the datum of the data model itself. Most 2D and 3D data are spatial in nature E.g. The 2D data example below has a West-to-East position offset as the first dimension (index), and a South-to-North position offset as the second dimension, all relative to the starting corner at southwest extent of the area being modeled. However, any index could represent something other than a spatial dimenension if required by the model: time; color; etc.
Example 1: digital images
One of the most common 2- and 3-D data structures is digital imagery, such as images produced by a CCD (cf. https://en.wikipedia.org/wiki/Charge-coupled_device). Here is an example from Google Maps.
Untitled.png
In that 2D image,

  • the data are pixels (picture elements),
  • each pixel's value represents the color of that pixel
  • each pixel's location is uniquely identified by the pair of indices:
    • its spatial offset from the left edge (1st dimension)
    • its spatial offset from the bottom edge (2nd dimension)

  • For example, the indicated pixel
    • is at offset i1 along the 1st dimension from the left edge, and
    • is at offset i2 along the second from the bottom edge), and
    • its datum value could be the color name string "dark green."
    • Or, if using the RGB (Red-Green-Blue) color model, the datum value could be the triplet (Red=0%, Green=50%, 0%), or (0,50,0)
      • In that case, the image could be considered a 3D data structure with three dimensions:
        • spatial (West-to-East);
        • spatial (South-to-North);
        • base color (Red, Green, Blue).
Example 2: Time Sampling
Consider a process that produces a tangible product, such as a bottle of water. The PLC controlling the process counts the numbers of bottles produced over each hour, 24 hours per day from midnight to midnight. It also keeps track of those hourly counts from yesterday, the day before yesterday, going back one week (7 days). To model those data, the progam would create a 2D array, COUNTS, with 24 elements (index offsets [0..23]) in the first dimension to model the hours in each day, and with 7 elements (index offsets [0..7]) in the second dimension to model the current day plus the past 7 days.

So if day-index of 0 represents today, the datum value at COUNTS array location COUNTS[13,2] would be the count of bottles produced between 1PM and 2PM on the day before yesterday.

At the start of each day i.e. immediately after midnight, the 24 data in 1D array COUNTS[0..23,0] just completed becomes "yesterday's" data, and the 24 data in 1D array COUNTS[0..23,7] are obsolete. To model this transition, the 24 data in COUNTS[0..23,6] are COPied to COUNTS[0..23,7] (Note 1), the 24 data in COUNTS[0..23,5] are COPied to COUNTS[0..23,6], etc., the 24 data in COUNTS[0..23,0] are COPied to COUNTS[0..23,1] (yesterday), and finally the 24 data in COUNTS[0..23] are zeroed out with a FLL instruction to initialize this new days counts before today's first bottle is produced.

Note 1: in actuality, all of those data in that array are in a contiguous 192 (=24 x 8) memory locations, with the first element COUNTS[0,day] of one day immediately after the last element COUNTS[23,day-1], so the entire 24-position shift of 7 days of data (168 elements) could be done with but two COP instructions and a temporary intermediate buffer [COP COUNTS[0,0] buffer168[0] COP buffer169[0] COUNTS[0,1] FLL 0 COUNTS[0,0] 24]
Memory Layout of Multi-Dimensional Data Arrays
Computer memory is always linear i.e. 1-dimensional, and as mentioned in Note 1 above a multi-dimensional array is laid out as a 1D stream of contiguous memory locations. It is only the notation, e.g. COUNTS[i1,i2] that emulates an actual 2D data structure. Specifically, the notation COUNTS[i1,i2] refers to a memory location that is offset (i1 + (24*i2)) elements from the memory location of the first element, COUNTS[0,0] in COUNTS. So there is nothing special about multi-dimensional arrays, and they could be as easily represented by a long single 1D array, e.g. COUNTS_1D[0..191], with the program calculating the 1D offset e.g. COUNTS_1D[i1 + (24*i2)], instead of declaring a 2D array COUNTS[0..23],0..7] and using the syntax COUNTS[i1,i2].​
 
Last edited:
...
Memory Layout of Multi-Dimensional Data Arrays
Computer memory is always linear ... So there is nothing special about multi-dimensional arrays, and they could be as easily represented by a long single 1D array, e.g. COUNTS_1D[0..191], with the program calculating the 1D offset e.g. COUNTS_1D[i1 + (24*i2)], instead of declaring a 2D array COUNTS[0..23,0..7] and using the syntax COUNTS[i1,i2].​


Update: the multi-dimensional indexing convention and memory layout used in different programming environments could vary across PLC brands, and even across models from the same PLC manufacturer, so that could also be COUNTS_1D[(24*i1) + i2] being equivalent to declaring a 2D array COUNTS[0..7,0..23] and using the syntax COUNTS[i1,i2], where i1 is the day offset back in time from today and i2 is the hour offset from each day's start.
 

Similar Topics

Howdy, I am currently struggling with an array of values that I want to re-arrange in a programmatic way. My program is a mix of ladder and STX...
Replies
6
Views
406
Hello, first time poster here! A belated "thank you" for the direction I've already received from this forum! So, can I do this? Move value 1...
Replies
6
Views
735
Exposing shades of “newbie”…. I haven’t used arrays much, and I think using one for multi-zone temperature control is probably the solution I...
Replies
12
Views
803
Hello, I'm digging all over the internet to find a solution for this but haven't come up with anything yet. We have 3 nearly identical machines...
Replies
2
Views
970
Hi All, On my site, the standard template for storing recipes in the PLC is to create a tag called PartRecipe which is an 2D array - for example...
Replies
4
Views
1,480
Back
Top Bottom