'***************************************************************************
'Description:
'The Accelera series motion controllers provide a mechanism for passing
'values on the subroutine stack.  Up to 8 values are passed from the calling
'stack through JS.  One value can be returned.  Passing arrays and variables
'by reference is supported.
'
'See end of code for expected terminal output
'
'***************************************************************************
'
'
'***************************************************************************
'EXAMPLE 1. Simple Summation
'***************************************************************************
#a
JS #sum(1,2);'pass two values to the subroutine
MG _JS;'print the returned value
EN
'
'********************************************************
'Function Signature
'sum(^a n1,^b n2) returns n1+n2
#sum
EN ,,^a+^b;'third argument of EN is value to return
'
'
'***************************************************************************
'EXAMPLE 2. Passing by reference
'***************************************************************************
#b
DM array[5];'create and fill an array with data
array[0]= 0
array[1]= 1
array[2]= 2
array[3]= 3
array[4]= 4
sum= 0;'declare a variable
JS #sumary("array",&sum)
MG sum
'
'********************************************************
'Function Signature
'sumAry(* ^a[],* ^b) populates ^b with sum of elements in ^a[]
#sumary
^b= 0;'zero the variable that was passed
^c= 0;'use ^c as an array index counter (local scope)
#sumaryh;'subroutine helper label
^b= ^b+^a[^c];'update value of passed variable
^c= ^c+1
JP #sumaryh,^c<^a[-1];' [-1] returns the dimension of the array
EN;'no return value, the variable passed already has the sum
'
'
'***************************************************************************
'EXAMPLE 3. Wrapping Position Relative
'***************************************************************************
#c
JS #move("A",1024,10240,10240,1000);'Call Move Function
MG "Axis Error:",_JS
EN
'
'
'********************************************************
'Function Signature
'#MOVE(^a Axis, ^b SP, ^c AC, ^d DC, ^e PR) Performs Position Relative Move,
'                                           returns _TEn
#move
~a= ^a;'Set variable axis designator to passed axis string
AM ~a;'Ensure any current movement completes before continuing
SP~a= ^b;'Set the axis Speed
AC~a= ^c;'Set the axis Accel
DC~a= ^d;'Set the axis Decel
PR~a= ^e;'Set the Position Relative distance
BG ~a;'Begin Motion
AM ~a;'Wait for movement completion
MG ^a{S1}," Move Complete. Current Commanded Position: ", _RP~a{Z10.0}
EN ,,_TE~a;'Return the current position error
'
'***************************************************************************
'EXPECTED OUTPUT
'***************************************************************************
':
':XQ#A
':
'3.0000
':XQ#B
':
'10.0000 
':XQ#C
':
'A Move Complete. Current Commanded Position: 1000 
'Axis Error: 1000.0000