Page 1 of 1

Minimal example for forward dynamics in python [SOLVED]

Posted: Tue Oct 23, 2018 12:45 pm
by pariterre
Hello,

I am struggling to get the the joint torques (or joint accelerations) from activated muscles. If someone can help me with a minimalist code, that would be appreciated!
So far I came up with this (based on StaticOptimizationTarget.cpp computeAcceleration function

Code: Select all

import numpy as np

import opensim as osim

subject = "mars"
pyosim_example_path = "/home/pariterre/Documents/Laboratoire/Programmation/pyosim/examples"
osim_model = "wu_scaled.osim"

Q = np.ones([19]) *2
QDot = np.ones([19])*10
muscle_excitation = np.zeros([29]) + 1.1


# Load the model
model_path = f"{pyosim_example_path}/results/{subject}/_models/{osim_model}"

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

# Update kinematic states
for i in range(state.getQ().size()):
    state.getQ().set(i, Q[i])  # Q From inverse kinematics
    state.getQDot().set(i, QDot[i])  # QDot from inverse kinematics
model.realizeVelocity(state)

# Update muscle states
for i in range(model.getMuscles().getSize()):
    model.getMuscles().get(i).setActivation(state, muscle_excitation[i])  # Set to desired muscle excitation
model.equilibrateMuscles(state)


# DOESN'T WORK FROM HERE
# This seems to be the key, but is not interfaced in python
udot = model.getMatterSubsystem().getUDot(state)  # ERROR

# Another idea that seems promizing is:
udot = osim.Vector()
A_GB = osim.VectorOfSpatialVec()
udot = model.getMatterSubsystem().calcAcceleration(state, appliedMobilityForces, appliedBodyForces, udot, A_GB) # ERROR
# Because I don't know how to get appliedForces (even if I don't have external for now)
Thanks!

Re: Minimal example for forward dynamics in python

Posted: Tue Oct 23, 2018 2:36 pm
by chrisdembia
Have you seen the Python examples? https://github.com/opensim-org/opensim- ... n/examples

By joint torques and joint accelerations, do you mean net joint moments and generalized accelerations?

For generalized accelerations:

model.realizeAcceleration(state)
state.getUDot()

Re: Minimal example for forward dynamics in python

Posted: Tue Oct 23, 2018 2:52 pm
by pariterre
Hello!

Yep, we just figured out that it was updated in the state variable.

Just to make sure I fully understand this variable, this UDot is the generalized accelerations computed from muscle (meaning: it takes excitation/activation set with the 'setActivation' method from getMuscles().get(x), then converted into muscle forces [probably something like activation*max_force], which are afterward used to compute generalized joint torques and finally converted to generalized joint accelerations)?

If so, this is indeed what I need :)

Re: Minimal example for forward dynamics in python

Posted: Tue Oct 23, 2018 3:46 pm
by chrisdembia
Yes. Note that at a given time point, activation directly affects muscle force but excitation does not. Excitation only affects muscle force through the forward integration of the dynamics.