# 3D vector calculation

• posted

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...

• posted

Not really enough information.

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.

```--
Regards,
Martin Brown```
• posted

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:

Clifford Heath.

• posted

vector cross product with the accelorometer, twice.

~ ~ ~ M x A x A

the first mutiplication gets you a vector perpendicular to both accelerometer and magentometer, (so it's pointing west to the horizon)

the second gets you a vector perpendicular to west and gravity (pointing south)

```--
Jasen.```
• posted

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).

• posted

Freescale application note 4248 was very helpful for me:

```--
Saludos```
• posted

Huh, that seems to work... I was picturing doing some coordinate transformation to make the g-field in the z-direction then calculating the Mag-component in that direction and subtracting it.

George H.

• posted

Thanks, but as soon as lockdown allows I intend to head for smaller latitudes!

• posted

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...

• posted

Thank you; I shall study that diligently!

• posted

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,

( B_horizontal)**2 +( B_vertical)**2 = (B_total)**2

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.