Minimal example for forward dynamics in python [SOLVED]
Posted: Tue Oct 23, 2018 12:45 pm
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
Thanks!
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)