Calculate the transformation matrices in a specific pose using MATLAB

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Rodrigo Mateus
Posts: 55
Joined: Thu Feb 22, 2018 3:07 am

Calculate the transformation matrices in a specific pose using MATLAB

Post by Rodrigo Mateus » Fri Jul 12, 2024 10:34 am

Hi there!

I am trying to calculate the transformation matrices in a specific pose given by a .mot file.

However, I am having some difficulty in placing the musculoskeletal model in the specific pose. Here it is the code I am implementing:

% Import OpenSim libraries
import org.opensim.modeling.*

% Load an OpenSim model
model = Model('output_scaled_model_file.osim'); % Replace with the path to your model

% Initialize the model to get the default state
state = model.initSystem();

% Load the motion data (e.g., a .mot file)
motionData = Storage('StaticCal_0001_scale_ik.mot'); % Replace with the path to your motion file

% Define the time at which you want to extract the pose (e.g., 1.0 seconds)
desiredTime = 0;

% Get the number of states (coordinates) in the model
numStates = size(state.getY());
% Create an array to store the state values
stateValues = zeros(numStates, 1);
% Extract the state values at the desired time from the motion data
for i = 0:numStates-1
stateValues(i+1) = motionData.getStateVector(desiredTime).getData().get(i);
end
coordinate_set = model.getCoordinateSet();
opensimVector = Vector(length(stateValues), 0);
for i = 1:length(stateValues)
opensimVector.set(i-1, stateValues(i)); % OpenSim uses 0-based indexing
end
% Set the state values in the model state
state.setY(opensimVector);
% Get the number of coordinates (states) in the model
numCoordinates = model.getCoordinateSet().getSize();
% Loop through the coordinates to extract values and speeds
for i = 0:numCoordinates-1
coordinateName = model.getCoordinateSet().get(i);
coordinateName.setValue(state, 1)
coordinateName.setSpeedValue(state, 1)
end
% Update the model with the state
model.realizeVelocity(state);
% Print the state information
model.printDetailedInfo(state);

% Optionally, you can save the model with the updated state
model.print('UpdatedModelWithPose.osim');


If anyone is able to help, I'd appreciate the help very much!

Rodrigo

Tags:

User avatar
Nicos Haralabidis
Posts: 188
Joined: Tue Aug 16, 2016 1:46 am

Re: Calculate the transformation matrices in a specific pose using MATLAB

Post by Nicos Haralabidis » Fri Jul 12, 2024 10:55 am

Hey Rodrigo!

Hope you are well!

After you have set the relevant state information and realized to the necessary stage you can extract the Transform as follows:
bodySet = model.getBodySet();
body1 = bodySet.get('your_body');
body1Transform = body1.getTransformInGround(state);

if you then want the rotation matrix:
body1RotationMatrix = body1Transform.R();

Hope that helps!

Nicos

User avatar
Rodrigo Mateus
Posts: 55
Joined: Thu Feb 22, 2018 3:07 am

Re: Calculate the transformation matrices in a specific pose using MATLAB

Post by Rodrigo Mateus » Fri Jul 12, 2024 12:07 pm

Hey Nicos!

Everything going well thanks!! Hope you are well too!

Thank you for the help. apparently I needed to calculate the transforms before printing the model, so it's all working out well now, thank you!

User avatar
Rodrigo Mateus
Posts: 55
Joined: Thu Feb 22, 2018 3:07 am

Re: Calculate the transformation matrices in a specific pose using MATLAB

Post by Rodrigo Mateus » Fri Jul 12, 2024 4:55 pm

Unfortunately, the marker positions are not being retrieved correctly, although I do not understand why... Here it is the code to retrieve the marker positions at the new state:

% Import OpenSim libraries
import org.opensim.modeling.*

% Load an OpenSim model
model = Model('output_scaled_model_file.osim'); % Replace with the path to your model

% Initialize the model to get the default state
state = model.initSystem();

% Load the motion data (e.g., a .mot file)
motionData = Storage('StaticCal_0001_scale_ik.mot'); % Replace with the path to your motion file

% Define the time at which you want to extract the pose (e.g., 1.0 seconds)
desiredTime = 0;

% Get the number of states (coordinates) in the model
numStates = size(state.getY());
% Create an array to store the state values
stateValues = zeros(numStates, 1);
stateSpeedValues = zeros(numStates, 1);
% Extract the state values at the desired time from the motion data
for i = 0:numStates-1
stateValues(i+1) = motionData.getStateVector(desiredTime).getData().get(i);
end
coordinate_set = model.getCoordinateSet();
opensimVector = Vector(length(stateValues), 0);
for i = 1:length(stateValues)
opensimVector.set(i-1, stateValues(i)); % OpenSim uses 0-based indexing
end
% % Set the state values in the model state
state.setY(opensimVector);

model.realizePosition(state);
markerSet = model.getMarkerSet();

% Initialize a cell array to store the marker positions
numMarkers = markerSet.getSize();
markerPositions = cell(numMarkers, 2); % Two columns: Marker name and Position

% Retrieve the positions of each marker in the ground frame
for i = 0:numMarkers-1
marker = markerSet.get(i);
markerName = char(marker.getName());
markerPosition = marker.getLocationInGround(state).getAsMat(); % Get the position as a MATLAB matrix
markerPositions{i+1, 1} = markerName;
markerPositions{i+1, 2} = markerPosition';
end
coordPositions = cell(numCoordinates, 2);
% Get the number of coordinates (states) in the model
numCoordinates = model.getCoordinateSet().getSize();
% Loop through the coordinates to extract values and speeds
for i = 0:numCoordinates-1
coordinateName = model.getCoordinateSet().get(i);
coordPositions{i+1, 1} = coordinateName.getValue(state);
coordPositions{i+1, 2} = coordinateName.getName();
end

POST REPLY