简体   繁体   中英

KRL: Length of an array

In KUKA Robot Language (KRL), is it possible to get the length of an array, or alternatively, is there a way to loop through all the values in an array? Currently I store the array length in a separate variable and do it like this:

; In the DAT file:
DECL CONST INT FOO_LENGTH = 3
DECL CONTS INT FOO[3]
FOO[1] = 12
FOO[2] = 34
FOO[3] = 56

; In the SRC file:
INT IDX
FOR IDX = 1 TO FOO_LENGTH
  do_something(FOO[IDX])
ENDFOR

Essentially I'd like to get rid of the FOO_LENGTH variable.

this is probably wayyy too late to answer, but it'll help for future references.

Unfortunately there is no variable used to determine the length of the array. Once you define it, you know your number, so instead of using FOO_LENGTH, just type in 3.

You can't really use a loop to determine the length of the array, as if you hit the last number, the robot stops and doesn't know what to do. However, this is a lengthy progress, where you can do that, and then as soon as the robot stops, monitor the value and record the number, and then skip the loop and start it again at that moment. But this serve no purpose because you can always go back to DAT file and find out your length by looking at DECL FOO[3].

Hope this helps.

For CHAR arrays there is a function

INT STRDECLLEN (CHAR STRVAR[470]:OUT)

that would do the job and determine the length of a char array. Eg you can write

DEF stringtests( )
char myString[80]
int i,n
n = strdecllen(myString[])
for i = 1 to n
  myString[i] = "Z"
endfor
n = strdecllen(myStringDat[])
for i = 1 to n
  myStringDat[i] = "Z"
endfor
end

with corresponding .dat file

DEFDAT  stringtests PUBLIC
CHAR myStringDat[10]
myStringDat[]="ZZZZZZZZZZ"
ENDDAT

The second loop does not see and does not need to know the length of the array.

Unfortunately there seems to be no function for arrays of arbitrary data types, see the list of official an inofficial KRL functions under http://www.robot-forum.com/robotforum/kuka-robot-forum/kuka-(possibly)-complete-list-of-functions-and-subprograms/ . So you have to write functions or subroutines which take both array name and length, or know your array length.

There isn't a System Function, but you can write it by yourselfe:

DEFFCT INT GetIntArrLength(intarray :OUT)
 DECL INT intArray[]
 DECL INT length
 FOR length = 1 TO 2147483647 STEP 1
  ON_ERROR_PROCEED
  intArray[length] = intArray[length]
  IF ($ERR.NUMBER <> 0) THEN
   ERR_CLEAR($ERR)
   RETURN length - 1
  ENDIF
 ENDFOR
 return -1
ENDFCT

Use it these Way:

DEF ArrayLength ( )
 FooLength = GetIntArrLength(Foo[]) 
END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM