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?
Muscle activation in Forward dynamics (API)
- Stefano Piazza
- Posts: 4
- Joined: Sun Sep 13, 2009 2:54 pm
- Ayman Habib
- Posts: 2254
- Joined: Fri Apr 01, 2005 12:24 pm
RE: Muscle activation in Forward dynamics (API)
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
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
- Stefano Piazza
- Posts: 4
- Joined: Sun Sep 13, 2009 2:54 pm
RE: Muscle activation in Forward dynamics (API)
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.
I wonder if there is a similar function for the initialization of system's states from file as well.
- Ayman Habib
- Posts: 2254
- Joined: Fri Apr 01, 2005 12:24 pm
RE: Muscle activation in Forward dynamics (API)
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
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
- Stefano Piazza
- Posts: 4
- Joined: Sun Sep 13, 2009 2:54 pm
RE: Muscle activation in Forward dynamics (API)
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
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
- Ayman Habib
- Posts: 2254
- Joined: Fri Apr 01, 2005 12:24 pm
RE: Muscle activation in Forward dynamics (API)
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
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