Page 1 of 1
Visualizer in Matlab
Posted: Sun Apr 09, 2017 5:57 am
by scutrichardhu
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.
Re: Visualizer in Matlab
Posted: Mon Apr 10, 2017 12:24 pm
by tkuchida
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.
Re: Visualizer in Matlab
Posted: Mon Jul 22, 2019 2:21 am
by mel_asfa
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.
Re: Visualizer in Matlab
Posted: Mon Jul 22, 2019 9:19 am
by tkuchida
the motion is not right
In what way?
Re: Visualizer in Matlab
Posted: Mon Jul 22, 2019 10:08 am
by mel_asfa
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?
Re: Visualizer in Matlab
Posted: Mon Jul 22, 2019 10:18 am
by jimmy
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
Re: Visualizer in Matlab
Posted: Tue Jul 23, 2019 12:56 am
by mel_asfa
It was exactly why it was not working ! Thanks a lot for your help
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)