Adding a force through matlab

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
User avatar
Yong Cho
Posts: 27
Joined: Wed Sep 21, 2016 4:45 pm

Adding a force through matlab

Post by Yong Cho » Tue Mar 28, 2017 12:07 pm

For the arm26.osim model, if I wanted to add a force (unspecified but not a torque) on the r_humerus through Matlab, how would it be done?

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

Re: Adding a force through matlab

Post by Thomas Uchida » Tue Mar 28, 2017 12:55 pm

I wanted to add a force (unspecified but not a torque) on the r_humerus
What do you mean by adding an unspecified force to the model? What type of simulation are you performing?

User avatar
Yong Cho
Posts: 27
Joined: Wed Sep 21, 2016 4:45 pm

Re: Adding a force through matlab

Post by Yong Cho » Mon Apr 10, 2017 9:37 am

Unspecified as in any type of force. It is not a simulation I am performing. I want to know how to add in a force by using either the GUI or MATLAB

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

Re: Adding a force through matlab

Post by Thomas Uchida » Mon Apr 10, 2017 10:44 am

Unspecified as in any type of force.
The code required to add a force to a model depends on the type of force you're adding. The OpenSim::Force page in the API documentation (https://simtk.org/api_docs/opensim/api_ ... Force.html) lists the forces that are available (expand the "Inheritance diagram"). Perhaps you mean an ExternalForce (https://simtk.org/api_docs/opensim/api_ ... Force.html)?
It is not a simulation I am performing.
If you aren't performing a simulation, it isn't clear why you would want to add a force to the model. Are you just trying to generate an image?

User avatar
Yong Cho
Posts: 27
Joined: Wed Sep 21, 2016 4:45 pm

Re: Adding a force through matlab

Post by Yong Cho » Wed Apr 12, 2017 9:03 pm

Rather than imaging or a simulation, I am just trying to learn how to modulate opensim by using Matlab since I am new to this. To be more specific in what I want, I am trying to find a way to a add a force to the r_humerus of the model arm26.osim in the (1,0,0) direction. The code below is what I have as of now but I am stuck on what to do from here on.

Code: Select all

import org.opensim.modeling.*
myModel = Model('C:\Users\Yong Cho\Documents\OSFolder\arm26e2.osim')
r_humerus = myModel.getBodySet().get('r_humerus');
mass_center = Vec3(0, -.180496, 0);
force_applied = 8;
direction = Vec3(1,0 , 0);
Can anyone give any in doing what I specified above?

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

Re: Adding a force through matlab

Post by Thomas Uchida » Wed Apr 12, 2017 11:35 pm

I am just trying to learn how to modulate opensim by using Matlab since I am new to this.
There might be some helpful information on the "Scripting with Matlab" page in the documentation (http://simtk-confluence.stanford.edu:80 ... ith+Matlab).
I am trying to find a way to a add a force to the r_humerus of the model arm26.osim in the (1,0,0) direction.
Thank you for clarifying. The class you're looking for is OpenSim::PrescribedForce (https://simtk.org/api_docs/opensim/api_ ... Force.html). Here's a code snippet:

Code: Select all

import org.opensim.modeling.*

model = Model('arm26.osim');
body  = model.getBodySet().get('r_humerus');

force = PrescribedForce('myForce', body);
force.setForceFunctions(Constant(1), Constant(0), Constant(0));
model.addForce(force);

model.print('arm26_withPrescribedForce.osim');

User avatar
Yong Cho
Posts: 27
Joined: Wed Sep 21, 2016 4:45 pm

Re: Adding a force through matlab

Post by Yong Cho » Thu Apr 20, 2017 6:00 pm

Thank you for the reply. How would I set the magnitude of the force? This is the code that I am trying to run but I am getting errors. I am having problems in setting the magnitude of the force.

Code: Select all

import org.opensim.modeling.*

model = Model('C:\Users\Yong Cho\Documents\OSFolder\arm26e2.osim');
body  = model.getBodySet().get('r_ulna_radius_hand');
force = PrescribedForce('myForce', body);
force.setForceFunctions(Constant(0), Constant(0), Constant(1));
model.addForce(force);
model.print('C:\Users\Yong Cho\Documents\OSFolder\arm26e2PrescribedForce.osim');

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

Re: Adding a force through matlab

Post by Thomas Uchida » Thu Apr 20, 2017 6:32 pm

How would I set the magnitude of the force?
The line

Code: Select all

force.setForceFunctions(Constant(1), Constant(0), Constant(0));
is setting the three components of the force vector. Please see the documentation for the PrescribedForce class (https://simtk.org/api_docs/opensim/api_ ... ml#details). Here is a direct link to the setForceFunctions() method: https://simtk.org/api_docs/opensim/api_ ... d562363414.
This is the code that I am trying to run but I am getting errors.
What are the error messages? Many things could be going wrong (e.g., MATLAB is incorrectly configured, the model file you've specified doesn't exist, the body you've specified doesn't exist).
I am having problems in setting the magnitude of the force.
To apply a force of 50 N in the global X direction, for example, you would replace the line above with

Code: Select all

force.setForceFunctions(Constant(50), Constant(0), Constant(0));

User avatar
Yong Cho
Posts: 27
Joined: Wed Sep 21, 2016 4:45 pm

Re: Adding a force through matlab

Post by Yong Cho » Sun Apr 23, 2017 8:38 pm

Thank you, I think I solved the problem. Another question I had was that when I attempted to run a simulation on the arm26 model with a 50 newton force in the y direction, nothing different happened when compared to the control arm26 model. Is this normal or am I doing something wrong?

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

Re: Adding a force through matlab

Post by Thomas Uchida » Sun Apr 23, 2017 11:38 pm

when I attempted to run a simulation on the arm26 model with a 50 newton force in the y direction, nothing different happened when compared to the control arm26 model. Is this normal or am I doing something wrong?
It depends what type of simulation you're running. If you're performing a forward dynamic simulation, the trajectory should be different when you add an external force (assuming the coordinates aren't locked or prescribed). If you're performing a tracking simulation, the motion would be the same but the joint torques, muscle forces, etc. would be different.

POST REPLY