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.
Visualizer in Matlab
- Thomas Uchida
- Posts: 1792
- Joined: Wed May 16, 2012 11:40 am
Re: Visualizer in Matlab
There may be an easier way, but something like this should work:
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.
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
- Mélissandre Asfazadourian
- Posts: 6
- Joined: Tue Jun 11, 2019 2:13 am
Re: Visualizer in Matlab
Hi there!tkuchida wrote: ↑Mon Apr 10, 2017 12:24 pmThere may be an easier way, but something like this should work: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.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
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.
- Thomas Uchida
- Posts: 1792
- Joined: Wed May 16, 2012 11:40 am
Re: Visualizer in Matlab
In what way?the motion is not right
- Mélissandre Asfazadourian
- Posts: 6
- Joined: Tue Jun 11, 2019 2:13 am
Re: Visualizer in Matlab
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
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
A previous post with a similiar issue is here
- Mélissandre Asfazadourian
- Posts: 6
- Joined: Tue Jun 11, 2019 2:13 am
Re: Visualizer in Matlab
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:
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)