# How to get angular velocity from difference orientation quaternion and time?

I am using Bullet Physic library to program some function, where I have difference between orientation from gyroscope given in quaternion and orientation of my object, and time between each frame in milisecond. All I want is set the orientation from my gyroscope to orientation of my object in 3D space. But all I can do is set angular velocity to my object. I have orientation difference and time, that is why I need vector of angular velocity [Wx,Wy,Wz] from that.

After reading this: angularVelocityArticle1 and this: angularVelocityArticle2

I did something like:

btQuaternion diffQuater = gyroQuater - boxQuater;
btQuaternion diffConjQuater;

diffConjQuater.setX(-(diffQuater.x()));
diffConjQuater.setY(-(diffQuater.y()));
diffConjQuater.setZ(-(diffQuater.z()));

////////////////
//W(t) = 2 * dq(t) /dt * conj(q(t))

btQuaternion velQuater;

velQuater = ((diffQuater * 2) / d_time) * diffConjQuater;


But it is not working as I expect, I mean, there is written, vector part of the result quaternion should be vector of angular velocity, and scalar part should be 0, but my result is not like that.

angular velocity vector represented as quaternion with zero scalar part, i.e W (t ) = (0, W x (t ), W y (t ), W z (t ))

Any ideas?

-

This question might go better over at the GameDev SE site; at first glance, though, this formula:

velQuater = ((diffQuater * 2) / d_time) * diffConjQuater;


is not representing what's in the comment above it. The formula you're looking for is $\omega(t) = 2q'(t)\bar{q}(t)$ (where as typical I've written $q'(t) = \frac{dq(t)}{dt}$), but what's written in that code is $\omega(t) = 2q'(t)\bar{q'}(t)$; in other words, you're not multiplying by the conjugate of your original orientation quaternion (as you should be) but by the conjugate of (the approximation of) the derivative. The line you want should be something like

velQuater = ((diffQuater * 2) / d_time) * conjBoxQuater;


but note that this is all predicated on another assumption - that gyroQuater and boxQuater represent the orientation of the same object at two nearby points in time. From your description it sounds like this might not be quite the case, and if it's not so then you may have to be more explicit about just what behavior you're after.

-
Thank you for your suggestion. Yes, these two objects have almost the same orientation, difference is very small, frames between each angular velocity setting is in milliseconds. But, unfortunately it is still not working as I expect (as it is described on info pages). –  Andrew Jun 20 '12 at 21:01
@Andrew My revised line of code had a bug in it, too - I forgot to conjugate $q$ itself before multiplying! I've corrected it now; note that you might want to conjugate gyroQuater (instead of boxQuater), but that should matter a bit less. –  Steven Stadnicki Jun 20 '12 at 21:04
Thank you very much, although scalar part is not 0.0, but I can use vector part to set velocity. Unfortunately I have some error when I get do 120 degrees: –  Andrew Jun 20 '12 at 21:31
The scalar part won't be 0.0 exactly, because of the approximations inherent in the estimate; still, you should find that it's no larger than (roughly) your time interval d_time. –  Steven Stadnicki Jun 20 '12 at 21:34
Can you tell me why it is problematic, when difference is not so small? (when speed of rotation gyroscope is high) And why it is problematic on 120 degrees angle and how to avoid that? I mean, I did condition if Q(w) is no larger than (roughly) my time interval d_time, but it makes another problems. Do you think working on morientation matrices instead of quaternions would be better? –  Andrew Jun 20 '12 at 21:45