API: Loading model states from an Inverse Kinematics motion file

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Navid Roux
Posts: 4
Joined: Wed May 23, 2018 5:25 am

API: Loading model states from an Inverse Kinematics motion file

Post by Navid Roux » Wed Jun 20, 2018 9:25 am

I have an OpenSim model file and an accompanying motion file from a prior inverse kinematics calculation. Now I would like to programmatically "animate" the model such that I get access to transformations for every body in the bodyset in every time step.

Unfortunately, I fail to see how to link Model, Storage and State instances API-wise. My code can already output the transformation for the initial state, but how do I load a new state from a specific row of the motion file?

I already browsed the API documentation several times, but all I could find was a way to manually set the coordintes as per one of the Python bindings examples:
model.setStateVariableValue(state, 'joint/q/value', 0.25 * math.pi)

Surely, there must be a better and more automatic way, I think. OpenSim's GUI also loads motion files, hence I suppose this logic has already been implemented, but I was only unable to find it.

Code: Select all

import opensim as osim

model = osim.Model("model.osim")
model.buildSystem()
state = model.initializeState()

motion_storage = osim.Storage("ik.mot")

# Acquiring the transformation for a specific body in the bodyset already works
model.getBodySet().get(0).getTransformInGround(state)
Thanks in advance!

Tags:

User avatar
Hide Kimpara
Posts: 135
Joined: Mon Sep 19, 2016 5:12 am

Re: API: Loading model states from an Inverse Kinematics motion file

Post by Hide Kimpara » Fri Jun 22, 2018 8:37 pm

Hopefully, following example based on MATLAB may give you some hints:

Code: Select all

osimModel = Model('model.osim');
osimState = osimModel.initSystem();

% Read IK post data
Des_StoreData = Storage(IK_output_file);

Des_Time = ArrayDouble();
Des_Size = Des_StoreData.getTimeColumn (Des_Time);

for i = 1:num_Coordinates
    coordvalue = ArrayDouble();
    Des_StoreData.getDataColumn(char(Coordinates_Name{i,1}),coordvalue);
    coordvect = coordvalue.getAsVector;

    motType = model.getCoordinateSet().get(Coordinates_Name{i,1}).getMotionType();
    
    % Check unit for "Rotational" or "Coupled" coordinate in your case: rad or degree
    if strcmp(motType,'Rotational')
        Des_Data_Array(i,:) = deg2rad(osimVectorToArray(coordvect));
    else
        Des_Data_Array(i,:) = osimVectorToArray(coordvect);
    end
end

t = target_time;
for j = 1:num_Coordinates
	osimModel.updCoordinateSet.get(Coordinates_Name{j,1}).setValue(osimState,Des_Data_Array(j,t));
end


User avatar
Mélissandre Asfazadourian
Posts: 6
Joined: Tue Jun 11, 2019 2:13 am

Re: API: Loading model states from an Inverse Kinematics motion file

Post by Mélissandre Asfazadourian » Fri Jul 19, 2019 5:50 am

Hey Navid,

Did you manage to animate your model with your IK results ? I'm trying to do it, a little help would be great :D
Thanks in advance

POST REPLY