2D array data in Karel

  • Hi guys,


    currently, I am looking for a proper way to define and use 2D array in KAREL program.

    the general concept is that,

    array[i,j]:

    10

    11 5

    7 13 14,

    and 13 will be read and use when i=3 and j=2,

    is there anyone happen to know how to program this?


    best regards,


    Chen

  • To declare the array:

    two_d_array: ARRAY[3,3] OF INTEGER--2D Array

    To access elements in the array:

    two_d_array[3,2]=13

    hi jmd2777,

    thank you for your reply.

    once the 2D array is created by Karel, is it possible to read the values with for-loops in TP program?

    for instance, using two registers represent i and j in for-loops in TP program, and in each loop a specific value corresponding to the index can be read from the array.

    regards,

    Chen

  • Chen,


    I don't know how to access a Karel variable in a TP program. One alternative is to have the Karel program read the registers storing i and j. Then the Karel program could copy the value from the 2D array into a register.

    So in your TP program you would set i and j register value. Then call the Karel program, or if the Karel program is always running you could turn on a FLAG that indicates you want to get a new value and the Karel program would see the FLAG and retrieve the value from the 2D array and store it in a register.

  • Chen,


    I don't know how to access a Karel variable in a TP program. One alternative is to have the Karel program read the registers storing i and j. Then the Karel program could copy the value from the 2D array into a register.

    So in your TP program you would set i and j register value. Then call the Karel program, or if the Karel program is always running you could turn on a FLAG that indicates you want to get a new value and the Karel program would see the FLAG and retrieve the value from the 2D array and store it in a register.

    JM,

    i agree with your strategy, but still have one concern that:

    once the 2D array[i,j] is created in KAREL program, is it saved permanently ? or it is only existing when karel program is running?

  • Use the translator directive %CMOSVARS


    If %CMOSVARS is specified in the program, then all static variables by default will be created in permanent memory.


    If %CMOSVARS is not specified, then all static variables by default will be created in temporary memory.

    • Helpful

    Chen,


    The variable is permanent. You may have noticed that after deleting a KAREL program named kar_prog from the robot, there is still a file named kar_prog.vr. That .vr file stores the KAREL variables. If you delete the .vr file, then the KAREL variable values will be lost.


    2 things that may help:

    1. The KAREL command GET_VAR can access variables from other KAREL programs. For example, you could have 2 KAREL programs: A) create_2d_array and B) get_2d_array_value. You could call get_2d_array_value from your TP program. It would use GET_VAR to access the 2D array in create_2d and then copy the value to a register.

    2. There is a convenient way to check the value of your KAREL variables from the Teach Pendant. Go to Select screen, select a KAREL program and press Enter key. Press Data button->Type->KAREL Vars. You will be able to see the KAREL variables for the currently selected program.

  • thank you for your help, that is really helpful.

  • Use the translator directive %CMOSVARS


    If %CMOSVARS is specified in the program, then all static variables by default will be created in permanent memory.


    If %CMOSVARS is not specified, then all static variables by default will be created in temporary memory.

    hi pdl,

    thanks for your reply,

    for creating a .DT file in Karel, i make file path as MC: xxx.dt, it works well in roboguide, but have not tried in real controller, do you have any experience or suggestion about choosing the memory device?

    regards,

    Chen

  • What is the data you're collecting and what is it used for? How often will this data change?

    pdl,

    i am trying to collect calculation result in KAREL program, so that the file can be export with USB. and the calculation is one time job only, not a realtime job which need to write and read hundred of thousands times.

    regards,

    Chen

  • Use the translator directive %CMOSVARS


    If %CMOSVARS is specified in the program, then all static variables by default will be created in permanent memory.


    If %CMOSVARS is not specified, then all static variables by default will be created in temporary memory.

    i used %CMOSVARS in program, but once i restart roboguide, the calculated variables are removed. the data karel variables are reset. do you know what is the reason for that?

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