Page 1 of 1

Kinematics in python

Posted: Wed Oct 24, 2018 12:45 pm
by nassila
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,

Re: Kinematics in python

Posted: Wed Oct 24, 2018 1:22 pm
by pariterre
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!

Re: Kinematics in python

Posted: Wed Oct 24, 2018 2:14 pm
by chrisdembia
The columns in the Storage file may not be in the same order as the coordinates in the state's Q.

Re: Kinematics in python

Posted: Wed Oct 24, 2018 2:17 pm
by pariterre
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