-
Notifications
You must be signed in to change notification settings - Fork 14
/
mainInverseKinematicsInverseDynamicsFromNexus.cpp
346 lines (295 loc) · 12.2 KB
/
mainInverseKinematicsInverseDynamicsFromNexus.cpp
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
/* -------------------------------------------------------------------------- *
* Copyright (c) 2010-2016 C. Pizzolato, M. Reggiani *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at: *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* -------------------------------------------------------------------------- */
#include "rtosim/rtosim.h"
using namespace rtosim;
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <Simbody.h>
void printAuthors() {
cout << "Real-time OpenSim inverse kinematics and inverse dynamics" << endl;
cout << "Authors: Claudio Pizzolato <c.pizzolato@griffit.edu.au>" << endl;
cout << " Monica Reggiani <monica.reggiani@unipd.it>" << endl << endl;
}
void printHelp() {
printAuthors();
auto progName(SimTK::Pathname::getThisExecutablePath());
bool isAbsolute;
string dir, filename, ext;
SimTK::Pathname::deconstructPathname(progName, isAbsolute, dir, filename, ext);
cout << "Option Argument Action / Notes\n";
cout << "------ -------- --------------\n";
cout << "-h Print the command-line options for " << filename << ".\n";
cout << "--model ModelFilename Specify the name of the osim model file for the investigation.\n";
cout << "--task-set TaskSetFilename Specify the name of the XML TaskSet file containing the marker weights to be used.\n";
cout << "--ext-loads LoadsFilename Specify the name of the XML ExternalLoads file.\n";
cout << "--hostname HostName Specify IP address and port for Vicon Nexus.\n";
cout << "--fc CutoffFrequency Specify the name of lowpass cutoff frequency to filter IK and GRF data.\n";
cout << "-j IK threads Specify the number of IK threads to be used.\n";
cout << "-a Accuracy Specify the IK solver accuracy.\n";
cout << "--output OutputDir Specify the output directory.\n";
cout << "-v Show visualiser.\n";
}
int main(int argc, char* argv[]) {
ProgramOptionsParser po(argc, argv);
if (po.exists("-h") || po.empty()) {
printHelp();
exit(EXIT_SUCCESS);
}
string osimModelFilename;
if (po.exists("--model"))
osimModelFilename = po.getParameter("--model");
else {
printHelp();
exit(EXIT_SUCCESS);
}
string ikTaskFilename;
if (po.exists("--task-set"))
ikTaskFilename = po.getParameter("--task-set");
else {
printHelp();
exit(EXIT_SUCCESS);
}
string externalLoadsXml;
if (po.exists("--ext-loads"))
externalLoadsXml = po.getParameter("--ext-loads");
else {
printHelp();
exit(EXIT_SUCCESS);
}
string hostname("127.0.0.1:801");
if (po.exists("--hostname"))
hostname = po.getParameter("--hostname");
double fc(8);
if (po.exists("--fc"))
fc = po.getParameter<double>("--fc");
unsigned nThreads(1);
if (po.exists("-j"))
nThreads = po.getParameter<unsigned>("-j");
double solverAccuracy(1e-5);
if (po.exists("-a"))
nThreads = po.getParameter<double>("-a");
string resultDir("Output");
if (po.exists("--output"))
resultDir = po.getParameter("--output");
bool showVisualiser(false);
if (po.exists("-v"))
showVisualiser = true;
resultDir = rtosim::FileSystem::getAbsolutePath(resultDir);
rtosim::FileSystem::createDirectory(resultDir);
string stopWatchResultDir(resultDir);
//define the shared buffers
MarkerSetQueue markerSetQueue;
GeneralisedCoordinatesQueue generalisedCoordinatesQueue, filteredGeneralisedCoordinatesQueue;
MultipleExternalForcesQueue grfQueue, adaptedGrfQueue, filteredGrfQueue;
ExternalTorquesQueue jointMomentsQueue;
//define the barriers
rtb::Concurrency::Latch doneWithSubscriptions;
rtb::Concurrency::Latch doneWithExecution;
FlowControl runCondition(true);
//define the filter
auto coordNames = getCoordinateNamesFromModel(osimModelFilename);
auto modelMarkerNames = getMarkerNamesFromModel(osimModelFilename);
GeneralisedCoordinatesStateSpace gcFilt(fc, coordNames.size());
//define the threads
DataFromNexus dataFromNexus(
markerSetQueue,
grfQueue,
doneWithSubscriptions,
doneWithExecution,
runCondition,
modelMarkerNames,
hostname);
//read from markerSetQueue, calculate IK, and save results in generalisedCoordinatesQueue
QueueToInverseKinematics inverseKinematics(
markerSetQueue,
generalisedCoordinatesQueue,
doneWithSubscriptions,
doneWithExecution,
osimModelFilename,
nThreads, ikTaskFilename, solverAccuracy);
//read from generalisedCoordinatesQueue, filter using gcFilt,
//and save filtered data in filteredGeneralisedCoordinatesQueue
QueueAdapter <
GeneralisedCoordinatesQueue,
GeneralisedCoordinatesQueue,
GeneralisedCoordinatesStateSpace
> gcQueueAdaptor(
generalisedCoordinatesQueue,
filteredGeneralisedCoordinatesQueue,
doneWithSubscriptions,
doneWithExecution,
gcFilt);
//corrects the value of the COP when the
//foot is not in contact with the force plate
AdaptiveCop adaptiveCop(
markerSetQueue,
grfQueue,
adaptedGrfQueue,
doneWithSubscriptions,
doneWithExecution,
osimModelFilename,
externalLoadsXml
);
using GrfFilter = QueueAdapter <
MultipleExternalForcesQueue,
MultipleExternalForcesQueue,
MultipleExternalForcesDataFilterStateSpace > ;
MultipleExternalForcesDataFilterStateSpace multipleExternalForcesDataFilterStateSpace(fc, 40, 2);
GrfFilter grfFilter(
adaptedGrfQueue,
filteredGrfQueue,
doneWithSubscriptions,
doneWithExecution,
multipleExternalForcesDataFilterStateSpace);
//read the filtered coordinates from IK and the filtered GRF,
//calculated the ID and write the results in jointMomentQueue
QueuesToInverseDynamics queueToInverseDynamics(
filteredGeneralisedCoordinatesQueue,
filteredGrfQueue,
jointMomentsQueue,
doneWithSubscriptions,
doneWithExecution,
osimModelFilename,
externalLoadsXml);
//read from filteredGeneralisedCoordinatesQueue and save to file
rtosim::QueueToFileLogger<GeneralisedCoordinatesData> filteredIkLogger(
filteredGeneralisedCoordinatesQueue,
doneWithSubscriptions,
doneWithExecution,
getCoordinateNamesFromModel(osimModelFilename),
resultDir, "filtered_ik_from_nexus", "sto");
//read from generalisedCoordinatesQueue and save to file
rtosim::QueueToFileLogger<GeneralisedCoordinatesData> rawIkLogger(
generalisedCoordinatesQueue,
doneWithSubscriptions,
doneWithExecution,
getCoordinateNamesFromModel(osimModelFilename),
resultDir, "raw_ik_from_nexus", "sto");
//read from generalisedCoordinatesQueue and save to file
rtosim::QueueToFileLogger<ExternalTorquesData> idLogger(
jointMomentsQueue,
doneWithSubscriptions,
doneWithExecution,
getCoordinateNamesFromModel(osimModelFilename),
resultDir, "id_from_nexus", "sto");
//read the frames from generalisedCoordinatesQueue and calculates some stats
rtosim::FrameCounter<GeneralisedCoordinatesQueue> ikFrameCounter(
generalisedCoordinatesQueue,
"time-ik-throughput");
//read the frames from generalisedCoordinatesQueue and calculates some stats
rtosim::FrameCounter<ExternalTorquesQueue> idFrameCounter(
jointMomentsQueue,
"time-id-throughput");
//measures the time that takes every single frame to appear in two different queues
rtosim::TimeDifference<
GeneralisedCoordinatesQueue,
GeneralisedCoordinatesQueue> gcQueueAdaptorTimeDifference(
generalisedCoordinatesQueue,
filteredGeneralisedCoordinatesQueue,
doneWithSubscriptions,
doneWithExecution);
rtosim::TimeDifference<
MarkerSetQueue,
GeneralisedCoordinatesQueue> ikTimeDifference(
markerSetQueue,
generalisedCoordinatesQueue,
doneWithSubscriptions,
doneWithExecution);
rtosim::TimeDifference<
MultipleExternalForcesQueue,
MultipleExternalForcesQueue> grfFilterTimeDifference (
grfQueue,
filteredGrfQueue,
doneWithSubscriptions,
doneWithExecution);
rtosim::TimeDifference<
MarkerSetQueue,
ExternalTorquesQueue> fromMarkersToIdTimeDifference(
markerSetQueue,
jointMomentsQueue,
doneWithSubscriptions,
doneWithExecution);
auto trigger([&runCondition](){
std::string cmd;
bool run(true);
std::cin >> cmd;
runCondition.setRunCondition(false);
});
doneWithSubscriptions.setCount(13);
doneWithExecution.setCount(13);
//launch, execute, and join all the threads
//all the multithreading is in this function
if (showVisualiser) {
StateVisualiser visualiser(generalisedCoordinatesQueue, osimModelFilename);
QueuesSync::launchThreads(
dataFromNexus,
inverseKinematics,
gcQueueAdaptor,
adaptiveCop,
grfFilter,
queueToInverseDynamics,
filteredIkLogger,
rawIkLogger,
idLogger,
gcQueueAdaptorTimeDifference,
ikTimeDifference,
grfFilterTimeDifference,
fromMarkersToIdTimeDifference,
ikFrameCounter,
idFrameCounter,
trigger,
visualiser
);
}
else {
QueuesSync::launchThreads(
dataFromNexus,
inverseKinematics,
gcQueueAdaptor,
adaptiveCop,
grfFilter,
queueToInverseDynamics,
filteredIkLogger,
rawIkLogger,
idLogger,
gcQueueAdaptorTimeDifference,
ikTimeDifference,
grfFilterTimeDifference,
fromMarkersToIdTimeDifference,
ikFrameCounter,
idFrameCounter,
trigger
);
}
//multithreaded part is over, all threads are joined
//get execution time infos
auto stopWatches = inverseKinematics.getProcessingTimes();
rtosim::StopWatch combinedSW("time-ikparallel-processing");
for (auto& s : stopWatches)
combinedSW += s;
combinedSW.print(stopWatchResultDir);
ikFrameCounter.getProcessingTimes().print(stopWatchResultDir);
idFrameCounter.getProcessingTimes().print(stopWatchResultDir);
queueToInverseDynamics.getProcessingTimes().print(stopWatchResultDir);
ikTimeDifference.getWallClockDifference().print(stopWatchResultDir + "/time-markerqueue-to-jointangles.txt");
gcQueueAdaptorTimeDifference.getWallClockDifference().print(stopWatchResultDir + "/time-jointangles-to-filteredjointangles.txt");
grfFilterTimeDifference.getWallClockDifference().print(stopWatchResultDir + "/time-grf-to-filteredgrf.txt");
fromMarkersToIdTimeDifference.getWallClockDifference().print(stopWatchResultDir + "/time-markers-to-jointmoments.txt");
dataFromNexus.printLatencyData(stopWatchResultDir + "/time-latency-nexus.txt");
return 0;
}