-
Notifications
You must be signed in to change notification settings - Fork 68
/
setRobotState.m
224 lines (162 loc) · 7.58 KB
/
setRobotState.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
function [] = setRobotState(varargin)
% SETROBOTSTATE sets the system state. The system state is composed of:
%
% - joints configuration and velocity plus gravity vector
% for fixed-base systems;
% - base pose and velocity, joints configuration and
% velocity plus gravity vector for floating-base systems.
%
% The gravity vector expresses the gravity acceleration
% in the inertial frame.
%
% This matlab function wraps a functionality of the iDyntree library.
% For further info see also: https://github.com/robotology/idyntree
%
% FORMAT: Floating base system:
%
% [] = setRobotState(KinDynModel,basePose,jointPos,baseVel,jointVel,gravAcc)
%
% Fixed base system:
%
% [] = setRobotState(KinDynModel,jointPos,jointVel,gravAcc)
%
% INPUTS: - KinDynModel: a structure containing the loaded model and additional info.
% - basePose: [4 x 4] from base frame to world frame transform;
% - jointPos: [nDof x 1] vector representing the joints configuration
% in radians;
% - baseVel: [6 x 1] vector of base velocities [lin, ang];
% - jointVel: [ndof x 1] vector of joints velocities;
% - gravAcc: [3 x 1] vector of the gravity acceleration in the
% inertial frame.
%
% Author : Gabriele Nava ([email protected])
%
% SPDX-FileCopyrightText: Fondazione Istituto Italiano di Tecnologia (IIT)
% SPDX-License-Identifier: BSD-3-Clause
%% ------------Initialization----------------
KinDynModel = varargin{1};
% check the number of inputs
switch nargin
case 6
basePose = varargin{2};
jointPos = varargin{3};
baseVel = varargin{4};
jointVel = varargin{5};
gravAcc = varargin{6};
% Debug inputs
if KinDynModel.DEBUG
disp('[setRobotState]: debugging inputs...')
% basePose must be a valid transformation matrix
if size(basePose,1) ~= 4 || size(basePose,2) ~= 4
error('[setRobotState]: basePose is not a 4x4 matrix.')
end
for ii = 1:4
if ii < 4
if abs(basePose(4,ii)) > 0.0001
error('[setRobotState]: the last line of basePose is not [0,0,0,1].')
end
else
if abs(basePose(4,ii)) > 1.0001 || abs(basePose(4,ii)) < 0.9999
error('[setRobotState]: the last line of basePose is not [0,0,0,1].')
end
end
end
% baseRotation = basePose(1:3,1:3) must be a valid rotation matrix
if det(basePose(1:3,1:3)) < 0.9 || det(basePose(1:3,1:3)) > 1.1
error('[setRobotState]: baseRotation is not a valid rotation matrix.')
end
IdentityMatr = basePose(1:3,1:3)*basePose(1:3,1:3)';
for kk = 1:size(IdentityMatr, 1)
for jj = 1:size(IdentityMatr, 1)
if jj == kk
if abs(IdentityMatr(kk,jj)) < 0.9 || abs(IdentityMatr(kk,jj)) > 1.1
error('[setRobotState]: baseRotation is not a valid rotation matrix.')
end
else
if abs(IdentityMatr(kk,jj)) > 0.01
error('[setRobotState]: baseRotation is not a valid rotation matrix.')
end
end
end
end
% debug gravity vector size
if length(gravAcc) ~= 3
error('[setRobotState]: the length of gravAcc vector is not 3.')
end
% check base velocity vector size
if length(baseVel) ~= 6
error('[setRobotState]: the length of baseVel is not 6.')
end
% check joints position vector size
if length(jointPos) ~= KinDynModel.NDOF
error('[setRobotState]: the length of jointPos is not KinDynModel.NDOF')
end
% check joints velocity vector size
if length(jointVel) ~= KinDynModel.NDOF
error('[setRobotState]: the length of jointVel is not KinDynModel.NDOF')
end
disp('[setRobotState]: done.')
end
% set the element of the rotation matrix and of the base
% position vector
for k = 0:2
KinDynModel.kinematics.baseOrigin_iDyntree.setVal(k,basePose(k+1,4));
for j = 0:2
KinDynModel.kinematics.baseRotation_iDyntree.setVal(k,j,basePose(k+1,j+1));
end
end
% add the rotation matrix and the position to basePose_iDyntree
KinDynModel.kinematics.basePose_iDyntree.setRotation(KinDynModel.kinematics.baseRotation_iDyntree);
KinDynModel.kinematics.basePose_iDyntree.setPosition(KinDynModel.kinematics.baseOrigin_iDyntree);
% set the base velocities
for k = 0:5
KinDynModel.kinematics.baseVel_iDyntree.setVal(k,baseVel(k+1));
end
case 4
jointPos = varargin{2};
jointVel = varargin{3};
gravAcc = varargin{4};
% Debug inputs
if KinDynModel.DEBUG
disp('[setRobotState]: debugging inputs...')
% debug gravity vector
if length(gravAcc) ~= 3
error('[setRobotState]: the length of gravAcc vector is not 3.')
end
% check joints position vector size
if length(jointPos) ~= KinDynModel.NDOF
error('[setRobotState]: the length of jointPos is not KinDynModel.NDOF')
end
% check joints velocity vector size
if length(jointVel) ~= KinDynModel.NDOF
error('[setRobotState]: the length of jointVel is not KinDynModel.NDOF')
end
disp('[setRobotState]: done.')
end
otherwise
error('[setRobotState]: wrong number of inputs.')
end
% set joints position and velocity
for k = 0:length(jointPos)-1
KinDynModel.kinematics.jointVel_iDyntree.setVal(k,jointVel(k+1));
KinDynModel.kinematics.jointPos_iDyntree.setVal(k,jointPos(k+1));
end
% set the gravity vector
for k = 0:2
KinDynModel.kinematics.gravityVec_iDyntree.setVal(k,gravAcc(k+1));
end
% set the current robot state
switch nargin
case 4
ack = KinDynModel.kinDynComp.setRobotState(KinDynModel.kinematics.jointPos_iDyntree,KinDynModel.kinematics.jointVel_iDyntree, ...
KinDynModel.kinematics.gravityVec_iDyntree);
case 6
ack = KinDynModel.kinDynComp.setRobotState(KinDynModel.kinematics.basePose_iDyntree,KinDynModel.kinematics.jointPos_iDyntree, ...
KinDynModel.kinematics.baseVel_iDyntree,KinDynModel.kinematics.jointVel_iDyntree, ...
KinDynModel.kinematics.gravityVec_iDyntree);
end
% check for errors
if ~ack
error('[setRobotState]: unable to set the robot state.')
end
end