Forward dynamics in C++ and OpenSim 4.1

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Ana de Sousa
Posts: 58
Joined: Thu Apr 07, 2016 4:21 pm

Forward dynamics in C++ and OpenSim 4.1

Post by Ana de Sousa » Thu Dec 03, 2020 6:46 am

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.

Tags:

User avatar
Dimitar Stanev
Posts: 1096
Joined: Fri Jan 31, 2014 5:14 am

Re: Forward dynamics in C++ and OpenSim 4.1

Post by Dimitar Stanev » Sat Dec 05, 2020 11:51 am

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.

User avatar
Ana de Sousa
Posts: 58
Joined: Thu Apr 07, 2016 4:21 pm

Re: Forward dynamics in C++ and OpenSim 4.1

Post by Ana de Sousa » Mon Dec 07, 2020 12:14 pm

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).

POST REPLY