MATLAB Forward Dynamics Tool Setup File Input

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Stephanie Hachem
Posts: 34
Joined: Wed Oct 26, 2016 12:40 pm

MATLAB Forward Dynamics Tool Setup File Input

Post by Stephanie Hachem » Sun May 27, 2018 10:43 pm

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!
Attachments
MoBL_ARMS_module4_Setup_wrist.xml
(3.64 KiB) Downloaded 38 times
MoBL_ARMS_module2_4_allmuscles.osim
(546.64 KiB) Downloaded 29 times
MoBL_ARMS_module2_4_allmuscles_wrist_states_degrees.mot
Originally a lot larger, but forum said it was too large so I took a small part of the file.
(16.2 KiB) Downloaded 29 times
controls_EMG_wrist.mot
Originally .sto, but forum said .sto was an invalid file extension.
(232.53 KiB) Downloaded 28 times
_states_degrees.mot
Originally a lot larger, but forum said it was too large so I took a small part of the file.
(3.72 KiB) Downloaded 22 times

Tags:

User avatar
Thomas Uchida
Posts: 1793
Joined: Wed May 16, 2012 11:40 am

Re: MATLAB Forward Dynamics Tool Setup File Input

Post by Thomas Uchida » Mon May 28, 2018 3:24 am

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

User avatar
Stephanie Hachem
Posts: 34
Joined: Wed Oct 26, 2016 12:40 pm

Re: MATLAB Forward Dynamics Tool Setup File Input

Post by Stephanie Hachem » Sat Jun 16, 2018 12:52 pm

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

User avatar
Thomas Uchida
Posts: 1793
Joined: Wed May 16, 2012 11:40 am

Re: MATLAB Forward Dynamics Tool Setup File Input

Post by Thomas Uchida » Sun Jun 17, 2018 2:46 pm

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.

User avatar
Stephanie Hachem
Posts: 34
Joined: Wed Oct 26, 2016 12:40 pm

Re: MATLAB Forward Dynamics Tool Setup File Input

Post by Stephanie Hachem » Sun Jun 17, 2018 3:13 pm

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

User avatar
Thomas Uchida
Posts: 1793
Joined: Wed May 16, 2012 11:40 am

Re: MATLAB Forward Dynamics Tool Setup File Input

Post by Thomas Uchida » Sun Jun 17, 2018 4:27 pm

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).

User avatar
Stephanie Hachem
Posts: 34
Joined: Wed Oct 26, 2016 12:40 pm

Re: MATLAB Forward Dynamics Tool Setup File Input

Post by Stephanie Hachem » Mon Jun 18, 2018 4:22 pm

Hello Mr. Uchida,

The webpage is really helpful. Thank you!

Stephanie

POST REPLY