Calculating Segment Angular Velocities In the Global Coordinate System
Posted: Thu May 04, 2023 11:40 am
I have been trying to write a script that computes the angular velocities of all segments in my Opensim model in the global coordinate system using an existing scaled .osim model file and the joint angle results from the Inverse Kinematics Tool stored in a .mot file. I am currently using Opensim 4.4 in Python 3.8.16.
I have started by building a script using ChatGPT (as I'm not very familiar with the scripting tools in Opensim):
However, I have run into several errors with this script. I can bypass the error in the line by removing that error or replacing it with i/sampling_frequency. But, I then run into two errors on line : 1) AttributeError: module 'opensim' has no attribute 'TimeSeriesTableUtilities', and 2) 'Storage' object has no attribute 'getRowAtIndex'. I am now unsure of which attributes I should be using in order to return each segment's angular velocity in the global coordinate system.
Any feedback on how to modify the existing script, or perhaps use a different script entirely, would be much appreciated!
I have started by building a script using ChatGPT (as I'm not very familiar with the scripting tools in Opensim):
Code: Select all
import opensim as osim
# Load your OpenSim model
model = osim.Model("path/to/your/model.osim")
# Create a Storage object to read in the .mot file
storage = osim.Storage("path/to/your/model_outputs.mot")
# Get the number of bodies in the model
num_bodies = model.getNumBodies()
# Loop through each time step in the .mot file
for i in range(storage.getSize()):
# Set the model state to the current time step
time = storage.getTime(i)
state = model.initSystem()
osim.TimeSeriesTableUtilities.setAllValues(state, storage.getRowAtIndex(i))
# Loop through all bodies in the model and get their global angular velocities
for j in range(num_bodies):
body = model.getBodySet().get(j)
ang_vel = body.getAngularVelocityInGround(state)
print("Time = {}, Body {}: global angular velocity = {}".format(time, j+1, ang_vel))
Code: Select all
time = storage.getTime(i)
Code: Select all
osim.TimeSeriesTableUtilities.setAllValues(state, storage.getRowAtIndex(i))
Any feedback on how to modify the existing script, or perhaps use a different script entirely, would be much appreciated!