Quick Start Guide Motion Basic Overview Using the Motion Basic User Interface Subroutine & Function Reference
Motion Basic Language Reference
Program Structure
Statement Layout
Comments
Variables and Constants
Program Statements
Assignments
Expressions
Arithmetic Operators
GOTO statment
GOSUB / RETURN Statments
IF / GOTO Statment
FOR / NEXT Statements
PRINT Statment
INPUT Statement
END Statement
Functions and Subroutines
Program Execution
The example program below illustrates the basic structure of a Motion Basic program:
#example Motion Basic Program
N = getnminit()
#Initialize controller modules
print "Number of NMC controllers:", N
svgain(1,100,1000,0,0,255,0,2000,1,1,1) #Set servo gain
parameters
stopmot(1, 5)
#Use stop motor to enable servo
10 for I=1 to 3
#FOR
loops code block 3 times
svtraj(1,151,5000,100000,100,0)
#Move to position 5000
waitmove(1)
#Wait
till move finished
svtraj(1,151,0,100000,100,0)
#Move back to 0
waitmove(1)
print "Loop count = ", I
next
input "Repeat sequence (0=no, 1=yes)?", A #Get data from user
if A=1 goto 10
#Jump to line number 100 to repeat
end
back to top
Statement Layout
You will notice that program lines are spaced in by 4 spaces, except for lines which have
a label. This is not required, but it makes the program more readable. The
TAB key in the Motion Basic user interface is set 4
spaces. It is recommended that you use the TAB rather than inserting spaces as this
will reduce the size of your program.
It does not matter if characters are typed in upper or lower case. However, it is recommended that you standardize your use of upper and lower case to make your program easier to read.
Line labels are just numbers used to allow your program to jump to a particular line using a GOTO or GOSUB statement. Line labels can be from 0 - 999. Note that not every line needs a label; it simplifies editing to only use labels for lines you need to jump to. The numbers used in line labels do not have to be in numerical order, but your program will be clearer if they are. (Early versions of Motion Basic without the (B) suffix only allowed line numbers 0-99.)
In general, only one program statement is allowed per line. However, sometimes
parameters for functions or subroutines can themselves be functions or complex
expressions.
back to top
Comments
Program comments can be entered following a # sign. Comments can appear following a
program statement, or on their own line. Blank lines may be inserted for
readability. Note, however, that because Motion Basic uses
a true line-by-line interpreter, excessive comments can slow down the execution of your
program.
back to top
Variables and Constants
Motion Basic allows the use of 26 variables A - Z. All
variables represent 32 bit integers.
Constants (written as explicit decimal numbers) are also 32 bit integer values which
are positive or negative (e.g. 75632 or -75632).
back to top
Assignments
The assignment statement uses an '=' to set a variable (A - Z) equal to a particular
value. The values which can be assigned to a variable may be constants, function
values, or expression values. For example:
N = 73
N = K + 22
N = getnminit()
back to top
Expressions
Expressions are arithmetic combinations of variables, constants and functions which can be
used in assignment statements or as parameters for functions or subroutines. They
can use the arithmetic operators listed in the next section. For example:
N = K + 22
print A * B
A = getpos(N + 1) + 500
Parentheses can be used to force the order of execution in complex expressions, e.g.: "(30 + 10) / 5" or "30 + (10 / 5)". They can also make expressions easier to read.
Parentheses can be nested for up to 3 levels in a single expression. Note that a function call within a parenthetical expression will use up one level of parentheses. Very complex expressions may need to use intermediate calculations and spread over more than one line. For example, the value of C below is written using intermediate values A and B:
A = (D + E) / (Q + getad(1))
B = (1 + F)*G
C = A/B
back to top
Arithmetic Operators
The following arithmetic operators are defined:
() parentheses
------------
- negation (e.g. -17)
------------
* 32-bit multiplication
/ 32 bit division
% Modulus (remainder) operator
------------
+ 32-bit addition
- 32-bit subtraction
------------
& Bit-wise AND operator
| Bit-wise OR operator
The operators are listed in order of operator precedence. When writing expressions using operators from the same precedence group (for example, */%), use parentheses to eliminate any ambiguity. For example, "5 * 4 % 2" should be written as "(5 * 4) % 2" or as "5 * (4 % 2)".
The / operator does integer division. This means that the result will be an integer truncated down to the lowest integer value. For example, both "16 / 5" and "19 / 5" will evaluate to a value of 3.
The % operator gives the remainder result of an integer division. For example "16 mod 5" equals 1, or "19 mod 5" equals 4.
The & operator AND's each bit of one number with the corresponding bit of another number. This is particularly useful when wanting to clear certain bits in a number. For example B = A & 7 will set B equal to the value of A with all bits cleared to zero except the first, second and third bits.
The | operator OR's each bit of one number with the corresponding bit of another
number. This is particularly useful when wanting to set a particular bit in a number
to 1. For example "B = A | 4" will set B equal to A, but with the 3rd bit
equal to 1.
back to top
GOTO Statment
The goto statement causes your program to unconditionally jump to a line with a line
label. For example:
goto 10
.
.
10 print "Hi there"
back to top
GOSUB / RETURN Statments
The gosub statement, like the goto, causes your program to unconditionally jump to a line
with a line label. However, when a return statment is finally reached, the program
will jump back to the program statment just after the original gosub. For example:
gosub 10
print "ho there"
.
.
10 print "hi there"
return
will print "hi there" followed by "ho
there"
back to top
IF / GOTO Statment
The if/goto statement evaluates a conditional expression which may be true or
false. If the conditional expression is true the following goto will be executed.
Conditional expressions use the comparative operators greater than (>),
less than (<), equal to (=), not equal to (<>).
For example, the statement:
if A <> 5 goto 25
will cause the program to jump to line label 25 if A is not equal to 5. You can also use expressions on either side of the arithmetic operator:
if A + B <> 5 goto 25
back to top
FOR / NEXT Statments
The for/next statements are used for creating iterative loops. A variable (A-Z) is
used as an index that gets incremented by a value of 1 every time through the loop.
The variable is given an initial value and also a maximum value is specified to determine
how many times the loop is executed. The next statement marks the end of the
loop. For example:
for I = 1 to 3
print I
next
will print 1, 2 and 3 on separate lines. For/next statements can also be nested up to three levels. For example:
for I = 1 to 3
for J = 1 to 2
print I, J
next
next
will print "1 1", "1 2", "2 1", "2 2", "3 1", "3 2" on separate lines.
Caution should be used when permanently jumping out of a FOR/NEXT loop before it has terminated normally. If the FOR/NEXT loop does not terminate normally the loop will still be active, and future parts of your program will be limited to fewer than 3 levels of FOR/NEXT nesting. If you need to jump out of a FOR/NEXT loop permanently (e.g., when an error condition is detected), you can force termination of the loop by setting the index variable to a value greater than the upper limit, and then going to the desired location. For example:
for I = 1 to 3
svtraj(1,151,1000*I,100000,100,0) # move to a
new position
waitmove(1)
# wait for move to terminate
refresh(1)
# refresh current status data
S = getstat(1)
# get status byte
if testbit(S, 5) <> 1 goto
10 # check for position error, if none goto "next"
normally
I = 4
# if there is a position error, force termination
10 next
if testbit(S, 5) = 1 goto 20
# finally jump to error code if there is a position
error
# continuation of your program
20 print "Position error detected"
back to top
PRINT Statment
The print statement is used to send ASCII character strings out of the serial port (RS232
or USB). If using the Motion Basic Windows User
Interface, these characters will be printed on the output screen. The print
statement can be used to print literal strings ("hi there"), expression or
variable values, or constant values. A single print statement can be used to print
out several items, separated by either a ',' or a ';'. A ',' separator cause the
next item to be printed immediately after the first. A ';' separator will tab over
the the next 8 space column and then print out the next item. Following are some examples:
A = 10
print A
output:
10
A = 10
B = 5
print A + B
output:
15
A = 10
B = 5
print A, " ", B
output:
10 5
A = 10
B = 5
print A; B
output:
10 5
The print statement will automatically append a New Line character and a Carraige Return character at the end of whatever is being printed. To suppress the New Line and Carraige Return, you can put a ',' or a ';' at the end of the print statment:
A = 10
B = 5
print A, " ",
print B
print "done"
output:
10 5
done
back to top
INPUT Statment
The input statement is used to get input from a user over the serial port (RS232 or
USB). It has a prompt string followed by the name of a variable to be entered by the
user. For example:
input "Enter Length:", L
will send out the prompt string characters (similar to a print statement, but with out
the new line/carraige return) and then wait for input characters to be received back from
the serial port. The program will continue to wait until a Carraige Return character
is received. The input statement will then convert the decimal digits received (with
an optional '-' in front for negative numbers) into a 32 bit integer value and assign it
to the variable.
back to top
END Statement
The end statement tells the interpreter to stop program execution. It is often
convenient to have subroutines for a program appear after the main program code, and the
end statement prevents the interpreter from executing the subroutine code appearing after
the main code.
print "All done. Goodbye."
end #end of main program
#Start subroutines here:
10 ...
Even with no subroutines, you should terminate your program with an end statement to
prevent the unused program memory from being interpreted as Motion Basic
code. Note that your program can actually have multiple end statements.
back to top
Functions and Subroutines
The remainder of the statements use by Motion Basic are an
assortment of functions and subroutines, mostly related to operating the PIC-SERVO,
the PIC-STEP and the PIC-I/O
controllers. Detailed descriptions of these functions appear in the Subroutine & Function Reference. Both functions and
subroutines may take a list of parameters in parentheses (e.g. homing(1, 35) ).
Functions, however, also return a value which can be either assigned to a variable
or used in an expression.
back to top
Program Execution
Motion Basic uses a line-by-line interpereter which executes
each line of code by first reading in the text for that line from EEPROM, converting it to
the proper program action, and then executing that action. The interpreter then
proceeds to the next line of code. If the program jumps to a line of code already
executed, it will still proceed to interpret that line of code from scratch. While
not very efficient, it enables the interpreter to operate with very little memory, and it
is also very easy to debug programs using the Windows user interface. There are
several thing you can do, however, to improve the execution speed of the program:
1. Eliminate comments within loops. Comments must be read by the interpreter along with the rest of the text for a line, even though they are not processed. Putting comments before a loop will keep that text from being read multiple times.
2. Minimize comments. While comments are useful in making code more readable, they must be read by the interpreter along with the code. Keeping comments to a minimum will improve the execution speed of the program.
3. Use tabs instead of spaces. The TAB key in Motion Basic
is set to 4 spaces. Using TABs instead of spaces when creating horizontal space will
reduce number of characters the interpreter has to read.
back to top