BRAND.02 / KR C4 + KR C5
KUKA's structured robot language, from the KR C1 to today's KR C4 and KR C5.
KRL (KUKA Robot Language) arrived with the PC-based KR C1 controller in the 1990s and is still the language of the KR C4 and KR C5. Each program is two files with the same name: a .SRC file with the logic in DEF … END and a .DAT file with the data, every taught point included, in DEFDAT … ENDDAT. Motion is PTP, LIN and CIRC (and the spline versions SPTP, SLIN and SCIRC), I/O goes through the $OUT[] and $IN[] arrays, and WAIT FOR and WAIT SEC handle timing. On the pendant most motion lines appear as inline forms such as PTP P1 Vel=100 % PDAT1; the examples on this page are the raw KRL behind those forms, laid out as the .SRC file followed by its .DAT file.
A KRL program is a pair of files with the same name. Main.src holds DEF Main() ... END and any local subprograms; Main.dat holds DEFDAT Main ... ENDDAT with declarations, initial values and every taught point. The first DEF in a .SRC file carries the file name. DEFFCT ... ENDFCT declares a function with a return type.
DEF Main() DECL INT i BAS(#INITMOV, 0) ; logic here END
PTP moves all axes together: fastest, free path. LIN keeps the TCP on a straight line at $VEL.CP in m/s. CIRC runs an arc through an auxiliary point. C_PTP and C_DIS after a motion let the robot blend past the point instead of stopping. The spline set (SPTP, SLIN, SCIRC, and SPL inside a SPLINE block) plans several points ahead and blends more smoothly.
PTP pHome $VEL.CP = 0.5 LIN pAbove C_DIS LIN pPick CIRC pAux, pEnd
Every Cartesian point is read in the active base frame ($BASE) for the active tool ($TOOL). The Tool[1] Base[2] in an inline form picks entries from TOOL_DATA[] and BASE_DATA[]. $IPO_MODE = #TCP is for the reverse case: the robot carries the part past a tool fixed in the cell.
$TOOL = TOOL_DATA[1] $BASE = BASE_DATA[2] $IPO_MODE = #BASE
POS is X, Y, Z in mm and A, B, C in degrees (rotations about Z, Y and X), plus Status (S) and Turn (T), the bits that choose one of the axis configurations for the same pose. E6POS adds external axes and is the type of every point you teach. AXIS holds axis angles A1 to A6; FRAME is a pose without S and T, used for frames and offsets.
DECL POS P1 = {X 800, Y 0, Z 300, A 0, B 90, C 0, S 2, T 35}
DECL AXIS jHome = {A1 0, A2 -90, A3 90, A4 0, A5 0, A6 0}Digital I/O are system arrays. $OUT[1] = TRUE switches an output, WAIT FOR $IN[2] waits for an input, PULSE($OUT[3], TRUE, 0.5) gives a 0.5 s pulse. Writing $OUT and waiting on $IN stop the advance run, the controller's look-ahead, so a C_DIS on the motion before is lost. TRIGGER switches an output on the path without that stop.
$OUT[1] = TRUE WAIT FOR $IN[2] PULSE($OUT[3], TRUE, 0.5) TRIGGER WHEN DISTANCE=1 DELAY=-50 DO $OUT[4]=TRUE
IF/ELSE/ENDIF, FOR/TO/STEP/ENDFOR, WHILE/ENDWHILE, REPEAT/UNTIL, LOOP/ENDLOOP with EXIT, and SWITCH/CASE/DEFAULT/ENDSWITCH. Equality is == and assignment is =. Every variable, a FOR counter included, is declared with DECL at the top of the routine.
DECL INT i
FOR i = 1 TO 5
IF $IN[i] == TRUE THEN
$OUT[i] = TRUE
ENDIF
ENDFOR| Instruction | Category | Purpose | Example |
|---|---|---|---|
| PTP | Motion | Point-to-point joint move. Fastest, but the TCP path is not a straight line. The only motion that uses the Status and Turn of the target. | PTP pHome |
| LIN | Motion | Straight-line move at $VEL.CP (m/s). Ignores S and T of the target and keeps the current axis configuration. | LIN pPick |
| CIRC | Motion | Circular move from the current position through an auxiliary point to the end point. CA sets a circular angle. | CIRC pAux, pEnd |
| C_PTP | Motion | Approximate a PTP motion: start the next motion before reaching the point. The distance comes from $APO.CPTP. | PTP pAbove C_PTP |
| C_DIS | Motion | Approximate a LIN or CIRC motion by distance: blending starts $APO.CDIS mm before the point. | LIN pAbove C_DIS |
| PTP_REL | Motion | Joint move relative to the current axis angles. | PTP_REL {A1 30} |
| LIN_REL | Motion | Linear move relative to the current TCP position, in the base frame unless #TOOL is given. | LIN_REL {Z 100} #TOOL |
| SPTP | Motion | Spline PTP motion (KSS 8.3 and later). Spline motions are planned ahead together and blend more smoothly than PTP/LIN/CIRC. | SPTP pHome |
| SLIN | Motion | Spline linear motion, as a single motion or inside a SPLINE ... ENDSPLINE block. | SLIN pPick |
| SCIRC | Motion | Spline circular motion through an auxiliary point. | SCIRC pAux, pEnd |
| SPL | Motion | Spline segment inside a SPLINE ... ENDSPLINE block: the path is a smooth curve through the points. | SPLINE SPL p1 SPL p2 ENDSPLINE |
| BAS | Motion | Call into KUKA's BAS.SRC helper. BAS(#INITMOV, 0) at the top of a program sets default speeds, acceleration and approximation. | BAS(#INITMOV, 0) |
| $OUT[] | I/O | Write a digital output. Writing $OUT stops the advance run, so approximation of the motion before it is lost unless you use CONTINUE or TRIGGER. | $OUT[1] = TRUE |
| $IN[] | I/O | Read a digital input. Reading it in an IF also stops the advance run. | IF $IN[2] == TRUE THEN ... ENDIF |
| PULSE | I/O | Pulse an output: signal, level, duration in seconds. | PULSE($OUT[3], TRUE, 0.5) |
| CONTINUE | I/O | Prevents an advance run stop for the next line only, so a $OUT write does not break approximation. | CONTINUE $OUT[5] = TRUE |
| WAIT FOR | Wait | Wait until a boolean expression is TRUE. | WAIT FOR $IN[1] |
| WAIT SEC | Wait | Wait a number of seconds. | WAIT SEC 0.5 |
| IF | Flow | Conditional with optional ELSE. Equality is ==, assignment is =. | IF count > 10 THEN ... ENDIF |
| FOR | Flow | Counted loop with optional STEP. The counter must be a declared INT. | FOR i = 1 TO 5 ... ENDFOR |
| WHILE | Flow | Loop tested before each pass. | WHILE running ... ENDWHILE |
| REPEAT | Flow | Loop tested after each pass: UNTIL ends it when TRUE. | REPEAT ... UNTIL done |
| LOOP | Flow | Endless loop, left with EXIT. The usual shape of a cell's main cycle. | LOOP ... ENDLOOP |
| SWITCH | Flow | Multi-way branch on an INT, CHAR or ENUM value. | SWITCH mode CASE 1 ... DEFAULT ... ENDSWITCH |
| GOTO | Flow | Jump to a label in the same routine. | GOTO DONE ... DONE: |
| RETURN | Flow | Leave a DEF, or return a value from a DEFFCT. | RETURN result |
| DEF | Structure | Declare a routine (DEF ... END). The first routine in a .SRC file has the file's name; the others are local subprograms. | DEF Main() ... END |
| DEFFCT | Structure | Declare a function with a return type (DEFFCT ... ENDFCT). Each parameter is declared again with DECL inside. | DEFFCT INT Add(a:IN, b:IN) ... ENDFCT |
| :IN / :OUT | Structure | Parameter passing: :IN is call by value, :OUT is call by reference, so the routine can change the caller's variable. | DEF IncCounter(n:OUT) |
| DEFDAT | Structure | Data list (.DAT file): declarations with initial values, including the taught points. PUBLIC makes it visible to other modules. | DEFDAT Main PUBLIC ... ENDDAT |
| GLOBAL | Structure | Makes a routine, or a variable in a PUBLIC data list, visible outside its own module. | GLOBAL DEF Helper() ... END |
| INTERRUPT DECL | Structure | Declare an interrupt: priority, triggering event, handler routine. It does nothing until switched on. | INTERRUPT DECL 21 WHEN $IN[5]==FALSE DO PART_LOST() |
| INTERRUPT ON | Structure | Switch a declared interrupt on (OFF, DISABLE and ENABLE also exist). | INTERRUPT ON 21 |
| BRAKE | Structure | Stop the robot from inside an interrupt routine. | BRAKE |
| TRIGGER | Structure | Switch an action relative to the start (DISTANCE=0) or end (DISTANCE=1) of the next motion, with a DELAY in ms. Does not stop the advance run. | TRIGGER WHEN DISTANCE=1 DELAY=-50 DO $OUT[1]=TRUE |
| DECL | Data | Declare a variable. In a .SRC file all DECLs come before the first executable line. | DECL INT count |
| INT | Data | Integer type. | DECL INT i |
| REAL | Data | Floating-point type. | DECL REAL speed |
| BOOL | Data | Boolean type (TRUE / FALSE). | DECL BOOL ready |
| CHAR | Data | Single character; arrays of CHAR hold text. | DECL CHAR msg[20] |
| ENUM | Data | Enumeration type. Values are written with a # in front. | ENUM MODE_T idle, run, fault |
| STRUC | Data | Composite record type. | STRUC POINT_T REAL X, Y, Z |
| POS | Data | Cartesian pose X, Y, Z (mm), A, B, C (degrees, rotations about Z, Y, X) plus Status and Turn, the bits that fix the axis configuration. | DECL POS P1 = {X 800, Y 0, Z 300, A 0, B 90, C 0, S 2, T 35} |
| E6POS | Data | POS plus external axes E1 to E6. Points taught on the pendant are E6POS. | DECL E6POS XP1 = {X 800, Y 0, Z 300, A 0, B 90, C 0, S 2, T 35, E1 0, E2 0, E3 0, E4 0, E5 0, E6 0} |
| AXIS | Data | Axis angles A1 to A6 in degrees (E6AXIS adds E1 to E6). No configuration ambiguity, so it is the safe type for home. | DECL AXIS jHome = {A1 0, A2 -90, A3 90, A4 0, A5 0, A6 0} |
| FRAME | Data | X, Y, Z, A, B, C with no Status or Turn. Used for $BASE, $TOOL and offsets. | DECL FRAME OFS = {X 0, Y 0, Z 100, A 0, B 0, C 0} |
| $BASE | Frames | Active base frame (work object). Points are stored relative to it. | $BASE = BASE_DATA[1] |
| $TOOL | Frames | Active tool frame (TCP). | $TOOL = TOOL_DATA[1] |
| $IPO_MODE | Frames | #BASE (default) for a tool on the flange; #TCP when the robot carries the part past a fixed (external) tool. | $IPO_MODE = #BASE |
| $VEL | System | Velocity of CP motions: $VEL.CP in m/s. PTP speed is $VEL_AXIS[1..6] in percent. | $VEL.CP = 0.5 |
| $ACC | System | Acceleration of CP motions: $ACC.CP in m/s². | $ACC.CP = 2.3 |
| $APO | System | Approximation settings: CDIS (mm), CPTP (percent), CORI (degrees), CVEL (percent). | $APO.CDIS = 20 |
| HALT | System | Stop program execution; the operator restarts it with the Start key. | HALT |
| ; | System | Comment to the end of the line. | ; this is a KRL comment |
These are the faults that turn up on real cells, most of them during commissioning.
Writing $OUT[1] between two C_DIS motions stops the advance run, so the robot cannot blend: it stops at the first point and the controller reports that approximation is not possible. Put a TRIGGER WHEN DISTANCE on the motion, or CONTINUE on the line before the $OUT.
S and T pick one of the axis configurations that reach the same X, Y, Z, A, B, C. A point calculated offline or copied from another cell with the wrong S and T makes axis 1 or axis 4 swing the long way round on the PTP. LIN and CIRC ignore S and T, so the fault only shows on a PTP.
After a program is selected or reset, or the block pointer is moved, the robot first makes a BCO run to the programmed point before it follows the path. Make that first motion a PTP to a fully defined point such as HOME, never a LIN to a point that depends on where the robot happened to stop.
A point taught with Tool[1] Base[2] and run with Base[0] goes somewhere else entirely, often into the fixture. Check Tool and Base in every inline form, and step a new program through in T1, where the TCP is limited to 250 mm/s.
A DECL after the first executable line, or a FOR counter that was never declared, stops the program from compiling. Keep every DECL at the top of the routine and keep initial values and taught points in the .DAT file.
An INTERRUPT with BRAKE is fine for a process reaction such as a vacuum switch dropping out. Guards, light curtains and E-stops are wired to the safety controller (KUKA SafeOperation or the cell safety PLC) and must never depend on a KRL program running.