Performing Inverse Dynamics via Matlab API

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
User avatar
jimmy d
Posts: 1375
Joined: Thu Oct 04, 2007 11:51 pm

Re: Performing Inverse Dynamics via Matlab API

Post by jimmy d » Sun Apr 27, 2014 10:24 pm

Hi Alice,

Its hard to tell without seeing the code but it sounds as if the IDTool isnt get updated correctly during each loop. So whats probably happening is that the wrong files are getting paired to run ID (kinematics and forces).

If you are still having an issues, perhaps posting the code that does the operation can help us see where an error may be?

cheers,
-james

User avatar
Alice Mantoan
Posts: 29
Joined: Fri Feb 24, 2012 11:51 am

Re: Performing Inverse Dynamics via Matlab API

Post by Alice Mantoan » Mon Apr 28, 2014 1:08 am

Hi James,

thanks for trying to help me.
Looking at the final setup files, it seems that the correct input files are loaded...
Sure I can post the code. Here it is:

Code: Select all

import org.opensim.modeling.*

% Apply the generic xml to the constructor
idTool = InverseDynamicsTool(genericSetupFileFullPath);

% Load the model and initialize
model = Model([inputDir '\' modelFileName]);
model.initSystem();

% Tell Tool to use the loaded model
idTool.setModel(model);

nTrials = size(motList,2);

% Loop through the trials
for trial= 1:nTrials;   
 
    % Get the name of the file for this trial
    motFile = motList{trial};
    
    % Create name of trial from .mot file name
    name = regexprep(motFile,'_ik.mot','');
    
    fullpath = ([inputDir '\' motFile]);

    % Get mot data to determine time range
    motData = Storage(fullpath);
    
    % Get initial and final time 
    initial_time = motData.getFirstTime();
    final_time = motData.getLastTime();    
    
    % Setup the idTool for this trial
    idTool.setName(name);
    idTool.setCoordinatesFileName(fullpath);
    idTool.setStartTime(initial_time);
    idTool.setEndTime(final_time);
    idTool.setOutputGenForceFileName([ 'inverse_dynamics_' name '.sto' ]);
    
    idTool.setResultsDir(outputDir);
    
    %External Load File
    % Create an external loads object
    extLoadsObject = ExternalLoads(model,genericExtLoadFullPath);
    
    fullpathGRFFile=[inputDir '\' name '.mot'];
    
    % Set the grf filename
    extLoadsObject.setDataFileName(fullpathGRFFile);
    
    % Set the model external loads file
    extLoadsObject.setExternalLoadsModelKinematicsFileName(fullpath);
    
    % Create name for a subject specific external loads xml
    extLoadsXml = [name '_' 'external_loads.xml'];
    
    % Print the external load xml file
    extLoadsObject.print([trialDir '\' extLoadsXml]);
        
    idTool.setExternalLoadsFileName([trialDir '\' extLoadsXml]);
    
    % Save the settings in a setup file
    outfile = [ 'setup_ID_' name '.xml' ];
    
    idTool.print([trialDir '\' outfile]);

    fprintf([ 'Performing ID on trial ' name '\n'])
      
    % Run ID
    idTool.run();
    
end

Thanks again.

Regards,

Alice

User avatar
Cosimo Aloisi
Posts: 11
Joined: Sun Feb 16, 2014 11:14 am

Re: Performing Inverse Dynamics via Matlab API

Post by Cosimo Aloisi » Thu May 08, 2014 9:41 am

Hi Alice,

looking at your final script about ID, I don't understand which parameters I must give to the tool "ExternalLoads" to let it working.
I'm referring especially to the input parameter 'genericExtLoadFullPath'!

Could you help me?
Thanks a lot.

Best regards,
Cosimo

User avatar
Alice Mantoan
Posts: 29
Joined: Fri Feb 24, 2012 11:51 am

Re: Performing Inverse Dynamics via Matlab API

Post by Alice Mantoan » Thu May 08, 2014 3:07 pm

Hi Cosimo,

yes, sure I can.
The genericExtLoadFullPath parameter is simply the path where a generic external load file is stored.
As for the setup file, a starting xml file for the configuration of external load is required. It must include the correct relation among the calcn striking each force platform and the corresponding grf according to your data.
The code just modifies the paths of the grf and ik result files to load to perform the id for different trials, as suggested by David.

Regards,

Alice

User avatar
Bradley Beck
Posts: 2
Joined: Thu Jul 14, 2016 5:09 pm

Re: Performing Inverse Dynamics via Matlab API

Post by Bradley Beck » Mon Oct 03, 2016 7:22 pm

Hello everbody,

I have been writing a code to batch process some files using Inverse Dynamics. I recently got the same error that Alice Mantoan had described, where data for her first file was correct but all others were returning incorrect values in the .sto file. Since this is the only mention of this error that I could find I figured I'd post my workaround for anybody else who encounters this.

Within the for loop that runs all the trials I created a new object from InverseDynamicsTool just after the xml file has been set and printed. This allowed me to use that file's absolute path as the input variable. An example using Alice's variable names would be something like:

idTool.print([trialDir '\' outfile]);
idRunner = InverseDynamicsTool([trialDir '\' outfile]);
idRunner.run();
% idTool.run() returned incorrect values, so no longer used

I don't understand well enough as to why this seems to work, but my guess is the initial object wasn't getting updated, so creating a new one each loop will fix that. Hope this works for others!

Bradley

User avatar
Enriqueta Dominguez
Posts: 2
Joined: Wed Oct 05, 2016 8:27 am

Re: Performing Inverse Dynamics via Matlab API

Post by Enriqueta Dominguez » Fri Jan 27, 2017 3:36 am

Hi all,

I am trying to solve the same problem: ID via Matlab API. I managed to compile the code and get the results of the generalized forces as expected.
The problem I have is that I get large forces in some columns of the results .sto file.
Does anybody have any idea of what is happening?
I wonder if when passing the files of the motion and external forces from Matlab, Opensim does not interpret the order of the columns in the same way as it does through the GUI and that is why the values are incorrect.
Thank you in advance.
Regards,
Enriqueta.

User avatar
Hossein Mokhtarzadeh
Posts: 37
Joined: Sun Dec 13, 2009 9:44 pm

Re: Performing Inverse Dynamics via Matlab API

Post by Hossein Mokhtarzadeh » Thu Mar 14, 2019 10:02 pm

Hi all,

I am pretty much following the same procedure in Matlab to do Inverse Dynamics using OpenSim 4.0.

Following the definition of GRF.xml so that I can change and set the external forces (i.e. GRFs.mot) i.e.

ExtLoads = ExternalLoads(model,[setupfiles_folder '\GRF.xml'])

Then I get this error below:
No constructor 'org.opensim.modeling.ExternalLoads' with matching signature found.

Has there been any changes in OpenSim 4.0?

This used to work for me when I was doing static optimisation. Or Am I missing anything. Any suggestion is appreciated.
Hossein

User avatar
Dimitar Stanev
Posts: 1096
Joined: Fri Jan 31, 2014 5:14 am

Re: Performing Inverse Dynamics via Matlab API

Post by Dimitar Stanev » Fri Mar 15, 2019 12:08 am

External loads does not accept a model as its first argument with OpenSim v4.0

https://simtk.org/api_docs/opensim/api_ ... Loads.html

User avatar
Hossein Mokhtarzadeh
Posts: 37
Joined: Sun Dec 13, 2009 9:44 pm

Re: Performing Inverse Dynamics via Matlab API

Post by Hossein Mokhtarzadeh » Sun Mar 17, 2019 4:11 pm

Thanks Dimitar for the information and the link. Now it works:

Here is what I did:
ExtLoads = ExternalLoads([setupfiles_folder '\GRF.xml'],1);
extloadpath = fullfile(GRF_results_folder, GRFFile);
ExtLoads.setDataFileName(extloadpath);
ExtLoads.print([setupfiles_folder '\GRF1.xml'])

Cheers,
Hossein

User avatar
Alex Peebles
Posts: 1
Joined: Thu Aug 06, 2020 11:13 am

Re: Performing Inverse Dynamics via Matlab API

Post by Alex Peebles » Fri Nov 20, 2020 2:39 pm

Hey Bradley,

I was having the same problem as you and Alice, where I was getting errors when batch processing using the InverseDynamicsTool. Your workaround works great, and I am extremely puzzled as to why. From what I could tell, then I was assigning the GRF file in the ExternalLoads class (using setDataFileName), it was updating the datafile field but not the data_source_name field within each ExternalForce object. Besides that, I have no idea why there would be an error in the for loop. Since posting this, did you figure out the bug?


Thanks,
Al

bradleybecknsw wrote:
Mon Oct 03, 2016 7:22 pm
Hello everbody,

I have been writing a code to batch process some files using Inverse Dynamics. I recently got the same error that Alice Mantoan had described, where data for her first file was correct but all others were returning incorrect values in the .sto file. Since this is the only mention of this error that I could find I figured I'd post my workaround for anybody else who encounters this.

Within the for loop that runs all the trials I created a new object from InverseDynamicsTool just after the xml file has been set and printed. This allowed me to use that file's absolute path as the input variable. An example using Alice's variable names would be something like:

idTool.print([trialDir '\' outfile]);
idRunner = InverseDynamicsTool([trialDir '\' outfile]);
idRunner.run();
% idTool.run() returned incorrect values, so no longer used

I don't understand well enough as to why this seems to work, but my guess is the initial object wasn't getting updated, so creating a new one each loop will fix that. Hope this works for others!

Bradley

POST REPLY