.mot in API Visualizer
- Deena Alabed
- Posts: 1
- Joined: Fri Dec 04, 2015 4:24 am
.mot in API Visualizer
Hi,
I have a hand model and its corresponding .mot file and I would like to run the simulation using API Visualizer through MATLAB.
I can see the model in the visualizer using this osimModel.getVisualizer.show(state) but I want it to move based on the motion file as it would in the GUI.
Is that possible?
I have a hand model and its corresponding .mot file and I would like to run the simulation using API Visualizer through MATLAB.
I can see the model in the visualizer using this osimModel.getVisualizer.show(state) but I want it to move based on the motion file as it would in the GUI.
Is that possible?
- Dick Ameln
- Posts: 4
- Joined: Thu Feb 20, 2014 9:15 am
Re: .mot in API Visualizer
I would also like to know this
- Ibraheem Al-Dhamari
- Posts: 35
- Joined: Thu Mar 16, 2017 2:44 am
Re: .mot in API Visualizer
This is an old post but I like to share an updated example for the sake of increasing forum knowledge-base
. Here is a complete example using cpp, there are some posts for similar matlab code
![Very Happy :D](./images/smilies/icon_e_biggrin.gif)
Code: Select all
#include <OpenSim/OpenSim.h>
#include "OpenSim/Common/STOFileAdapter.h"
#include <time.h>
#include <string>
#include <sstream>
using namespace OpenSim;
using namespace std;
int main(int argc, char* argv[]) {
std::cout<<"=============================================="<<std::endl;
std::cout<<"= OpenSim Visualizer ="<<std::endl;
std::cout<<"=============================================="<<std::endl;
double toRad = (22. / 7.0) / 180.0 ; // PI/180
string inputModelPath = "../data/model.osim" ;
string inputModelMotionPath = "../data/motion.mot" ;
string inputModelGeometryPath = "../data/Geom";
Model inputModel(inputModelPath) ;
inputModel.setUseVisualizer(true);
inputModel.finalizeConnections();
ModelVisualizer::addDirToGeometrySearchPaths(inputModelGeometryPath);
SimTK::State& si = inputModel.initSystem();
SimTK::Visualizer viz = inputModel.updVisualizer().updSimbodyVisualizer();
viz.setShowSimTime(true);
viz.setShowFrameNumber(true);
viz.setBackgroundType(SimTK::Visualizer::GroundAndSky);
Storage inputMotion(inputModelMotionPath);
double coordValue, stateIndex;
int numCoords = inputModel.getNumCoordinates();
SimTK::Vector q (numCoords);
for (int t = 0; t < inputMotion.getSize()-1; t++) {
inputModelStateVector = inputMotion.getStateVector(t)->getData();
for (int i = 0; i < numCoords - 1; i++) {
coordValue=inputModelStateVector.get(i) * toRad;
inputModel.updCoordinateSet().get(i).setValue(si,coordValue);
}//endfor
inputModel.realizePosition(si);
inputModel.getVisualizer().show(si);
//TODO: add frame dely as an argument
this_thread::sleep_for(chrono::milliseconds(50));
}//endfor
viz.shutdown(); // does not work, we have to close the window manually
}
- Ayman Habib
- Posts: 2239
- Joined: Fri Apr 01, 2005 12:24 pm
Re: .mot in API Visualizer
Thanks for sharing, Ibraheem, much appreciated.
I'd just be careful setting values directly from a file to model coordinates just based on order rather than name/more elaborate mapping, but otherwise looks good.
Best regards,
-Ayman
I'd just be careful setting values directly from a file to model coordinates just based on order rather than name/more elaborate mapping, but otherwise looks good.
Best regards,
-Ayman
- Ibraheem Al-Dhamari
- Posts: 35
- Joined: Thu Mar 16, 2017 2:44 am
Re: .mot in API Visualizer
Thanks for the information. I already tried different codes from this forum but nothing st e.g.
I get the correct coordinate name but the state index value is -1 which produces a runtime error "array out of bounds" when trying to get coordValue.
Code: Select all
// t loop through storage, i loop through coordinates
currentCoord = inputModel.getCoordinateSet().get(i);
coordName = currentCoord.getName();
stateIndex = inputMotion.getStateIndex(coordName) ;
coordValue = inputMotion.getStateVector(t)->getData().get(stateIndex);
- Ayman Habib
- Posts: 2239
- Joined: Fri Apr 01, 2005 12:24 pm
Re: .mot in API Visualizer
Hello,
An index of -1 indicates the name not found, could be that the motion file does not contain all coordinates or that the file contains the full path name of the coordinate state variable (/ separated) while the method expects just the name, may help if you print out which names are not found.
Best regards,
-Ayman
An index of -1 indicates the name not found, could be that the motion file does not contain all coordinates or that the file contains the full path name of the coordinate state variable (/ separated) while the method expects just the name, may help if you print out which names are not found.
Best regards,
-Ayman
- Ibraheem Al-Dhamari
- Posts: 35
- Joined: Thu Mar 16, 2017 2:44 am
Re: .mot in API Visualizer
Thanks for the info,
to
but I still get the white screen.
Here is the code:
Probably this is the reason. The file are generated by the gui simulate button. When I run the code bellow I get white screen, I changed the labels from:or that the file contains the full path name of the coordinate state variable (/ separated) while the method expects just the name,
Code: Select all
time /jointset/pin1/q1/value /jointset/pin1/q1/speed /jointset/pin2/q2/value /jointset/pin2/q2/speed
to
Code: Select all
time q1value q1speed q2value q2speed
but I still get the white screen.
Here is the code:
Code: Select all
double coordValue, stateIndex;
int numCoords = inputModel.getNumCoordinates();
string coordName ;
for (int t = 0; t < inputMotion.getSize()-1; t++) {
inputModelStateVector = inputMotion.getStateVector(t)->getData();
for (int i = 0; i < numCoords - 1; i++) {
coordName = inputModel.getCoordinateSet().get(i).getName();
stateIndex = inputMotion.getStateIndex(coordName) ;
coordValue = inputMotion.getStateVector(t)->getData().get(stateIndex)* toRad;
inputModel.updCoordinateSet().get(i).setValue(si,coordValue);
}//endfor
inputModel.realizePosition(si);
inputModel.getVisualizer().show(si);
//TODO: add frame dely as argument
this_thread::sleep_for(chrono::milliseconds(50));
}//endfor
- Ayman Habib
- Posts: 2239
- Joined: Fri Apr 01, 2005 12:24 pm
Re: .mot in API Visualizer
Hello,
You're better off, leaving the file alone and instead asking the Coordinates for their then append "/Value", and look the resulting string up in the file headers/labels. You'll also need to:
- account for missing coordinates since there's no guarantee that all coordinates will be in the file.
- may need to convert degrees to radians since the coordinates expect values in radians for pure rotations, while files can be in either. File header typically contains this info.
Hope this helps,
-Ayman
You're better off, leaving the file alone and instead asking the Coordinates for their
Code: Select all
getAbsolutePathString()
- account for missing coordinates since there's no guarantee that all coordinates will be in the file.
- may need to convert degrees to radians since the coordinates expect values in radians for pure rotations, while files can be in either. File header typically contains this info.
Hope this helps,
-Ayman