Page 1 of 1

MATLAB Forward Dynamics Tool Setup File Input

Posted: Sun May 27, 2018 10:43 pm
by gy89
Hello all,

I'm not quite sure how to input a forward dynamics setup file or EMG input file to a forward dynamics object in MATLAB. Using the excellent MoBL-ARMS project <https://simtk.org/projects/upexdyn>, as described in their documentation I've used the model MoBL_ARMS_module2_4_allmuscles.osim, forward dynamics setup file MoBL_ARMS_module4_Setup_wrist.xml, and EMG input controls_EMG_wrist.sto to produce a file MoBL_ARMS_module2_4_allmuscles_wrist_states_degrees.mot in the OpenSim GUI. However, I can't reproduce this in MATLAB.
Here is a small MATLAB code snippet of what I've tried ; files are attached:

Code: Select all

import org.opensim.modeling.*

% model
model = Model('MoBL_ARMS_module2_4_allmuscles.osim'); 

% forward dynamics
genericSetupForFD = 'MoBL_ARMS_module4_Setup_wrist.xml';
ft = ForwardTool(genericSetupForFD);
ft.setModel(model);
%ft.setInitialTime(0.); 
%ft.setFinalTime(1.); 
ft.run();
If I uncomment the lines 'ft.setInitialTime(0.);' and 'ft.setFinalTime(1.);', the simulation runs for hours. I've never let it 'finish' and produce files because I don't think it would finish. With these lines commented out, if I run the above program it produces a _states_degrees.mot file which I know isn't correct because if I load it in the OpenSim GUI with the model, the movement is unnatural and certainly not like the MoBL_ARMS_module2_4_allmuscles_wrist_states_degrees.mot file produced through forward dynamics using the GUI.

Any help would be greatly appreciated! Thank you very much!

Re: MATLAB Forward Dynamics Tool Setup File Input

Posted: Mon May 28, 2018 3:24 am
by tkuchida
I got identical results in the GUI and MATLAB, using the following steps:

GUI
- open MoBL_ARMS_module2_4_allmuscles.osim
- Tools > Forward Dynamics...
- load MoBL_ARMS_module4_Setup_wrist.xml
- change final time to 0.05 seconds (for a quick test)
- Run

MATLAB

Code: Select all

% OpenSim 3.3
import org.opensim.modeling.*
model = Model('MoBL_ARMS_module2_4_allmuscles.osim');
ft = ForwardTool('MoBL_ARMS_module4_Setup_wrist-2.xml'); %changed final_time to 0.05
ft.setModel(model);
ft.run();

Re: MATLAB Forward Dynamics Tool Setup File Input

Posted: Sat Jun 16, 2018 12:52 pm
by gy89
Hello Mr. Uchida,

You're quite correct. I was able to reproduce GUI results in MATLAB.

As a note to future users, there are 2 possible ways to reproduce using an OpenSim tool in MATLAB after setting default values of/locking coordinates (and neither involve using an initialStates.sto or .mot file in the associated setup .xml). As per obtaining a list of methods for the org.opensim.modeling.Model object (type 'methods org.opensim.modeling.Model;' in the MATLAB command window after configuring OpenSim-MATLAB use and other installations), it seems the Model object has certain methods such as model.getConstraintSet(), but with no API I didn't know what parameters each method expected, and I wasn't able to effectively use MATLAB to set default values of or lock coordinates, if possible in the first place.
However, you may set default values of/lock coordinates in MATLAB through editing the model file .osim (see the first block of code), instantiating an OpenSim model in MATLAB, performing any other necessary actions before running the tool (such as adding an analysis or setup files), and running the tool (see the second block of code).

In the .osim,
change the default_value as necessary; be sure to set the coordinate in radians if editing a rotational coordinate.
change <clamped>false</clamped> to true (at least, that's what I did; I see no reason to have a <range> if the coordinate's values may leave the range (that is, the coordinate's values aren't <clamped>)).
change <locked>false</locked> to true if the joint would be locked if you're using the GUI. (Locked joints cannot change their value, and the simulation typically adjusts other model values to compensate.)

