Visualizer in Matlab

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
richard hu
Posts: 1
Joined: Mon Apr 03, 2017 10:56 pm

Visualizer in Matlab

Post by richard hu » Sun Apr 09, 2017 5:57 am

Hi,

I create a hand model and its .mot file and I would like to run the simulation using API Visualizer through Matlab . But how can I make it move based on the motion file as it would in the GUI.

User avatar
Thomas Uchida
Posts: 1792
Joined: Wed May 16, 2012 11:40 am

Re: Visualizer in Matlab

Post by Thomas Uchida » Mon Apr 10, 2017 12:24 pm

There may be an easier way, but something like this should work:

Code: Select all

import org.opensim.modeling.*;

model = Model('myModel.osim');
model.setUseVisualizer(true);
state = model.initSystem();

storage = Storage('results.sto');

numCoords = model.getNumCoordinates();
q = Vector(numCoords, 0);

for i = 0:storage.getSize()-1
    dat = storage.getStateVector(i).getData();
    for j = 0:numCoords-1
        q.set(j, dat.get(j));
    end
    state.setQ(q);
    
    model.getVisualizer().show(state);
end
Note that this code will display every frame (row) in the .sto file, so the animation will slow down when the integrator takes more time steps. You can skip rows or interpolate if that is an issue.

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

Re: Visualizer in Matlab

Post by Mélissandre Asfazadourian » Mon Jul 22, 2019 2:21 am

tkuchida wrote:
Mon Apr 10, 2017 12:24 pm
There may be an easier way, but something like this should work:

Code: Select all

import org.opensim.modeling.*;

model = Model('myModel.osim');
model.setUseVisualizer(true);
state = model.initSystem();

storage = Storage('results.sto');

numCoords = model.getNumCoordinates();
q = Vector(numCoords, 0);

for i = 0:storage.getSize()-1
    dat = storage.getStateVector(i).getData();
    for j = 0:numCoords-1
        q.set(j, dat.get(j));
    end
    state.setQ(q);
    
    model.getVisualizer().show(state);
end
Note that this code will display every frame (row) in the .sto file, so the animation will slow down when the integrator takes more time steps. You can skip rows or interpolate if that is an issue.
Hi there!
I tried the code you suggested adapted in python but it does not seem to work, I can see my model move in the visualizer window but the motion is not right and I don't know why because the code makes sense.

User avatar
Thomas Uchida
Posts: 1792
Joined: Wed May 16, 2012 11:40 am

Re: Visualizer in Matlab

Post by Thomas Uchida » Mon Jul 22, 2019 9:19 am

the motion is not right
In what way?

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

Re: Visualizer in Matlab

Post by Mélissandre Asfazadourian » Mon Jul 22, 2019 10:08 am

I have a mot file with inverse kinematics results of gait. My model is supposed to walk, but the motion is a complete no sense, bodies are moving really weirdly, it's not even close from what we should expect... The bodies seem entangled during the motion. Maybe the angles are not set to the right coordinates at each step?

User avatar
jimmy d
Posts: 1375
Joined: Thu Oct 04, 2007 11:51 pm

Re: Visualizer in Matlab

Post by jimmy d » Mon Jul 22, 2019 10:18 am

IK reports rotational coordinates in degrees, but when you set states they have to be in Radians. You will have to convert Rotational coordinates into radians.

A previous post with a similiar issue is here

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

Re: Visualizer in Matlab

Post by Mélissandre Asfazadourian » Tue Jul 23, 2019 12:56 am

It was exactly why it was not working ! Thanks a lot for your help :D

I have changed the code after reading another topic, so for those who want to "animate" their model from IK results in the visualizer here it is:

Code: Select all

modele=opensim.Model('modele.osim')
modele.setUseVisualizer(True)
state=modele.initSystem()
IK=osim.Storage(common.path('data','test.mot'))

for t in range(IK.getSize()):
        for i in range(modele.getNumCoordinates()):
            n = modele.getCoordinateSet().get(i).getName()
            j =IK.getStateIndex(n)
            v = IK.getStateVector(t).getData().get(j)
            modele.updCoordinateSet().get(i).setValue(state,radians(v))
            modele.realizePosition(state)
        modele.getVisualizer().show(state)

POST REPLY