1.2 AScript BASIC Commands
The BASIC part of Ascript can be broken down into different categories.
- There is a command to create Loops. This is the WHILE command. Unlike more advanced BASICs which have the FOR NEXT loop, the only loop command in Ascript is the WHILE command. To create a loop with a certain number of iterations, you must define a counter for the loop. You can store a variable in a counter, and then increment it in the loop. The WHILE command tests for the variable counter.
- The Logic or Comparison command set, handled by the IF statement. There are a number of ways to nest the IF statement, so by using multiple ELSEIF commands you can implement something similar to the SELECT CASE command in other BASICs. The IF statement can consist of the IF command, multiple ELSEIF commands after it (as needed) and then only one ELSE command (per IF) and finally the ENDIF command to end the block.
- Subroutines are numbered (no name) from 0 to 31. Subroutine number 31 is a special routine which is called when there is an error. It is important to break up a program into smaller subroutines so it is easier to see the flow of code. Modular design makes code easier to work with, but also allows you to do more with less lines of code. If a subroutine can be used multiple times, the number of lines of code in your program is decreased. The Commander has a limit of how many lines of code (converted to Pcode) it can store.
- AScript allows up to 100 variables, numbered from 0 to 99 (for 4 axis Commander, but 0 to 64 for 2 axis Commander). Variables allow you to store a value for later comparison and to do calculations and store the results of the calculations.
Note: Math calculations can only be used during the setting of a variable (V# = something). During comparisons, such as in an IF statement you can not do a math calculation.
Available math calculations:
|
Operator |
Description |
Example |
|
+ |
Integer Addition |
V1=V2+V3 |
|
- |
Integer Subtraction |
V1=V2-V3 |
|
* |
Integer Multiplication |
V1=V2*V3 |
|
/ |
Integer Division (round down) |
V1=V2/V3 |
|
% |
Modulus |
V1=V2%5 |
|
>> |
Bit Shift Right |
V1=V2>>2 |
|
<< |
Bit Shift Left |
V1=V2<<2 |
|
& |
Bitwise AND |
V1=V2&7 |
|
| |
Bitwise OR |
V1=V2|8 |
|
~ |
Bitwise NOT |
V1=~V2 |
If you require a more complex calculation you will need to have multiple lines of code and do one calculation on each line of code.
- Motion control functions
Some motion control commands include a function syntax where they return a state or value from the Commander. When you use the motion control commands in their function syntax (READ, not WRITE) you can use them in BASIC statements as a numeric value. You can store a motion control state into a variable, or compare a motion control state in an IF statement. So in their function (READ) syntax motion control commands become part of the BASIC language to return state values back from the Commander.
; [some text] Any text following semicolon in the same line appears as a comment
‘ [some text] Any text following single quote in the same line appears as a comment
Note: comments also can come after any command or statement
DELAY Sets delay in ms
GOSUB[0-31] Calls and executes subroutine [0-31] and then returns to next line
IF [comparison]
ELSEIF [comparison]
ELSE
ENDIF
Executes the instruction following the IF statement when condition is true. Execute the instruction following the next ELSE or ELSEIF statement when the condition is false. When all conditions are false, continue executing program after ENDIF. The following operands are used in the conditional IF statement: =, >, <, >=, <=, != , <>
The comparisons in an IF statement are always simple and not complex. They are simply one value compared to another value (no parantheses , no math). The operands are obvious but with one slight variation. The operand != and <> both mean the same thing of not equal. The first one is typical of not equal as used in C and the second one is typical of not equal in BASIC. Both are acceptable.
Both the IF line and the ELSEIF lines can have a comparison. The ELSE and ENDIF lines do not.
The new version of the Ascript compiler now has the ability of having unlimited nested IF and WHILE statements. The old version of the compiler was limited to just a few levels deep. The new compiler uses a two pass compiler which allows it to accomplish unlimited nesting.
In the comparisons used in the IF statement, each side of the comparison can be one of the following: a variable (the letter V followed by a number), a numeric value (integer only) or any motion control command which has a read syntax which returns a single value. Such motion control commands (read) act like a function and return a value.
Here is an example:
PRG 2 ‘ Program start line, program 2
HSPD=20000 ‘ Set high speed to 20,000 pulses/sec
LSPD=1000 ‘ Set low speed to 1000 pulses/sec
ACC=300 ‘ Set acceleration to 300 msec
EO=1 ‘ Enable the X-axis
V1=1 ‘ Set Variable # 1 to 1
WHILE 1=1 ‘ Forever loop
IF DI1=V1 ‘ If digital input is on (= V1 which is 1) statement is TRUE
X0 ‘ Move X-axis to zero
WAITX ‘ Wait for X-axis move to complete
X1000 ‘ Move X-axis to 1,000
WAITX ‘ Wait for X-axis move to complete
ENDIF ‘ End of the IF loop
ENDWHILE ‘ Go back to WHILE Statement
END ‘ End program # 2
Note: The THEN keyword is allowed after the comparison in both the IF and ELSEIF commands. This is how many BASIC languages work, so Ascript can handle it either way (no THEN or use THEN). But when using THEN you can not put code after it like some BASIC’s. You must put that code on the next line. So both of the following are acceptable:
IF DI1=V1 ‘ If digital input 1 is on, execute the following commands
X0 ‘ Move X-axis to zero
WAITX ‘ Wait for X-axis move to complete
X1000 ‘ Move X-axis to 1,000
WAITX ‘ Wait for X-axis move to complete
ENDIF ‘ End of the IF loop
IF DI1=V1 THEN ‘ If digital input 1 is on, execute the following commands
X0 ‘ Move X-axis to zero
WAITX ‘ Wait for X-axis move to complete
X1000 ‘ Move X-axis to 1,000
WAITX ‘ Wait for X-axis move to complete
ENDIF ‘ End of the IF loop
PRG[0-3] starts a program (thread) which is one of four possible programs
END ends current program
Ascript is multitasking (similar to threads, but technically it does time slicing and executes one line of code for each active program in a sequence and then starts over again so giving each program its own slice of the cycle). The PRG / END block defines the first and last line of a standalone program [0-3].
Example:
PRG 0 ‘ Program start line, program 0
HSPD=20000 ‘ Set high speed to 20,000 pulses/sec
LSPD=1000 ‘ Set low speed to 1000 pulses/sec
ACC=300 ‘ Set acceleration to 300 msec
EO1=1 ‘ Enable the X-axis
V1=1 ‘ Set Variable # 1 to 1
WHILE 1=1 ‘ Forever loop
IF DI1=V1 ‘ If digital input 1 is on, execute the following commands
X0 ‘ Move X-axis to zero
WAITX ‘ Wait for X-axis move to complete
X1000 ‘ Move X-axis to 1,000
WAITX ‘ Wait for X-axis move to complete
ENDIF ‘ End of the IF loop
ENDWHILE ‘ Go back to WHILE Statement
END ‘ End program # 0
PRG 1 ‘ Program start line, program 1
HSPD=20000 ‘ Set high speed to 20,000 pulses/sec
LSPD=1000 ‘ Set low speed to 1000 pulses/sec
ACC=300 ‘ Set acceleration to 300 msec
EO2=1 ‘ Enable the Y-axis
V1=1 ‘ Set Variable # 1 to 1
WHILE 1=1 ‘ Forever loop
IF DI2=V1 ‘ If digital input 2 is on, execute the following commands
Y0 ‘ Move Y-axis to zero
WAITY ‘ Wait for Y-axis move to complete
Y1000 ‘ Move Y-axis to 1,000
WAITY ‘ Wait for Y-axis move to complete
ENDIF ‘ End of the IF loop
ENDWHILE ‘ Go back to WHILE Statement
END ‘ End program # 1
SUB[0-31] Beginning of subroutine (31 is for errors)
ENDSUB End of subroutine and then return to caller
The SUB / ENDSUB block is used to define the first and last line of a Subroutine [0-31]
Example:
PRG 0 ‘ Program start line, Program 0
HSPD=20000 ‘ Set high speed to 20,000 pulses/sec
LSPD=1000 ‘ Set low speed to 1000 pulses/sec
ACC=300 ‘ Set acceleration to 300 msec
ABS ‘ Set move mode to absolute mode
EO=1 ‘ Enable the X-axis
V1=0 ‘ Set variable 1 to 0
WHILE 1=1 ‘ Forever loop
IF DI1=1 THEN ‘ If digital input 1 is on, execute the following commands
GOSUB 1 ‘ Execute subroutine 1
ENDIF ‘ End of the IF block
ENDWHILE ‘ Go back to WHILE statement
END ‘ End of program 0
SUB 1 ‘ Subroutine start line, subroutine 1
XV1 ‘ Move X-axis to V1 target position
WAITX ‘ Wait for X-axis move to complete
V1=V1+1000 ‘ Increment V1 by 1,000
WHILE DI1=1 ‘ Wait until the DI1 is turned off (prevent multiple increments)
ENDWHILE ‘ Go back to WHILE statement
ENDSUB ‘ End of subroutine 1
V[var] (read) Get a variables value (acts like a function)
V[var] = value (write) Set a variables value.
Variables are defined with a number (0 to 99) for 4 axis controller and (0 to 64) for a 2 axis controller. Variables are 32 bit signed integers only. Ascript currently does not support floating point.
WHILE [comparison] Start WHILE loop and test for comparison to exit loop
ENDWHILE Return to WHILE (loop)
Example:
WHILE DI1=1 ‘ Wait until the DI1 is turned off (prevent multiple increments)
ENDWHILE ‘ Go back to WHILE statement
Continues executing the program after the WHILE statement when the conditional statement is true. Repeat until WHILE condition is false, and then execute program after ENDWHILE. Conditional operands available include: =, >, <, >=, <=, !=, <>