Code: Select all

<Coordinate name="deviation">
	<!--Coordinate can describe rotational, translational, or coupled motion. Defaults to rotational.-->
	<motion_type>rotational</motion_type>
	<!--The value of this coordinate before any value has been set. Rotational coordinate value is in radians and Translational in meters.-->
	<default_value>0.1857030324</default_value>
	<!--The speed value of this coordinate before any value has been set. Rotational coordinate value is in rad/s and Translational in m/s.-->
	<default_speed_value>0</default_speed_value>
	<!--The minimum and maximum values that the coordinate can range between. Rotational coordinate range in radians and Translational in meters.-->
	<range>-0.17453293 0.43633231</range>
	<!--Flag indicating whether or not the values of the coordinates should be limited to the range, above.-->
	<clamped>true</clamped>
	<!--Flag indicating whether or not the values of the coordinates should be constrained to the current (e.g. default) value, above.-->
	<locked>false</locked>
	<!--If specified, the coordinate can be prescribed by a function of time. It can be any OpenSim Function with valid second order derivatives.-->
	<prescribed_function />
	<!--Flag indicating whether or not the values of the coordinates should be prescribed according to the function above. It is ignored if the no prescribed function is specified.-->
	<prescribed>false</prescribed>
</Coordinate>
In MATLAB,

Code: Select all

import org.opensim.modeling.*
model = Model('modelFile.osim');
ft = ForwardTool('forwardDynamicsSetup.xml');
ft.setModel(model);
ft.run();
I hope the post helps,

Stephanie Hachem

Re: MATLAB Forward Dynamics Tool Setup File Input

Posted: Sun Jun 17, 2018 2:46 pm
by tkuchida
it seems the Model object has certain methods such as model.getConstraintSet(), but with no API I didn't know what parameters each method expected
Information about the MATLAB interface can be found on the "Scripting with Matlab" page in the Confluence documentation (https://simtk-confluence.stanford.edu/d ... ith+Matlab). The API documentation can be found here: https://simtk.org/api_docs/opensim/api_docs/. These links can also be found in the GUI's Help menu.

Re: MATLAB Forward Dynamics Tool Setup File Input

Posted: Sun Jun 17, 2018 3:13 pm
by gy89
Hello Mr. Uchida,

The API link provided doesn't mention MATLAB, based on searching the API in the site's search bar, as well as based on searching the site for the word 'MATLAB' through google (google 'site:https://simtk.org/api_docs/opensim/api_docs/ matlab').

The other link only provides the MATLAB commands of 'methods model' and 'methodsview model', which I used. However, that site doesn't provide any documentation (a list of classes, methods, and so forth, with corresponding descriptions) of OpenSim functions implemented in MATLAB. Examples of using OpenSim functions in MATLAB are provided in certain OpenSim versions, which were also helpful.

Thank you for the replies, as they were very helpful,

Stephanie Hachem

Re: MATLAB Forward Dynamics Tool Setup File Input

Posted: Sun Jun 17, 2018 4:27 pm
by tkuchida
The API link provided doesn't mention MATLAB... The other link only provides the MATLAB commands of 'methods model' and 'methodsview model', which I used. However, that site doesn't provide any documentation (a list of classes, methods, and so forth, with corresponding descriptions) of OpenSim functions implemented in MATLAB.
A good place to start is the "Common Scripting Commands" page (the first page in the "Scripting" section of the documentation; https://simtk-confluence.stanford.edu/d ... g+Commands), which provides additional background information, "Beginner Scripting Resources", links to specific pages in the API documentation, example code, etc. Some of the examples use MATLAB as well (see, e.g., "Dynamic Walking Challenge: Go the Distance!" https://simtk-confluence.stanford.edu/p ... Id=5113821).

Re: MATLAB Forward Dynamics Tool Setup File Input

Posted: Mon Jun 18, 2018 4:22 pm
by gy89
Hello Mr. Uchida,

The webpage is really helpful. Thank you!

Stephanie