I think this works out by looking at triangles...
Using the diagram...
S is the separation of the two transmitters, R the range to the target, A,B are the distances measured by the device and 'x' is what we're trying to find.
By Pythagoras: (here A^2 denotes A squared)
A^2 = R^2 + x^2 or A^2 - x^2 = R^2
and
B^2 = R^2 + (S+x)^2 or B^2 - (S+x)^2 = R^2
rearrange this to
A^2 - x^2 = B^2 - (S+x)^2
A^2 - x^2 = B^2 - S^2 - 2Sx - x^2 expand (S+x)^2
A^2 = B^2 - S^2 - 2Sx lose x^2 from both sides
A^2 - B^2 +S^2 = -2Sx rearrange
finally:
A^2 - B^2 + S^2
_______________ = x
-2S
ta da !
I did a quick check measuring on paper and I think it's correct. Another quick check is if A is the same as B, in which case it boils down to:
-S^2 / 2S == -S/2 == 1/2 way between the transmitters, which is right.
Edit: following comment below...
Note that the final formula only involves A,B and S. We need S (the distance between transmitters) because this completes the triangle A/B/S and therefore scales the output value. You could use a fixed value for this but the output would not relate directly to the scale of the A or B values. However, you could use s=100 giving...
A^2 - B^2 + 10000
x = _________________
-200
or, in javascript:
var pos= ((ranges.left*ranges.left)-(ranges.right*ranges.right)+10000)/200;
this will give a value between 0..100 for the object being directly somewhere between the two sensors e.g. if both sensor values are, say 42, we get:
pos = (42*42 - 42*42 + 10000) /200; // answer = 50
If the object is directly in front of sensor A, we could have values:
pos= (100*100 - 140*140 + 10000)/200; // answer = 2
or directly in front of B:
pos= (140*140 - 100*100 + 10000)/200; // answer = 98
Further along your 'x' axis will obviously get larger values in one direction and go -ve in the other direction. I hope that makes it clearer.