Page 1 of 1

Forward dynamics in C++ and OpenSim 4.1

Posted: Thu Dec 03, 2020 6:46 am
by anacsousa
Hi guys,

I am trying to build an API, so I can control muscle excitation (forward dynamics).

A few years ago, I successfully used these scripts API (for OpenSim 3.3 and Matlab): https://github.com/anacsousa1/opensim-f ... simLibrary

I just call for addInControls with the correct excitations and controllers: https://github.com/anacsousa1/opensim-f ... _v2.m#L333

Now, I started working on OpenSim 4.1, C++, and Ubuntu 20.04. I am trying to create a similar application: control the excitation of muscles based on state response.

1. My main function:

After loading the model, I initiate the forward dynamics and the integrator:

Code: Select all

fdManager_.reset(new Manager(model_));
fdManager_->setIntegratorAccuracy(1.0e-4);
fdManager_->initialize(s_);
Then, I start a loop, in which I'll get the current time, call the openSimPlantFunction (supposed to call functions for forward dynamics) and update the visualizer:

Code: Select all

for (int i{0}; i < n; ++i) {
	t = i * Ts;	
	manager.openSimPlantFunction(t, t + Ts, excitations);
	manager.updateVisualizer();
}
In this minimal example, excitations is a vector with some muscles at maximum excitation and all others at zero.

2. My openSimPlantFunction function:

Get states, get controls, apply addInControls for each muscle, set initial time, integrate, and compute state variable derivatives:

Code: Select all

s_.setTime(initialTime);
auto controls = model_.updControls(s_);
for (int i = 0; i < model_.updActuators().getSize(); i++){
	model_.updActuators()[i].addInControls(Vector(1, excitations[i]), controls);
}

forwardIntegrate(finalTime);
model_.computeStateVariableDerivatives(s_);
The system runs, but the response is the same for any excitation values that I set, so it does not seem to be actually assigning muscle excitations.

I appreciate it if someone could let me know what I am missing here.

Re: Forward dynamics in C++ and OpenSim 4.1

Posted: Sat Dec 05, 2020 11:51 am
by mitkof6
Hi Ana,

I think that a direct call to the addInControls controls on the actuators will not work. I have the impression that they are reset to their default value during numerical integration stepping so that the controllers can start adding values. OpenSim expects you to define a controller in order to actuate the model.

You can append multiple prescribed controllers for each actuator:

https://github.com/stanfordnmbl/osim-rl ... sim.py#L62

and then update their values as follows:

https://github.com/stanfordnmbl/osim-rl ... sim.py#L92

Hope this helps.

Re: Forward dynamics in C++ and OpenSim 4.1

Posted: Mon Dec 07, 2020 12:14 pm
by anacsousa
Hi Dimitar!

Thanks a lot for your response.
Yeah, that seems to be the reason that I couldn't make it work on the other way.

So I overwrote that computeControls functions with my controllers like in https://github.com/mitkof6/upat_eye_mod ... m_v4.0/src (which seems to be from your repository).