Floating point position calculation imprecision drift

I need to check an axis actual travel position, and to compare it to the master travel position.
To do this I have to multiply the axis travel position by the strange ratio of 36/21 , and then add the result to the total calculated value.
This happens at every scan of the plc program, and so I slowly lose the digits after the 7th one. After some time, I notice some differences that are coming from these calculations.
The used Rockwell plc has 7 digits precision for real data type.
36 / 21 = 1.7142857 . . .
 
1) use LREAL. This kicks the can down the road, but it may be far enough down the road to solve the problem.

OR

2) scale the master travel position, presumably a REAL, to an integer (scale by 21/36),
2.1) do the addition in the integer domain,
2.3) and execute the comparison in the integer domain.

OR

3) maintain an integer [leftover] quantity, initialized to 0, then on each scan
3.1) increase measured [per-scan axis travel] by [leftover]
3.2) calculate [per-scan axis travel MODULO 21], save it in [leftover]
3.3) decrease [per-scan axis travel] by [leftover]
3.4) scale [per-scan axis travel] by 36/21 into REAL
3.5) add scaled REAL result to [total calculated value, REAL]
3.6) As long as total calculated value does not exceed 16,777,216, there will be no loss of precision.
 
I have to multiply the axis travel position by the strange ratio of 36/21

This looks like there could be a gearbox or toothed belt in the system with this ratio.

I have used the first method suggested by drbitboy with success. Once you get used to working in counts inside the program it is a much easier way to go. The master position is always in counts and you only do a conversion to REAL do display the position for the operator
 
I would probably do that with integers, and adding the remainder of the previous division to the next division, so the smallest variations are not lost
 
Based on your suggestions, I reached the following solution, which worked well on the simulator, to be tried on the real machine.

MasterCalcDeg = (MasterActDeg * 21) modulo 360
SlaveCalcDeg = (SlaveActDeg * 36) modulo 360
DiffCalcDeg := (MasterCalcDeg - SlaveCalcDeg) / 21
SlaveFinalDeg := MasterActDeg - DiffCalcDeg

Thank you all ...
 

Similar Topics

I am trying to do a very simple floating point function that will add and subtract a constant value 0.0002 to the value of 0.0160. Therefore, at...
4
Replies
676
Views
I have a Multilin SR489 talking modbus serial to a prosoft card on a PLC5. The data I want to read is ground current. The serial interface is...
4
Replies
1,226
Views
Hi All, I am trying to display a double word into floating point in either ladder or structured text in CX-programmer with Omron CPU12. For...
6
Replies
2,171
Views
Hi guys i came across a problem when convecting float to integer ie. 8/3=2.666666 if i convect it i get 2 only 0.6666 is left out how do i get...
20
Replies
3,892
Views
We have AOIs for projects, which handle material dosing (by hand or by pipes and pumps), obviously they have comparison between setpoint and...
50
Replies
18,447
Views
Top