.mot in API Visualizer

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Deena Alabed
Posts: 1
Joined: Fri Dec 04, 2015 4:24 am

.mot in API Visualizer

Post by Deena Alabed » Fri Mar 18, 2016 10:53 am

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?

User avatar
Dick Ameln
Posts: 4
Joined: Thu Feb 20, 2014 9:15 am

Re: .mot in API Visualizer

Post by Dick Ameln » Mon Sep 12, 2016 1:49 pm

I would also like to know this

User avatar
Ibraheem Al-Dhamari
Posts: 35
Joined: Thu Mar 16, 2017 2:44 am

Re: .mot in API Visualizer

Post by Ibraheem Al-Dhamari » Wed Jan 29, 2020 7:17 am

This is an old post but I like to share an updated example for the sake of increasing forum knowledge-base :D . 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
}

User avatar
Ayman Habib
Posts: 2238
Joined: Fri Apr 01, 2005 12:24 pm

Re: .mot in API Visualizer

Post by Ayman Habib » Wed Jan 29, 2020 11:53 am

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

User avatar
Ibraheem Al-Dhamari
Posts: 35
Joined: Thu Mar 16, 2017 2:44 am

Re: .mot in API Visualizer

Post by Ibraheem Al-Dhamari » Wed Jan 29, 2020 3:39 pm

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.

User avatar
Ayman Habib
Posts: 2238
Joined: Fri Apr 01, 2005 12:24 pm

Re: .mot in API Visualizer

Post by Ayman Habib » Thu Jan 30, 2020 12:31 am

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

User avatar
Ibraheem Al-Dhamari
Posts: 35
Joined: Thu Mar 16, 2017 2:44 am

Re: .mot in API Visualizer

Post by Ibraheem Al-Dhamari » Thu Jan 30, 2020 1:29 pm

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

User avatar
Ayman Habib
Posts: 2238
Joined: Fri Apr 01, 2005 12:24 pm

Re: .mot in API Visualizer

Post by Ayman Habib » Thu Jan 30, 2020 4:21 pm

Hello,

You're better off, leaving the file alone and instead asking the Coordinates for their

Code: Select all

getAbsolutePathString()
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

POST REPLY