Hello, I am trying to write a c++ program to use the OpenSim API to perform a static simulation using the Stanford upperlimb model. In a nutshell what I'm trying to do is:
1 - Position the arm into a specific pose
2 - Apply an external load to the model's hand
3 - Calculate the forces in the muscles
I am able to write a program which can open the existing model, change some parameters (mass, inertia, etc) and save the model with these changed parameters.
I am having difficulty with writing code to define a new external force at the hand. I have tried creating a "PerscribedForce" class with a "Constant" function, and adding it to the "lunate" body in the model. My program compiles but I get an unrecognized exception error. I suspect I am not using the pointers of the classes properly.
If someone could tell me the basic code I need to get this working, I would greatly appreciate. I expect once it's working I can open the modified model in the java gui and I should see the new force in the browser.
Even further, if you could give me a hint on where to head next with the rest of my program (putting the model into a particular pose, running static simulation, getting muscle forces ...) I would be so grateful
Thank you
PS, the following is my code (simplified):
// Load Existing opensim model
Model osimModel(osim_file);
osimModel.setName("Standford_modified");
// Create pointer to BodySet inside osimModel
BodySet& bodyset = osimModel.updBodySet();
// Add external force
OpenSim::Body * body = &bodyset.get("lunate");
PrescribedForce *force1 = new PrescribedForce(body);
force1->setForceFunctions(&OpenSim::Constant(1.0), &OpenSim::Constant(1.0), &OpenSim::Constant(1.0));
osimModel.addForce(force1);
// Save copy of the modified model
osimModel.print("model_modified.osim");
Using API for static analysis with forces
- Marc Carmichael
- Posts: 45
- Joined: Thu Jul 16, 2009 2:50 am
- Ayman Habib
- Posts: 2242
- Joined: Fri Apr 01, 2005 12:24 pm
RE: Using API for static analysis with forces
Marc,
The code looks OK as far as I can tell as well as the use of pointers/references. What do you see in the model file? and when do you get the exception?Can you load your model in the GUI and see the force in the navigator?
-Ayman
The code looks OK as far as I can tell as well as the use of pointers/references. What do you see in the model file? and when do you get the exception?Can you load your model in the GUI and see the force in the navigator?
-Ayman
- Marc Carmichael
- Posts: 45
- Joined: Thu Jul 16, 2009 2:50 am
RE: Using API for static analysis with forces
Hi Ayman,
My program throws the exception as it performs the osimModel.print() function, so no new model is made so I can't load the model in the GUI to see if the new force is added.
Does this give a clue to why the exception is being thrown? If I comment out the osimModel.addForce(force1); command then the program runs fine.
I will keep playing around and see if I can find out any more clues.
Cheers
Marc
My program throws the exception as it performs the osimModel.print() function, so no new model is made so I can't load the model in the GUI to see if the new force is added.
Does this give a clue to why the exception is being thrown? If I comment out the osimModel.addForce(force1); command then the program runs fine.
I will keep playing around and see if I can find out any more clues.
Cheers
Marc
- Ayman Habib
- Posts: 2242
- Joined: Fri Apr 01, 2005 12:24 pm
RE: Using API for static analysis with forces
Marc,
Please report a bug and attach your full code. We should disallow the creation of invalid objects or otherwise give a useful error message for you to fix your code.
Best,
-Ayman
Please report a bug and attach your full code. We should disallow the creation of invalid objects or otherwise give a useful error message for you to fix your code.
Best,
-Ayman
- Marc Carmichael
- Posts: 45
- Joined: Thu Jul 16, 2009 2:50 am
RE: Using API for static analysis with forces
I think I sorted out my problem. I guess I was just wrong in my implementation.
***This is what I was doing:
PrescribedForce *force1 = new PrescribedForce(body);
force1->setForceFunctions(&OpenSim::Constant(1.0), &OpenSim::Constant(1.0), &OpenSim::Constant(1.0));
osimModel.addForce(force1);
***This is what I am doing now:
PrescribedForce *force1 = new PrescribedForce(body);
force1->setName("Force1");
force1->setForceIsInGlobalFrame(1);
force1->setPointIsInGlobalFrame(0);
Constant *Fx = new Constant(0.0);
Constant *Fy = new Constant(0.0);
Constant *Fz = new Constant(100.0);
force1->setForceFunctions(Fx,Fy,Fz);
osimModel.addForce(force1);
The program runs fine and the added force shows up in the GUI navigator when I load the modified model
***This is what I was doing:
PrescribedForce *force1 = new PrescribedForce(body);
force1->setForceFunctions(&OpenSim::Constant(1.0), &OpenSim::Constant(1.0), &OpenSim::Constant(1.0));
osimModel.addForce(force1);
***This is what I am doing now:
PrescribedForce *force1 = new PrescribedForce(body);
force1->setName("Force1");
force1->setForceIsInGlobalFrame(1);
force1->setPointIsInGlobalFrame(0);
Constant *Fx = new Constant(0.0);
Constant *Fy = new Constant(0.0);
Constant *Fz = new Constant(100.0);
force1->setForceFunctions(Fx,Fy,Fz);
osimModel.addForce(force1);
The program runs fine and the added force shows up in the GUI navigator when I load the modified model