An array is a numerically indexed sequence of elements of the same data type. The elements of an array are specified by the index. In the RMC, an array index starts at 0 and extends to the number of elements minus 1. The RMC supports only one-dimensional arrays. For an example on using arrays, see Example: Using Arrays.
This topic treats arrays in the Variable Table. For information on local arrays in user program steps, see Local Variables in Expressions. For information on arrays in User Functions, see the User Functions and Declaring Variables in User Functions topics.
Array indexing can only be used within the RMC expressions. When accessing RMC arrays from a host controller, you must use the register addresses for the protocol you are using.
Creating Arrays
Before declaring an array, make sure there is room in the Variable Table for it. If the new array will overwrite existing variables, RMCTools will warn you and ask whether to continue or not.
To create an array in the variable table:
Open the Variable Table Editor.
On the Edit tab, click the Type cell for some variable, then click the ellipsis button ( ).
Choose the Data Type and the Size of the array, then click OK.
For each element in the array, you can set the initial value in the same manner as for any variable.
If you plan on using a variable to index through the array, you should create it also. Make sure it is a DINT.
Example
An array of 5 REALs starting at variable 2:
Indexing Array Elements
To specify an element of an array, insert the index in brackets after the tag name in the format 'array_name[index]'. The index can be an expression and must evaluate to a DINT data type.
Examples
Definitions |
Indexing Examples |
Description |
MyArray as REAL[20] |
MyArray[4] |
Specifies the element of the array with the index 4. This is actually the fifth element because the indices are zero-based. |
MyArray as REAL[10] Myindex as DINT |
MyArray[MyIndex] |
The value of 'Myindex' specifies the element in 'MyArray'. |
MyArray as DINT[5] MyVar as REAL |
MyArray[ REAL_TO_DINT (MyVar +5.0) ] |
If you wish to use a REAL value as an index, you can convert it to a DINT as illustrated here. |
|
_VarTbl.CurVal[2] |
You can index variables as shown here, but it is much better to first assign a tag name to a variable, then use the tag name, as shown in the MyArray example above. |
|
_Axis[0] |
Specifies the first axis. Note that '_Axis[0]' is not a valid data type by itself. You must specify a tag, such as _Axis[0].ActPos. |
i as DINT |
_Axis[i+2] |
An index can be an expression, but must evaluate to a DINT. |
Special Case: _Axis[] with no specified element
In the user programs, the "_Axis[]" tag can be used without any value in the brackets. In this case, the default axis of the task that is running the user program will be used. This is for advanced users only. See the Default Axis section of theTaskstopic for details.
Special Case: _Task[] with no specified element
In the user programs, the "_Task[]" tag can be used without any value in the brackets. In this case, the task that is running that user program will be used. This is for advanced users only.
For example, you can use _Task[].CurAxis to change the default axis of the task.
Array Functions
The following functions are intended for handling arrays:
Returns the number of elements in the Array.
For example:
LENGTH(MyArray)
Sets Len registers starting at the To address to the specified value (Val).
For example, in the array MyArray, to set elements 0-9 to 1:
FILL(MyArray[0],1,10)
See Also
Expressions Overview | Example: Using Arrays
Copyright © 2024 Delta Computer Systems, Inc. dba Delta Motion