Page 1 of 1

apply a force at a given point

Posted: Wed Oct 14, 2020 9:01 pm
by yangkaiwen
Hello,

Is there a way to specify a body force after setting the states. What I wish to do is something like:

for (i=0:state_size-1){
osimModel.setStateVariable(osimState,stateNames,*(parameters + i));
}

applyForceToPoint (osimState, body,point, force,)

realizeToAcceleration(osimState)

However, I found out that applyForceToPoint cannot be called directly like that. Is there a workaround?

Kaiwen

Re: apply a force at a given point

Posted: Thu Oct 15, 2020 2:32 am
by mitkof6
Hi Kaiwen,

You can apply a prescribed force which should be added before you initialize your model. Please look at the following example:

https://github.com/mitkof6/OpenSim_API_ ... d_force.py

Re: apply a force at a given point

Posted: Thu Oct 15, 2020 5:57 am
by yangkaiwen
Thanks for the reply. However, this body force has to be calculated from the current state, something like a contact force. Can I update this prescribed force after the system is initialized?

Re: apply a force at a given point

Posted: Thu Oct 15, 2020 6:35 am
by mitkof6
Hi,

I think you can update the values of the function. The following code is adapted from PrescribedController which does something similar but for muscles:

Code: Select all

force = opensim.PrescribedForce.safeDownCast(model.getForceSet().get('Name of the force'))
forceFunctionSet = force.get_forceFunctions()
pointFunctionSet = force.get_pointFunctions()
toequeFunctionSet = force.get_torqueFunctions()

for j in range(forceFunctionSet.getSize()):
    func = opensim.Constant.safeDownCast(functionSet.get(j))  # this depends on the type of function that you used to initially, here assumed Constant
    func.setValue( float(action[j]) )
The above code should work with Python, but I have not tested it. If not minor changes should be made. Please use the doxygen to see how to access the different data members of the class:

https://simtk.org/api_docs/opensim/api_ ... Force.html

Re: apply a force at a given point

Posted: Thu Oct 15, 2020 6:57 am
by yangkaiwen
Thanks a lot. This is very helpful. Looks like prescribed force works similarly with prescribed controller. I will try to implement this.