I have two systems, each of which has a direction sensor (0-360 degrees), but the sensors can provide wildly different values depending on the orientation of each system and the linearity of each sensor. I have a mechanical reference that I can use to generate a table of where each system is actually pointing. This yields a table with three columns:
Physical SystemA SystemB
-------- ------- -------
000.0 005.7 182.3
005.0 009.8 178.4
... ... ...
From just the data shown, we can see that SystemA isn't far from the physical reference, but SystemB is about 180 degrees off, and goes in the opposite direction (imagine it is mounted upside-down).
I need to be able to map back and forth between all three values: If SystemA reports something is at 105.7, I need to tell the user what physical direction that is, then tell SystemB to point to the same location. The same if SystemB makes the initial report. And the user can request both systems to point to a desired physical direction, so SystemA and SystemB would need to be told where to point.
Linear interpolation isn't hard, but I'm having trouble when data is going in opposite directions, and is modular/cyclical.
Is there a Pythonic way to do all these mappings?
EDIT: Let's focus on the most difficult case, where we have two paired lists of values:
A B
----- -----
0.0 182.5
10.0 172.3
20.0 161.4
... ...
170.0 9.7
180.0 359.1
190.0 348.2
... ...
340.0 163.6
350.0 171.8
Let's say the lists come from two different radars with pointers that aren't aligned to North or anything else, but we did manually take the above data by moving a target around and seeing where each radar had to point to see it.
When Radar A says "I have a target at 123.4!", where do I need to aim Radar B to see it? If Radar B finds a target, where do I tell Radar A to point?
List A wraps between the last and first elements, but list B wraps nearer to the middle of the list. List A increases monotonically, while list B decreases monotonically. Notice that the size of a degree on A is generally not the same size as a degree on B.
Is there a simple interpolator that will wrap correctly when:
Interpolating from List A to list B.
Interpolating from List B to list A.
It is OK to use two separate interpolator instantiations, one for going in each direction. I'll assume a linear (first-order) interpolator is OK, but I may want to use higher-order or spline interpolation in the future.
Some test cases:
A = 356.7, B = ?
A = 179.2, B = ?
This is what works for me. Could probably use some clean-up.
And a test:
Answer to part 1: translation table containing the calibration values + drift value.
Basically, if DialA reports 5.7 when it is physically at 0, 9.7 when it is at 5, then I would set the drift value to be +/- .25 of the distance between each readout position to account for mechanical and readout drift.
Answer to part 2: keeping the same values on both dials, while displaying the expected position.
If you are not direction dependent, then just spin the output dial until it is in the correct position as per your calibration table.
If you are direction dependent, then you will need to track the last 1-2 values to determine direction. Once you have determined direction, you can then move the dependent dial in the direction you require, until the destination position is reached.
Your calibration table should include direction as well(positive or negative, for instance).
With the above two parts in mind, you will be able to compensate for rotational offset and directional flips, and produce an accurate position and direction reading.
Here is some code that given a calibration table, will yield position and direction, which will solve the problem of display and making the dependent dial match up with the primary dial:
Running the program shows that the direction is determined correctly, even for WheelB, which is mounted backwards on the panel/device/etc:
Note that some of the "readout" values fed to the functions are off. That is compensated for, by the drift value. Whether you need one depends on the equipment you are interfacing with.
The easiest solution is to make all of your table elements be increasing (or decreasing as the case may be), adding or subtracting 360 to individual elements to make it so. Double up the table back to back so that it covers the entire range of 0 to 360 even after all the additions and subtractions. This makes a simple linear interpolation possible. Then you can take a modulo 360 after the calculation to bring it back into range.
But you can use linear interpolation. If your sample A value is e.g. 7.75, that resembles 2.5 degrees. If the sample B value is 180.35, it also resembles 2.5 degrees. The tricky part is when the values overflow, if that is possible at all. Just set up a bunch of unittests to check if your algorithm works and you should quickly be going.