Page 1 of 1

Muscle activation in Forward dynamics (API)

Posted: Wed Jul 20, 2011 5:43 am
by loste
Hi all,
I want to perform a FD simulation using Opensim API.
Activation data and model properties are ok since I can perform the FD from the GUI, but I'm failing in doing the same with APIs.
I'm trying to import the .sto file with

Storage myStorage("excitation_control.sto");

and getting a segmentation fault.
Is this the correct procedure to import the controls.sto data?




RE: Muscle activation in Forward dynamics (API)

Posted: Wed Jul 20, 2011 9:20 am
by aymanh
Hi Stefano,

I'm not sure about your context/use-case but if you want to build a ControlSet, there's a ControlSet constructor that takes a Storage as input, so you can do something like:

Storage myStorage("excitation_control.sto");
ControlSet cs(myStorage);

Hope this helps,
-Ayman

RE: Muscle activation in Forward dynamics (API)

Posted: Thu Jul 21, 2011 4:34 am
by loste
Thank you Ayman, indeed it did help.

I wonder if there is a similar function for the initialization of system's states from file as well.

RE: Muscle activation in Forward dynamics (API)

Posted: Tue Jul 26, 2011 11:25 am
by aymanh
Hi Stefano,

There's no method to set initial states directly from a file, however you should be able once you have a state object to populate it using the code snippet:

SimTK::Vector configuration;
.... // Code to populate configuration
state.updY() = configuration;

This however is risky and highly not recommended unless you know exactly the layout of the state object/vector. Instead, a more robust way to set the state is to set members of the state vector using higher order methods, for example:
Cooridnate.setValue(state, newValue) .. etc.

Please let me know if that answers your question.

Best regards,
-Ayman

RE: Muscle activation in Forward dynamics (API)

Posted: Thu Jul 28, 2011 2:29 am
by loste
Dear Ayman,
thank you for the feedback. I resolved the issue by doing:

// obtain the complete generalized coordinates (q's) and generalized speeds (u's) from file
osimModel.getSimbodyEngine().formCompleteStorages(si, siFile, completeQs, completeUs);

// get the descriptor of the state in the initial instant
OpenSim::Array<double> aQInitial;
OpenSim::Array<double> aUInitial;
(..)
// get the values of state variable in the first instant of the simulation
completeQs->getDataAtTime(INITIAL_TIME, aQInitial.getSize(), aQInitial);
completeUs->getDataAtTime(INITIAL_TIME, aUInitial.getSize(), aUInitial);

// transform to vector
SimTK::Vector vQInitial(aQInitial.getSize(), &aQInitial[0]);
SimTK::Vector vUInitial(aUInitial.getSize(), &aUInitial[0]);

// set initial state
initialState.setQ(vQInitial);
initialState.setU(vUInitial);

// realize the state
osimModel.getSystem().realize(initialState, Stage::Acceleration);

After that I call equilibrateMuscles.
Since I am in a dynamic state (during walking), with the presence of accelerations and muscle activities, is it correct to do the call now, since equilibrateMuscles will take into accout all these contributes?
Or is it better to make it before, while the system is in a default quiet condition, and just then realize to this state?

Thank you.
Stefano

RE: Muscle activation in Forward dynamics (API)

Posted: Fri Jul 29, 2011 1:32 pm
by aymanh
Hi Stefano,

EquilibriateMuscles doesn't depend on the "dynamic/inertial" properties of the multibody system, so either place would be fine (as long as you're not doing it during an integration loop as it can interfere with the state variables being integrated.

Hope this helps,
-Ayman