Skip to content

Commit e883fac

Browse files
committed
optimized realisation of kinematics
1 parent 0d4294d commit e883fac

3 files changed

Lines changed: 81 additions & 27 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function [out, missing_coords_list] = isKinMatchingModelCoords(osimModel, headers_in_struct)
2+
3+
% OpenSim suggested settings
4+
import org.opensim.modeling.*
5+
OpenSimObject.setDebugLevel(3);
6+
7+
% getting model coordinates and their number
8+
coordsModel = osimModel.updCoordinateSet();
9+
N_coordsModel = coordsModel.getSize();
10+
11+
% check if coordinates are the same (sometimes time is also here)
12+
if any(strcmp('time', headers_in_struct))
13+
coordsNames_in_struct = headers_in_struct(~strcmp('time', headers_in_struct));
14+
else
15+
coordsNames_in_struct = headers_in_struct;
16+
end
17+
n_miss = 1;
18+
n_matched = 1;
19+
coordNames = {};
20+
missing_coords_list = {};
21+
22+
for n = 0:N_coordsModel-1
23+
%extracting the column for the state variable of interest
24+
cur_coordName = char(coordsModel.get(n).getName());
25+
if find(strcmp(cur_coordName, coordsNames_in_struct)) == n+1
26+
coordNames{n_matched} = cur_coordName;
27+
n_matched = n_matched+1;
28+
else
29+
missing_coords_list{n_miss} = cur_coordName;
30+
n_miss = n_miss+1;
31+
end
32+
end
33+
34+
if length(coordNames) == length(coordsNames_in_struct)
35+
disp('Coordinate structure and model are properly matched (coordinates and their order).');
36+
out = 1;
37+
return
38+
else
39+
out = 0;
40+
41+
end
42+
43+
end

matlab_tool/isMuscleConnectedToBody.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
% initialise answ
4343
flag = 0;
4444

45+
% check name
46+
if isjava(osimBodyName)
47+
osimBodyName = char(osimBodyName);
48+
end
49+
4550
% extract muscle pathpointset
4651
muspathpointset = osimMuscle.getGeometryPath.getPathPointSet();
4752

@@ -51,9 +56,9 @@
5156
% current attachment body of the muscle point
5257
curr_attach_body = char(muspathpointset.get(n_p).getBodyName);
5358
% check if this is the specified body
54-
if strcmp(curr_attach_body, char(osimBodyName))
59+
if strcmp(curr_attach_body, osimBodyName)
5560
flag = 1;
56-
display(['Muscle ', char(osimMuscle.getName),' is attached to body ', char(osimBodyName)]);
61+
display(['Muscle ', char(osimMuscle.getName),' is attached to body ', osimBodyName]);
5762
return
5863
end
5964
end

matlab_tool/realizeMatStructKinematics.m

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,41 +39,47 @@
3939
coordsModel = osimModel.updCoordinateSet();
4040
N_coordsModel = coordsModel.getSize();
4141

42+
% checking is kinematics matches coordinates order
43+
[out, missing_coords_list] = isKinMatchingModelCoords(osimModel, kinStruct.colheaders);
44+
45+
% correct if there are generalised coordinates not computed in IK (e.g.
46+
% models modified after running IK)
47+
% NB: not tested!!
48+
if ~out
49+
for n_m = 1:length(missing_coords_list)
50+
missing_coords_vals(n_m) = coordsModel.get(coordName).getDefaultValue;
51+
end
52+
else
53+
missing_coords_vals = [];
54+
end
55+
% if needed
56+
% kinStruct.colheader = [kinStruct.colheaders, missing_coords_list];
57+
58+
% account for time column in data structure
59+
if any(strcmp('time', kinStruct.colheaders))
60+
kin_state_angles = [kinStruct.data(n_frame, 2:end), missing_coords_vals];
61+
else
62+
kin_state_angles = [kinStruct.data(n_frame, :), missing_coords_vals];
63+
end
64+
4265
% looping thought coordinates to assign them a value
43-
for n_StateCoord = 0:N_coordsModel-1
44-
45-
%%%%%% updating pose of the model %%%%%
46-
%extracting the column for the state variable of interest
47-
coordName = char(coordsModel.get(n_StateCoord).getName());
66+
for n_modelCoords = 0:N_coordsModel-1
4867

49-
% check if the coordinates as an equivalent in storage.
50-
CoordInd_in_kinStorage = getMatStructColumn(kinStruct, coordName);
51-
52-
if isempty(CoordInd_in_kinStorage)
53-
% this is the case when a coordinate is not in the storage file
54-
if n_frame==0
55-
warndlg([coordName,' not found in storage. Default value will be assigned.'])
56-
end
57-
% assign default value
58-
currentCoordValue = coordsModel.get(coordName).getDefaultValue;
59-
else
60-
coordColumValues = getMatStructColumn(kinStruct, coordName);
61-
% Value of the state variable at that frame
62-
currentCoordValue = coordColumValues(n_frame);
63-
end
68+
% Value of the state variable at that frame
69+
cur_coordValue = kin_state_angles(n_modelCoords+1);
6470

6571
% assigning the coordinates depending on their motion type
66-
switch char(coordsModel.get(n_StateCoord).get_motion_type())
72+
switch char(coordsModel.get(n_modelCoords).get_motion_type())
6773
case 'rotational'
6874
% transform to radiant for angles
69-
coordsModel.get(n_StateCoord).setValue(state,currentCoordValue*pi/180);
75+
coordsModel.get(n_modelCoords).setValue(state,cur_coordValue*pi/180);
7076
case 'translational'
7177
% not changing linear distances
72-
coordsModel.get(n_StateCoord).setValue(state,currentCoordValue);
78+
coordsModel.get(n_modelCoords).setValue(state,cur_coordValue);
7379
case 'coupled'
74-
error('motiontype ''coupled'' is not managed by this function');
80+
error('motiontype ''coupled'' is not managed by realizeMatStructKinematics.m');
7581
otherwise
76-
error('motion type not recognized. Error due to OpenSim update?')
82+
error('motion type not recognized. Please check OpenSim model (maybe update error?')
7783
end
7884
end
7985

0 commit comments

Comments
 (0)