MC-Basic Generic Elements
Writing complex multi-axis applications requires high level software organization. Avoid code repetition by encapsulating blocks of code into functions or subroutines. Code blocks of setup or query properties, which can be applied for all (or at least several) groups or axes in the system, and can be assembled into functions and subroutines. For example, if there are a number of identical motors in the system, their setup is the same. Write the following lines only once, instead of repeating them for each axis:
A1.PFac = 0x8000 A1.VFac = A1.PFac/1000 A1.AFac = A1.VFac/1000 A1.JFac = A1.AFac/1000 A1.VMax = 290 A1.AMax = 1500 A1.DMax = A1.AMax A1.Acc = A1.Amax A1.Dec = A1.DMax …
Another example is writing common procedures as subroutines or functions that can later be applied in any system independently of how many axes or groups are defined. For example, switching on an output when an axis reaches its target:
While A1.IsMoving <> 0 Sleep 1 End While Sys.Dout.1 = 1
ELEMENTID
Depending on the system configuration, the MC can have several axes or groups defined. These motion elements are actually user interfaces of the system's internal memory or internal function calls. They are pointers to actual groups and axes.
During system configuration (“Sys.NumberAxes = n” and group definitions), each axis and group receives a unique element identifier. Axes get successive element identifiers ranging from 1 to 32, according to axis number. Groups get successive identifiers ranging from 33 to 64, according to declaration order. You cannot change these element identifiers.
The value of the element identifier is queried directly with ELEMENTID (read-only).
SYS.NUMBERAXES = 4 Common Shared G2 As Group AxNm = A3 AxNm = A4 Common Shared G1 As Group AxNm = A1 AxNm = A2 ? A1.ELEMENTID 1 ? A2.ELEMENTID 2 ? G2.ELEMENTID 33 ? G1.ELEMENTID 34
Declaration
Generic elements resemble other MC-Basic variables (longs, doubles, strings) in declaration syntax and scope (global, static and local), as well as in the ability to define arrays of up to 10 dimensions.
COMMON SHARED|DIM {SHARED} <axis_name> {[]…} AS GENERIC AXIS COMMON SHARED|DIM {SHARED} <group_name> {[]…} AS GENERIC GROUP
Assignment
<generic_element_name>{[]…} = <real_element_name> <generic_element_name>{[]…} = <generic_element_name>{[]…}
During declaration, all generic elements receive a zero element identifier, which prevents their usage as motion elements prior to assignment. If an unassigned generic element is used, an error is returned:
Common Shared Gen_Axis As Generic Axis ? Gen_Axis.ElementID 0 ? Gen_Axis.VMax Error: 3005, "Nonexistent group or axis", Module: Motion
Through assignment, a generic element receives the element identifier of another motion element, either “real” or generic, thus becoming a pointer to this element and acquiring all its properties.
Gen_Axis = A1 ? Gen_Axis.ElementID 1 ? A1.VMax 290 ? Gen_Axis.VMax 290
By recurring assignments, generic elements have the ability to change their pointed axis or group as many times as desired.
Gen_Axis = A2 ? Gen_Axis.ElementID 2 ? A2.VMax 360 ? Gen_Axis.VMax 360
The left-side (assigned) element of the assignment statement cannot be a “real” axis or group. It must always be a generic element.
A1 = Gen_Axis Error: 7039, "Syntax Error", Module: Translator
The right-side element of the assignment statement can be either generic or a “real”. A group cannot be assigned to a generic axis, and an axis cannot assign a generic group.
Common Shared Gen_Group As Generic Group Gen_Axis = G1 Error: 7067, "Wrong input type", Module: Translator Gen_Group = A1 Error: 7067, "Wrong input type", Module: Translator
Joint axes cannot be used in either sides of assignment statement.
Gen_Group.j1 = A1 Error: 7039, "Syntax Error", Module: Translator Gen_Axis = G1.j1 Error: 7039, "Syntax Error", Module: Translator
Limitations
Generic elements cannot be printed. Arithmetic, logic and bitwise operators cannot be applied on generic elements.
Generic elements cannot be used as conditions in flow control statements and event definitions.
Generic elements cannot be recorded.
Generic elements cannot be structure elements.
Generic element cannot serve as parameters of C functions.
Deletion of global generic element cannot be performed with DELETEVAR or DELETEGROUP. Hardware or software reset (RESET ALL) is required.
Dim Shared Gen_Axes_List[32] As Generic Axis Gen_Axes_List[1] = A1 Gen_Axes_List[2] = A2 Gen_Axes_List[3] = A3 Gen_Axes_List[4] = A4 ? Gen_Axes__List[1] Error: 7039, "Syntax Error", Module: Translator Gen_Axes__List[4] = Gen_Axes__List[1]+ 3 Error: 7039, "Syntax Error", Module: Translator If Gen_Axes__List[3] > Gen_Axes__List[2] Then -->Syntax Error DeleteGroup Gen_Group ‘ Gen_Group is a global generic group Error: 7039, "Syntax Error", Module: Translator
Functions
Generic elements can be defined “by reference” in MC-Basic functions and subroutines. Axes or groups passed as arguments could be either “real” or generic. For example, axis setup can be encapsulated in a subroutine. In this example, the subroutine arguments are “real” axes:
Sub AxisSetUp(Ax As Generic Axis) Ax.PFac = 0x8000 Ax.VFac = Ax.PFac/1000 Ax.AFac = Ax.VFac/1000 Ax.JFac = Ax.AFac/1000 Ax.VMax = 290 Ax.AMax = 1500 Ax.DMax = Ax.Amax Ax.Acc = Ax.Amax Ax.Dec = Ax.DMax … End Sub Call AxisSetUp(A1) Call AxisSetUp(A2) Call AxisSetUp(A3) Call AxisSetUp(A4)
An entire array of generic elements can be passed (by reference) to functions and subroutines. Here, the argument is generic:
Sub AxesListSetUp(AxList[*] As Generic Axis) Dim i as long For i = 1 To Sys.NumberAxes AxList[i].PFac = 0x8000 AxList[i].VFac = AxList[i].PFac/1000 AxList[i].AFac = AxList[i].VFac/1000 AxList[i].JFac = AxList[i].AFac/1000 … Next End Sub Call AxesListSetUp(Gen_Axes_List)
The element type (i.e., axis or group) of an argument must match the function or subroutine declaration. Otherwise, a translation error occurs.
Call AxisSetUp(G1) ‘ G1 is a group --> Variable passed by reference has another type as in subroutine/function declaration
Trying to define generic elements “by value” generates an error:
Sub AxisSetUp(ByVal Ax As Generic Axis) … End Sub --> Cannot pass an axis or a group by value to subroutine/function
Joint (J) axes cannot serve as function or subroutine arguments.
Call AxisSetUp(G1.j1) --> Syntax Error
Generic elements can also serve as returned values of MC-Basic functions. Both “real” and generic elements can be assigned to returned values. For example, to generalize the former code, an axis-indexing function can be added, in which a “real” axis is assigned to the return value:
Function JointAxisByIndex(Byval I As Long) As Generic Axis Select Case I Case 1 JointAxisByIndex = G1.J1 Case 2 JointAxisByIndex = G1.J2 … End Select End Function
Then, write a general loop:
Dim A As Generic Axis For Index = 1 to Sys.NumberAxes A = AxisByIndex(Index) Call AxisSetUp(A) Next
The generic element-returning function itself cannot be used as an argument since generic elements can only be passed by reference,and a function call is passed by value.
For Index = 1 to Sys.NumberAxes Call AxisSetUp( AxisByIndex(Index) ) Next --> Cannot pass a constant or a complex expression by reference to subroutine/function
Joint (J) axes cannot be assigned to return values.
Function JointAxisByIndex(Byval I As Long) As Generic Axis Select Case I Case 1 JointAxisByIndex = G1.J1 --> Syntax Error Case 2 JointAxisByIndex = G1.J2 --> Syntax Error … End Select End Function
WITH
WITH can be given in three contexts: configuration file, terminal and task. All three contexts of WITH can be applied to generic elements.
With Gen_Axis PFac = 0x8000 VFac = PFac/1000 AFac = VFac/1000 JFac = AFac/1000 … End With
WITH can be used on assigned and unassigned generic elements. These generic elements can be assigned and/or re-assigned later. This changes the WITH element without writing a new WITH command.
With Gen_Axis ? With GEN_AXIS ? VMax Error: 3005, "Nonexistent group or axis", Module: Motion Gen_Axis = A1 ? VMax 290 Gen_Axis = A2 ? VMax 360