-
Notifications
You must be signed in to change notification settings - Fork 0
/
orientationTracker.m
440 lines (397 loc) · 18.8 KB
/
orientationTracker.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
classdef orientationTracker < matlab.mixin.SetGet
properties
modelFileName
imuFileName
accFileName
model
model_calibrated
qTable
aTable
oTable
oTable_backup
oTable_opensim
oTable_headingCorrection
corrdinateDirection
baseIMU
sensorErrorTable
oRefs
end
methods
function self = orientationTracker(modelFileName, imuFileName, accFileName)
import org.opensim.modeling.*
self.imuFileName = imuFileName;
self.accFileName = accFileName;
self.modelFileName = modelFileName;
self.model = Model(modelFileName);
self.qTable = TimeSeriesTableQuaternion(imuFileName);
self.aTable = TimeSeriesTableVec3(accFileName);
end
function convertQuaternionToRotations(self)
import org.opensim.modeling.*
% Get the table properties
nc = self.qTable.getNumColumns();
nt = self.qTable.getNumRows();
% Get the data times
times = self.qTable.getIndependentColumn();
% Make a empty Matrix of Rotations
matrix = MatrixRotation(nt, nc, Rotation());
% Convert Quaternions to Rotations while filling the Matrix
for i = 0 : times.size() - 1
cnt = times.get(i);
quatRow = self.qTable.getRowAtIndex(i);
for j = 0 : nc - 1
matrix.set(i, j, Rotation(quatRow.getElt(0,j)) );
end
end
% Generate a Table of rotations using the new Matrix
oTable = TimeSeriesTableRotation(times, matrix, self.qTable.getColumnLabels());
% Copy the MetaData to the new Matrix
oTable = self.copyMetaData(self.qTable, oTable);
% store oTable internally
self.oTable = oTable;
% Store original backup version of the orientations Table
self.oTable_backup = oTable;
% Disp message
disp('Orientations Table successfully created')
end
function rotateOrientations2OpenSimFrame(self)
import org.opensim.modeling.*
% Define a negative Rotation about the world X
R_XG = Rotation(-pi/2, CoordinateAxis(0));
% Rotate the data
rotTable = self.rotationOrientationTable(self.oTable, R_XG);
% Update the stored orientation table
self.oTable = rotTable;
% Store a backup of the opensim rotated table
self.oTable_opensim = rotTable;
% Display message
disp('Orientations Table successfully rotated into OpenSim World')
end
function setBaseIMUDirection(self, baseIMU, baseHeadingDirection)
import org.opensim.modeling.*
% Determine direction from input string
if strcmp(baseHeadingDirection,'x')
direction = 1; axis = 0;
elseif strcmp(baseHeadingDirection,'y')
direction = 1; axis = 1;
elseif strcmp(baseHeadingDirection,'z')
direction = 1; axis = 2;
elseif strcmp(baseHeadingDirection,'-x')
direction = -1; axis = 0;
elseif strcmp(baseHeadingDirection,'-y')
direction = -1; axis = 1;
elseif strcmp(baseHeadingDirection,'-z')
direction = -1; axis = 2;
else
error( 'baseHeading direction is incorrect. Must be either x,y,z or -x,-y,-z');
end
% Generate CoordinateDirection object from the axis and
% direction.
self.corrdinateDirection = CoordinateDirection( CoordinateAxis(axis), direction);
self.baseIMU = baseIMU;
% Get the labels of the IMus from the table
pix = self.findLabelIndex(self.oTable, self.baseIMU);
% If no base can be found but one was provided, throw.
if isempty(pix)
error(['No Data column with base IMU name ' self.baseIMU ' found. Enter a different name']);
end
% Display message
disp('Heading axis and direction set')
end
function R_HG = computeHeadingCorrection(self)
% Base will rotate to match <base>_imu, so we must first remove the base
% rotation from the other IMUs to get their orientation with respect to
% individual model bodies and thereby compute correct offsets unbiased by the
% initial base orientation.
import org.opensim.modeling.*
% Get the labels of the IMus from the table
pix = self.findLabelIndex(self.oTable_opensim, self.baseIMU);
% The initial orientation of the base IMU
base_R = self.oTable.getRowAtIndex(0).getElt(0, pix);
% Heading direction of the base IMU in this case the pelvis_imu heading is its ZAxis
if self.corrdinateDirection.getAxis().isXAxis
col = 0;
elseif self.corrdinateDirection.getAxis().isYAxis
col = 1;
elseif self.corrdinateDirection.getAxis().isZAxis
col = 2;
end
pelvisHeading = UnitVec3(base_R.asMat33().get(0,col),...
base_R.asMat33().get(1,col),...
base_R.asMat33().get(2,col));
if self.corrdinateDirection.getDirection() < 0
pelvisHeading = pelvisHeading.negate();
end
% Get the heading of the Model base body
s = self.model.initSystem();
body = self.model.getBodySet.get('pelvis');
baseRotation = body.getTransformInGround(s).R();
groundX = UnitVec3(baseRotation.get(0,0),baseRotation.get(0,1),baseRotation.get(0,2));
% Compute the heading angle
u = [pelvisHeading.get(0) pelvisHeading.get(1) pelvisHeading.get(2)];
v = [groundX.get(0) groundX.get(1) groundX.get(2)];
CosTheta = dot(u,v)/(norm(u)*norm(v));
angularDifference = acosd(CosTheta);
format long g;
%disp(['Heading correction computed to be ' num2str(angularDifference) ' degs about ground Y'])
% Compute the direction of angular difference
xproduct = cross(v,u);
if xproduct(2) > 0
angularCorrection = angularDifference * -1;
else
angularCorrection = angularDifference;
end
disp(['Heading correction computed to be ' num2str(angularCorrection) ' degs about ground Y'])
% Compute the Rotation Matrix about Y of the OpenSim World
R_HG = Rotation();
R_HG.setRotationFromAngleAboutY(deg2rad( angularCorrection ));
% Rotate the table using the computed Rotation Matrix
oTable_headingCorrection = self.rotationOrientationTable(self.oTable, R_HG);
% Store the table
self.oTable = oTable_headingCorrection;
self.oTable_headingCorrection = oTable_headingCorrection;
% Display message
disp('Orientation Table has been rotated using the given Base Heading Direction')
end
function R_HG = addHeadingCorrectionFromAngle(self, angularDifference)
import org.opensim.modeling.*
% Compute the Rotation Matrix about Y of the OpenSim World
R_HG = Rotation();
R_HG.setRotationFromAngleAboutY( deg2rad( angularDifference ));
% Rotate the original table using the computed Rotation Matrix
oTable_headingCorrection = self.rotationOrientationTable(self.oTable_opensim, R_HG);
% Store the table
self.oTable = oTable_headingCorrection;
self.oTable_headingCorrection = oTable_headingCorrection;
% Display message
disp('Orientation Table has been rotated using the given Base Heading Direction')
end
function calibrateModelFromOrientations(self)
import org.opensim.modeling.*
% Make a copy of the original model to work on.
model = self.model.clone();
% Pose the model using the default coordinate values.
s0 = model.initSystem();
s0.setTime(0);
model.realizePosition(s0);
% Compute the relative offset between each model body and its
% corresponding IMU. Add an Offset frame on the model body.
for u = 0 : model.getNumBodies() - 1
body = model.getBodySet().get(u);
bodyName = body.getName();
for i = 0 : self.oTable.getColumnLabels().size() - 1
imuName = char(self.oTable.getColumnLabels().get(i));
ix = strrep(imuName,'_imu', '');
if strcmp(ix, bodyName)
% Get the orientation of the model body in Ground
R_BG = body.getTransformInGround(s0).R();
% Get the orientation of the IMU frame
R_FG = self.oTable.getRowAtIndex(0).getElt(0,i);
% Compute the offset rotation between the body and the
% (rotated) IMU data.
R_FB = R_BG.multiply(R_FG);
%disp(['Computed offset for ' char(imuLabels.get(i))]);
%disp(['Offset is; ']);disp(R_FB);
%imuOffset = PhysicalOffsetFrame();
% disp(['Creating Model offset frame for ' char(imuLabels.get(i))]);
% Set the point location to be the mass center of the body
p_FB = body.getMassCenter();
% Add an Offset Frame to the body with the orientation of the
% of the IMU
imuOffset = PhysicalOffsetFrame(imuName,body,Transform(R_FB, p_FB));
brick = Brick(Vec3(0.01, 0.01, 0.002));
brick.setColor(Vec3(255,165,0));
imuOffset.attachGeometry(brick);
%imuOffset.attachGeometry(FrameGeometry)
%FrameGeometry.setColor(Vec3(255,165,0))
body.addComponent(imuOffset);
disp(['Added offset frame for ' imuName]);
end
end
end
model.initSystem();
% Set the calibrated model
self.setCalibratedModel(model)
% Write calibrated model to file.
calibrated_model_name = strrep(self.modelFileName, '.osim', '_calibrated.osim');
model.print(calibrated_model_name);
disp(['Calibrated Model printed: ' calibrated_model_name ]);
end
function setCalibratedModel(self,model)
self.model_calibrated = model;
disp('Calibrated Model has been internally set')
end
function InverseKinematics(self, varargin)
import org.opensim.modeling.*
%varargin
nRows = self.oTable.getNumRows();
stime = self.oTable.getIndependentColumn().get(0);
etime = self.oTable.getIndependentColumn().get(nRows-1);
speed = 1;
%visualize, stime, etime,speed
visualize = varargin{1};
if nargin == 3
stime = varargin{2};
elseif nargin == 4
stime = varargin{2};
etime = varargin{3};
elseif nargin == 5
stime = varargin{2};
etime = varargin{3};
speed = varargin{4};
end
% Make a copy of the model and orientations to perform IK with
model = self.model_calibrated.clone();
oTable = self.oTable.clone();
% Instantiate a Reporter()
ikReporter = TableReporter();
ikReporter.setName('ik_reporter');
% Get the IKReporter to track coordinates of the model
coordinates = model.getCoordinateSet();
for i = 0 : coordinates.getSize() - 1
coord = coordinates.get(i);
ikReporter.updInput("inputs").connect(coord.getOutput("value"), coord.getName());
if contains(char(coord.getMotionType()), 'Translational')
coord.setDefaultLocked(true)
end
end
% Add Reporter to model
model.addComponent(ikReporter);
% Instantiate the Reference objects for tracking
mRefs = MarkersReference();
cRefs = SimTKArrayCoordinateReference();
% Check if orientation references have already been created.
if isempty(self.oRefs)
oRefs = OrientationsReference(oTable);
else
oRefs = self.oRefs;
end
% if Visualizing
if visualize
model.setUseVisualizer(true);
end
s0 = model.initSystem();
t0 = s0.getTime();
% Instantiate an IK solver
ikSolver = InverseKinematicsSolver(model, mRefs, oRefs, cRefs);
% Set the accuracy
accuracy = 1e-4; ikSolver.setAccuracy(accuracy);
% Set the time on the solver and do the initial assembly
if isempty(stime);sTimeIndex = 0;else;sTimeIndex = oTable.getRowIndexAfterTime(stime);end;
if isempty(etime);eTimeIndex = 0;else;eTimeIndex = oTable.getRowIndexAfterTime(etime);end;
if isempty(speed);speed = 1;end
% Get the Vector of times
times = oRefs.getTimes();
s0.setTime(times.get(sTimeIndex));
ikSolver.assemble(s0);
if visualize
model.getVisualizer().show(s0);
model.getVisualizer().getSimbodyVisualizer().setShowSimTime(true);
end
% Construct a Table and some Arrays and Vectors for storing the
% orientation error
oErrors = SimTKArrayDouble( ikSolver.getNumOrientationSensorsInUse() );
errorTable = DataTable();
rowVec = RowVector(ikSolver.getNumOrientationSensorsInUse());
labels = StdVectorString;
for i = 0 : ikSolver.getNumOrientationSensorsInUse - 1
oName = char(ikSolver.getOrientationSensorNameForIndex(i));
labels.add(oName)
end
errorTable.setColumnLabels(labels);
for i = sTimeIndex : speed : eTimeIndex - 1
time = times.get(i);
s0.setTime(time);
ikSolver.track(s0);
if visualize
model.getVisualizer().show(s0);
else
disp(['Solved frame at time: ' num2str(time) ]);
end
% Compute and store the orientation error
% Get the current Sensor Orientation
ikSolver.computeCurrentOrientationErrors(oErrors)
for u = 0 : ikSolver.getNumOrientationSensorsInUse - 1
rowVec.set(u, oErrors.getElt(u))
end
errorTable.appendRow(time,rowVec)
% Realize to the report stage
model.realizeReport(s0);
end
% Write coordinates to file
report = ikReporter.getTable();
outputFileName = 'inverseKinematics_coordinates.mot';
STOFileAdapter.write(report,outputFileName);
disp(['Kinematics written to file: ' outputFileName]);
% Write Errors to file
eTable = TimeSeriesTable(errorTable);
outputFileName = 'inverseKinematics_sensors_errors.sto';
STOFileAdapter.write(eTable,outputFileName);
disp(['Sensor Errors written to file: ' outputFileName]);
% Store errors locally
self.sensorErrorTable = eTable;
end
function m = getModel(self)
m = self.model;
end
function setOrientationRefs(self, keepSensor)
import org.opensim.modeling.*
otable = self.oTable.clone();
labels = otable.getColumnLabels();
for i = 0 : labels.size() - 1
deleteColumn = 1;
for u = 1 : length(keepSensor)
if strfind(char(labels.get(i)), keepSensor{u})
deleteColumn = 0;
break
end
end
if deleteColumn
otable.removeColumn(labels.get(i));
end
end
% Store the orientationRefs
self.oRefs = OrientationsReference(otable);
disp('New Orientations Reference Set');
end
end
methods (Access = private, Hidden = true)
function rotOTable = rotationOrientationTable(self, oTable, R)
import org.opensim.modeling.*
% Get table Properties
nc = oTable.getNumColumns();
nt = oTable.getNumRows();
% Get the Data Times
times = oTable.getIndependentColumn();
% Make a empty Matrix of Rotations
matrix = MatrixRotation(nt, nc, Rotation());
% Perform the Rotations
for i = 0 : oTable.getNumRows() - 1
row = oTable.getRowAtIndex(i);
for u= 0 : oTable.getNumColumns() - 1
matrix.set(i,u, R.multiply( row.getElt(0,u)) );
end
end
% Convert Matrix into a TimesSeriesTable
rotOTable = TimeSeriesTableRotation(times, matrix, oTable.getColumnLabels());
% Copy the Metadata
rotOTable = self.copyMetaData(oTable,rotOTable);
end
function newTable = copyMetaData(self, oldTable, newTable)
for i = 0 : oldTable.getTableMetaDataKeys.size() - 1
metaKey = oldTable.getTableMetaDataKeys.get(i);
newTable.addTableMetaDataString(metaKey, oldTable.getTableMetaDataString(metaKey));
end
end
function pix = findLabelIndex(self, otable, label)
imuLabels = self.oTable.getColumnLabels();
pix = [];
for i = 0 : imuLabels.size() - 1
if strcmp(label,imuLabels.get(i))
pix = i;
break
end
end
end
end
end