Extracting the angular velocity
Posted: Wed May 08, 2019 1:20 pm
Dear Opensim community,
I am trying to extract a body (in this example the tibia_r) angular velocity using the SimBodyEngine on Python 2.7 without going through the Analyze Tool.
- The model is the Gait2354
- The states file consists of joint coordinates extracted from IK
- I realise the state variable derivatives
- the COM_global is correctly computed
- However the value obtained for the w_local is all zeroes
Has anyone faced this issue or am I missing a crucial step ?
Thank you very much for your help!
Best Regards
This is the Python code that I am running:
##############################################
model = opensim.Model('Gait2354.osim')
bodyset = model.getBodySet()
bodypart = bodyset.get('tibia_r')
states = 'IK_joint_coordinates.mot'
### COM in local
COM_local = opensim.Vec3(0)
COM_local = bodypart.getMassCenter()
ground_body = model.getGround()
MotStorage = opensim.Storage(states)
n_coord = model.getNumCoordinates()
COM1 = []
COM2 = []
COM3 = []
W1 = []
W2 = []
W3 = []
if MotStorage.isInDegrees():
r = radians
else:
r = lambda x: x
## Realize the state
for n_frame in range (0 , MotStorage.getSize()):
## initialize state
state = model.initSystem()
## loop through coordinate set
for i in range(n_coord):
name = model.getCoordinateSet().get(i).getName()
index = MotStorage.getStateIndex(name)
vector = MotStorage.getStateVector(n_frame).getData().get(index)
model.updCoordinateSet().get(i).setValue(state,r(vector))
## generate vector for COM in global
COM_global = opensim.Vec3(0)
w_local = opensim.Vec3(0)
## transform COM to global
model.getSimbodyEngine().transformPosition(state, bodypart, COM_local, ground_body, COM_global)
## realize to the acceleration stage
model.computeStateVariableDerivatives(state)
## get the angular velocity
model.getSimbodyEngine().getAngularVelocityBodyLocal(state, bodypart, w_local);
COM1.append([COM_global.get(0)])
COM2.append([COM_global.get(1)])
COM3.append([COM_global.get(2)])
W1.append([w_local.get(0)])
W2.append([w_local.get(1)])
W3.append([w_local.get(2)])
COM_glob = np.hstack((COM1,COM2,COM3))
W_loc = np.hstack((W1,W2,W3))
I am trying to extract a body (in this example the tibia_r) angular velocity using the SimBodyEngine on Python 2.7 without going through the Analyze Tool.
- The model is the Gait2354
- The states file consists of joint coordinates extracted from IK
- I realise the state variable derivatives
- the COM_global is correctly computed
- However the value obtained for the w_local is all zeroes
Has anyone faced this issue or am I missing a crucial step ?
Thank you very much for your help!
Best Regards
This is the Python code that I am running:
##############################################
model = opensim.Model('Gait2354.osim')
bodyset = model.getBodySet()
bodypart = bodyset.get('tibia_r')
states = 'IK_joint_coordinates.mot'
### COM in local
COM_local = opensim.Vec3(0)
COM_local = bodypart.getMassCenter()
ground_body = model.getGround()
MotStorage = opensim.Storage(states)
n_coord = model.getNumCoordinates()
COM1 = []
COM2 = []
COM3 = []
W1 = []
W2 = []
W3 = []
if MotStorage.isInDegrees():
r = radians
else:
r = lambda x: x
## Realize the state
for n_frame in range (0 , MotStorage.getSize()):
## initialize state
state = model.initSystem()
## loop through coordinate set
for i in range(n_coord):
name = model.getCoordinateSet().get(i).getName()
index = MotStorage.getStateIndex(name)
vector = MotStorage.getStateVector(n_frame).getData().get(index)
model.updCoordinateSet().get(i).setValue(state,r(vector))
## generate vector for COM in global
COM_global = opensim.Vec3(0)
w_local = opensim.Vec3(0)
## transform COM to global
model.getSimbodyEngine().transformPosition(state, bodypart, COM_local, ground_body, COM_global)
## realize to the acceleration stage
model.computeStateVariableDerivatives(state)
## get the angular velocity
model.getSimbodyEngine().getAngularVelocityBodyLocal(state, bodypart, w_local);
COM1.append([COM_global.get(0)])
COM2.append([COM_global.get(1)])
COM3.append([COM_global.get(2)])
W1.append([w_local.get(0)])
W2.append([w_local.get(1)])
W3.append([w_local.get(2)])
COM_glob = np.hstack((COM1,COM2,COM3))
W_loc = np.hstack((W1,W2,W3))