-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaskRecorder.cpp
More file actions
518 lines (427 loc) · 13.8 KB
/
taskRecorder.cpp
File metadata and controls
518 lines (427 loc) · 13.8 KB
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
* Copyright (C) 2012 MACSi Project
* Author: Serena Ivaldi
* email: serena.ivaldi@isir.upmc.fr
* website: www.macsi.isir.upmc.fr
* Permission is granted to copy, distribute, and/or modify this program
* under the terms of the GNU General Public License, version 2 or any
* later version published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details
*/
#include <yarp/os/all.h>
#include <yarp/sig/all.h>
#include <yarp/dev/all.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include "taskRecorder.h"
using namespace std;
using namespace yarp::os;
using namespace yarp::sig;
using namespace yarp::dev;
// utils for printing parameters
#define DSCP(V,oss) V; oss<< #V <<endl;
#define DSCPI(V,oss,i) V; oss<< #V <<" i="<<i<<endl;
#define DSCPS(V,oss) oss<< #V <<endl;
////////////////////////////////////////
////////////////////////////////////////
// ARMS RECORDER
////////////////////////////////////////
////////////////////////////////////////
//-----------------------------------------------------------
ArmsRecorder::ArmsRecorder(string moduleName, string robotName):RateThread(20)
{
counter = 0;
ddTO=ddRA=ddLA=0; ddCartRA=ddCartLA=0;
robot=robotName;
//part=armPart;
name=moduleName;
status = IDLE;
startTime=Time::now();
fileName="object.txt";
// useful stuff for recording
cur_x_RA.resize(3,0.0); cur_x_LA=cur_x_RA;
cur_o_RA.resize(4,0.0); cur_o_LA=cur_x_LA;
cur_q_RA.resize(15,0.0);
cur_q_LA.resize(15,0.0);
cur_q_TO.resize(3,0.0);
recordedTraj_RA.clear();
recordedTraj_LA.clear();
recordedTraj_TO.clear();
tmp.clear();
descriptor.clear();
}
//-----------------------------------------------------------
ArmsRecorder::~ArmsRecorder(){}
//-----------------------------------------------------------
thread_status ArmsRecorder::getThreadStatus()
{
return status;
}
//-----------------------------------------------------------
bool ArmsRecorder::threadInit()
{
//now connect to the robot
bool dderror = false;
// RIGHT ARM: joint
optionsRA.put("device","remote_controlboard");
optionsRA.put("local",string("/"+name+"/right_arm").c_str());
optionsRA.put("remote",string("/"+robot+"/right_arm").c_str());
ddRA = new PolyDriver;
// LEFT ARM: joint + cartesian
optionsLA.put("device","remote_controlboard");
optionsLA.put("local",string("/"+name+"/left_arm").c_str());
optionsLA.put("remote",string("/"+robot+"/left_arm").c_str());
ddLA = new PolyDriver;
// TORSO: joint only
optionsTO.put("device","remote_controlboard");
optionsTO.put("local",string("/"+name+"/torso").c_str());
optionsTO.put("remote",string("/"+robot+"/torso").c_str());
ddTO = new PolyDriver;
// CHECK DD
if(!ddRA->open(optionsRA))
{
cout<<"TaskRecorder: problems connecting to the remote driver of RIGHT ARM" << endl;
dderror = true;
}
if(!ddLA->open(optionsLA))
{
cout<<"TaskRecorder: problems connecting to the remote driver of LEFT ARM" << endl;
dderror = true;
}
if(!ddTO->open(optionsTO))
{
cout<<"TaskRecorder: problems connecting to the remote driver of TORSO" << endl;
dderror = true;
}
if(!ddRA->view(iencRA) )
{
cout<<"TaskRecorder: problem in acquiring interfaces, aborting thread for RIGTH ARM"<<endl;
dderror = true;
}
if(!ddLA->view(iencLA) )
{
cout<<"TaskRecorder: problem in acquiring interfaces, aborting thread for LETF ARM"<<endl;
dderror = true;
}
if(!ddTO->view(iencTO) )
{
cout<<"TaskRecorder: problem in acquiring interfaces, aborting thread for TORSO"<<endl;
dderror = true;
}
if (dderror)
{
delete ddTO;
delete ddRA;
delete ddLA;
return false;
}
// RA now connect to cartesian interfaces
optionsCartRA.put("device","cartesiancontrollerclient");
optionsCartRA.put("remote",string("/"+robot+"/cartesianController/right_arm").c_str());
optionsCartRA.put("local",string("/"+name+"/right_arm/cartesian").c_str());
ddCartRA = new PolyDriver;
// LA now connect to cartesian interfaces
optionsCartLA.put("device","cartesiancontrollerclient");
optionsCartLA.put("remote",string("/"+robot+"/cartesianController/left_arm").c_str());
optionsCartLA.put("local",string("/"+name+"/left_arm/cartesian").c_str());
ddCartLA = new PolyDriver;
if (!ddCartRA->open(optionsCartRA))
{
cout<<"TaskRecorder: problems connecting to the Cartesian interface of RIGTH ARM"<<endl;
dderror = true;
}
if (!ddCartLA->open(optionsCartLA))
{
cout<<"TaskRecorder: problems connecting to the Cartesian interface of LEFT ARM"<<endl;
dderror = true;
}
if(!ddCartRA->isValid())
{
cout<<"TaskRecorder: invalid Cartesian interface for RIGTH ARM" <<endl;
dderror = true;
}
if(!ddCartLA->isValid())
{
cout<<"TaskRecorder: invalid Cartesian interface for LEFT ARM" <<endl;
dderror = true;
}
if(!ddCartRA->view(icrtRA))
{
cout<<"TaskRecorder: problem in acquiring Cartesian interface, aborting thread"<<endl;
dderror = true;
}
if(!ddCartLA->view(icrtLA))
{
cout<<"TaskRecorder: problem in acquiring Cartesian interface, aborting thread"<<endl;
dderror = true;
}
if (dderror)
{
delete ddCartRA;
delete ddCartLA;
return false;
}
// initializing filters
// left arm
filter_xd_L = new AWLinEstimator(16,1.0);
filter_od_L = new AWLinEstimator(16,1.0);
filter_qd_L = new AWLinEstimator(16,1.0);
// right arm
filter_xd_R = new AWLinEstimator(16,1.0);
filter_od_R = new AWLinEstimator(16,1.0);
filter_qd_R = new AWLinEstimator(16,1.0);
// torso
filter_qd_T = new AWLinEstimator(16,1.0);
//
status = IDLE;
startTime=Time::now();
cout<<"ArmsRecorder is ready"<<endl;
return true;
}
//-----------------------------------------------------------
void ArmsRecorder::threadRelease()
{
cout<<"Closing drivers"<<endl;
if(filter_xd_L){ delete filter_xd_L;filter_xd_L = 0;}
if(filter_od_L){ delete filter_od_L;filter_od_L = 0;}
if(filter_qd_L){ delete filter_qd_L;filter_qd_L = 0;}
if(filter_xd_R){ delete filter_xd_R;filter_xd_R = 0;}
if(filter_od_R){ delete filter_od_R;filter_od_R = 0;}
if(filter_qd_R){ delete filter_qd_R;filter_qd_R = 0;}
if(filter_qd_T){ delete filter_qd_T;filter_qd_T = 0;}
delete ddRA;
delete ddLA;
delete ddTO;
delete ddCartRA;
delete ddCartLA;
ddRA = NULL;
ddLA = NULL;
ddTO = NULL;
ddCartRA = NULL;
ddCartLA = NULL;
cout<<"Closing ArmsRecorder"<<endl;
}
//-----------------------------------------------------------
void ArmsRecorder::run()
{
//cout<<curTime<<endl;
// in any case, update the filters for estimating velocities
//
icrtRA->getPose(cur_x_RA, cur_o_RA);
iencRA->getEncoders(cur_q_RA.data());
//
icrtLA->getPose(cur_x_LA,cur_o_LA);
iencLA->getEncoders(cur_q_LA.data());
//
iencTO->getEncoders(cur_q_TO.data());
filterEstimation();
if(status==RECORDING)
{
//collect datas from ports
if (counter == 0)
{
startTime = Time::now ();
}
curTime = Time::now();
actualTime = curTime - startTime;
// save them somewhere (they'll be written when we stop recording)
int c=0;
descriptor.clear();
descriptor.str("");
// saving data: time endeff=(xyz o1234) fingers=(q7..15)
recordedTraj_timestamp.push_back (actualTime);
descriptor << "actualTime" <<endl;
printf("\nRecording time (ms) \r%f \n",actualTime);
//cout << " Recording " << actualTime << endl;
//=================== RA
c = 0;
tmp.clear();
tmp.resize(7+16+7+16);
descriptor<< "Rigth Arm" <<endl;
tmp[c++] = DSCP(cur_x_RA[0],descriptor);
tmp[c++] = DSCP(cur_x_RA[1],descriptor);
tmp[c++] = DSCP(cur_x_RA[2],descriptor);
tmp[c++] = DSCP(cur_o_RA[0],descriptor);
tmp[c++] = DSCP(cur_o_RA[1],descriptor);
tmp[c++] = DSCP(cur_o_RA[2],descriptor);
tmp[c++] = DSCP(cur_o_RA[3],descriptor);
for(int i=0; i<=15;i++)
{
tmp[c++] = DSCPI(cur_q_RA[i],descriptor,i);
}
tmp[c++] = DSCP(cur_xd_RA[0],descriptor);
tmp[c++] = DSCP(cur_xd_RA[1],descriptor);
tmp[c++] = DSCP(cur_xd_RA[2],descriptor);
tmp[c++] = DSCP(cur_od_RA[0],descriptor);
tmp[c++] = DSCP(cur_od_RA[1],descriptor);
tmp[c++] = DSCP(cur_od_RA[2],descriptor);
tmp[c++] = DSCP(cur_od_RA[3],descriptor);
for(int i=0; i<=15;i++)
{
tmp[c++] = DSCPI(cur_qd_RA[i],descriptor,i);
}
recordedTraj_RA.push_back(tmp);
//=================== LA
c = 0;
tmp.clear();
tmp.resize(7+16+7+16);
descriptor<< "Left Arm" <<endl;
tmp[c++] = DSCP(cur_x_LA[0],descriptor);
tmp[c++] = DSCP(cur_x_LA[1],descriptor);
tmp[c++] = DSCP(cur_x_LA[2],descriptor);
tmp[c++] = DSCP(cur_o_LA[0],descriptor);
tmp[c++] = DSCP(cur_o_LA[1],descriptor);
tmp[c++] = DSCP(cur_o_LA[2],descriptor);
tmp[c++] = DSCP(cur_o_LA[3],descriptor);
for(int i=0; i<=15;i++)
{
tmp[c++] = DSCPI(cur_q_LA[i],descriptor,i);
}
tmp[c++] = DSCP(cur_xd_LA[0],descriptor);
tmp[c++] = DSCP(cur_xd_LA[1],descriptor);
tmp[c++] = DSCP(cur_xd_LA[2],descriptor);
tmp[c++] = DSCP(cur_od_LA[0],descriptor);
tmp[c++] = DSCP(cur_od_LA[1],descriptor);
tmp[c++] = DSCP(cur_od_LA[2],descriptor);
tmp[c++] = DSCP(cur_od_LA[3],descriptor);
for(int i=0; i<=15;i++)
{
tmp[c++] = DSCPI(cur_qd_LA[i],descriptor,i);
}
recordedTraj_LA.push_back(tmp);
//=================== TO
c = 0;
tmp.clear();
tmp.resize(3+3);
descriptor<< "Torso" <<endl;
for(int i=0; i<3;i++)
{
tmp[c++] = DSCPI(cur_q_TO[i],descriptor,i);
}
for(int i=0; i<3;i++)
{
tmp[c++] = DSCPI(cur_qd_TO[i],descriptor,i);
}
recordedTraj_TO.push_back(tmp);
counter++;
}
else
{
/*
if(curTime-startTime) > 60)
{
cout<<"ArmsRecorder: waiting alive "<<endl;
}
*/
Time::delay(1.0);
}
}
//-----------------------------------------------------------
bool ArmsRecorder::startRecording()
{
if(status==RECORDING)
{
cout<<"ArmsRecorder is already recording. Error."<<endl;
return false;
}
counter=0;
startTime = Time::now();
recordedTraj_timestamp.clear ();
status = RECORDING;
return true;
}
//-----------------------------------------------------------
double ArmsRecorder::stopRecording()
{
if(status==IDLE)
{
cout<<"ArmsRecorder is not recording, why do you want to stop it? Error."<<endl;
return -1.0;
}
status = IDLE;
return (Time::now() - startTime);
}
//-----------------------------------------------------------
bool ArmsRecorder::flushRecording()
{
if(status==RECORDING)
{
cout<<"ArmsRecorder will only write to file when the recording is over. Error."<<endl;
return false;
}
int totalRec = recordedTraj_RA.size();
ofstream out;
out.open(fileName.c_str());
if(!out)
{
cout<<"TaskRecorder can't open in write mode the trajectory file: "<<fileName<<endl
<<"Aborting."<<endl;
return false;
}
else
cout<<"ArmsRecorder: saving "<<totalRec<<" items on "<<fileName<<endl;
tmp.clear();
for(int i=0; i<totalRec;i++)
{
out << recordedTraj_timestamp.front () << " ";
out << recordedTraj_RA.front().toString () << " ";
out << recordedTraj_LA.front ().toString () << " ";
out << recordedTraj_TO.front ().toString () << " ";
out << endl;
recordedTraj_timestamp.pop_front ();
recordedTraj_RA.pop_front();
recordedTraj_LA.pop_front();
recordedTraj_TO.pop_front();
}
recordedTraj_RA.clear();
recordedTraj_LA.clear();
recordedTraj_TO.clear();
tmp.clear();
out.close();
out.open(string(fileName+".info").c_str());
if(!out)
{
cout<<"ArmsRecorder: there were errors saving the information file: "<<string(fileName+".info")<<endl
<<"Aborting."<<endl;
return false;
}
else
cout<<"ArmsRecorder: saving info file on "<<string(fileName+".info")<<endl;
out<<descriptor.str()<<endl;
out.close();
return true;
}
//----------------------------------------------------------
void ArmsRecorder::setRecording(string outFile)
{
fileName = outFile;
cout << "ArmsRecorder will save object in file=" << fileName << endl;
}
//-----------------------------------------------------------
void ArmsRecorder::filterEstimation()
{
AWPolyElement el;
el.time=Time::now();
// LA
el.data = cur_x_LA;
cur_xd_LA = filter_xd_L->estimate(el);
el.data = cur_o_LA;
cur_od_LA = filter_od_L->estimate(el);
el.data = cur_q_LA;
cur_qd_LA = filter_qd_L->estimate(el);
// RA
el.data = cur_x_RA;
cur_xd_RA = filter_xd_R->estimate(el);
el.data = cur_o_RA;
cur_od_RA = filter_od_R->estimate(el);
el.data = cur_q_RA;
cur_qd_RA = filter_qd_R->estimate(el);
//TO
el.data = cur_q_TO;
cur_qd_TO = filter_qd_T->estimate(el);
}