Onboard Variables
NI-Motion supports 120 general-purpose variables (0x01 through 0x78) for use in onboard programs. Variables are 32 bits wide and can hold either signed (i32) or unsigned (u32) values. Variables can be referenced in input and return vector parameters. Data Operations functions use variables exclusively for input operands and the output result.
In general, most functions have a single data parameter that fits into a single 32-bit variable. If the function uses only a 16-bit data value, it is in the least significant portion (right-most) of the 32-bit variable. However, some functions with input or return vectors have more than one data parameter. As a general rule, each parameter, regardless of size, requires its own variable. For these functions, the vector points to the first variable in a sequential group of variables. Parameters are then associated with variables in sequential order.
A few advanced functions handle variables differently from the previous description. Refer to Using Inputs and Return Vectors with Onboard Variables for more information about variables and vectors.
Indirect Variables
Indirect variables provide functionality similar to pointers in a programming language such as C. When you use an indirect variable (0x81 through 0xF8) as an input vector or return vector, the NI-Motion software looks at the contents of the indirect variable and uses that value as the input or return vector. Indirect variables 0x81 through 0xF8 correspond to variables 0x01 through 0x78. So when you specify an input vector of 0x81, the NI motion controller uses the contents of variable 0x01 as the input vector. This can be very useful in looping structures in onboard programs, as well as changing the input values to functions dynamically.
Example
Suppose the values of onboard variables 1-10 are as follows:
0x01 | 0x02 | 0x03 | 0x04 | 0x05 | 0x06 | 0x07 | 0x08 | 0x09 | 0x0A |
0 | 7 | 0 | 0 | 0 | 0 | 1000 | 0 | 0 | 0 |
In this example, reading the indirect variable 0x82 returns the value 1000.
NoteĀ You can save the entire set of onboard general-purpose variables to Flash ROM with the Save Default Parameters functions. |
Variable Array
Variable arrays are used when reading or writing a buffer of data to or from onboard memory using the Read Buffer or Write Buffer functions.
In memory, the buffer occupies n + 2 variables, where n is the number of buffer elements read. The first variable contains the number of values read from the buffer. The second variable contains the error code of the read operation. If this value is zero, then no error occurred. The next n elements are the values read from the array. The read buffer accounts for the two additional values. However, when you are writing a buffer using a variable array, the second parameter is the regeneration mode, not an error code.
Example
Suppose the onboard variable contains the following information:
0x1 | 0x2 | 0x3 | 0x4 | 0x5 | 0x6 | 0x7 | 0x8 | ... |
5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... |
Further, suppose that there is a buffer containing the following elements:
1 | 2 | 3 | 4 | 5 |
If you were to read the first five elements of the buffer to onboard memory location 0x81, the onboard memory would look like the following:
0x1 | 0x2 | 0x3 | 0x4 | 0x5 | 0x6 | 0x7 | 0x8 | 0x9 | 0xA | 0xB | 0xC | ... |
5 | 0 | 0 | 0 | 5 | 0 | 1 | 2 | 3 | 4 | 5 | 0 | ... |
0x5 contains the number of elements to read. 0x6 displays the errors that occurred. In this case, the location contains a 0, meaning no errors occurred. 0x7 through 0xB are the five elements that were read.