For example, the code below uses the Arm26 model. It sets the shoulder angle to 0.1 radians and the elbow to 0.2 radians.
If you look at the state order from getStateVariableNames, you get:
'r_shoulder/r_shoulder_elev/r_shoulder_elev' <-- Shoulder Angle
'r_shoulder/r_shoulder_elev/r_shoulder_elev_u'
'r_elbow/r_elbow_flex/r_elbow_flex' <-- Elbow Angle
'r_elbow/r_elbow_flex/r_elbow_flex_u'
'TRIlong/activation' ....
So the shoulder is the 1st entry and the elbow is the 3rd. But when you look at the output from the states you get:
0.1 <-- Shoulder Angle
0.2 <-- Elbow Angle
0
0
0.05 ....
The elbow angle is the 2nd entry here (but was 3rd above).
Is this expected behavior and if so, is there a better way to get the titles of the states returned by getY?
Code: Select all
% Load Library
import org.opensim.modeling.*;
% Open a Model by name
model = Model('arm26.osim');
% Initialize the system and get the initial state
si = model.initSystem();
%Set the Shoulder to 0.1 Radians
model.getJointSet().get('r_shoulder').getCoordinateSet().get('r_shoulder_elev').setValue(si,0.1)
%Set the Elbow to 0.2 Radians
model.getJointSet().get('r_elbow').getCoordinateSet().get('r_elbow_flex').setValue(si,0.2)
%Realize the model
model.computeStateVariableDerivatives(si)
%Get the names of the state variables
stateVarArray=model.getStateVariableNames();
nStateVar=stateVarArray.getSize(); %Number of State Variables
for i=0:(nStateVar-1)
stateVarNames{i+1}=char(stateVarArray.get(i))
end
%Now get the states
numVar = si.getNY();
stateArray = si.getY();
for i = 0:1:numVar-1
stateValues(i+1)=stateArray.get(i);
end