Switch to unified view

a b/MATLAB/private/realizeMatStructKinematics.m
1
%-------------------------------------------------------------------------%
2
% Copyright (c) 2019 Modenese L.                                          %
3
%                                                                         %
4
% Licensed under the Apache License, Version 2.0 (the "License");         %
5
% you may not use this file except in compliance with the License.        %
6
% You may obtain a copy of the License at                                 %
7
% http://www.apache.org/licenses/LICENSE-2.0.                             %
8
%                                                                         %
9
% Unless required by applicable law or agreed to in writing, software     %
10
% distributed under the License is distributed on an "AS IS" BASIS,       %
11
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or         %
12
% implied. See the License for the specific language governing            %
13
% permissions and limitations under the License.                          %
14
%                                                                         %
15
%    Author:   Luca Modenese                                              %
16
%    email:    l.modenese@imperial.ac.uk                                  %
17
% ----------------------------------------------------------------------- %
18
%
19
% A function to realize the kinematic state of a model. It is currently
20
% slow but robust.
21
%
22
% TODO: write comments
23
%
24
% written: 2014/2015 (Griffith University)
25
% last modified: 
26
% February 2017 (comments)
27
% 29/11/2018 renamed and added to MFD Matlab version.
28
% ----------------------------------------------------------------------- %
29
function state = realizeMatStructKinematics(osimModel, state, kinStruct, n_frame)
30
% TO DO
31
% this function is slow. I should find a way of passing in only a row of
32
% coordinates! That would eliminate the need of a n_frame as well.
33
34
% OpenSim suggested settings
35
import org.opensim.modeling.*
36
OpenSimObject.setDebugLevel(3);
37
38
% getting model coordinates and their number
39
coordsModel = osimModel.updCoordinateSet();
40
N_coordsModel = coordsModel.getSize();
41
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
if ~out
48
    for n_m = 1:length(missing_coords_list)
49
        coordName = missing_coords_list{n_m};
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
65
% looping thought coordinates to assign them a value
66
for n_modelCoords = 0:N_coordsModel-1
67
    
68
    % Value of the state variable at that frame
69
    cur_coordValue = kin_state_angles(n_modelCoords+1);
70
    
71
    % assigning the coordinates depending on their motion type
72
    switch char(coordsModel.get(n_modelCoords).get_motion_type())
73
        case 'rotational'
74
            % transform to radiant for angles
75
            coordsModel.get(n_modelCoords).setValue(state,cur_coordValue*pi/180);
76
        case 'translational'
77
            % not changing linear distances
78
            coordsModel.get(n_modelCoords).setValue(state,cur_coordValue);
79
        case 'coupled'
80
            error('motiontype ''coupled'' is not managed by realizeMatStructKinematics.m');
81
        otherwise
82
            error('motion type not recognized. Please check OpenSim model (maybe update error?')
83
    end
84
end
85
86
end