Adjusting ExternalForces through Matlab scripting interface
- Pavlos Silvestros
- Posts: 43
- Joined: Sun Oct 16, 2016 4:10 am
Adjusting ExternalForces through Matlab scripting interface
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
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
- Thomas Uchida
- Posts: 1793
- Joined: Wed May 16, 2012 11:40 am
Re: Adjusting ExternalForces through Matlab scripting interface
Have you tried setExternalLoadsFileName() (https://simtk.org/api_docs/opensim/api_ ... 180eb1d0e7)?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?
No, in MATLAB you should be able to do the following:When setting up the FDTool is it necessary to pass a general .xml FD setup file?
Code: Select all
import org.opensim.modeling.*;
fwdtool = ForwardTool();
- Pavlos Silvestros
- Posts: 43
- Joined: Sun Oct 16, 2016 4:10 am
Re: Adjusting ExternalForces through Matlab scripting interface
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.
My FD setup in Matlab is below:
Thank you for your help again!
Best regards,
Pavlos
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>
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();
Best regards,
Pavlos
- Thomas Uchida
- Posts: 1793
- Joined: Wed May 16, 2012 11:40 am
Re: Adjusting ExternalForces through Matlab scripting interface
Please post example files so I can investigate.
- Pavlos Silvestros
- Posts: 43
- Joined: Sun Oct 16, 2016 4:10 am
Re: Adjusting ExternalForces through Matlab scripting interface
Thank you again Tom,
Example force file Controls file Model and Matlab script (Bushings are for future use please ignore parameter values) FD setup file Best regards,
Pavlos
Example force file Controls file Model and Matlab script (Bushings are for future use please ignore parameter values) FD setup file Best regards,
Pavlos
- Thomas Uchida
- Posts: 1793
- Joined: Wed May 16, 2012 11:40 am
Re: Adjusting ExternalForces through Matlab scripting interface
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: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.
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.
- Pavlos Silvestros
- Posts: 43
- Joined: Sun Oct 16, 2016 4:10 am
Re: Adjusting ExternalForces through Matlab scripting interface
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
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
- Thomas Uchida
- Posts: 1793
- Joined: Wed May 16, 2012 11:40 am
Re: Adjusting ExternalForces through Matlab scripting interface
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.Is it preferable to add the control file to the ForwardDynamics tool or Prescribe it to the model as you have done?
- Pavlos Silvestros
- Posts: 43
- Joined: Sun Oct 16, 2016 4:10 am
Re: Adjusting ExternalForces through Matlab scripting interface
Thank you for all your help Tom, much appreciated!!