Page 1 of 1

Acceleration of center of mass

Posted: Wed Sep 23, 2015 12:46 am
by gilserrancoli
Hi,

I am trying to calculate the mass acceleration of the total multibody system from the API in Matlab. I have the states stored in a .sto file. So, the code I was using is the following:

Code: Select all

model=Model('myModel.osim');
statesStorage=Storage('states_Storage.sto');
state=model.initSystem();
for i=0:(statesStorage.getSize-1)
    statei=statesStorage.getStateVector(i);
    state.setY(statei.getData.getAsVector)
    state.setTime(statei.getTime);
    
    valuesVec3=model.calcMassCenterAcceleration(state);
    CoMacc(i+1,1)=valuesVec3.get(0);
    CoMacc(i+1,2)=valuesVec3.get(1);
    CoMacc(i+1,3)=valuesVec3.get(2);
end
However, the acceleration that I got is just the gravity. I tried to run model.computeStateVariableDerivatives(states) before calculating the mass center acceleration, but I obtain the same. Should I calculate the accelerations of each body first? or does the function calcMassCenterAcceleration need to compute something else previously?

Then, I was also trying to compute the mass center acceleration of each body, with the following code:

Code: Select all

massCenter = Vec3(0.0,0.0,0.0);
acceleration = Vec3(0.0,0.0,0.0);
state=model.initSystem();
for i=0:(statesStorage.getSize-1)
    statei=statesStorage.getStateVector(i);
    state.setY(statei.getData.getAsVector)
    state.setTime(statei.getTime);
    Bodies=model.getBodySet();
    for j=0:(Bodies.getSize-1);
        Bodies.get(j).getMassCenter(massCenter);
        simbodyEngine=model.getSimbodyEngine();
        simbodyEngine.getAcceleration(state,Bodies.get(j),massCenter,acceleration);
        CoMAcc(i+1,1,j+1)=acceleration.get(0);
        CoMAcc(i+1,2,j+1)=acceleration.get(1);
        CoMAcc(i+1,3,j+1)=acceleration.get(2);
    end
end
However, I got the following error when computing the acceleration:
Java exception occurred:
java.lang.RuntimeException: SimTK Exception thrown at State.cpp:2069:
Expected stage to be at least Position in StateImpl::getCacheEntry() but current stage was Instance

Do you know what I am missing?

Thank you very much,

Gil SerrancolĂ­

Re: Acceleration of center of mass

Posted: Wed Oct 14, 2015 4:02 pm
by jimmy
Hi Gil,

looks like you need to realize the system to accelerations. Try adding this;

Code: Select all

model.computeStateVariableDerivatives(s)
Under the hood, the call will realize the system through to accelerations.

Let me know if that works,
-J

Re: Acceleration of center of mass

Posted: Fri Oct 16, 2015 6:24 am
by gilserrancoli
Thanks James for the answer.

However, I tried it and it does not seem to work. I still obtain an acceleration equal to gravity.

I just added the line to compute state derivatives after setting the states.

Code: Select all

model=Model('myModel.osim');
statesStorage=Storage('states_Storage.sto');
state=model.initSystem();
for i=0:(statesStorage.getSize-1)
    statei=statesStorage.getStateVector(i);
    state.setY(statei.getData.getAsVector)
    state.setTime(statei.getTime);
    model.computeStateVariableDerivatives(state);

    valuesVec3=model.calcMassCenterAcceleration(state);
    CoMacc(i+1,1)=valuesVec3.get(0);
    CoMacc(i+1,2)=valuesVec3.get(1);
    CoMacc(i+1,3)=valuesVec3.get(2);
end
Does anyone know if it is possible to run the function model.calcMassCenterAcceleration(state) in Matlab? Am I missing something when setting the states?

Thank you very much,

Gil

Re: Acceleration of center of mass

Posted: Tue Jan 26, 2016 2:13 pm
by rkhuffman
I'm not sure I saw a resolution to this problem in this chain, but I am having the same issue in an analysis plugin. I tried adding the call to computeStateVariableDerivatives(s), but it didn't make any difference. I am actually working on an on orbit exercise simulation for NASA, so I have set my gravity accel to 0,0,0 in the model.

_model->computeStateVariableDerivatives(s);
totMass = _model->getTotalMass(s);
sysCgPos = _model->calcMassCenterPosition(s);
sysCgVel = _model->calcMassCenterVelocity(s);
sysCgAcc = _model->calcMassCenterAcceleration(s);
// sysCgAcc = (sysCgVel - sysCgVelLast) / dt;
sysCgVelLast = sysCgVel;

Anyway, sysCgAcc as coded, contains very small, essentially zero numbers, while if I do a finite difference calc (commented) based on the velocity call, I get something fairly reasonable. I've run through this in the debugger, and put in some prints (below), and it is calculating non-zero accels for each body, but the sum of the body accels * mass cancel each other out.

Vec3 SimbodyMatterSubsystem::calcSystemMassCenterAccelerationInGround(const State& s) const {
Real mass = 0;
Vec3 coma = Vec3(0);

for (MobilizedBodyIndex b(1); b < getNumBodies(); ++b) {
const MassProperties& MB_OB_B = getMobilizedBody(b).getBodyMassProperties(s);
const Vec3 a_G_CB = getMobilizedBody(b).findStationAccelerationInGround(s, MB_OB_B.getMassCenter());
const Real mb = MB_OB_B.getMass();

mass += mb;
coma += mb * a_G_CB; // weighted by mass

cout << b << " coma = " << coma << " " << mb * a_G_CB << " mass = " << mb << endl;
}

if (coma[1] > .1 || coma[1] < -.1)
cout << coma << endl;

if (mass != 0)
coma /= mass;

return coma;
}

Thanks,
Ken

Re: Acceleration of center of mass

Posted: Fri Mar 18, 2016 5:56 am
by kohli
For some reason, the more I try to think about this problem, the more confused I seem to become :(. I hate to ask this, but might you be willing to give me a step-by-step explanation of how to solve it?
I don't know if they have any significance (or if they are even correct), but here are some values I've come up with so far:
x1 = -0.5, y1 = 0
x2 = 0, y2 = -0.1
xcom,sys = (m1x1 + m2x2)/(M) = -0.3
ycom,sys = (m1y1 + m2y2)/(M) = -0.04
and a couple of the equations I've been trying to work with:
Fnet = Macom
Macom = m1a1 + m2a2

Re: Acceleration of center of mass

Posted: Tue Jan 17, 2017 4:48 am
by mlancini
I have a similar problem using the OpenSim IDE. Whenever I try to compute the center of mass accelerations I get only gravity, while differentiating all the markers I get a result compatible with the movements of my subjects.
It seems that the inverse kinematics tools sets the center of mass to a constant velocity movement. Is that a bug or am I forgetting an option?

thanks
matteo