# Working with Quaternions

Hi,

I will capture the ingress and the egress of a truck with a new inertial mocap system. There is no interface to AnyBody yet. So I have to work with the raw data, the quaternions.
Is there a detailed procedure how I can use the quaternions to get my model?

Kind regards,
Julika

Dear Julika,

Sorry for the slow reply! Yes it possible to do this in AnyBody, although it could be tricky depending on how good the data is.

Before I explain this, I have two questions for you - Do your quaternions form a unit vector? (such special quaternions are called euler parameters) Can you do some pre-processing with your data in MATLAB/Python?

If you have euler parameters:
You might know that you prescribe motion in AnyBody using tools known as measures and drivers. For example, to define a time variying rotation of a body, you would (a) create a rotational measure which say measures its orientation relative to a global frame. (b) A driver can then constrain the values of the measured quantity to equal values from experimental data.

The AnyKinRotational class can measure orientation as euler parameters (Look it up in the reference manual). If you use this setting, you could then use the AnyKinEqInterPolDriver to constrain the values of euler parameters to a time series that you’ve read in through a text file.

NOTE: AnyBody may not always handle this case very well, since you are using 4 quaternions to define 3 DOF of rotation (overconstrained).

If you can pre-process (Recommended approach):
The refernce manual lists other options for AnyKinRotational, which for example allow you to measure rotation as a cartesian rotation vector.

If you could convert your quaternions into such a rotation vector beforehand by using MATLAB/Python, you could again use AnyKinEqInterPolDriver to drive your measure values using experimental data.

Hope this helps. Regards
Ananth
AnyBody Support

Dear Ananth,

When I output my data I can choose between cartan and euler.
A screenshot of this csv file is in the attachment.

My Knowledge about Matlab ist not very good. So, if the pre-process is the better choice, do you have an instruction for this process, too? Julika

Hi Julika,

I’m slightly confused. You mentioned that your raw data was quaternions but have marked out what looks like euler and cardan angles in your csv file. Have I misunderstood something?

Regards
Ananth

Hi Ananth,

the data I get are qx, qy, qz and qw for each sensor, but with the Software of the System it seems that I can get the other outputs, too - sorry. I can output the quaternions as csv file and open it in Excel.
I get a Chart with the categories: time, qx, qy, qz and qw (new attachement).

So, if its possible I have to work with the Quaternions. Regards
Julika

Hi Julika,

Is there any reason why you would like to work with quaternions, or would you rather use the equivalent cardan angles that your system can output?

If you can use these angles, the AnyKinRotational class can measure relative rotations of two reference frames as cardan angles (see reference manual for class, “RotAxesAngles” setting). You can then drive the values of these angles using experimental angles from your csv file using the AnyKinEqInterPolDriver.

If you must work with quaternions, and do not want to use MATLAB, try using Excel instead. You could set AnyKinRotational in the “RotVector” mode (see reference manual) where it measures rotations as a cartesian rotation vector.

You can find a description of cartesian rotation vectors here https://en.wikipedia.org/wiki/Axis–angle_representation#Unit_quaternions

You could use Excel formulas to calculate the three components of the vector representing the axis of rotation (e in the wikipedia article). For example ex = qx/sqrt(qx^2 + qy^2 + qz^2). Same for qy and qz. the formula for theta (from the wikipedia article) is given further below in the same article.

Once you have calculated the rotation vector as e*theta, copy-paste the time varying rotation vector components into a separate csv file and supply this to the AnyKinEqInterPolDriver.

Hope this helps Ananth
AnyBody Support

Dear Ananth,

Sorry für the slow reply. I tested a lot with my measuring system…
…and I’m working without the quaternions. I output now the joint angles (which calculated from the program from the quaternions automatically) from the system software as txt.file and want to import it to AnyBody.
What did I do wrong that it is stucking at “evaluating constants…” ?
(FileName: FreePostureMove.Main_IngressEgress)

Regards
Julika