Help with Karel code

  • So I want to set a variable from a TP program but my problem is the Variable name is too long to use the $...=.... instruction.


    So I found this Karel code that seems to be working


    PROGRAM SET_INI_OFS


    VAR
    ENTRY:INTEGER
    STATUS:INTEGER


    BEGIN
    SET_VAR(entry, '*SYSTEM*', '$DPM_SCH[1].$GRP[1].$OFS[1].$INI_OFS', 10.0, STATUS)
    END SET_INI_OFS



    However instead of changing the variable to 10.0 I want to reference a register.


    I'm trying to use the Karel reference manual to figure it out but i have never used Karel before so I'm not getting very far.


    Can someone show me the code I need to do this?


    Thanks

  • 1. declare a new REAL variable (let's name it value)
    2. before your SET_VAR: read the register (GET_REG built-in? don't remember the built-in names and don't have the manual at hand atm.), saving it to value
    3. change you SET_VAR to:


    SET_VAR(entry, '*SYSTEM*', '$DPM_SCH[1].$GRP[1].$OFS[1].$INI_OFS', value, STATUS)


    The program will now read the register and save it to the variable (as REAL), and then use this variable to set the system variable that you wish.


    EDIT: to make it neater, I would also add some evaluation of STATUS - at least a check against a non-zero value.

    Edited once, last by bidzej ().

  • PROGRAM SET_INI_OF2


    VAR
    ENTRY:INTEGER
    STATUS:INTEGER
    REGISTER:INTEGER
    RFLAG:BOOLEAN
    RVALUE:REAL


    BEGIN
    GET_REG(150,RFLAG,REGISTER,RVALUE,STATUS)
    SET_VAR(entry, '*SYSTEM*', '$DPM_SCH[1].$GRP[1].$BI_CH[1].$BUMP_RATE', RVALUE, STATUS)
    END SET_INI_OF2



    OK I think that is what I did. What I was having trouble with is for some reason I thought the variable that "GET_REG" stored to was the "REGISTER:INTEGER" variable.


    Then I found that in order to get the above code to work you had to have a decimal number in the register. 10.1 will work, but 10 will not.


    Can you explain why if behaves like this? whats the difference/significance between "REGISTER:INTEGER" and "RVALUE:REAL" in the GET_REG line.


    The manual says


    • RFLAG is set to TRUE and RVALUE to the register content if the specified register has a real
    value. Otherwise, RFLAG is set to FALSE and REGISTER is set to the contents of the register.

    I understand that as: If the value is a integer store the result in REGISTER if its a real number store it in RVALUE. but when I enter a integer number in the register and use REGISTER in the SET_VAR line it doesn't work.


    Thanks

  • There is a good explanation of this issue somewhere on this forum - search for it :smiling_face:


    This is what I've found in the manual (R-30iA/iB):



    So, to avoid such problems, you can do the following:
    1. after using GET_REG, evaluate the real_flag to check if the value read from the register is REAL or INTEGER (with an IF).
    2. if real_flag is false, read the int_value and convert it to REAL, saving it to your RVALUE. I don't see a built-in which can be directly used for int-to-real conversion, so I would try adding 0.0 to int_value...


  • 2. if real_flag is false, read the int_value and convert it to REAL, saving it to your RVALUE. I don't see a built-in which can be directly used for int-to-real conversion, so I would try adding 0.0 to int_value...


    You can just directly assign an int to a real, and vic versa (although going from real to int truncates the real). Also, reals can contain larger values than ints, so you have to watch out for that.

    Check out the Fanuc position converter I wrote here! Now open source!

    Check out my example Fanuc Ethernet/IP Explicit Messaging program here!

Advertising from our partners