views:

40

answers:

1

Real-life case (helps understand the question)

I am building a device that can freely rotate around all its axis (x, y, z) and is equipped with:

  • an accelerometer (A) that gives me a vector pointing to the centre of the Earth (Va)
  • a 3D magnetometer (M) that gives me the direction of the magnetic field of the Earth (Vm)

The two vectors share the same reference system (x, y, z), but what I am interested to find is the vector that points to the north relative to the Earth surface [think of a hand-held compass: I want to find where the needle should point to].

This video shows a "ball compass" that has pretty much the same behaviour that my device should replicate electronically.

The coding question

I did a bit of research, and it seems to me that I should use is 3D rotation matrices doing the following two steps:

  1. rotate the reference system of Vm from R to R', in such a way that y' will be parallel to Va,
  2. "flatten" the Vm vector setting its y' component to 0

Unluckily I am still confused on how I should proceed in concrete terms (I have no previous experience of working with vectors and matrices). One of the things that confuses me is that most of the material I could google, talks in terms of angles, but the data I am receiving from both sensors is in the form V(Vx, Vy, Vz), where Vz, Vy and Vz are the components of V along the reference system.

So my question really boils down to: what is the matrix I have to use in order to perform the transformation of which at step #1?

Thank you in advance for your time and expertise.

A: 

One simpler answer that comes to mind is to use cross products.

you can find east with Ve = Va ⨉ Vm and then north with Vn = Va ⨉ Ve

(this might actually be south, haven't thought through the handedness)

cobbal
Again: first-timer in the world of vectors, here, so correct me if I am wrong... but wouldn't this method give opposite results (in terms of vector sense) according to the angle between Va and Vm being clockwise or counter-clockwise? If I visualise in my mind two different Va which are symmetrical with respect to Vm, I come up with two opposite results for what Ve should be.... or am I wrong about this?
mac
@mac you will get an opposite Ve but you've also just changed which direction is up (-Va), and so they _shouldn't_ end up pointing in the same direction. I think it works out to be consistant
cobbal
@cobbal - It's difficult to explain without images, but I was not imagining to change the sign of Va: I imagined that my three vectors Va, Vm and Va' lay on the same plane, and that the three of them are in the portion of space that have x, y and z all positive. [An ascii-art of the vectors would look like: /|\]. Wouldn't this lead to non-consistent results? If I have time I will however make a try later tonight for the sake of it...
mac
@mac I think I realize what you're asking. Let me try to show it another way. As Va and Vm get close, any calculations will blow up, this is because if north is straight down then you can't find a horizontal north direction. now if the 2 vectors stay arbitrarily far apart, then your result will move smoothly with the vectors, and won't "suddenly" flip (so think of your /|\ as a rotation around Vm instead of a move through it). If you rotate your device 180˚ about Vm then north _should_ end up on the other side of the device. I hope this makes some sort of sense, it's a little hard to describe
cobbal
@cobbal - will try to implement it and see what happens, then. Sometimes empirical manipulation works wonders! :) If I find that it does not work, I will report back. :)
mac