views:

128

answers:

4

I have gyroscope + accelerometer data at each time period T. I want to calculate the rotation of the object at each time - it can rotate on its axises. So I've read that it is convenient to represent the rotation of the system in terms of quaternions (not in Euler angles).

Is there any source code that can transform from angular velocity (from gyroscope) to the quaternions representation? Because in order to do it I need to solve the differential equation using numerical methods, what I don't want to do.

EDIT: Sorry forgot to say, the C++ source needed, thank you!

+4  A: 
andand
There is no function for converting to quaternions from angular velocity..
maximus
@maximus: I updated my answer to better address your concern.
andand
Thank you very much! Very useful information.
maximus
@andand: One question about what you've written above, I know the angular velocity and initial angular position at time t0. Then as you said I calculate the next angle rotations using data about angular velocity and using that formula, at this step everything is OK. But one more step further. Actually how to keep records and at each time to know the angular position of the object relatively to the coordinate system at time t0?
maximus
@maximus: There's some additional book-keeping you'll need to manage that will be abstracted inside your C++ class. This includes t0, your initial angular position, angular velocity, and if necessary the angular acceleration as well. You apply the equation whenever asked to determine the angular position of your rotation; you don't actually need to change any of the parameters (unless you want to). But, any time you do change the position or the velocity (or acceleration), you should also provide the new t0 that applies to those new values.
andand
+2  A: 

Would you be talking about a Slerp? (Spherical Linear Interpolation)

See Jonathan Blow's article "Understanding Slerp, Then Not Using It" which has example source in C++...

http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/

Allbite
+1  A: 

What language? E.g. for Python cgkit has a nifty quat module (quaternions initialized from a rotation matrix, not "an angular velocity", but presumably you can build the former from the latter?) and euclid.py has Python source code for a Quaternion class including class methods to build it from rotation matrix and in other ways.

Alex Martelli
A: 

Each sample from your gyroscopes represents a small rotation:

rot_x = angV_x * timestep
rot_y = angV_y * timestep
rot_z = angV_z * timestep

If the resulting rotations are small, you can convert this directly into a quaternion by taking half the rotation angle:

// for small rotations, quick & dirty quaternion is sufficient
// (note: all angles *must* be in radians!)
float k = timestep * 0.5;
quaternion raw_delta_Q(1.0, angV_x*k, angV_y*k, angV_z*k);  // unnormalized!

// combine rotation for current timestep with orientation state
estimated_orient_Q *= raw_delta_Q;  // multiply by unnormalized delta
estimated_orient_Q *= 1 / norm(estimated_orient_Q);  // then renormalize it!

If your rotations are larger than a few degrees, or if you need maximum accuracy, you will need to pay closer attention to how you get your quaternion.

EDIT: Note that the above code assumes *= is defined to do quaternion multiplication by both quaternions and real scalars. Some form of these functions (as well as the obvious constructor) will be present in any reasonable quaternion library.

comingstorm