How to do Kinematics with IMU data workflow in Python

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Joshua Galvan
Posts: 2
Joined: Mon Oct 02, 2023 3:37 pm

How to do Kinematics with IMU data workflow in Python

Post by Joshua Galvan » Mon Oct 02, 2023 3:52 pm

Hello!

I am currently trying to follow the tutorial located at: https://simtk-confluence.stanford.edu:8 ... h+IMU+Data

This tutorial allows you to use IMU data to compute motions of body segments. I do not have access to MatLab so I am trying to follow the tutorial with Python. There are no Python examples on that tutorial so I'm trying to do it myself, and the tutorial mentions support for Python.

This issue is in step 2, and I am unable to create an orientation file from IMU sensor data. My current Python file is:

Code: Select all

import opensim as osim
import os

ROOT = "C:\OpenSim 4.4\Resources\Code\Python\OpenSenseExample"

xsensSettings = osim.XsensDataReaderSettings(os.path.join(ROOT, 'myIMUMappings.xml'))
xsensReader = osim.XsensDataReader(xsensSettings)

tables = xsensReader.read(os.path.join(ROOT, 'IMUData\\'))
quaternionTable = xsensReader.getOrientationsTable(tables)

trialPrefix = xsensSettings.get_trial_prefix()
osim.STOFileAdapterQuaternion.write(quaternionTable, [trialPrefix, '_orientations.sto'])

print("Done")
I am receiving an error due to the osim.STOFileAdapterQuaternion.write() call. The error is:

Code: Select all

TypeError: in method 'STOFileAdapterQuaternion_write', argument 2 of type 'std::string const &'
The type of 'trialPrefix' is a 'str', so I'm unsure of how else to pass this to the function. In the tutorial it wraps the trial prefix with char() in MatLab, so perhaps it expects a char array? Char arrays do not exist in Python, so this is an unusable function? Does it need to be updated to be able to accept a string?

I would try using the CLI for OpenSense, but when I try to run it from the command line it tells me it is not recognized. Either way I would still prefer for it to work in Python.

Tags:

User avatar
Ayman Habib
Posts: 2252
Joined: Fri Apr 01, 2005 12:24 pm

Re: How to do Kinematics with IMU data workflow in Python

Post by Ayman Habib » Mon Oct 02, 2023 3:59 pm

Hello,

Just guessing here that the function expects a reference to string, and that fails because the argument is created on the call line.
I'd split the last line into two:

Code: Select all

full_file_name = ....
osim.STOFileAdapterQuaternion.write(quaternionTable, full_file_name )
Please let us know if that works,
-Ayman

User avatar
Joshua Galvan
Posts: 2
Joined: Mon Oct 02, 2023 3:37 pm

Re: How to do Kinematics with IMU data workflow in Python

Post by Joshua Galvan » Mon Oct 02, 2023 4:17 pm

Hello Ayman,

Thank you, that worked. I appreciate the help!

If you're still able to respond, do you know of how to do this workflow without having the quaternions computed for me? I'm only asking because I have quaternion data from my IMUs already, and don't need it computed. Does this workflow only work if it creates the .sto file from the 6 or 9 axes sensor data?

To phrase differently, I know it needs the .sto file, so am I able to convert my quaternion data to the .sto format in some way, or does it only work from accelerometer, gyroscope, and magnetometer axis data?

Thank you again for solving this issue for me.

User avatar
Kevin Sommler
Posts: 4
Joined: Tue Apr 30, 2024 6:22 am

Re: How to do Kinematics with IMU data workflow in Python

Post by Kevin Sommler » Tue Apr 30, 2024 10:12 am

Hello everyone,

at the moment I am facing the same question as you @Joshua Galvan . To me it seems the quaternions wanted for the .sto file are supposed to be in the world coordinate system (even though they mention "IMU coordinate system in the quote below) as I found a small hint to that in the documentation:

Quote:
IMU Orientation Data & Transformation: Typically users select an orientation file containing collected sensor orientation at the default model pose (called placement pose). This section is also used to convert orientation data from the sensor/IMU coordinate system (typically based on magnetic north and gravity) to the OpenSim default world coordinate system (Y-up, X-forward). The transformation is specified in space fixed Euler angles that are displayed in degrees (in the dialog) and can be incremented/decremented by 90 degrees using the spinners.
Source: OpenSim Doc - https://opensimconfluence.atlassian.net ... s/53084302

I developed a pipeline to record and generate .sto files with quaternions generated by a state of the art quaternion filter (without the use of magnetometer data), meaning I would receive the IMU coordinate system. Because of this, I believe I am facing the problems regarding the IK data generated by OpenSim as the IMUs only seem to "work" if they are attached to the frontal area of the upper and lower leg. The data just seems a bit off when visualized and becomes even more off when placing the IMUs on the sides of the upper and lower leg as they show in the example of the main OpenSense documentation (https://opensimconfluence.atlassian.net ... s/53084203).

If anyone does have any leads or a even more detailed documentation on that topic regarding the quaternions in the .sto files, I would really appreciate it!

Regards
Kevin

User avatar
Kevin Sommler
Posts: 4
Joined: Tue Apr 30, 2024 6:22 am

Re: How to do Kinematics with IMU data workflow in Python

Post by Kevin Sommler » Thu May 02, 2024 8:22 am

After some testing, my theory seemed to have proven correct concluding from my received results today.
That means, if you want to use your own quaternions, make sure they are all in the same coordinate system, before trying to use them in OpenSim. Meaning, a sensor to segment calibration needs to be one by yourself.

One way of getting all of them into one coordinate system, which I do not recommend though, would be to use 9D motion tracking (magnetometer data) which should align all quaternions into the earths coordinate system. Hopefully this information will save time for some people who want to implement their own quaternion filter as this information is not explicitly given in the OpenSim documenation.

I recommend to check out the full body tracking examples of QMT tool or generally the work of Daniel Laidig.


Regards
Kevin

POST REPLY