Page 1 of 1
.mot in API Visualizer
Posted: Fri Mar 18, 2016 10:53 am
by deenaalabed
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?
Re: .mot in API Visualizer
Posted: Mon Sep 12, 2016 1:49 pm
by djdameln
I would also like to know this
Re: .mot in API Visualizer
Posted: Wed Jan 29, 2020 7:17 am
by idhamari
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
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
}
Re: .mot in API Visualizer
Posted: Wed Jan 29, 2020 11:53 am
by aymanh
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
Re: .mot in API Visualizer
Posted: Wed Jan 29, 2020 3:39 pm
by idhamari
Thanks for the information. I already tried different codes from this forum but nothing st e.g.
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);
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.
Re: .mot in API Visualizer
Posted: Thu Jan 30, 2020 12:31 am
by aymanh
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
Re: .mot in API Visualizer
Posted: Thu Jan 30, 2020 1:29 pm
by idhamari
Thanks for the info,
or that the file contains the full path name of the coordinate state variable (/ separated) while the method expects just the name,
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:
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
Re: .mot in API Visualizer
Posted: Thu Jan 30, 2020 4:21 pm
by aymanh
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