Kinematics in python

The 2018 Fall Virtual Workshop will bring together a group of international scholars and OpenSim experts to help each other advance their research using modeling and simulation. This forum is a place to accelerate OpenSim based projects while also buildin
POST REPLY
User avatar
Najoua Assila
Posts: 34
Joined: Thu Aug 30, 2018 9:24 am

Kinematics in python

Post by Najoua Assila » Wed Oct 24, 2018 12:45 pm

Hello,

I am new to using API commands in Python. I am trying to use the kinematics class to find the joints angular velocities, however I am having some difficulties to implement it in python?
I would appreciate if you have some hints or code examples.

Thanks,

User avatar
Benjamin Michaud
Posts: 31
Joined: Mon May 03, 2010 6:35 am

Re: Kinematics in python

Post by Benjamin Michaud » Wed Oct 24, 2018 1:22 pm

Hello,

We have the same problem, from this piece of code we get up to loading coordinate states from a file, but we can't figure out how to derivate it properly

Code: Select all

model = osim.Model(model_path)
state = model.initSystem()
working_state = model.updWorkingState()

# Read the data
data_storage = osim.Storage(Qfile)

for irow in range(data_storage.getSize()):
    # Store Q in state
    state.setTime(data_storage.getStateVector(irow).getTime())
    state.setQ(data_storage.getStateVector(irow).getData().getAsVector())
    model.computeStateVariableDerivatives(state)
    QDot = state.getQDot()
    print(QDot.toString())
This code returns vectors of zeros for QDot

Thanks for your help!

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: Kinematics in python

Post by Christopher Dembia » Wed Oct 24, 2018 2:14 pm

The columns in the Storage file may not be in the same order as the coordinates in the state's Q.

User avatar
Benjamin Michaud
Posts: 31
Joined: Mon May 03, 2010 6:35 am

Re: Kinematics in python

Post by Benjamin Michaud » Wed Oct 24, 2018 2:17 pm

Hi Najoua,

We have just figure out how!
So after loading a mot file, you have to compute spline for each coordinate, idealy filter it and then evaluate values at each time. That would look something like this :

Code: Select all

# Prepare a model
model = osim.Model(model_path)
state = model.initSystem()
working_state = model.updWorkingState()

# Read the data
data_storage = osim.Storage(Qfile)
model.getSimbodyEngine().convertDegreesToRadians(data_storage)
data_storage.lowpassIIR(10)
coord_function = osim.GCVSplineSet(5, data_storage)

# At each frame
for i in range(data_storage.getSize()):
Q = []
    QDot = []
    QDDot = []
    for q in range(data_storage.getStateVector(i).getSize()):
        Q.append(coord_function.evaluate(q, 0, data_storage.getStateVector(i).getTime()))
        QDot.append(coord_function.evaluate(q, 1, data_storage.getStateVector(i).getTime()))
        QDDot.append(coord_function.evaluate(q, 2, data_storage.getStateVector(i).getTime()))
Regards

POST REPLY