My Android smartphone yields two triplet x;y;z combinations which describe its orientation relative to gravity and the ambient magnetic field relative to the phone. How would I combine these readings to extract the horizontal direction of the ambient magnetic field?
Any hint, from full abstract algorithm to a reliable search phrase for Google or Wikipedia, will be gratefully received...
Do the triplets look like unit vectors or is one of them plausibly your location on the Earth's surface or a vector field strength ?
Take a reading. Move 100m N take another reading and then same for E. That should allow you to figure out their chosen coordinate system.
Direction of gravity defines a local horizontal tangent plane.
I'd guess they might well have x as E-W, y=N-S and z=Up-Down but only a bit of experimentation will establish this for sure. The other common one on a sphere is x=E-W (at Greenwich) y = 90 on from E-W and z=N-S.
There are endless other possibilities though. The choice of origin on a sphere or even on an oblate spheroid is fairly arbitrary.
You are free to pick any orthogonal base perpendicular to the direction of local gravity but things will be easier if you choose the same E-W and N-S convention as the magnetic data.
You are looking for an IMU, Inertial Measurement Unit, such as is used in autopilot software. Typically there will be gyro, accelerometer and magnetometer, all in 3D. You have the latter two, so your dynamics will be limited, but basically it's the same problem; you have noisy unreliable data and need to implement sensor fusion to get the best estimate of attitude.
The best autopilot software uses Kalman filtering to perform this sensor fusion. Of the open source ones, Px4 is the best known:
At least close to magnetic poles, the magnetic field is tilted moire or less downwards, so make sure the that the phone is in a horizontal orientation when taking magnetic readings, if there is only one magnetic sensor (no readings in vertical direction).
Thanks Martin, but in fact Android documentation is quite specific. Orientation x;y;z are all in degrees (although of course not independent; when the phone is held flat yaw has no datum). And magnetic values in micro-Tesla. Not that this makes much difference...
The horizontal direction of the ambient field is 'magnetic north'; you also know its direction relative to the phone, so could paint an arrow on the screen of the phone. Up and down DOES interact with the horizontal amplitude component of the magnetic field; is that what you want?
The horizontal amplitude is computable, with vector dot and cross products. Dot the B field with the unit vertical vector, and the unit vertical vector is the negative of the "Down" vector of gravity, divided by the gravity magnitude. Then, knowing that to be the vertical component,