RTOS Length

helphelphelp

Member
Join Date
Feb 2020
Location
Canada
Posts
41
Hello again,

I'm using an Allen Bradley PLC connected to a laser marker which marks the data from a string tag to a part. The data in the string tag is a counter, for example 0001, 0002, 0003, etc. It's always 4 places.

The problem I'm running into is rounding with the RTOS instruction. I am dividing the real number by 10000 in order to get 4 decimal places after the zero. For example, the part count could be 880. The result looks like '0.0880'. I then use a middle string instruction to extract those 4 values to my string output to the laser marker.

Occasionally, I'll get a result of '0.17219999999999' or something similar in my RTOS instruction. This value should be '.01722'. My middle string conversion then pulls the '0.1721' instead of what should be a '0.1722'.

It's pretty difficult to explain what is happening in words but what I'd like to know is what can I do to limit my RTOS string to 4 decimal places so that it rounds correctly.

Thanks in advance!

Cheers
 
Another way to describe my question would be:

My string value when I go to monitor tags is showing up as "0.17290000". Is there anyway to make it so that those last 4 decimal points aren't included in my string?
 
I'm not sure I follow. My LEN currently on my string tag is 10 for some reason and I'm not sure how to change that to 5.

Anytime I change it to 5 it automatically switches back to 10.
 
The issue you're having is that floating point numbers can't represent all numbers exactly. So you might expect the result of your "divide by 10000" calculation to be 0.1722, but 0.1722 can't be exactly represented. When you convert it to a string, you get the closest representation to 0.1722 possible, which is 0.17219999...

I'm assuming that you do the "divide by 10000" trick because you need leading zeroes if there are less than four digits. As you've found, you'll run into trouble if your result can't be exactly represented by a floating point number. What AustralIan is proposing (and what I'd propose also) is that you don't divide by 10000; instead you convert your value of 880 or 1722 or 9999 or 3 to a string as-is, with no leading zeroes. Then use the CONCAT instruction to add leading zeroes to the front as many times as necessary, until the length of your string is 4. You don't need to tell the string how long it is; the string's .LEN element is automatically set by the PLC according to the actual length of the string. That's why it's "overwriting" it, but you need that to happen so you know when to stop adding zeroes to the front of your string.
 
Sorry just a follow since I can't seem to wrap my head around that instruction perfectly. I have about 16 constants plus a couple other variables as well as the 4 digit string instruction listed above. How does the CONCAT instruction decide what goes in what order?
 
My method for such issues leading zeroes in a string - is to create a string of the original number. I assume thee original numberis not a float. Make sure to create a string composed of 4 zeroes. The CONCAT your number on to the right side of the 4 zeros string to a third string. It will obviously have more zeroes than necessary. But now just extract the rightmost 4 characters to a final string.


Example - make sure to have a string tag (ST1 for example) already created with "0000" in it. Use the tag table to fill in (one time) the "0000"



1. Number - 8 for example - DTOS???
2. To a string (ST2) - "8"
3. CONCAT ST1 and ST2 to ST3 = "00008" - this will be various lengths depending on the original number.

4. Extract the last 4 characters (Not sure how MID is used for this - need help) into ST4 - "00008"
 
My Meaning is in pic below. Sorry that I wrote it in shorthand before, ladder is after all a graphical language. Obviously a different editor, but the meanings should be the same. I turned the "Flow Control Mode" on so you can see the value of String4 as it passes through the logic. I used a DTOS rather than RTOS too for no particular reason other than the input is a DINT. I hope you can translate to your controller as needed.

Screen Shot 2020-02-20 at 00.15.27.png
 

Similar Topics

Context: We had an AOI that would stop working correctly at seemingly random times. It was noticed that a string tag would mysteriously lose its...
Replies
3
Views
1,412
Is there anyway to improve the accuracy of the Real To String conversion instruction in contrologix? using the instruction for a test...
Replies
6
Views
2,134
Wow.. it seems to have taken on a life of its own. :) I guess that's a good thing... here's a quick summary response.. They're Allen Bradley...
Replies
7
Views
2,865
Hello everybody, I believe that I will find experts here who have already solved a similar topic. I need to calculate the length of the...
Replies
7
Views
293
Maybe this is just not possible, or maybe I am doing something wrong. Background; I have a data array of over 1500 products. For sorting I...
Replies
6
Views
757
Back
Top Bottom