Page 3 of 4

Re: Performing Inverse Dynamics via Matlab API

Posted: Sun Apr 27, 2014 10:24 pm
by jimmy
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

Re: Performing Inverse Dynamics via Matlab API

Posted: Mon Apr 28, 2014 1:08 am
by mantoana
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

Re: Performing Inverse Dynamics via Matlab API

Posted: Thu May 08, 2014 9:41 am
by cosimo_989
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

Re: Performing Inverse Dynamics via Matlab API

Posted: Thu May 08, 2014 3:07 pm
by mantoana
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

Re: Performing Inverse Dynamics via Matlab API

Posted: Mon Oct 03, 2016 7:22 pm
by bradleybecknsw
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

Re: Performing Inverse Dynamics via Matlab API

Posted: Fri Jan 27, 2017 3:36 am
by enrdomcor
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.

Re: Performing Inverse Dynamics via Matlab API

Posted: Thu Mar 14, 2019 10:02 pm
by hossein
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

Re: Performing Inverse Dynamics via Matlab API

Posted: Fri Mar 15, 2019 12:08 am
by mitkof6
External loads does not accept a model as its first argument with OpenSim v4.0

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

Re: Performing Inverse Dynamics via Matlab API

Posted: Sun Mar 17, 2019 4:11 pm
by hossein
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

Re: Performing Inverse Dynamics via Matlab API

Posted: Fri Nov 20, 2020 2:39 pm
by apeebles
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