REAL to INT conversion

  • KSS 8.5.8 KR6 900 KRC4

    I've made a probing cycle that measures the height of a stack of parts to be picked. The probe result is a position and I want to divide this number by the thickness of each part to get the number of cycles the robot should run in order to go through the stack.


    DECL INT CYCLE_COUNT

    PROBE_HIT = $POS_RET

    CYCLE_COUNT = PROBE_HIT.Z / 2.5



    1. What happens if the value of CYCLE_COUNT is 7.9 in REAL? Will the INTEGER be 7 or 8? And if it is 7 how can I get it to round up to 8 for values above 7.5?

    2. I get this message in WV " When using a value of type 'REAL' for a parameter of type 'INT' some values may not be initialized correctly". How can I do a better conversion to INT and at the same time get rid of this warning?

  • CYCLE_COUT is an INT and can NEVER have decimal places.


    calculation and assignment are two completely separate things.

    result of the calculation can be 7.9

    after calculation is complete, result is assigned to a target variable.

    if that variable is of a different type, attempt is made to "make it fit" if possible.

    because of this, during assignment rounding may take place.

    in that case, if the first digit after decimal point is less than 5, result is rounded down, otherwise it is rounded up. and i think that is what you may be after.


    but... if you want only values that are above 7.5 to be rounded up (but not 7.5), you would need to handle that in your code.

    1) read pinned topic: READ FIRST...

    2) if you have an issue with robot, post question in the correct forum section... do NOT contact me directly

    3) read 1 and 2

  • Thanks, that is what I wanted to achieve.

    Regarding the warning message in WV. " When using a value of type 'REAL' for a parameter of type 'INT' some values may not be initialized correctly".


    Is there a way to assign a REAL to an INT without getting this warning?

  • Is there a way to assign a REAL to an INT without getting this warning?

    The robot has no issue executing this code, it will simply do the conversion.


    The warning is coming from the WorkVisual program editor, which is trying to be helpful, and thinks you might have intended to do a REAL-to-REAL or INT-to-INT. But it's merely a suggestion. Similar to the "warning" it gives when you use inconsistent capitalization on variable names. If you DECL INT iMyVariable but use imyVariable=0 in your executable code, WV will throw a friendly warning at you, but the code will execute perfectly well -- KRL is case-insensitive, so iMyVariable and imyVariable are the same variable, as far as the interpreter is concerned.

Advertising from our partners