Aeroscript Programming Language Reference
Total Page:16
File Type:pdf, Size:1020Kb
AeroScript Programming Language Reference Table of Contents Table of Contents 2 Structure of a Program 5 Comments 6 Preprocessor 7 Text Replacement Macro (#define/#undef) 7 Source File Inclusion (#include) 8 Conditional Inclusion (#if/#ifdef/#ifndef) 8 Data Types and Variables 11 Fundamental Data Types 11 Fundamental Numeric Data Types 11 Fundamental String Data Type 11 Fundamental Axis Data Type 11 Fundamental Handle Data Type 12 Aggregate Data Types 12 Array Data Types 12 Structure Data Types 13 Enumerated Data Types 14 Variables 15 Variable Declaration 15 Variable Names 15 Numeric, Axis, and Handle Variable Declaration Syntax 15 String Variable Declaration Syntax 15 Syntax for Declaring Multiple Variables on the Same Line 16 Array Variable Declaration Syntax 16 Structure Variable Definition and Declaration Syntax 16 Definition Syntax 16 Declaration Syntax 17 Member Access Syntax 17 Enumeration Variable Definition and Declaration Syntax 18 Definition 18 Declaration Syntax 19 Enumerator Access Syntax 19 Variable Initialization Syntax 20 Basic Variable Initialization Syntax 20 Array Variable Initialization Syntax 21 Structure Variable Initialization Syntax 22 Enumeration Variable Initialization Syntax 22 Variable Scope 23 Controller Global Variables 23 User-Defined Variables 23 User-Defined Variable Accessibility 23 User-Defined Local Variable Declaration Location 25 Variable Data Type Conversions 26 Properties 27 Property Declaration 27 Property Names 27 Property Declaration 28 Property Usage 28 Expressions 29 Literals 29 Numeric Literals 29 Integer Literals 29 Floating-Point Literals 29 String Literals 30 String Character Escape Sequences 30 Operators and Intrinsic Functions 31 Mathematical, Logical, and Bitwise Operators 31 Assignment and Compound Assignment Operators 31 2 Increment/Decrement Operators 31 Arithmetic Operators 31 Comparison (Relational) Operators 31 Logical Operators 32 Bitwise Operators 32 Member Access Operators 32 String Operators 32 Miscellaneous Operators and Intrinsic Functions 32 Operation Result Data Type 32 Short-Circuiting 33 Operator Precedence 33 Array Manipulation 35 Array Manipulation Overview 35 Advanced Array Manipulation 35 String Manipulation 37 Statements 38 Conditional Execution 38 If Statement 38 Switch Statement 38 Iterative Execution 40 While Loop 40 For Loop 40 ForEach Loop 41 Repeat Loop 42 Conditional and Iterative Statement Nesting 42 Labels 42 Unconditional Branching 43 Goto Statement 43 Break Statement 44 Continue Statement 45 Return Statement 45 Wait Statement 46 OnError Statement 47 Functions 48 Aerotech Standard Library API Functions 48 User-Defined Functions 48 User-Defined Function Overview 48 7.2.2: Function Arguments 49 Function Return Value 50 Function Body Definition 51 Function Overloading 51 Calling a Function 53 Simple Function Calling 53 Synchronizing Function Calls With Motion 54 Syntax for Calling a Function Synchronously 56 Declaring a Function That Can Execute Synchronously 56 Blocking and Non-Blocking Motion-Synchronized Function Calls 57 Blocking Motion-Synchronized Function Calls 58 Non-Blocking Motion-Synchronized Function Calls 59 Restrictions When Calling a Function Synchronously 61 Libraries 62 Library Types 62 Automation1 Standard Library 62 Aerotech Extending Libraries 62 User-Defined Libraries 62 Creating a Library 62 Using a Library 64 G-Code (RS-274) Support 65 Supported G-Code Letters 65 E Command 65 F Command 66 3 G-Codes 66 I/J/K Offsets 66 M-Codes 66 N-Codes 67 P Command 67 Q Command 67 R Command 67 S Command 68 Supported G-codes 68 Supported M-codes 70 G-code Specification Rules 70 G-code and Exponential Notation 70 G-code and Hexadecimal Notation 71 Appendix A: Reserved Keywords 72 Appendix B: Axis Naming Conventions 73 Appendix C: Valid Identifier Format 74 Appendix D: Valid Variable Name Format 75 4 Structure of a Program The example that follows is a simple program written in the Automation1 programming language. #include"MyDefines.ascript" #define NUM_POS 2 program var $positions[NUM_POS] as real // Enable and home axes Enable([X, Y, Z]) Home([X, Y, Z]) // Start free-running axis Z MoveFreerun(Z, 100) // Move X and Y to positions 10 and 20, respectively MoveRapid([X, Y], [10, 20]) // Perform G-code motion on axes X and Y G1 X20 Y10 F100 G1 X30 Y5 G1 X45 Y25 F35 // Dwell for 0.5 seconds using G-code G4 P0.5 // Call a function to get axis positions, perform a trigonometric // function, and then multiply the result by 1000 $positions[0] = Sin(MyFunc(X)) * 1000.0 $positions[1] = Cos(MyFunc(Y)) * 1000.0 end function MyFunc($myAxisas axis) as real return StatusGetAxisItem($myAxis, AxisStatusItem.PositionCommand) end This program example has the elements that follow: l Preprocessor (e.g., #include "MyDefines.ascript" and #define NUM_POS 2) l User-Defined Variables (e.g., $positions[]) l Comments (e.g., // Set up axes) l Aerotech Standard Library API Functions (e.g., Enable(), Home(), Sin(), Cos(), StatusGetAxisItem()) l G-Code (RS-274) Support (e.g., G4, G1) l Operators and Intrinsic Functions (e.g., =, *) l User-Defined Functions User-defined functions (e.g., MyFunc()) 5 Comments Comments that you include are ignored during the compilation of source code. Comments can document code or can prevent code from compiling. // Enable axes Enable([X, Y]) /* Home axes Home([X, Y]) */ The syntax of a single-line comment must match the regular expression that follows. ["//";'][^\n]*\n A single-line comment must start with either a double forward slash (//) and semicolon (;), or with a single quote character ('). All characters between the start of a comment and the next new line (or the end-of-file if the comment starts on the last line in the file) are ignored during compilation. You can include single-line comments at the end of a line that contains code. Enable([X, Y]) // Enable axes The syntax of a multi-line comment must match the regular expression that follows. ["/*"].*[^"*/"] That is, a multi-line comment starts with a forward slash followed by an asterisk (/*), contains any number of characters in between (including newlines), and ends with an asterisk followed by a forward slash (*/). Multi-line comments do not need to span multiple lines. They can start and terminate on the same line. /* Everything in this block is ignored by the compiler Enable([X, Y, Z]) Home([X, Y, Z]) BrakeOff([X, Y, Z]) */ Disable([X, Y, Z]) /* Multi-line comment on a single line */ 6 Preprocessor The preprocessor is used to perform certain translation operations on the source program, such as replacing text, conditionally compiling areas of code, or including macros, definitions, or access to a pre-compiled library within a program. Text Replacement Macro (#define/#undef) The #define preprocessor directive is used to define a macro, which associates some value with an identifier. The syntax variations for a #define directive are as follows. #define <Identifier> #define <Identifier><Value> #define <Identifier1>(<Identifer2>, ...) #define <Identifier1>(<Identifer2>, ...) <Value> The <Identifier*> arguments must be valid identifiers (see Appendix C: Valid Identifier Format). Macros have two general purposes: l They allow text to be substituted in a program before the program is compiled. l They allow an identifier to be defined, which can be used to perform conditional compilation based on the existence of that identifier. // Define a macro without a token string #define DEBUG_ENABLE // Define a macro with some replacement text #define MY_ADD(x, y) (x + y) // Define some macros with replacement strings #define MY_STRING "Hello" #define MY_VALUE 2 // The following is replaced with var $myStr as string = "Hello" var $myStr as string= MY_STRING #ifdef DEBUG_ENABLE // This code compiles because DEBUG_ENABLE has been #defined Enable([X, Y]) #endif // The following is replaced with $rglobal[0] = (1 + 2) $rglobal[0] = MY_ADD(1, MY_VALUE) The #define preprocessor directive cannot be used to replace text within string literals. // Define a macro with a token string #define MY_VALUE 3 // Text replacement occurs and $myInt is assigned a value of 3 var $myInt as integer = MY_VALUE // No text replacement occurs, so $myStr is assigned "MY_VALUE" var $myStr as string = "MY_VALUE" 7 The #undef preprocessor directive can be used to undo a definition that was previously defined with the #define preprocessor directive. The syntax of an #undef preprocessor directive is as follows. #undef <Identifier> Similar to the #define preprocessor directive, the <Identifier> argument must be a valid identifier (see Appendix C: Valid Identifier Format). #define DEBUG_MODE #undef DEBUG_MODE #ifdef DEBUG_MODE // This code is not compiled because DEBUG_MODE is no longer #defined Enable(X) #endif The backslash character (\) can be used to allow a #define preprocessor directive macro to span multiple lines. #define M300 \ Enable(X) \ Home(X) \ AnalogOutputSet(X, 0, 0.0) // The following will be replaced with all 3 lines specified in the // above #define macro M300 Source File Inclusion (#include) The #include preprocessor directive is used to directly include the text from another source file into the program in the line directly following the directive. This can be used, for example, to include another program that contains #defines. Only preprocessor directives and comments can be specified in a file that you includes with the #include preprocessor directive. // defines.ascript #define VAL1 123.4 #define VAL2 0xFF // main.ascript #include"defines.ascript" $rglobal[0] = VAL1 $rglobal[2] = VAL2 Conditional