19

I am getting a quaternion from sensor data that is in the coordinate system Y=up, X=right, and Z= backwards.Mine is X=forward, Y=right, Z=up.

So OX=Y, OY=Z and OZ=-X.

I have a function that can convert quaternions into 4by4 matrices, but no idea where to go from here. Any help would be greatly appreciated.

Craig Delancy
  • 191
  • 1
  • 1
  • 3
  • 1
    Looks like you could get the axis/angle, rearrange the coordinates as needed, and build a new quaternion from them. Probably simpler than farting around with matrices. But my 3D math is nowhere near strong enough to say for sure. – cHao Sep 15 '13 at 22:45

3 Answers3

20

Quaternions in the form of [X, Y, Z, W] are equivalent to axis-angle rotations where W is dependent only on the angle of rotation (but not the axis) and X, Y, Z are the axis of rotation multiplied by sin(Angle/2). Since X, Y, Z have this property, you can just swap and negate them as you would to convert a 3D coordinate between. To convert from your sensor's coordinate system to yours, you can simply do this:

MyQuat.X = -SensorQuat.Z
MyQuat.Y = SensorQuat.X
MyQuat.Z = SensorQuat.Y
MyQuat.W = SensorQuat.W
mkimball
  • 764
  • 4
  • 9
  • 3
    can you link any source that explain such math property? – CoffeDeveloper Nov 01 '15 at 14:36
  • 6
    I noticed that you are converting from a right handed system to a left handed one. In that case you would want to negate your X Y Z rotation. So MyQuat.X = SensorQuat.Z MyQuat.Y = -SensorQuat.X MyQuat.Z = -SensorQuat.Y MyQuat.W = SensorQuat.W – Roman Nov 24 '15 at 22:11
  • I think @Roman is correct here. Whilst mkimball handles the conversion of coordinates correctly, the left-handed rotation in the new system means we want "0.5(2*pi - theta)" terms in MyQuat, where we had "0.5*theta" terms in SensorQuat – Neil Gatenby Apr 18 '18 at 11:01
  • 3
    What if my 2 coordinate axes are not 90 degrees apart, but are a complex-rotation apart ? (so not as simple as just swapping and negating) what is the general rule in this case ? – MohamedEzz May 17 '18 at 23:32
  • More context for @Roman's addition about Right-handed vs Left-handed: https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#:~:text=confer%20an%20orientation%20(-,%22handedness%22,-)%20to%20space%3A%20in – CourageousPotato Aug 19 '22 at 09:17
10

Assume you have two coordinate systems F1 and F2. For simplicity, assume both have same origin. Now let,

qo_f1 = orientation of frame F1 as seen from frame F2
qo_f2  = orientation of frame F2 is as seen from F1
q_f1 = some quaternion in F1 frame
q_f2 = q_f1 as seen from F2

Then,

q_f2 = qo_f2 * q_f1 * qo_f2.inverse()

Explanation

To rotate anything by quaternion q you just do q*p*q.inverse(). If p is a vector then you first convert it to "fake" quaternion by setting w=0 and x,y,z same as vector. If p is quaternion then you are good to go.

Shital Shah
  • 63,284
  • 17
  • 238
  • 185
  • 2
    This is the best explanation I've seen so far. Can you elaborate on how to calculate `qo_f2`? For example I have an IMU which is not aligned with body. It has it's own frame of reference (probably based on north and gravity). I want a frame of reference where the third axis is in gravity direction (say I have gravity vector), the other two directions are not really important. – anishtain4 Jun 18 '19 at 17:55
  • 1
    What is "orientation of frame F2 is as seen from F1"? If I have `F1` as `X=Forward, Y=Left, Z=Up`, and `F2` is `X=Right, Y=Down, Z=Backward`, what would "orientation of frame F2 is as seen from F1" be? – trusktr Aug 26 '20 at 21:02
0

You just Need to Multiply it by q, which is the quaternion that converts points from Coord2 to the Coord1 equivalent. Explanation:

q2 * point2 * (q2)^(-1) = (q1 * (q^(-1))) * point2 * (q1 * (q^(-1)))^(-1) 
= q1 * (q^(-1)) * point2 * q * (q1^(-1)) = q1 * ((q^(-1)) * point2 * q) * (q1^(-1))
= q1 * point1_equivalent * (q1^(-1))