# Quaternions and rotation

Basically, I am programming an iOS application where I use attitude of the device in quaternion format. Problem is following:

Practically:

I have a device that does a measurement #1 of magnetic field relative to it's orientation and saves it. After that this device is placed in some other point in space, rotated somehow and does a measurement #2 of magnetic field relative to it's orientation again.

Question: how magnetic field #3 will be different from #1, if after #2 we rotate device back to same orientation as during the measurement #1 ( instead of rotating device we can rotate field #2 ).

Mathematically:

We have a Latin reference system $O_lxyz$ and two Greek reference systems systems $O\xi\eta\zeta$ and $O'\xi'\eta'\zeta'$. Rotation of Greek systems relative to Latin system is defined by quaternions $q, q'$.

As well, we know coordinates of vector $\vec{F} = (F_\xi, F_\eta, F_\zeta)$ relative to $O\xi\eta\zeta$ and coordinates of vector $\vec{F'} = (F'_{\xi'}, F'_{\eta'}, F'_{\zeta'})$ relative to $O'\xi'\eta'\zeta'$.

Question: find difference between $\vec{F}$ and vector $\vec{F''}$ that can be obtained by rotating $O'\xi'\eta'\zeta'$ to match $O\xi\eta\zeta$.

My solution:

Magnetic field $\vec{F'}$ must be rotated in opposite direction to rotation from $O'\xi'\eta'\zeta'$ to $O\xi\eta\zeta$. Thus, rotation from $O'\xi'\eta'\zeta'$ to $O\xi\eta\zeta$ is defined by quaternion $qq'$, rotation of the field by quaternion $(qq')^{-1} = q' {q}^{-1} \Rightarrow \vec{F''} = q' {q}^{-1} \vec{F'} q{q'}^{-1} = (F''_{\xi}, F''_{\eta}, F''_{\zeta})$. Now $\vec{F''}$ can be compared with $\vec{F}$.

Can't attach sketch directly because of rating:

Experiment:

I get updates from the sensor and print components of $\vec{F}$ and $\vec{F''}$. Obvious, that they must be same (at least near) if device is just rotated, but not translated.

BUT

1) If I rotate device in $Oxy$ plane, then it works fine, components are same.

2) If I rotate it in not $Oxy$ plane, then components change significantly.

What is wrong with my approach?

I believe your formula for $\vec F''$ should be $\vec F'' = q^{-1} q' \vec F' {q'}^{-1} q$. The idea is that you first want to convert $\vec F'$ back to the Latin reference system by conjugating by $q'$, and only then conjugate by $q^{-1}$ to go back to the first Greek reference system. In general, it makes a difference which order we do this in, unless both rotations are along the same axis. If this still doesn't work, then you may need to replace $q$ and $q'$ everywhere by their inverses, in case they are defined the opposite way from how I'm interpreting your definition.
• Referring to my sketch, If You apply $q'$ first, then $\vec{F'}$ will rotate clockwise in $O'\xi'\eta'\zeta'$. After that, applying $q^{-1}$ will rotate it clockwise again. Thus, it will not be rotated as supposed for sure. – Tzoiker Jul 28 '15 at 20:22