Adjusting ExternalForces through Matlab scripting interface

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Pavlos Silvestros
Posts: 43
Joined: Sun Oct 16, 2016 4:10 am

Adjusting ExternalForces through Matlab scripting interface

Post by Pavlos Silvestros » Fri Jul 14, 2017 7:43 am

Hello all,

I am transitioning to scripting with Matlab to accees the OpenSim API. I am trying to "rewrite" a forward dynamics pipeline I had running in Matlab that just wrote xml setup files and passsed them to OpenSim.

I have managed to set up the model and adjust it as I like, as well as set up the FDtool. However I am struggling to get my head around a couple of concepts.

1) When we want to apply a time varying external force (eg. grf or impact force) do we add it to the model or the FDtool? From what I understand we add it to the model but I cannot get it to work with PerscribedForce, ExternalLoads or ExternalForce. The force data that I am trying to pass to the script is from an external file so it maybe that I am not entering correctly as a opensim-Vector. The reason I want to get the force data into the script is so that I can change different parrameters (eg. point of application, magnitude and direction...) programmatically. Should I just do this separately outside OpenSim environment?

2)When setting up the FDTool is it necessary to pass a general .xml FD setup file?

Thank you for your help!!

Best regards,
Pavlos

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

Re: Adjusting ExternalForces through Matlab scripting interface

Post by Thomas Uchida » Sat Jul 15, 2017 10:01 pm

When we want to apply a time varying external force (eg. grf or impact force) do we add it to the model or the FDtool?
Have you tried setExternalLoadsFileName() (https://simtk.org/api_docs/opensim/api_ ... 180eb1d0e7)?
When setting up the FDTool is it necessary to pass a general .xml FD setup file?
No, in MATLAB you should be able to do the following:

Code: Select all

import org.opensim.modeling.*;
fwdtool = ForwardTool();
See the doxygen documentation for ForwardTool (https://simtk.org/api_docs/opensim/api_ ... dTool.html) which inherits from AbstractTool (https://simtk.org/api_docs/opensim/api_ ... tTool.html).

User avatar
Pavlos Silvestros
Posts: 43
Joined: Sun Oct 16, 2016 4:10 am

Re: Adjusting ExternalForces through Matlab scripting interface

Post by Pavlos Silvestros » Mon Jul 17, 2017 6:09 am

Hello Tom,

Thank you for your help, I managed to get it working without the initial forward dynamics (FD) setup file.

The fdTool now applies the forces to the model but will not apply the muscle controllers' file I have specified when I call the tool through Matlab. Furthermore, the values in the output ..._contols file of the simulation are all zeros. However when I run the simulation through the GUI with the same forward dynamics setup file, that I create in Matlab and pass to the fdTool, it applies the controllers?! I have not been able to figure this out. I have noticed that in .xml FD set up files there is an anable_controller tag which I cannot find in the API.

Code: Select all

      <ControllerSet name="Controllers">
         <objects>
            <ControlSetController name="">
               <enable_controller>ALL</enable_controller>
               <controls_file>C:\Users\User\Documents\Pavlos\UniversityofBath\Year4\FYP\Modelling\Simulation_Inputs\Controllers\controls_FR03A00_FL200_EX200.sto</controls_file>
            </ControlSetController>
         </objects>
      </ControllerSet>
My FD setup in Matlab is below:

Code: Select all

            fdTool = ForwardTool(); %setUpFile
            fdTool.setName(simName);
            fdTool.setModel(model);
            fdTool.setModelFilename(NewModelFile);
            
            fdTool.setStartTime(timeStart);
            fdTool.setFinalTime(timeEnd);
            %fdTool.setUseSpecifiedDt(1);
            
            fdTool.setErrorTolerance(errorTol);
            fdTool.setMaximumNumberOfSteps(maxIntergratorSteps);
            fdTool.setMinDT(minStepSize);
            fdTool.setMaxDT(maxStepSize);
            fdTool.setOutputPrecision(outputPrecision);
            
            fdTool.setSolveForEquilibrium(1);
            fdTool.setControlsFileName(FDControlsFile);
            fdTool.setExternalLoadsFileName(grfFile);
            
            %fdTool.setToolOwnsModel(1);
            %fdTool.setPrintResultFiles(1);
            fdTool.setResultsDir([mainDir 'Outputs\API\Results\'])
            fdTool.print([mainDir 'Outputs\API\FDSetUpFiles\' simName '.xml']);
            fdTool.run();
           
Thank you for your help again!

Best regards,
Pavlos

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

Re: Adjusting ExternalForces through Matlab scripting interface

Post by Thomas Uchida » Mon Jul 17, 2017 1:37 pm

Please post example files so I can investigate.

User avatar
Pavlos Silvestros
Posts: 43
Joined: Sun Oct 16, 2016 4:10 am

Re: Adjusting ExternalForces through Matlab scripting interface

Post by Pavlos Silvestros » Tue Jul 18, 2017 1:35 am

Thank you again Tom,

Example force file
Forces.zip
(285.21 KiB) Downloaded 106 times
Controls file
Controls.zip
(102.83 KiB) Downloaded 113 times
Model and Matlab script (Bushings are for future use please ignore parameter values)
Model and Matlab Script.zip
(21.63 KiB) Downloaded 106 times
FD setup file
test1707-MySetup-forEquil_Bushings (2).xml
(4.06 KiB) Downloaded 134 times
Best regards,
Pavlos

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

Re: Adjusting ExternalForces through Matlab scripting interface

Post by Thomas Uchida » Tue Jul 18, 2017 5:01 am

The fdTool now applies the forces to the model but will not apply the muscle controllers' file I have specified when I call the tool through Matlab.
It isn't clear (to me, at least) why fdTool.setControlsFileName() isn't working; I've opened an issue on GitHub to investigate (https://github.com/opensim-org/opensim-core/issues/1832). In the meantime, you can use the following two-line workaround:

Code: Select all

pc = PrescribedController('controls_FR03A00_FL200_EX200.sto'); %Add these two lines...
model.addController(pc);
state = model.initSystem(); %...before this line in your script.
This workaround will add the controller to the model before it's passed to ForwardTool.

User avatar
Pavlos Silvestros
Posts: 43
Joined: Sun Oct 16, 2016 4:10 am

Re: Adjusting ExternalForces through Matlab scripting interface

Post by Pavlos Silvestros » Tue Jul 18, 2017 10:28 am

Thank you Tom,

Works well now! Thank you for your help over this!

One last question regarding this:

Is it preferable to add the control file to the ForwardDynamics tool or Prescribe it to the model as you have done?

Best regards,
Pavlos

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

Re: Adjusting ExternalForces through Matlab scripting interface

Post by Thomas Uchida » Tue Jul 18, 2017 10:44 am

Is it preferable to add the control file to the ForwardDynamics tool or Prescribe it to the model as you have done?
There should be no difference in performance; setControlsFileName() just ends up adding a controller to the model anyway. Actually, the PrescribedController approach gives you more flexibility because you can specify the interpolation method.

User avatar
Pavlos Silvestros
Posts: 43
Joined: Sun Oct 16, 2016 4:10 am

Re: Adjusting ExternalForces through Matlab scripting interface

Post by Pavlos Silvestros » Wed Jul 19, 2017 1:11 am

Thank you for all your help Tom, much appreciated!!

POST REPLY