Karel Indirect Addressing Issue

  • Ran into a road block and not sure what i am doing wrong? This is the error i am getting and below is a sample portion of code


    127 UseZone[n]=TRUE

    ^ ERROR

    Id must be defined before this use. Id: USEZONE




    n: Integer

    BoxLength:Integer

    CaseLength:Integer

    UseZone1:BOOLEAN

    UseZone2:BOOLEAN



    n=1

    BoxLength=100

    IF CaseLength > Zone1Length THEN

    UseZone[n]=TRUE

    ENDIF


    EDIT:

    sorry I didnt explain too well but i am not trying to use an array i want to replace the n with a number


    UseZone1:BOOLEAN

    n=1

    UseZone[n]=true ------------I want to replace the n with a 1 and create the expression : UseZone1=True

    Edited once, last by Iprogram ().

  • Place your Ad here!
  • At the moment I don't remember how arrays are declared in Karel. But you haven't declared

    Code
    UseZone

    as an array. But you use it as an array

    Code
    UseZone[n]=TRUE

    Instead following should work:

    Code
    UseZone1=TRUE
  • Sorry, that isn't how variables work. You could use a SELECT or IF statement to set UseZone[n] to true if you don't want to use an array.

    I guess i am really asking how to do this? and what i did wrong to not make that work. for instance i can do the following expression:



    n=35

    DOUT[n]=ON


    so what am i doing wrong with the indirect addressing of UseZone[n]=true

  • You can do DOUT[n] because "DOUT" is a special "variable": you don't need to declare it and in the Karel (and TP) environment it is a sort of array (i.e. you can reference its elements with the [<number>] syntax).


    UseZone instead is a variable you declare, so it is not automatically an array, you must declare it as an array (as hermann already said)

    What do you think is the benefit of not using arrays but that late binding stuff (that's not possible in Karel anyway)?

    Use the array and you are done.

    In order to use the array syntax, you need to declare UseZone variable as an array.

  • What do you think is the benefit of not using arrays but that late binding stuff (that's not possible in Karel anyway)?

    Use the array and you are done.

    No thought there just want to use the True or False in my code. what would be the benefit of an array over a single variable that is declared?

  • You can do DOUT[n] because "DOUT" is a special "variable": you don't need to declare it and in the Karel (and TP) environment it is a sort of array (i.e. you can reference its elements with the [<number>] syntax).


    UseZone instead is a variable you declare, so it is not automatically an array, you must declare it as an array (as hermann already said)

    In order to use the array syntax, you need to declare UseZone variable as an array.

    Makes Sense Thank you for the explanation!

  • DOUT is a predefined port array. You can address it as if it were an array because it is one.


    UseZone is not a declared array. Therefore you can't use array addressing.


    To do what you want, you could do the following:


    1) declare UseZone as an array.

    UseZone: BOOLEAN[2]


    2) Use a select statement

    SELECT n OF

    CASE(1):

    UseZone1 = True

    CASE(2):

    UseZone2 = True

    ELSE:

    ENDSELECT


    3) Do the same thing with if statements



    Without any further information about what you're trying to do, I would recommend declaring UseZone as an array and going that route.

  • They are totally different things, by writting "DOUT[n]=ON" you are already accessing some type of array, that's how Fanuc handles the I/O addressing, just the same as it works if you put a register inside, that is called indirect accessing and it just works because fanuc has done it that way.


    But no, you cannot do what you are trying that way by simply writting a number after the variable name, the only brand I know that allow this is ABB, but only if you use functions that search the variable tables for variables with the name you provide on a string.


    You may be able to do it on fanuc by using the BYNAME routine, search it on the "Karel reference manual" if you are interested and willing to try it, but I sugggest you to an array of booleans, that will be the easiest way you will be able to handle it, there is not need to reinvent the wheel if you only want to apply this on a little routine.

  • Maybe this could give an approch



  • Here is a quick example of Byname if you want to set variables using an indirect address


    --------------------
    PROGRAM DgTest
    --------------------
    %COMMENT = 'Tests Byname'

    VAR
    iTest : ARRAY[16] OF INTEGER

    ROUTINE IntToString(piNumber : INTEGER) : STRING  FROM DgGeneral
    -----------------------------------------------------------------------------
    ROUTINE DoByname(piByname : INTEGER)
    --
    -- This routine sets Frame and Track Uframe if needed
    --

    BEGIN

    iTest[11] = piByname

    END DoByname
    -----------------------------------------------------------------------------
    ROUTINE DoTest
    --------------------------------------------------------------------------------
    -- Creation date: 2023.07.01
    --
    --Routine
    --
    -- Used to: DoSomething
    --
    -- Arguments: Any Pass In Arguments
    --
    -- Return value: AnyReturn Values
    --
    --------------------------------------------------------------------------------
    CONST---------|
    COUNTER_DEL = 2000
    VAR-----------|
    liStatus,
    liCnt : INTEGER
    lsCnt : STRING[2]
    BEGIN

    iTest[1] = 1
    iTest[2] = 2
    iTest[3] = 3
    iTest[4] = 4  
    iTest[5] = 5    
    iTest[6] = 11
    iTest[7] = 12
    iTest[8] = 13
    iTest[9] = 14  
    iTest[10] = 15

      FOR liCnt = 1 TO 10 DO
    lsCnt = IntToString(liCnt)
    DoByname(BYNAME('', 'iTest[' + lsCnt + ']', liStatus))
        DELAY(COUNTER_DEL)
      ENDFOR
       

    END DoTest
    -----------------------------------------------------------------------------
    --Begin Main program
    BEGIN

    DoTest

    END DgTest

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account
Sign up for a new account in our community. It's easy!
Register a new account
Sign in
Already have an account? Sign in here.
Sign in Now

Advertising from our partners