-
Notifications
You must be signed in to change notification settings - Fork 2
/
LoadHRPdata.m
executable file
·250 lines (218 loc) · 7.26 KB
/
LoadHRPdata.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
function uLINK0 = LoadHRPdata(fname, verbose)
%% parse OpenHRP data (VRML) and make uHRP data s.kajita AIST
%% 2007 July 5 General algorithm for VRML
%% 2016 June 2 Introduce GetLineWithoutComment() and GetCompleteVectorString()
if nargin == 1
verbose = 0;
end
fid = fopen(fname);
nest_level = 0;
idx = 1;
idx_stack = 1;
jid_stack = [];
segments = [];
jid2idx = 1;
FRAME.translation = [0 0 0]';
FRAME.rotation = [0 0 1 0]';
FRAME.mother = 0;
FRAME.jointId = -1;
FRAME.name = '';
if verbose
fprintf('Nest level monitor:\n')
end
line_count = 0;
while 1
tline = GetLineWithoutComment(fid);
line_count = line_count + 1;
if ~ischar(tline), break, end
%% nest level of {}
nc = NestLevelCount(tline);
nest_level = nest_level + nc; % nest level
if nc > 0
idx = idx+1; % new FRAME
FRAME(idx).translation = [0 0 0]'; % Default
FRAME(idx).rotation = [0 0 1 0]'; % Default
FRAME(idx).mother = idx_stack(nest_level);
FRAME(idx).jointId = -1;
FRAME(idx).name = '';
idx_stack = [idx_stack(1:nest_level), idx];
end
if verbose
fprintf('%d:',nest_level);
if rem(line_count,30) == 0
fprintf('\n');
end
end
%% find joint definition
[T1,rest] = strtok(tline);
if strcmp(T1,'DEF')
[T2,rest] = strtok(rest);
[T3,rest] = strtok(rest);
if strcmp(T3,'Joint')
FRAME(idx).name = T2;
FRAME(idx).jaxis = [0 0 1]'; % default joint axis
if strcmp(T2,'WAIST')
jid2idx(1) = idx;
end
elseif strcmp(T3,'Segment');
FRAME(idx).name = T2;
segments = [segments idx];
end
elseif strcmp(T1,'translation')
FRAME(idx).translation = str2num(rest)';
elseif strcmp(T1,'rotation')
FRAME(idx).rotation = str2num(rest)';
elseif strcmp(T1,'jointId')
FRAME(idx).jointId = str2num(rest);
jid2idx(str2num(rest)+2) = idx;
elseif strcmp(T1,'jointAxis')
if findstr('X',rest)
FRAME(idx).jaxis = [1 0 0]';
elseif findstr('Y',rest)
FRAME(idx).jaxis = [0 1 0]';
elseif findstr('Z',rest)
FRAME(idx).jaxis = [0 0 1]';
else
FRAME(idx).jaxis = str2num(rest)';
end
elseif strcmp(T1,'jointType')
rest = strrep(rest,'"',''); % remove double quote
FRAME(idx).jtype = strrep(rest,' ',''); % remove space
elseif strcmp(T1,'mass')
FRAME(idx).m = str2num(rest);
elseif strcmp(T1,'centerOfMass')
FRAME(idx).c = str2num(rest)';
elseif strcmp(T1,'momentsOfInertia')
vector_str = GetCompleteVectorString(fid, rest);
wk = str2num(vector_str);
if isempty(wk)
Iwk = zeros(3,3);
else
Iwk = reshape(wk,3,3);
FRAME(idx).I = (Iwk + Iwk')/2;
end
elseif strcmp(T1,'gearRatio')
FRAME(idx).gr = str2num(rest);
elseif strcmp(T1,'rotorInertia')
FRAME(idx).Ir = str2num(rest);
elseif strcmp(T1,'llimit')
FRAME(idx).llimit = str2num(rest);
elseif strcmp(T1,'ulimit')
FRAME(idx).ulimit = str2num(rest);
elseif strcmp(T1,'lvlimit')
FRAME(idx).lvlimit = str2num(rest);
elseif strcmp(T1,'uvlimit')
FRAME(idx).uvlimit = str2num(rest);
end
end
fclose(fid);
if verbose
fprintf('\n')
end
if verbose
fprintf('No : name : mother : jointId \n')
for n=1:length(FRAME)
fprintf('%2d: %15s : %3d : jointId=%2d \n',n,FRAME(n).name,FRAME(n).mother,FRAME(n).jointId);
end
for n=1:length(jid2idx)
fprintf('jid=%2d idx=%2d \n',n,jid2idx(n));
end
end
%fprintf('*** Segments ***\n');
for n=1:length(segments)
idx = segments(n);
mom_idx = FRAME(idx).mother;
while ~strcmp(FRAME(mom_idx).name,'WAIST') && FRAME(mom_idx).jointId == -1
mom_idx = FRAME(mom_idx).mother; % ascend tree to find a valid Joint node
end
jid = FRAME(mom_idx).jointId + 2;
jid2segment(jid) = idx;
%fprintf('%3d %15s :%15s m=%8.2f\n',idx,FRAME(idx).name,FRAME(mom_idx).name,FRAME(idx).m);
end
%%%%%%% FRAME => uLINK
global uLINK0
rsize = length(jid2idx);
uLINK0 = FRAME( jid2idx(1) );
uLINK0.mother = 0;
for n=2:rsize
idx = jid2idx(n);
if idx > 0
uLINK0(n) = FRAME( idx );
mom_idx = FRAME(idx).mother;
while ~strcmp(FRAME(mom_idx).name,'WAIST') && FRAME(mom_idx).jointId == -1
gmom_idx = FRAME(mom_idx).mother; % ascend tree to find a valid Joint node
if ~(gmom_idx > 0)
fprintf('Subscript index error: mom_idx = %d, gmom_idx = %d\n',mom_idx, gmom_idx);
return;
else
mom_idx = gmom_idx;
end
end
uLINK0(n).mother = FRAME(mom_idx).jointId+2;
end
end
%%%%%%%% Set physical property
for n=1:rsize
idx = jid2segment(n);
if idx > 0
uLINK0(n).m = FRAME(idx).m;
uLINK0(n).I = FRAME(idx).I;
uLINK0(n).c = FRAME(idx).c;
if verbose
fprintf('%2d: %15s : %3d : jointId=%2d m=%6.2f \n',...
n,uLINK0(n).name,uLINK0(n).mother,uLINK0(n).jointId,uLINK0(n).m);
end
end
end
%%%%%%% find child and sister %%%%%%
%% initialize
for n=1:rsize
uLINK0(n).sister = 0;
uLINK0(n).child = 0;
uLINK0(n).q = 0;
uLINK0(n).dq = 0;
uLINK0(n).ddq = 0;
uLINK0(n).vertex = [];
uLINK0(n).Rs = eye(3);
end
uLINK0(1).p = [0,0,0]';
uLINK0(1).R = eye(3);
%% who am i sequence
for n=1:rsize
mom = uLINK0(n).mother; % I know you are my mother.
if mom ~= 0
if uLINK0(mom).child == 0
uLINK0(mom).child = n; % Mother, I'm your daughter !!
else
elder_sister = uLINK0(mom).child; % I have elder sister!
while(uLINK0(elder_sister).sister ~= 0)
elder_sister = uLINK0(elder_sister).sister; % I have another elder sister!
end
uLINK0(elder_sister).sister = n; % I'm your younger sister!
end
end
end
for n=1:length(uLINK0)
%fprintf('%2d %15s %2d : \n',uLINK0(n).jointId,uLINK0(n).name,uLINK0(n).mother);
eval([uLINK0(n).name,'=',num2str(n),';']);
%disp(uLINK0(n).jaxis');
end
VRML2OpenHRP(1);
%%%%%%%%%%%%%%%%% convert VRML to OpenHRP %%%%%%%%%%%%%%%%%%%%%%%%%%
function VRML2OpenHRP(n)
global uLINK0
if n == 0, return, end;
mom = uLINK0(n).mother;
if mom == 0
uLINK0(n).Rs = Rodrigues(uLINK0(n).rotation(1:3), uLINK0(n).rotation(4));
uLINK0(n).a = uLINK0(n).Rs * [0 0 1]';
uLINK0(n).b = uLINK0(n).translation;
else
uLINK0(n).Rs = uLINK0(mom).Rs * Rodrigues(uLINK0(n).rotation(1:3), uLINK0(n).rotation(4));
uLINK0(n).a = uLINK0(n).Rs * uLINK0(n).jaxis;
uLINK0(n).b = uLINK0(mom).Rs * uLINK0(n).translation;
uLINK0(n).p = uLINK0(mom).R * uLINK0(n).b + uLINK0(mom).p;
uLINK0(n).R = uLINK0(mom).R * Rodrigues(uLINK0(n).a, uLINK0(n).q);
end
VRML2OpenHRP(uLINK0(n).child);
VRML2OpenHRP(uLINK0(n).sister);