410 likes | 787 Vues
SPiiPlus Training Class. Program Flow Commands. Program Execution. ACSPL+ programs execute 1 command “block” per controller cycle Normally 1 line of code = 1 command “block” Multiple commands can be on a single line, separated by semicolon (;)
E N D
SPiiPlus Training Class Program Flow Commands
Program Execution ACSPL+ programs execute 1 command “block” per controller cycle • Normally 1 line of code = 1 command “block” • Multiple commands can be on a single line, separated by semicolon (;) • Statements between BLOCK…END control structure are executed as one command “block” • Certain commands cannot be executed as a single block (background tasks, loops, array processing, subroutine calls, goto statements) After “block” executes, program flow moves to the next “block” in the sequence unless special program flow commands are used
Program Execution vs. Motion Execution ACSPL+ provides dedicated motion commands for starting and ending motion. • Motion commands are executed in a single command “block” • After motion command is executed, program flow moves to the next command “block” unless special program flow commands are used This means that unless you explicitly program it, motion execution and program execution are decoupled.
Program Execution vs. Motion Execution velocity time ENABLE (0) PTP/r (0), 10000 program execution time STOP
Program Execution vs. Motion Execution velocity time STOP ENABLE (0) PTP/r (0), 10000 program execution time WAIT 2000
Program Execution vs. Motion Execution velocity time ENABLE (0) PTP/r (0), 10000 STOP program execution time TILL MST(0).#MOVE = 1
Program Flow Commands ACSPL+ provides dedicated program flow commands to control and manipulate program execution • IF…ELSEIF…ELSE…END • WHILE…END • LOOP…END • WAIT • TILL • GOTO • CALL…RET • START, STOP, PAUSE, RESUME
IF…ELSEIF…ELSE…END IF statement is one of the most basic conditional programming constructs • IF statement checks if conditional expression is true (evaluates to non-zero), and if it is true it will only execute its command list • If false (evaluates to zero), it will check the next ELSEIF statement to see if its expression is true, and if it is true it will only execute its command list • If the IF and ELSEIF statements are all false, it will execute the ELSE statements command list • IF statement is terminated with an END statement • ELSEIF and ELSE statements are optional
IF…ELSEIF…ELSE…END Syntax: IF (conditional expression) (command list) [ELSEIF (conditional expression)] [(command list)] [ELSE] [(command list)] END
IF…ELSEIF…ELSE…END Example:
WHILE…END WHILE loop is a basic program flow statement that allows code to be executed repeatedly • WHILE statement checks if conditional expression is true (evaluates to non-zero), and if it is true it will execute its command list • WHILE statement will repeatedly execute command list until expression is false (evaluates to zero) • WHILE statement is terminated with an END statement
WHILE…END Syntax: WHILE (conditional expression) (command list) END
WHILE…END Example:
LOOP…END LOOP is another basic program flow statement that allows code to be executed repeatedly • LOOPstatement evaluates expression and executes command list with that many iterations • If expression evaluates to zero or negative number, command list is not executed • If expression evaluates to non-integer number, it will be rounded to closest integer • Expression is only evaluated the first time the loop executes (it is not re-evaluated every iteration) • LOOP statement is terminated with an END statement
LOOP…END Syntax: LOOP (expression) (command list) END
LOOP…END Example:
WAIT WAIT is a special program statement that delays program execution • WAIT statement evaluates expression and delays program execution for the specified number of milliseconds • If expression evaluates to zero or negative number, no wait occurs • Expression is rounded to nearest control cycle interval (typically 0.5 or 1 millisecond)
WAIT Syntax: WAIT (expression)
WAIT Example:
TILL TILL is another special program statement that delays program execution • TILL statement checks conditional expression and delays program execution until the expression becomes true (evaluates to non-zero) • TILL has an optional timeout argument that will set a maximum delay time in milliseconds • If a timeout is used, the user can check the conditional expression after the delay to see if the timeout occurred
TILL Syntax: TILL (expression) [, (timeout)]
TILL Example:
Program Labels Program labels are special programming constructs used to provide entrances into programs • Used by GOTO, CALL, and START program flow commands • Can be any alphanumeric sequence that must begin with a letter, end with a colon : and can also include the underscore _ • Many labels can exist within a program buffer, but each name must be unique • Same label name can be used in different program buffers • A special label called AUTOEXEC is known by the firmware • Any buffer with an AUTOEXEC is automatically started at the AUTOEXEC label upon power up
GOTO GOTO is a program flow command that transfers program execution to a specified label • GOTO behaves as a program jump and never returns control back to the GOTO statement • GOTO can only transfer program execution to a label in the same buffer (cannot GOTO a different buffer) • GOTO should not be used to enter / exit a subroutine
GOTO Syntax: GOTO (label)
GOTO Example:
CALL…RET CALL is a program flow command that is used to call a subroutine • CALL will transfer program execution to a specified label • After subroutine completes it must end in a RET command so program execution will return back to the where the CALL was executed • A CALL stack is implemented so that nested subroutine calls can be used • CALL can only call subroutines within the same buffer
CALL…RET Syntax: CALL (label) (label): … RET
CALL…RET Example:
START…STOP…PAUSE…RESUME START, STOP, PAUSE, and RESUME are special program management commands • START will execute another buffer at a specified label or line number • Will generate run-time error if starting buffer that is already executing • STOP will stop another buffer from executing • PAUSE will pause another buffers execution • RESUME will resume a paused buffer • Commands can be executed by a buffer or streamed from a host
START…STOP…PAUSE…RESUME Syntax: START (buffer number), (line number or label) STOP (buffer number) PAUSE (buffer number) RESUME (buffer number)
START…STOP…PAUSE…RESUME Example:
ACSPL+ Programming Example: 1 • Load program “Programming 05 – ProgramFlow.prg” to the controller • Should populate buffer 21 • Open communication terminal and set it up to show DISP messages • From the communication terminal start buffer 21 at label ‘Begin’ (“START 21, BEGIN”). Follow the instructions on the screen What happens?
ACSPL+ Programming Example: 2 • Copy previous examples code into buffer 22 • Modify code so that after the data collection has completed, run_dc = 0, the arrays will be re-organized so the last collected data point will be at the end of the array
ACSPL+ Programming Example: 3 • Open program buffer 23 • Write a routine to calculate the index of the maximum value of the first column of an array of a given length and display both the index and value to the communication terminal • Test the routine on the analog input array from example 2
ACSPL+ Programming Example: 4 • Open program buffer 24 • Write a routine that will do a simple moving average of the input array and test on the analog input array from example 2. • Plot the filtered analog input data vs. the actual