Page 1 of 1

(SimTK::State &s) argument of InverseDynamicsSolver Tool

Posted: Fri Aug 26, 2016 6:38 am
by sariah
Dear OpenSim Team,

I have a state output of arm26 Model at each time step from Forward dynamics ( I am coding in cpp). The state output is a vector 16x1

I want to run the InverseDynamicsSolver Tool ( called in cpp) to get the joint torque at each time step .

I set : SimTK::State s ; // declare the argument state of the IDsolver Tool
taw = InverseDynamicsSolver.solve(SimTK::State s) ;
I was wandering how to define the argument s to the Tool , and now when I left it as it is "s" I got no errors. So the state argument should be the array of stateVariableNames or one of the stateVaribleNames. Does this makes sense ? example : taw = InverseDynamicsSolver.solve(SimTK::State r_elbow_flex )

And in case I want to input my own vectors for q, u,z ( continuous Variables) I should in this case use:
SimTK::State::setQ(q_vector) ; //q_vector is my input q vector at each timestep
SimTK::State::setU(u_vector) ;
SimTK::State::setZ (z_vector) ;

SimTK::State::setTime (t_vector) ;

Then use Model.getMultibodySystem().realize( SimTK::State s, Dynamics) here i should leave s as it is ?
Then to get the torque vector , i use torque = InverseDynamicsSolver.solve(SimTK::State s) ;

My question is that the realize method is responsible of updating the state of the system ? and my main issue resides in the definition of (SimTK::State s) argument which now I think this should be left as string s , Does this makes sense ? If someone can clarify to me this issue it will be of great help .

Thanks in advance for any help you can provide,
Sariah,

Re: (SimTK::State &s) argument of InverseDynamicsSolver Tool

Posted: Sat Aug 27, 2016 9:32 am
by mitkof6
Hi,
The state output is a vector 16x1
The state is not a vector but it is something that keeps track of all the internal variables that describe the state of your model. Also the state is realized into stages, and at each stage some states are updated.
I set : SimTK::State s ; // declare the argument state of the IDsolver Tool
The above definition creates a state object which does not contain any information about your model.

When you build and initialize your model, you get the initial state of the model. You pass this state to different functions and you either alter the state or compute something based on the current one. If the function that your are calling takes the state as non-const reference, that means that it alters its content (e.g. manager.step()). Make sure that you have a single state variable and pass it correctly to the different function calls.

You should do:

Code: Select all

model.buildSystem(); \\ build the model
State s = model.initializeState(); \\ get the initial state of the model

And in case I want to input my own vectors for q, u,z ( continuous Variables) I should in this case use:
SimTK::State::setQ(q_vector) ; //q_vector is my input q vector at each timestep
SimTK::State::setU(u_vector) ;
SimTK::State::setZ (z_vector) ;
Yes.

Code: Select all

s.updQ() = qVector;
SimTK::State::setTime (t_vector) ;
Here you set the current time (double).
Then use Model.getMultibodySystem().realize( SimTK::State s, Dynamics) here i should leave s as it is ?
You can call model.realizeDynamics(s) or .realize[Stage], where s is your state variable.

Best

Re: (SimTK::State &s) argument of InverseDynamicsSolver Tool

Posted: Mon Aug 29, 2016 1:22 am
by sariah
Thanks alot, very helpful and clear

So now if I am not wrong, to get joint torques, after realizing the state to the Dynamics stage( Model.realizeDynamics(state) ) , I should call:
SimTK::Vector Torque = state.getZDot() ;

Otherwise I call :
SimTK::Vector Torque = InverseDynamicsSolver.solve(state)

Both give the same output. Does it make sense ?
Regards,

Re: (SimTK::State &s) argument of InverseDynamicsSolver Tool

Posted: Mon Aug 29, 2016 3:54 am
by mitkof6
ZDot are auxiliary state variables, and are not related to joint torques. You should use the solver, providing q, qdot, qddot and the applied forces, thus solving the equation of motion for tau.

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

I would suggest you to get more familiar with the user guide (algorithms) and the theory behind the simulation engine.

http://simtk-confluence.stanford.edu:80 ... %27s+Guide
https://simtk.org/docman/?group_id=47

Best

Re: (SimTK::State &s) argument of InverseDynamicsSolver Tool

Posted: Wed Aug 31, 2016 4:00 am
by sariah
Thanks for the help,

I applied the inverseDynamicsSolver on the model ( arm 26 , OpenSim 2.4.0) , but:
when I add to my cpp code:


InverseDynamicsSolver ids(model); // model is already defined and ids is object of type InverseDynamics solver

I got no error while compiling and linking the cpp code

However when I add after : SimTK::Vector torque= ids.solve(state); // assumed udot (acceleration) to be the default

I got: A dynamic link library (DLL) initialization routine failed. The solve function of the InverseDynamicsSolver class is related to the osimSimulation.dll library and that library is correctly added to matlab path ( in matlab path since I am implementing a cpp S-function in the simulink environment of matlab) and to the environment variables path.

I am now wondering if the dll related library of the solve() function has a problem in the initialization because all include files, dll and static libraries are added correctly and in addition the function InverseDynamicsSolver(model) of the class InverseDynamicsSolver() doesn't generate any error.

I am looking for a help in solving such bug. Thanks in advance.

Regards,

Re: (SimTK::State &s) argument of InverseDynamicsSolver Tool

Posted: Wed Aug 31, 2016 6:31 am
by mitkof6
Hi,
However when I add after : SimTK::Vector torque= ids.solve(state); // assumed udot (acceleration) to be the default
If you assume default acceleration = Vector(0) you perform static analysis!!!

You can do ids.solve(state, state.getUDot()).
I got: A dynamic link library (DLL) initialization routine failed. The solve function of the InverseDynamicsSolver class is related to the osimSimulation.dll library and that library is correctly added to matlab path ( in matlab path since I am implementing a cpp S-function in the simulink environment of matlab) and to the environment variables path.

I am now wondering if the dll related library of the solve() function has a problem in the initialization because all include files, dll and static libraries are added correctly and in addition the function InverseDynamicsSolver(model) of the class InverseDynamicsSolver() doesn't generate any error.
Make sure to link the static libraries. You can try to make a stand alone c++ project to test your implementation before moving to Matlab s-function. I don't know about version 2.4, but in the new version you generate your project through CMake, and all the dependencies (includes, static libraries) are resolved.

Best