I am trying to perform a runtime analysis (frame-by-frame, as data becomes available) based on generalized coordinates. I calculate generalized coordinates and would like to get segment velocity (linear and angular) back from OpenSim. This output can also be obtained from BodyKinematics, but there is no solver for BodyKinematics. I therefore browsed the BodyKinematics.cpp and replicated the record(s) part, as this appears (to me) as what is being done when calling step(State,stepNumber)
From BodyKinematics.cpp
Code: Select all
int BodyKinematics::
step(const SimTK::State& s, int stepNumber)
{
if(!proceed(stepNumber )) return(0);
record(s);
return(0);
}
Code: Select all
//previous steps: create model - load data(generalized coordinates)
//Update the state with the generalized coordinates of that frame
s.setQ(qFromFile_1);
// I also tried s.updQ(), but noticed in a previous forum comment of Ajay that this could invalidate higher system stages (position and above)
osimModel.computeStateVariableDerivatives(s); // no difference in commenting this in/out
osimModel.getMultibodySystem().realize(s,Stage::Velocity); // also tried realizing up to Acceleration
//select a body
BodySet& bs = osimModel.updBodySet();
OpenSim::Body& body=bs.get("femur_r");
Vec3 COM_femur_r;
body.getMassCenter(COM_femur_r);
cout<<"COM femur R = "<<COM_femur_r<<endl;
osimModel.getSimbodyEngine().getPosition(s,body,COM_femur_r,vec);
cout<<"position COM femur R = "<<vec<<endl;
osimModel.getSimbodyEngine().getDirectionCosines(s,body,dirCOSIN);
osimModel.getSimbodyEngine().convertDirectionCosinesToAngles(dirCOSIN,
&angVec[0],&angVec[1],&angVec[2]);
cout<<"dircos femur R = "<<angVec<<endl;
// the above appears to work
osimModel.getSimbodyEngine().getAngularVelocity(s,body,angVelFem);
cout<<"ang vel femur "<<angVelFem<<endl;
// when I try calling the velocity values I get zero vectors
I am looking for the computationally most efficient way to obtain segment velocity and position values on a runtime (frame-by-frame) basis.
Does anyone know if you can access the Velocity stage directly from SimbodyEngine or if you always have to go through an analysis?
If I have to go through an analysis (BodyKinematics in this example), should I also create a Solver?