Index Manuals FANUC Robotics SYSTEM R-30iA and R-30iB Controller. KAREL Reference Manual (MARRC75KR07091E Rev D)
|
|
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
----
confirmation. These two keys are monitored by global
----
condition handler,so User response can be trapped.
------------------------------------------------------------------------------
ROUTINE YES_NO
VAR
key_press : INTEGER
str : STRING[1]
n_chars: INTEGER
l_status: INTEGER
BEGIN
---- YES_NO
WRITE CRTFUNC
(CHR(128),CHR(137))
--- Clear Window, Home Cursor
-- Display YES above F1 & NO above F2 & clear rest of Function window
WRITE CRTFUNC
(’
YES
NO ’,chr(129))
F1_press = FALSE
F2_press = FALSE
REPEAT -- until user presses either the F1 or F2 key
-- Read just the function keys of the CRT/KB.
-- The read will be satisfied only when a function key is pressed.
READ_KB (crt_kb, str , 0, 0, kc_func_key, -1,
’’, n_chars, key_press, l_status)
-- key_press must be converted from a "raw" CRT character to the teach
-- pendant equivalent character.g
key_press = $CRT_KEY_TBL[key_press+1]
IF (key_press = ky_f1) THEN -- The user pressed F1
F1_press = true
ENDIF
IF (key_press = ky_f2) THEN -- The user pressed F2
F2_press = true
ENDIF
UNTIL ((f1_press = TRUE) OR (F2_press = TRUE))
WRITE CRTFUNC
(CHR(128),CHR(137))
--- Clear Window, Home Cursor
WRITE CRTPROMPT
(CHR(128),CHR(137))
--- Clear Window, Home Cursor
END YES_NO
Copy Path Variables Program - Copying Path Variables
------------------------------------------------------------------------------
---- Section 3-C: PTH_CPY Declaration
----
Copy one user defined path variable to another user defined path
----
variable. The first parameter is the source path. The second
----
parameter is the destination path. The path parameters can only
----
be passed using BYNAME and the paths must be user defined
------------------------------------------------------------------------------
ROUTINE PTH_CPY(src_path: USER_PATH; des_path: USER_PATH)
VAR
node_indx
:INTEGER
B-22
MARRC75KR07091E Rev D
B. KAREL EXAMPLE PROGRAMS
do_it
:BOOLEAN
1_stat
:INTEGER
BEGIN --- pth_cpy
CRT_CLS
-- Clear the CRT/KB USER Menu screen
do_it = true
WRITE (’Perform copy?’,CR)
yes_no
do_it = F1_press -- F1_press will be true only if the user selected
YES
IF (do_it) THEN
-- Copy the entire path of src_path to des_path
COPY_PATH (src_path, 0,0, des_path, l_stat)
IF (l_stat <> 0) THEN
WRITE (’Error in COPY_PATH’, l_stat, CR)
ELSE
WRITE (’Path Copy function Completed ’,cr)
ENDIF
ELSE
WRITE (’Path Copy function canceled by choice’,cr,cr)
ENDIF
END PTH_CPY
----------------------------------------------------------------------------
----
Section 3-D: Routine move_to_pr is a TP
----
routine for doing moves
----------------------------------------------------------------------------
ROUTINE move_to_pr FROM move_to_pr
-- move_to_pr must also be loaded.
--
1:J PR[1] 100% FINE
;
Copy Path Variables Program - Opens CRT/KB & Sends Data to Default Device
-----------------------------------------------------------------------------
----
Section 4: Main Program
-----------------------------------------------------------------------------
BEGIN --- CPY_PATH
-----------------------------------------------------------------------------
----
Section 4-A: Open CRT KB for reading YES/NO inputs from user
-----------------------------------------------------------------------------
CRT_CLS -- will force the CRT USER menu to be visible & clear the screen
SET_FILE_ATR(crt_kb, ATR_FIELD) -- Needed so the read is satisfied with one
-- character.
OPEN FILE crt_kb (’RO’, ’KB:crkb’) -- Open a file to the CRT/KB
-- Used within the YES_NO routine.
-----------------------------------------------------------------------------
----
Section 4-B: Check if SAVE_VRS.PC is loaded. If loaded then execute
-----------------------------------------------------------------------------
---- First check if the "SAVE_VRS" program is loaded or not.
B-23
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
prog_name = ’SAVE_VRS’ -- Only interested in SAVE_VRS program
prog_type = 6
-- Interested only in PC type files
n_skip = 0
-- First time do not skip any files
format = 1
-- Return just the filename
do_copy = TRUE
WRITE (’Checking Program List’,cr)
PROG_LIST(prog_name, prog_type, n_skip, format, ary_nam, n_match, status)
IF (status <>SUCCESS ) THEN
IF (status = 7073 ) THEN
---- Program does not exist error
--- Program SAVE_VRS is not loaded on the controller.
WRITE ( ’LOADING ’,prog_name, CR)
LOAD (prog_name+’.PC’, 0, status)
IF (status <> SUCCESS) THEN
WRITE (’Error loading ’, prog_name,cr)
WRITE CRTPROMPT(’Copy paths WITHOUT saving program variables?’,CR)
YES_NO
do_copy = F1_press -- F1_press is true only if user selected
YES
-- Copy without saving variables.
ENDIF
ELSE
-- The program listing failed.
WRITE (’PROG_LIST built-in failed’,cr,’ with Status = ’,status,cr)
WRITE CRTPROMPT(’Copy paths WITHOUT saving program variables?’,CR)
YES_NO
do_copy = F1_press -- F1_press is true only if user selected
YES
ENDIF
-- Copy without saving variables.
ENDIF
Copy Path Variables Program - Checks Path Initialization
IF (status = SUCCESS) THEN
-- This is one way to set variables within another program without
-- using the FROM clause in the variable section.
-- It is very useful if you want to have run-time independent code,
-- where the program or variable name you are setting is not
-- known until run-time.
cur_name = CURR_PROG
SET_VAR (entry, prog_name, ’del_vr’, TRUE, status)
SET_VAR (entry, prog_name, ’prog_name’,cur_name , status)
SET_VAR (entry, prog_name, ’sav_type’, 1, status)
SET_VAR (entry, prog_name, ’dev’, ’FLPY:’, status)
WRITE (’Saving program variables before copy’, CR)
CALL_PROG(prog_name,prog_indx) -- call SAVE_VRS
ENDIF
B-24
MARRC75KR07091E Rev D
B. KAREL EXAMPLE PROGRAMS
---------------------------------------------------------------------------
----
Section 4-C: Check for initialization of PATHs pth1 and pth2.
---------------------------------------------------------------------------
IF (NOT do_copy) THEN
WRITE (’Program exiting, unable to save variables,’,cr)
WRITE (’before copying path’’s content’,cr)
-- NOTICE:
-- Two single quotes will display as one single quote
-- so this write statement will appear as :
-- "before copying path’s content"
ABORT
ENDIF
WRITE (’Checking Variable initialization’,cr)
-- Check if the pth variables are initialized.
p1_len = PATH_LEN(pth1)
; p2_len = PATH_LEN(pth2)
IF ( (p1_len = 0) OR (p2_len = 0) )THEN
WRITE
(’PTH1 or PTH2 is empty path’,cr)
WRITE
(’Please make sure both paths are taught then restart’,cr)
ABORT
-- Cannot copy uninitialized variables.
ENDIF
-- Check if the pth3 variable is initialized.
IF
(PATH_LEN(pth3) <> 0) THEN
WRITE (’Deleting nodes from pth3’,cr) -- Delete the old path of pth3
FOR indx = PATH_LEN(pth3) DOWNTO 1 DO
-- its easy to delete nodes from the end instead of deleting node from
-- the front end. Since after every deletion the nodes are renumbered.
DELETE_NODE(pth3, indx, status) -- Delete last node of pth3
IF status <> SUCCESS THEN
WRITE (’While Deleting ’,indx, ’ node’,cr)
WRITE (’DELETE_NODE unsuccessful: Status = ’,status,cr)
ENDIF
ENDFOR
ENDIF
Copy Path Variables Program - Path Initialization
-----------------------------------------------------------------------------
----
Section 4-D: Add pth1 and pth2 together to create pth3.
----
Move along pth1 and pth2.
----
Move backwards through pth3.
-----------------------------------------------------------------------------
total_node = p1_len + p2_len
-- Total number of nodes needed for pth3
-- Copy the node data from pth1 to pth3
WRITE (’copying pth1 to pth3’,cr)
COPY_PATH (pth1, 0,0, pth3, status)
IF (status <> 0) THEN
B-25
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
WRITE (’ERROR in COPY_PATH’, status, CR)
ENDIF
-- Create the required number of nodes for pth3.
-- We know that pth3 now has PATH_LEN(pth3) nodes.
WRITE (’Appending nodes to pth3’,cr)
FOR indx = p1_len+1 TO total_node DO -- Append the correct number
of
nodes.
APPEND_NODE(pth3, status)
IF (status <> 0) THEN
WRITE (’While Appending ’,indx, ’ node’,cr)
WRITE (’APPEND_NODE unsuccessful: Status = ’,status,cr)
ENDIF
ENDFOR
-- Append the node data of pth2 to pth3.
WRITE (’Appending pth2 to pth3’,cr)
FOR indx = p1_len+1 TO total_node DO
USING pth2[indx - p1_len] DO
pth3[indx].node_pos
= node_pos
ENDUSING
ENDFOR
-- Move along the path pth1 and pth2
WRITE (’Moving Along Path pth1’,cr)
FOR node_ind = 1 TO p1_len DO
tmp_xyz = pth1[node_ind]
SET_POS_REG(1, tmp_xyz, status)
move_to_pr -- Call TP program to move to PR[1]
ENDFOR
WRITE (’Moving Along Path pth2’,cr)
FOR node_ind = 1 TO p2_len DO
tmp_xyz = pth2[node_ind]
SET_POS_REG(1, tmp_xyz, status)
move_to_pr -- Call TP program to move to PR[1]
ENDFOR
--Copy pth3 in reverse order to pth4
COPY_PATH (pth3, PATH_LEN(pth3), 1, pth4, status)
IF (status <> 0) THEN
WRITE (’ERROR in COPY_PATH’, status, CR)
ENDIF--- Move along pth4 which is a reverse order
of
pth3.
WRITE (’Moving Along Path pth4’,cr)
FOR node_ind = 1 TO PATH_LEN(pth4) DO
tmp_xyz = pth4[node_ind]
SET_POS_REG(1, tmp_xyz, status)
move_to_pr -- Call TP program to move to PR[1]
ENDFOR
Copy Path Variables Program - Copy User Defined Paths
B-26
MARRC75KR07091E Rev D
B. KAREL EXAMPLE PROGRAMS
-----------------------------------------------------------------------------
----
Section 4-E: Copy User Defined Paths.
----
Copy one user defined path to another user defined path,
----
where the user specifies which paths to be copied.
-----------------------------------------------------------------------------
CRT_CLS
SET_CURSOR(OUTPUT,2,10, status)
-- Position cursor nicely on CRT
IF (status <> 0 ) THEN
WRITE (’SET_CURSOR built-in failed with status = ’,status,cr)
ENDIF
-- write message in reverse video and then set back to normal video
WRITE (chr(139),’ COPY PATH FUNCTION’,chr(143),CR,cr)
WRITE (’Currently you have the following ’,cr)
WRITE (’User Defined Paths’,cr,cr)
n_skip = 0
var_type = 31
-- Get listing of only PATH type variables
REPEAT
VAR_LIST (’CPY_PTH’, ’*’,var_type, n_skip, 2, ary_nam, n_match, status)
FOR indx = 1 TO n_match DO
IF (SUB_STR (ary_nam[indx], 1, 4) = ’UPTH’) THEN -- Verify it’s one of
-- the user defined paths
WRITE (ary_nam[indx], CR)
ENDIF
ENDFOR
n_skip = n_skip + n_match
UNTIL (n_match < ARRAY_LEN(ary_nam))
Write (’Enter the source path number:’)
READ(src_num);
Write (’Enter the destination path number:’)
READ(des_num);
CNV_INT_STR(src_num,2,0,dummy_str)
-- Convert source number to string
src_var = ’UPTH’+ SUB_STR(dummy_str,2,1) -- SUB_STR will remove the leading
-- blank from dummy_str before
-- concatenating to create the
var_type = 0
-- source variable name
VAR_LIST (’CPY_PTH’, src_var, var_type, 0, 2, ary_nam, n_match, status)
IF (status <> SUCCESS) THEN
WRITE (’Var_list unsuccessful for src_var: status ’, status, cr)
ENDIF
Copy Path Variables Program - Copy User Defined Paths Continued
-- If the variable does not exist create it.
IF (n_match = 0) THEN
CREATE_VAR (’’, src_var, ’’, ’USER_PATH’, 1, 0, 0, 0, status, mem_loc)
IF (status <> SUCCESS) THEN
B-27
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
WRITE (’Error creating ’, src_var, ’:’, status, cr)
ENDIF
ENDIF
--Create the destination variable name
CNV_INT_STR(des_num,2,0,dummy_str)
-- Convert des_num to a string
des_var = ’UPTH’+ SUB_STR(dummy_str,2,1)-- The SUB_STR will remove the leading
-- blank from dummy_str before
-- concatenating to create the
-- source variable name
-- Verify that the des_var variable exists.
VAR_LIST (’CPY_PTH’, des_var, var_type, 0, 2, ary_nam, n_match, status)
IF (status <> SUCCESS) THEN
WRITE (’Var_list unsuccessful for des_vr: status’, status, cr)
ENDIF
-- If the variable does not exist create it.
IF (n_match = 0) THEN
CREATE_VAR (’’, des_var, ’’, ’USER_PATH’, 1, 0, 0, 0, status, mem_loc)
IF (status <> SUCCESS) THEN
WRITE (’Error creating ’, des_var, ’:’, status, cr)
ENDIF
ENDIF
-- Copy the specified source path to the specified destination path
pth_cpy(BYNAME(’’, src_var, indx), BYNAME(’’, des_var, indx) )
-- Close file before quitting
CLOSE FILE crt_kb
WRITE (’CPY_PTH example completed’,cr)
END CPY_PTH
B.3
SAVING DATA TO THE DEFAULT DEVICE
This program will save variables or teach pendant programs to the default device. If the user specified
to overwrite the file then the file will be deleted before performing the save.
Note This program is called by the CPY_PTH.KL program. Refer to Section B.2 , for information on
CPY_PTH.KL.
Saving Data Program - Overview
-----------------------------------------------------------------------------
----
SAVE_VRS.KL
-----------------------------------------------------------------------------
----
Section 0: Detail about SAVE_VRS.KL
-----------------------------------------------------------------------------
---- Elements of KAREL Language Covered:
In Section:
----
Actions:
B-28
MARRC75KR07091E Rev D
B. KAREL EXAMPLE PROGRAMS
----
Clauses:
----
Conditions:
----
Data types:
----
BOOLEAN
Sec
2
----
INTEGER
Sec
2
----
STRING
Sec
2
----
Directives:
----
COMMENT
Sec
1
----
ENVIRONMENT
Sec
1
----
NOLOCKGROUP
Sec
1
----
Built-in Functions & Procedures:
----
DELETE_FILE
Sec
4-B
----
SAVE
Sec
4-B
----
Statements:
----
IF, THEN, ENDIF
Sec
4-B
----
SELECT, CASE, ENDSELECT
Sec
4-A
----
WRITE
Sec
4-B
----
Reserve Words:
----
BEGIN
Sec
4
----
CONST
Sec
2
----
CR
Sec
4-B
----
END
Sec
4-B
----
PROGRAM
Sec
1
----
VAR
Sec
2
Saving Data Program - Declarations Section
--------------------------------------------------------------------------
----
Section 1: Program and Environment Declaration
--------------------------------------------------------------------------
PROGRAM SAVE_VRS
%NOLOCKGROUP
%COMMENT = ’Save .vr, .tp, .sv’
%ENVIRONMENT MEMO
%ENVIRONMENT FDEV
--------------------------------------------------------------------------
----
Section 2: Constant, Variable and Type Declarations
--------------------------------------------------------------------------
CONST
DO_VR
= 1
-- Save variable file(s)
DO_TP
= 2
-- Save TP program(s)
DO_SYS = 3
-- Save system variables
SUCCESS = 0
-- The value expected from all built-in calls.
VAR
sav_type
: INTEGER
-- Specifies the type of save to perform
prog_name : STRING[12]
-- The program name to save
B-29
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
status
: INTEGER
-- The status returned from the built-in calls
file_spec : STRING[30]
-- The created file specification for SAVE
dev
: STRING[5]
-- The device to save to specify whether to
del_vr
: BOOLEAN
-- delete file_spec before performing the SAVE.
--------------------------------------------------------------------------
----
Section 3: Routine Declaration
--------------------------------------------------------------------------
Saving Data Program - Create File Spec
--------------------------------------------------------------------------
----
Section 4: Main Program
--------------------------------------------------------------------------
BEGIN -- SAVE_VRS
--------------------------------------------------------------------------
----
Section 4-A: Create the file_spec, which contains the device, file
----
name and type to be saved.
---------------------------------------------------------------------------
SELECT (sav_type) OF
CASE (DO_VR):
-- If prog_name is ’*’ then all PC variables will be saved with the
-- correct program name, irregardless of the file name part of
-- file_spec.
file_spec = dev+prog_name+’.VR’
-- Create the variable file name
CASE (DO_TP):
-- If prog_name is ’*’ then all TP programs will be saved with the
-- correct TP program name, irregardless of the prog_name part of
-- file_spec.
file_spec = dev+prog_name+’.TP’
-- Create the TP program name
CASE (DO_SYS):
prog_name = ’*SYSTEM*’
file_spec = dev+’ALLSYS.SV’
-- All system variables will be
-- saved into this one file.
ENDSELECT
Saving Data Program - Delete/Overwrite
---------------------------------------------------------------------------
----
Section 4-B: Decide whether to delete the file before saving
----
and then perform the SAVE.
---------------------------------------------------------------------------
-- If the user specified to delete the file before saving, then
-- delete the file and verify that the delete was successful.
-- It is possible that the delete will return a status of:
--
10003 : "file does not exist", for the FLPY: device
B-30
MARRC75KR07091E Rev D
B. KAREL EXAMPLE PROGRAMS
--
OR
--
85014 : "file not found", for all RD: and FR: devices
-- We will disregard these errors since we do not care if the
-- file did not previously exist.
IF (del_vr = TRUE) THEN
DELETE_FILE (file_spec, FALSE, status) -- Delete the file.
IF (status <> SUCCESS) AND (status <> 10003) AND
(status <> 85014) THEN
WRITE (’Error ’, status,’ in attempt to delete ’,cr, file_spec,cr)
ENDIF
ENDIF
-- If prog_name is specified as an ’*’ for either .tp or .vr files then
-- the SAVE builtin will save the appropriate files/programs with the
-- correct names.
SAVE (prog_name, file_spec, status) -- Save the variable/program
IF (status <> SUCCESS) THEN
-- Verify SAVE was successful
WRITE (’error saving ’, file_spec, ’variables’, status, cr)
ENDIF
END SAVE_VRS
B.4
STANDARD ROUTINES
This program is made up of several routines which are used through out the examples. The following
is a list of the routines within this file:
• CRT_CLS Clears the CRT/KB USER Menu screen
• TP_CLS Clears the teach pendant USER Menu screen
Standard Routines - Overview
-------------------------------------------------------------------------
----
ROUT_EX.KL
-------------------------------------------------------------------------
----
Section 0: Detail about ROUT_EX.kl
-------------------------------------------------------------------------
---- Elements of KAREL Language Covered:
In Section:
----
Actions:
----
Clauses:
----
Conditions:
----
Data types:
----
Built-in Functions & Procedures:
----
CHR
Sec 3-A,B
----
FORCE_SPMENU
Sec 3-A,B
----
Statements:
----
ROUTINE
Sec 3-A,B
B-31
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
----
WRITE
Sec 3-A,B
----
----
Reserve Words:
----
BEGIN
Sec 3-A,B; 4
----
CR
Sec 3-B
----
END
Sec 3-A,B; 4
----
PROGRAM
Sec 1
----
Predefined File Names:
----
CRTERROR
Sec 3-A
----
CRTFUNC
Sec 3-A
----
CRTPROMPT
Sec 3-A
----
CRTSTATUS
Sec 3-A
----
OUTPUT
Sec 3-A
----
TPERROR
Sec 3-B
----
TPFUNC
Sec 3-B
----
TPSTATUS
Sec 3-B
----
TPPROMPT
Sec 3-B
Standard Routines - Declaration Section
-----------------------------------------------------------------------
----
Section 1: Program and Environment Declaration
-----------------------------------------------------------------------
PROGRAM ROUT_EX
%NOLOCKGROUP
---- Don’t lock any motion groups
%COMMENT = ’MISC_ROUTINES’
-----------------------------------------------------------------------
----
Section 2: Constant and Variable Declarations
-----------------------------------------------------------------------
-----------------------------------------------------------------------
----
Section 3: Routine Declarations
-----------------------------------------------------------------------
-----------------------------------------------------------------------
----
Section 3-A: CRT_CLS Declaration
----
Clear the predefined windows:
----
CRTPROMPT, CRTSTATUS, CRTFUNC, CRTERROR, OUTPUT
----
Force Display of the CRT/KB USER SCREEN.
-----------------------------------------------------------------------
ROUTINE CRT_CLS
BEGIN
---- CRT_CLS
--See Chapter 7.9.2 for more information on the PREDEFINED window names
WRITE CRTERROR
(CHR(128),CHR(137))
-- Clear Window, Home Cursor
WRITE CRTSTATUS
(CHR(128),CHR(137))
-- Clear Window, Home Cursor
WRITE CRTPROMPT
(CHR(128),CHR(137))
-- Clear Window, Home Cursor
B-32
MARRC75KR07091E Rev D
B. KAREL EXAMPLE PROGRAMS
WRITE CRTFUNC
(CHR(128),CHR(137))
-- Clear Window, Home Cursor
WRITE OUTPUT
(CHR(128),CHR(137))
-- Clear Window, Home Cursor
FORCE_SPMENU(CRT_PANEL,SPI_TPUSER,1) -- Force the CRT USER Menu
-- to be visible last. This will
-- avoid the screen from flashing
-- since the screen will be clean
-- when you see it.
END CRT_CLS
Standard Routines - Clears Screen and Displays Menu
------------------------------------------------------------------------
----
Section 3-B:
TP_CLS Declaration
Clear the predefined windows:
----
TPERROR, TPSTATUS, TPPROMPT, TPFUNC TPDISPLAY
----
Force Display of the TP USER Menu SCREEN.
------------------------------------------------------------------------
ROUTINE TP_CLS
BEGIN
WRITE (CHR(128),CHR(137)) -- By default this will clear TPDISPLAY
WRITE TPERROR (CR,’
’,CR)
WRITE TPSTATUS(CR,’
’,CR)
WRITE TPPROMPT(CR,’
’,CR)
WRITE TPFUNC
(CR,’
’,CR)
FORCE_SPMENU(TP_PANEL,SPI_TPUSER,1) -- Force the USER menu screen
-- to be visible last.
-- This will avoid the screen from
-- flashing since the screen will
-- be clean when you see it.
END TP_CLS
------------------------------------------------------------------------
----
Section 4: Main Program
------------------------------------------------------------------------
BEGIN -- ROUT_EX
END ROUT_EX
B.5
USING REGISTER BUILT-INS
This program demonstrates the use of the REGISTER builtins. REG_EX.KL retrieves the current
position and stores it in PR[1]. Then it executes the program PROG_VAL.TP. PROG_VAL will
modify the value within the Position Register PR[1].
After PROG_VAL is completed, REG_EX.KL retrieves the PR[1] position. The position is then
manipulated and restored in PR[2], and an INTEGER number is stored in R[1]. A different teach
B-33
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
pendant program, PROG_1.TP, is executed which loops through some positions and stores a value to
R[2]. The number of loops depends on the value of the R[1] (which was initially set by the KAREL
program.)
After PROG_1.TP has completed, the KAREL program gets the value from R[2] and verifies it
was the expected value.
The PROG_VAL.TP teach pendant program should look similar to the following.
PROG_VAL
JOINT 10%
1:
!POSITION REG VALUE ;
2:J P[1:ABOVE JOINT] 100% FINE ;
3: PR[1,2]=600 ;
4:L PR[1] 100.0 Inch/mm FINE ;
5:J P[1:ABOVE JOINT]100% FINE ;
The PROG_VAL.TP teach pendant program does the following:
• Moves to position 1 in joint mode.
• Changes the ’y’ location of the position in Position Register 1, PR[1] (which was set by the
KAREL program).
• Moves to the new PR[1] position.
• Finally moves back to position 1.
The PROG_1.TP teach pendant program should look similar to the following.
PROG_1
JOINT 10%
1: LBL[1:START] ;
2: IF R[1]=0, JMP LBL[2] ;
3:J P[1] 100% FINE ;
4:J P[2] 100% FINE ;
5: R[1]=R[1] 1 ;
6: JMP LBL[1] ;
7: LBL[2:DONE] ;
8: R[2]=1 ;
The PROG_1.TP teach pendant program does the following:
• Checks the value of the R[1].
• If the value of R[1] is not 0, then moves to J P[1] and J P[2] and decrements the value of R[1].
PROG_1.TP continues in this loop until the Register R[1] is zero.
B-34
MARRC75KR07091E Rev D
B. KAREL EXAMPLE PROGRAMS
• After the looping is complete, PROG_1.TP stores value 1 in R[2], which will be checked
by the KAREL program.
Using Register Built-ins Program - Overview
--------------------------------------------------------------------------
----
REG_EX.Kl
--------------------------------------------------------------------------
----
Elements of KAREL Language Covered:
In Section:
----
Actions:
----
Clauses:
----
Conditions:
----
Data types:
----
BOOLEAN
Sec 2
----
JOINTPOS
Sec 2
----
REAL
Sec 2
----
XYZWPR
Sec 2
----
Directives:
----
ALPHABETIZE
Sec 1
----
COMMENT
Sec 1
----
NOLOCKGROUP
Sec 1
----
Built-in Functions & Procedures:
----
CALL_PROGLIN
Sec
4-A,
4-C
----
CHR
Sec
4
----
CURPOS
Sec
4-A
----
FORCE_SPMENU
Sec
4
----
GET_POS_REG
Sec
4-B
----
GET_JPOS_REG
Sec
4-B
----
GET_REG
Sec
4-C
----
POS_REG_TYP
Sec
4-B
----
SET_JPOS_REG
Sec
4-B
----
SET_INT_REG
Sec
4-B
----
SET_POS_REG
Sec
4-A
----
Statements:
----
WRITE
Sec
4, 4-A,B,C
----
IF..THEN..ELSE..ENDIF
Sec
4-A,B,C
----
SELECT...CASE...ENDSELECT
Sec
4-B
----
Reserve Words:
----
BEGIN
Sec
4
----
CONST
Sec
2
----
CR
Sec
4-A,B,C
----
END
Sec
4-C
----
PROGRAM
Sec
4
----
VAR
Sec
2
B-35
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
Using Register Built-ins Program - Declaration Section
-------------------------------------------------------------------------
----
Section 1: Program and Environment Declaration
-------------------------------------------------------------------------
PROGRAM reg_ex
%nolockgroup
%comment = ’Reg-Ops’
%alphabetize
-------------------------------------------------------------------------
----
Section 2: Variable Declaration
-------------------------------------------------------------------------
CONST
cc_success
= 0
-- Success status
cc_xyzwpr
= 2
-- Position Register has an XYZWPR
cc_jntpos
= 9
-- Position Register has a JOINTPOS
VAR
xyz
:XYZWPR
jpos
:JOINTPOS
r_val
:REAL
prg_indx,
i_val,
pos_type,
num_axes,
status
:INTEGER
r_flg
:BOOLEAN
------------------------------------------------------------------------
----
Section 3: Routine Declaration
------------------------------------------------------------------------
------------------------------------------------------------------------
----
Section 4: Main program
------------------------------------------------------------------------
BEGIN -- REG_EX
write(chr(137),chr(128));
-- Clear the TP USER menu screen
FORCE_SPMENU(TP_PANEL,SPI_TPUSER,1) -- Force the TP USER menu to be
-- visible
Using Register Built-ins - Storing and Manipulating Positions
----------------------------------------------------------------------------
---- Section 4-A: Store current position in PR[1] and execute PROG_VAL.TP
----------------------------------------------------------------------------
WRITE(’Getting Current Position’,cr)
xyz = CURPOS(0,0)
-- Get the current position
WRITE(’Storing Current position to PR[1]’,cr)
SET_POS_REG(1,xyz, status)
-- Store the position in PR[1]
B-36
MARRC75KR07091E Rev D
B. KAREL EXAMPLE PROGRAMS
IF (status = cc_success) THEN
-- verify SET_POS_REG is successful
WRITE(’Executing "PROG_VAL.TP"’,cr)
CALL_PROGLIN(’PROG_VAL’,2,prg_indx, FALSE)
--Execute ’PROG_VAL.TP’ starting
-- at line 2. Do not pause on
-- entry of PROG_VAL.
----------------------------------------------------------------------------
---- Section 4-B: Get new position from PR[1]. Manipulate and store in PR[2]
----------------------------------------------------------------------------
WRITE(’Getting Position back from PR[1]’,cr)
-- Decide what type of position is stored in Position Register 1, PR[1]
POS_REG_TYPE(1, 1, pos_type, num_axes, status)
IF (status = cc_success) THEN
-- Get the position back from PR[1], using the correct builtin.
-- This position was modified in PROG_VAL.TP
SELECT pos_type OF
CASE (cc_xyzwpr):
xyz= GET_POS_REG(1, status)
CASE (cc_jntpos):
jpos = GET_JPOS_REG(1, status)
xyz = jpos
ELSE:
write (’The position register set to invalid type’, pos_type,CR)
status = -1
-- set status so do not continue.
ENDSELECT
IF (status = cc_success) THEN
-- Verify GET_POS_REG/GET_JPOS_REG is
-- successful
xyz.x = xyz.x+10
-- Manipulate the position.
xyz.z = xyz.z-10
jpos
= xyz
-- Convert to a JOINTPOS
WRITE(’Setting New Position to PR[2]’,cr)
SET_JPOS_REG(2,jpos,status)
-- Set the JOINTPOS into PR[2]
IF (status = cc_success) THEN
-- Verify SET_JPOS_REG is successful
WRITE(’Setting Integer Value to R[1]’,cr)
SET_INT_REG(1, 10, status)
-- Set the value 10 into R[1]
Using Register Built-ins - Executing Program and Checking Register
----------------------------------------------------------------------
----
Section 4-C: Execute PROG_1.TP and check the R[2]
----------------------------------------------------------------------
IF (status=cc_success) THEN --Verify SET_INT_REG is successful
WRITE(’Executing "PROG_1.TP"’,cr)
CALL_PROGLIN(’PROG_1’,1, prg_indx, FALSE)
--Execute PROG_1.TPstarting on first line.
--Do not pause on entry of PROG_1.
B-37
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
WRITE(’Getting Value from R[2]’,cr)
GET_REG(2,r_flg, i_val, r_val, status) --Get R[2] value
IF (status = cc_success) THEN
--Verify GET_REG success
IF (r_flg) THEN
--REAL value in register
WRITE(’Got REAL value from R[2]’,cr)
IF (r_val <> 1.0) THEN
--Verify value
set
WRITE (’PROG_1 failed to set R[2]’,cr)-- by PROG_1_TP
WRITE (’PROG_1 failed to set R[2]’,cr)
ENDIF
ELSE
--Register contained an INTEGER
WRITE(’Got INTEGER value from R[2]’,cr)
IF (i_val <> 1) THEN
--Verify value set by
WRITE (’PROG_1 failed to set R[2]’,cr) --PROG_1.TP
ENDIF
ENDIF
ELSE
--GET_REG was NOT successful
WRITE(’GET_REG Failed’,cr,’ Status = ’,status,cr)
ENDIF
ELSE
--SET_INT_REG was NOT successful
WRITE(’SET_INT_REG Failed, Status = ’,status,cr)
ENDIF
ELSE
--SET_JPOS_REG was NOT successful
WRITE(’SET_JPOS_REG Failed, Status = ’,status,cr)
ENDIF
ELSE
-- GET_POS_REG was NOT Successful
WRITE(’GET_POS_REG Failed, Status = ’,status,cr)
ENDIF
ELSE
WRITE (’POS_REG_TYPE Failed, Status =’, status, cr)
ENDIF
ELSE
-- SET_POS_REG was NOT successful
WRITE(’SET_POS_REG Failed, Status = ’,status,cr)
ENDIF
IF (status = cc_success) THEN; WRITE (’Program Completed
Successfully’,cr)
ELSE ;
WRITE (’Program Aborted due to error’,cr)
ENDIF
END reg_ex
B.6
PATH VARIABLES AND CONDITION HANDLERS PROGRAM
This program checks to determine if PATH variables are taught or not. If the paths are taught, the
robot moves to a joint position and then loops along a path 5 times.
This example also sets up two global condition handlers.
B-38
MARRC75KR07091E Rev D
B. KAREL EXAMPLE PROGRAMS
• The first condition handler detects if the user has pushed a teach pendant key, and if so aborts the
program.
• The second condition handler sets a variable when the program is aborted.
Path Variables and Condition Handlers Program - Overview
----------------------------------------------------------------------
----
PTH_MOVE.Kl
----------------------------------------------------------------------
----------------------------------------------------------------------
----
Section 0: Detail about PTH_MOVE.kl
----------------------------------------------------------------------
----
Elements of KAREL Language Covered:
In
Section:
----
Actions:
----
ABORT
Sec
4-A
----
Clauses:
----
WHEN
Sec
4-A
----
FROM
Sec
3-A
----
Conditions:
----
ABORT
Sec
4-A
----
Data types:
----
ARRAY OF REAL
Sec
2
----
BOOLEAN
Sec
2
----
INTEGER
Sec
2
----
JOINTPOS6
Sec
2
----
PATH
Sec
2
----
XYZWPR
Sec
2
----
Directives:
----
ALPHABETIZE
Sec
1
----
COMMENT
Sec
1
----
ENVIRONMENT
Sec
1
----
Built-in Functions & Procedures:
----
PATH_LEN
Sec
4-C
----
CHR
Sec
3-B;
4-B,D
----
CNV_REL_JPOS
Sec
4-D
----
SET_CURSOR
Sec
4-B
----
SET_POS_REG
4-D
Path Variables and Condition Handlers Program - Overview Continued
----
Statements:
----
Abort
Sec 4-C
----
CONDITION...ENDCONDITION
Sec 4-A
----
FOR...ENDFOR
Sec 4-D
B-39
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
----
ROUTINE
Sec 3-A, B, C
----
WAIT FOR
Sec 3-B
----
WRITE
Sec 3-B; 4-B,C,D
----
Reserved Words:
----
BEGIN
Sec 3-A,B, 4
----
CONST
Sec 2
----
END
Sec 3-A,B: 4-D
----
VAR
Sec 2
----
PROGRAM
Sec 1
----
Predefined File Names:
----
TPFUNC
Sec 3-B; 4-D
----
TPDISPLAY
Sec 4-B
Path Variables and Condition Handlers Program - Declaration Section
------------------------------------------------------------------------------
----
Section 1: Program and Environment Declaration
------------------------------------------------------------------------------
PROGRAM PTH_MOVE
-- Define the program name
%ALPHABETIZE
-- Create the variables in alphabetical order
%COMMENT
= ’PATH MOVES’
%ENVIRONMENT PATHOP
-- Necessary for PATH_LEN
%ENVIRONMENT UIF
-- Necessary for SET_CURSOR
------------------------------------------------------------------------------
----
Section 2: Constant and Variable Declarations
------------------------------------------------------------------------------
CONST
CH_ABORT
= 1
-- Number associated with the
-- abort Condition handler
CH_F1
= 2
-- Number associated with the
-- F1 key Condition handler
VAR
status
:INTEGER
-- Status from built-in calls
node_ind
:INTEGER
-- Index used when moving along path
loop_pth
:INTEGER
-- Used in a FOR loop counter
prg_abrt
:BOOLEAN
-- Set when program is aborted
pth1
:PATH
strt_jnt
:JOINTPOS6
-- Starting position of a move
via_pos
:XYZWPR
-- Via point for a circular move
des_pos
:XYZWPR
-- Destination point
tmp_xyz
:XYZWPR
-- Temporary XYZWPR
real_ary
:ARRAY[6] OF REAL
-- This is used for creating
-- a joint position with 6 axes
index
:INTEGER
-- FOR loop counter
B-40
MARRC75KR07091E Rev D
B. KAREL EXAMPLE PROGRAMS
Path Variables and Condition Handlers Program - Declare Routines
------------------------------------------------------------------------------
----
Section 3: Routine Declaration
------------------------------------------------------------------------------
----
Section 3-A: TP_CLS Declaration
----
ROUTINE TP_CLS FROM ROUT_EX
-- ROUT_EX must also be loaded.
------------------------------------------------------------------------------
------------------------------------------------------------------------------
----
Section 3-B: YES_NO Declaration
---
Display choices on the function line of the TP.
----
Asks for user response.
---
F1 key is monitored by the Global condition handler
----
[CH_F1] and the F2 is monitored here.
----
If F1 is pressed the program will abort.
---
But, if the F2 is pressed the program will continue.
------------------------------------------------------------------------------
ROUTINE YES_NO
BEGIN
WRITE TPFUNC (CHR(137))
-- Home Cursor in Function window
WRITE TPFUNC (’ ABORT CONT’)
-- Display Function key options
WAIT FOR TPIN[131]
-- Wait for user to respond to
-- continue. If the user presses
-- F1 (abort) condition handler
-- CH_ABORT will abort program.
WRITE TPFUNC (CHR(137))
-- Home Cursor in Function window
WRITE TPFUNC (’ ABORT’,chr(129))
-- Redisplay just Abort option and
-- clear rest of Function window
END YES_NO
----------------------------------------------------------------------------
----
Section 3-C: Routines move_to_pr and move_circ are TP
----
routines for doing moves
----------------------------------------------------------------------------
ROUTINE move_to_pr FROM move_to_pr
-- move_to_pr must also be loaded.
--
1:J PR[1] 100% FINE
;
ROUTINE move_circ FROM move_circ
-- move_circ must also be loaded.
--
1:C PR[1]
--
: PR[2] 100mm/sec FINE
;
Path Variables and Condition Handlers Program - Declare Condition Handlers
-----------------------------------------------------------------------------
----
Section 4: Main Program
-----------------------------------------------------------------------------
BEGIN
-- PTH_MOVE
-----------------------------------------------------------------------------
B-41
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
----
Section 4-A: Global Condition Handler Declaration
-----------------------------------------------------------------------------
CONDITION[CH_ABORT]:
WHEN ABORT DO
-- When the program is aborting set prg_abrt flag.
-- This will be triggered if this program aborts itself
-- or if an external mechanism aborts this program.
prg_abrt = TRUE
-- You may then have another task which detects
-- prg_abrt being set, and does shutdown operations
-- (ie: set DOUT/GOUT’s, send signals to a PLC)
ENDCONDITION
CONDITION[CH_F1]:
WHEN TPIN[129] DO
-- Monitor TP ’F1’ Key. If ’F1’ key is pressed,
ABORT
-- abort the program.
ENDCONDITION
prg_abrt = false
-- Initialize variable which is set only if
-- the program is aborted and CH_ABORT is
-- enabled.
ENABLE CONDITION[CH_ABORT]
-- Start scanning abort condition as defined.
ENABLE CONDITION[CH_F1]
-- Start scanning F1 key condition as defined.
-----------------------------------------------------------------------------
----
Section 4-B: Display banner message and wait for users response
-----------------------------------------------------------------------------
TP_CLS
-- Routine Call; Clears the TP USER
-- menu, and forces the TP USER menu
-- to be visible.
SET_CURSOR(TPDISPLAY,2,13, status)
-- Set cursor position in TP USER menu
IF (status <> 0 ) THEN
-- Verify that SET_CURSOR was successful
WRITE (’SET_CURSOR built-in failed with status = ’,status,cr)
YES_NO
-- Ask whether to quit, due to error.
ENDIF
--- Write heading in REVERSE video, then turn reverse video off
WRITE (chr(139),’ PLEASE READ ’,chr(143),CR)
WRITE (cr,’ *** F1 Key is labelled as ABORT key *** ’)
WRITE (cr,’ Any time the F1 key is pressed the program’)
WRITE (cr,’ will abort. However, the F2 key is active ’)
WRITE (cr,’ only when the function key is labeled.’,cr,cr)
YES_NO
-- Wait for user response
Path Variables and Condition Handlers Program - Teach and Move Along Path
----------------------------------------------------------------------------
----
Section 4-C: Verify PATH variable, pth1, has been taught
----------------------------------------------------------------------------
-- Check the number of nodes in the path
IF PATH_LEN(pth1) = 0 THEN
-- Path is empty (has no nodes)
WRITE (’You need to teach the path.’,cr)
-- Display instructions to user
B-42
MARRC75KR07091E Rev D
B. KAREL EXAMPLE PROGRAMS
WRITE (’before executing this program.’,cr)
WRITE (’Teach the PATH variable pth1’, CR, ’ and restart the program’,cr)
ABORT
-- Simply ABORT the task
-- do not continue since there
ENDIF
-- are no nodes to move to
----------------------------------------------------------------------------
----
Section 4-D: Creating a joint position and moving along paths
----------------------------------------------------------------------------
FOR indx = 1 to 6 DO
-- Set all joint angles to zero
real_ary[indx] = 0.0
ENDFOR
real_ary[5] = 90.0
-- Make sure that the position
-- is not at a singularity point.
CNV_REL_JPOS(real_ary, strt_jnt, status)
-- Convert real_ary values into
-- a joint position, strt_jnt
IF (status <> 0 ) THEN
-- Converting joint position
-- was NOT successful
WRITE (’CNV_REL_JPOS built-in failed with status = ’,status,cr)
YES_NO
-- Ask user if want to continue.
ELSE
-- Converting joint position was
-- successful.
-- The start position, strt_jnt, has been created and is located at
-- axes 1-4 = 0.0, axes 5 = 90.0, axes 6 = 0.0.
via_pos = strt_jnt
-- Copy the strt_jnt to via_pos
via_pos.x = via_pos.x +200
-- Add offset to the x location
via_pos.y = via_pos.y +200
-- Add offset to the y location
-- The via position, via_pos, has been created to be the same position
-- as strt_jnt except it has been offset in the x and y locations by
-- 200 mm.
des_pos = strt_jnt
-- Copy the strt_jnt to des_pos
des_pos.x = des_pos.x + 400
-- Add offset to the x location
-- The destination position, des_pos, has been created to be the same
-- position as strt_jnt except it has been offset in the x location by
-- 400 mm.
Path Variables and Condition Handlers Program - Move Along Path
tmp_xyz = strt_jnt
-- Convert start position to XYZWPR
SET_POS_REG(1, tmp_xyz, status) -- Put start position in PR[1]
move_to_pr
-- Call TP program to move to PR[1]
tmp_xyz = des_pos
-- Convert destination position to XYZWPR
SET_POS_REG(1, tmp_xyz, status) -- Put destination position in PR[1]
tmp_xyz = via_pos
-- Convert via position to XYZWPR
SET_POS_REG(2, tmp_xyz, status) -- Put via position in PR[2]
WRITE (cr,’Circular move to Destination Position’,cr)
move_circ
-- Call TP program to
B-43
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
-- Move robot to destination
-- position using circular motion
-- via the via_pos
ENDIF
--- Execute the same path for 5 times.
FOR loop_pth = 1 TO 5 DO
WRITE (’Moving along pth1 ’,loop_pth::2, ’ times’,cr)
-- Display the loop iteration
-- NOTICE: that "loop_pth::2" will cause 2 blanks to be
-- displayed after "pth1 ’" and before loop_pth.
FOR node_ind = 1 TO PATH_LEN(pth1) DO
tmp_xyz = pth1[node_ind]
SET_POS_REG(1, tmp_xyz, status)
move_to_pr
ENDFOR
ENDFOR
WRITE TPFUNC
(CHR(128),CHR(137))
-- Home Cursor and Clear to
-- End-of-line. This will remove
-- the ABORT displayed above F1.
WRITE (’pth_move Successfully Completed’,cr)
END PTH_MOVE
B.7
LISTING FILES AND PROGRAMS AND MANIPULATING
STRINGS
This program displays the list of files on the FLPY: device, and lists the programs loaded on the
controller. It also shows basic STRING manipulating capabilities, using semi-colons(;) as a statement
separator, and nesting IF statements.
Listing Files and Programs and Manipulating Strings - Overview
------------------------------------------------------------------------------
----
LIST_EX.Kl
------------------------------------------------------------------------------
----
Section 0: Detail about LIST_EX.kl
------------------------------------------------------------------------------
---- Elements of KAREL Language Covered:
In Section:
----
Actions:
----
Clauses:
----
FROM
Sec 3-B
----
----
Conditions:
----
Data types:
----
ARRAY OF STRING
Sec 2
----
BOOLEAN
Sec 2, 3
B-44
MARRC75KR07091E Rev D
B. KAREL EXAMPLE PROGRAMS
----
INTEGER
Sec 2, 3
----
STRING
Sec 2
----
Directives:
----
%COMMENT
Sec 1
----
%NOLOCKGROUP
Sec 1
----
Built-in Functions & Procedures:
----
ABS
Sec 4-A
----
ARRAY_LEN
Sec 4-C&D
----
CNV_INT_STR
Sec 4-A
----
FILE_LIST
Sec 4-C
----
LOAD
Sec 4-B
----
LOAD_STATUS
Sec 4-B
----
PROG_LIST
Sec 4-D
----
ROUND
Sec 4-A
----
SUB_STR
Sec 4-A
Listing Files and Programs and Manipulating Strings - Overview Continued
----
Statements:
----
FOR
ENDFOR
Sec 3-B
----
IF...THEN...ENDIF
Sec 4-A,B,C,D
----
ROUTINE
Sec 3-A,B,C
----
REPEAT...UNTIL
Sec 4-C,D
----
RETURN
Sec 3-A
----
WRITE
Sec 3-B; 4-A,B
----
Reserve Words:
----
BEGIN
Sec 3-A,B; 4
----
CONST
Sec 2
----
CR
Sec 3-B; 4-A,B
----
END
Sec 3-A,B, 4-B
----
PROGRAM
Sec 1
----
VAR
Sec 2
----
----
Operators:
----
MOD
Sec 3-A
----
/
Sec 3-A
----
Sec 3-A
----
Devices Used:
----
FLPY:
Sec 4-C
----
Basic Concepts:
----
Semi-colon(;) as statement separator
----
Nested IF..THEN..ELSE..IF..THEN..ELSE..ENDIF..ENDIF
structure
----
Concatenation of STRINGS using ’+’
----
B-45
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
Listing Files and Programs and Manipulating Strings - Declarations Section
------------------------------------------------------------------------------
----
Section 1: Program and Environment Declaration
------------------------------------------------------------------------------
PROGRAM LIST_EX
%NOLOCKGROUP
---- Don’t lock any motion groups
%COMMENT = ’FILE_LIST’
------------------------------------------------------------------------------
----
Section 2: Constant and Variable Declarations
------------------------------------------------------------------------------
CONST
INCREMENT
= 13849
MODULUS
= 65536
MULTIPLIER
= 25173
VAR
pr_cases
:STRING[6]
-- psuedo random number converted to string
prg_nm
:STRING[50] -- Concatenated program name
loaded
:BOOLEAN
-- Used to see if program is loaded
initi
:BOOLEAN
-- Used to see if variables initialized
indx1
:INTEGER
-- FOR loop index
cases,
-- Random number returned
max_number,
-- Maximum random number
seed
:INTEGER
-- Seed for generating a random number
file_spec
:STRING[20] -- File specification for FILE_LIST
n_files
:INTEGER
-- Number of files returned from FILE_LIST
n_skip
:INTEGER
-- Number to skip for FILE_LIST & PROG_LIST
format
:INTEGER
-- Format of returned names
-- For FILE_LIST & PROG_LIST
ary_nam
:ARRAY[9] OF STRING[20] -- Returned names
-- from FILE_LIST & PROG_LIST
prog_name
:STRING[10] -- Program names to list from PROG_LIST
prog_type
:INTEGER
-- Program types to list form PROG_LIST
n_progs
:INTEGER
-- Number of programs returned from PROG_LIST
status
:INTEGER
-- Status of built-in procedure call
Listing Files and Programs and Manipulating Strings - Declare Routines
-----------------------------------------------------------------------------
----
Section 3: Routine Declaration
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
----
Section 3-A: RANDOM Declaration
---
Creates a pseudo-random number and returns the number.
-----------------------------------------------------------------------------
ROUTINE random(seed : INTEGER) : REAL
B-46
MARRC75KR07091E Rev D
B. KAREL EXAMPLE PROGRAMS
BEGIN
seed = (seed * MULTIPLIER + INCREMENT) MOD MODULUS
RETURN(seed/65535.0)
END random
-----------------------------------------------------------------------------
----
Section 3-B: DISPL_LIST Declaration
----
Display maxnum elements of ary_nam.
-----------------------------------------------------------------------------
ROUTINE displ_list(maxnum :INTEGER)
BEGIN
FOR indx1 = 1 TO maxnum DO ; WRITE (ary_nam[indx1],cr);
ENDFOR
-- Notice the use of the semi-colon, which allows multiple statements
-- on a line.
END displ_list
-----------------------------------------------------------------------------
----
Section 3-C: TP_CLS Declaration
----
This routine is from ROUT_EX.KL and will
----
clear the TP USER menu screen and force it to be visible.
-----------------------------------------------------------------------------
ROUTINE tp_cls FROM rout_ex
Listing Files and Programs and Manipulating Strings - Main Program
------------------------------------------------------------------------------
----
Section 4: Main Program
------------------------------------------------------------------------------
BEGIN -- LIST_EX
tp_cls
-- Use routine from the rout_ex.kl file
------------------------------------------------------------------------------
---- Section 4-A: Generate a pseudo random number, convert INTEGER to STRING
------------------------------------------------------------------------------
max_number = 255 ;
-- So the random number is 0..255
seed = 259 ;
WRITE (’Manupulating String’,cr)
cases = ROUND(ABS((random(seed)*max_number)))-- Call random then take the
-- absolute value of the number
-- returned and round off the
-- number.
CNV_INT_STR(cases, 1, 0, pr_cases)
-- Convert cases to its
-- ascii representation
pr_cases = SUB_STR(pr_cases, 2,3)
-- get at most 3 characters,
-- starting at the second
-- character, since first
-- character is a blank.
B-47
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
Listing Files and Programs and Manipulating Strings - Create and Load Program
------------------------------------------------------------------------------
----
Section 4-B: Build a program name from the number and try to load it
------------------------------------------------------------------------------
--- Build a random program name to show the manipulation of
--- STRINGS and INTEGERs.
prg_nm = ’MYPROG’ + pr_cases + ’.PC’
-- Concatenate the STRINGs together
-- which create a program name
--- Verify that the program is not already loaded
WRITE (’Checking load status of ’,prg_nm,cr)
LOAD_STATUS(prg_nm, loaded, initi)
IF (NOT loaded) THEN
-- The program is not loaded
WRITE (’Loading ’,prg_nm,cr)
LOAD(prg_nm, 0 , status)
-- Load in the program
IF (status =
0 ) THEN
-- Verify load is successful
WRITE (’Loading ’,’MYPROG’ + pr_cases + ’.VR’,cr)
LOAD(’MYPROG’ + pr_cases + ’.VR’, 0, status) -- Load the .vr file
IF (status <> 0 ) THEN
-- Loading variables failed
WRITE (’Loading of ’, ’MYPROG’ + pr_cases + ’.VR’, ’ failed’,cr)
WRITE (’Status = ’,status);
ENDIF
ELSE
-- Load of program failed
IF (status = 10003) THEN
-- File does not exist
WRITE (prg_nm, ’ file does not exist’,cr)
ELSE
WRITE (’Loading of ’,prg_nm, ’ failed’,cr,’Status = ’,status);
ENDIF
ENDIF
ELSE
-- The program is already loaded
IF (NOT initi) THEN
-- Variables not initialized
WRITE (’Loading ’,’MYPROG’ + pr_cases + ’.VR’,cr)
LOAD(’MYPROG’ + pr_cases + ’.VR’, 0, status) -- Load in variables
IF (status <> 0 ) THEN
-- Load of variables failed
WRITE (’Loading of ’, ’MYPROG’ + pr_cases + ’.VR’, ’ failed’,cr)
WRITE (’Status = ’,status);
ENDIF
ENDIF
ENDIF
Listing Files and Programs and Manipulating Strings - List Programs
------------------------------------------------------------------------------
---- Section 4-C: Check the file listing of the drive FLPY: and display them
------------------------------------------------------------------------------
B-48
MARRC75KR07091E Rev D
B. KAREL EXAMPLE PROGRAMS
--- Display a directory listing of files on the Flpy:
file_spec = ’FLPY:*.*’
-- All files in FLPY: drive
n_skip = 0
-- First time do not skip any files
format = 3
-- Return list in filename.filetype format
WRITE (’Doing File list’,cr)
REPEAT
-- UNTIL all files have been listed
FILE_LIST(file_spec, n_skip, format, ary_nam, n_files, status)
IF (status <>0 ) THEN
-- Error occurred
WRITE (’FILE_LIST builtin failed with Status = ’,status,cr)
ELSE
displ_list(n_files)
-- Write the names to the TP USER menu
n_skip = n_skip + n_files
-- Skip the files we already got.
ENDIF
UNTIL (ARRAY_LEN(ary_nam) <> n_files) -- When n_files does not equal
-- declared size of ary_name then
-- all files have been listed.
------------------------------------------------------------------------------
----
Section 4-D: Show the programs loaded in controller
------------------------------------------------------------------------------
--- Display the list of programs loaded on the controller
prog_name = ’*’
-- All program names should be listed
prog_type = 6
-- Only PC type files should be listed
n_skip = 0
-- First time do not skip any file
format = 2
-- Return list in filename.filetype format
WRITE (’Doing Program list’,cr)
REPEAT
-- UNTIL all programs have been listed
PROG_LIST(prog_name, prog_type, n_skip, format, ary_nam, n_progs, status)
-- The program names are stored in ary_nam
-- n_progs is the number of program names stored in ary_nam
IF (status <>0 ) THEN
WRITE (’PROG_LIST builtin failed with Status = ’,status,cr)
ELSE
displ_list(n_progs)
-- Display the current list
n_skip = n_skip + n_progs
-- Skip the programs already listed
ENDIF
UNTIL (ARRAY_LEN(ary_nam) <> n_progs)
-- When n_files does not equal the
-- declared size of ary_name then all
-- programs have been listed.
END LIST_EX
B.8
GENERATING AND MOVING ALONG A HEXAGON PATH
This program generates a hexagonal path and moves along each side of the hexagon.
B-49
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
Generate and Move Along Hexagon Path - Overview
------------------------------------------------------------------------------
----
GEN_HEX.KL
------------------------------------------------------------------------------
----
Section 0: Detail about GEN_HEX.KL
------------------------------------------------------------------------------
----
Elements of KAREL Language Covered:
In Section:
----
Action:
----
Clauses:
----
Conditions:
----
Data types:
----
ARRAY OF REAL
Sec 3-C
----
ARRAY OF XYZWPR
Sec 2
----
INTEGER
Sec 2; 3-B,C
----
JOINTPOS6
Sec 2
----
REAL
Sec 3-B
----
Directives:
%COMMENT
Sec 1
----
Built-in Functions & Procedures:
----
CHECK_EPOS
Sec 4-B
----
CNV_REL_JPOS
Sec 3-C
----
COS
Sec 3-B
----
SIN
Sec 3-B
----
SET_POS_REG
4-B
----
SET_JPOS_REG
Sec 3-C
----
Statements:
----
CONNECT TIMER
Sec 4-A
----
FOR ... ENDFOR
Sec 3-B,C; 4-B
----
ROUTINE
Sec 3-A,B,C
----
WRITE
Sec 4-A,B
----
Reserve Word:
----
BEGIN
Sec 3-B,C; 4
----
CONST
Sec 2
----
CR
Sec 4-A
----
END
Sec 3-B,C; 4-B
----
PROGRAM
Sec 1
----
VAR
Sec 2
Generate and Move Along Hexagon Path - Declaration Section
------------------------------------------------------------------------------
----
Section 1: Program and Environment Declaration
------------------------------------------------------------------------------
PROGRAM gen_hex
%COMMENT = ’HEXAGON’
B-50
MARRC75KR07091E Rev D
B. KAREL EXAMPLE PROGRAMS
------------------------------------------------------------------------------
----
Section 2: Constant and Variable Declaration
------------------------------------------------------------------------------
CONST
L_HEX_SIDE = 300
-- Length of one side of the hexagon
NUM_AXES
= 6
-- Number of robot axes
VAR
p_cntr
: JOINTPOS6
-- Center of the hexagon
p_xyzwpr
: ARRAY[NUM_AXES] OF XYZWPR
-- Six vertices of the hexagon
tmp_xyz
:XYZWPR
clock,
t_start,
t_end,
t_total
: INTEGER
status,
p_indx
: INTEGER
Generate and Move Along Hexagon Path - Declare Routines
------------------------------------------------------------------------------
----
Section 3: Routine Declaration
------------------------------------------------------------------------------
----------------------------------------------------------------------------
----
Section 3-A: Routines move_to_pr and movl_to_pr are TP
----
routines for doing moves
----------------------------------------------------------------------------
ROUTINE move_to_pr FROM move_to_pr
-- move_to_pr must also be loaded.
--
1:J PR[1] 100% FINE
;
ROUTINE movl_to_pr FROM movl_to_pr
-- movl_to_pr must also be loaded.
--
1:L PR[1] 1000mm/sec FINE
;
------------------------------------------------------------------------------
----
Section 3-B: R_HEX_CENTER Declaration
----
Calculates the hexagon points based on distance
----
between point 1 and 4 of the hexagon.
------------------------------------------------------------------------------
ROUTINE r_calc_hex
VAR
p1_to_pcntr
: REAL -- Distance from the center of the hex to point 1
vertice
: INTEGER
-- the index used specify each vertice of hexagon
BEGIN
p1_to_pcntr = (L_HEX_SIDE / 2) + (L_HEX_SIDE * COS(60))
p_xyzwpr[1] = p_cntr
-- p_cntr was calculated in r_hex_center
p_xyzwpr[1].y = p_xyzwpr[1].y - p1_to_pcntr --set the first vertice of hex
FOR vertice = 2 TO NUM_AXES DO
-- start at 2 since 1 is already set
p_xyzwpr[vertice] = p_xyzwpr[1]
-- Intialize all vertices
B-51
B. KAREL EXAMPLE PROGRAMS
MARRC75KR07091E Rev D
ENDFOR
-- Calculating individual components for each vertice of the hexagon
p_xyzwpr[2].x = p_xyzwpr[1].x + (L_HEX_SIDE * SIN(60))
p_xyzwpr[2].y = p_xyzwpr[1].y + (L_HEX_SIDE * COS(60))
p_xyzwpr[3].x = p_xyzwpr[1].x + (L_HEX_SIDE * SIN(60))
p_xyzwpr[3].y = p_xyzwpr[1].y + (L_HEX_SIDE + (L_HEX_SIDE * COS(60)))
p_xyzwpr[4].y = p_xyzwpr[1].y + (L_HEX_SIDE + (2 * (L_HEX_SIDE * COS(60)))
p_xyzwpr[5].x = p_xyzwpr[1].x - (L_HEX_SIDE * SIN(60))
p_xyzwpr[5].y = p_xyzwpr[3].y
p_xyzwpr[6].x = p_xyzwpr[1].x - (L_HEX_SIDE * SIN(60))
p_xyzwpr[6].y = p_xyzwpr[2].y
END r_calc_hex
Generate and Move Along Hexagon Path - Declare Routines
-----------------------------------------------------------------------------
----
Section 3-C: R_HEX_CENTER Declaration
----
Positions the face plate perpendicular
----
to the xy world coordinate plane.
-----------------------------------------------------------------------------
ROUTINE r_hex_center
VAR
status, indx
: INTEGER
p_cntr_arry
: ARRAY[NUM_AXES] OF REAL
BEGIN
-- Initalize the center position array to zero
FOR indx = 1 TO NUM_AXES DO
p_cntr_arry[indx] = 0
ENDFOR
-- Set JOINT 3 and 5 to -45 and 45 degrees
p_cntr_arry[3] = -45
p_cntr_arry[5] =
45
-- Convert the REAL array to a joint position,
-- p_cntr
CNV_REL_JPOS(p_cntr_arry, p_cntr, status)
SET_JPOS_REG(1, p_cntr, status) -- Put p_cntr in PR[1]
move_to_pr
-- Call TP program to move to PR[1]
END r_hex_center
Generate and Move Along Hexagon Path - Main Program
-----------------------------------------------------------------------------
----
Section 4: Main Program
-----------------------------------------------------------------------------
B-52
|
||
|
|
|