generated from vvv-school/assignment_make-it-roll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
409 lines (349 loc) · 10.3 KB
/
main.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
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
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
//
// Author: Ugo Pattacini - <ugo.pattacini@iit.it>
#include <string>
#include <mutex>
#include <yarp/os/all.h>
#include <yarp/dev/all.h>
#include <yarp/sig/all.h>
#include <yarp/math/Math.h>
#include "helpers.h"
using namespace std;
using namespace yarp::os;
using namespace yarp::dev;
using namespace yarp::sig;
using namespace yarp::math;
/***************************************************/
class CtrlModule: public RFModule
{
protected:
PolyDriver drvArm, drvGaze;
ICartesianControl *iarm;
IGazeControl *igaze;
BufferedPort<ImageOf<PixelRgb> > imgLPortIn,imgRPortIn;
BufferedPort<ImageOf<PixelRgb> > imgLPortOut,imgRPortOut;
RpcServer rpcPort;
mutex mtx;
Vector cogL,cogR;
Vector x0, o0;
bool okL,okR;
bool simulation;
ObjectRetriever object;
/***************************************************/
bool getCOG(ImageOf<PixelRgb> &img, Vector &cog)
{
int xMean=0;
int yMean=0;
int ct=0;
for (int x=0; x<img.width(); x++)
{
for (int y=0; y<img.height(); y++)
{
PixelRgb &pixel=img.pixel(x,y);
if ((pixel.b>5.0*pixel.r) && (pixel.b>5.0*pixel.g))
{
xMean+=x;
yMean+=y;
ct++;
}
}
}
if (ct>0)
{
cog.resize(2);
cog[0]=xMean/ct;
cog[1]=yMean/ct;
return true;
}
else
return false;
}
/***************************************************/
Vector retrieveTarget3D(const Vector &cogL, const Vector &cogR)
{
// FILL IN THE CODE
Vector pos(3);
igaze->triangulate3DPoint(cogL, cogR, pos);
return pos;
}
/***************************************************/
void fixate(const Vector &x)
{
// FILL IN THE CODE
igaze->lookAtFixationPoint(x);
}
/***************************************************/
Vector computeHandOrientation()
{
// FILL IN THE CODE
Vector x(3);
Vector o(4);
iarm->getPose(x, o);
return o;
}
/***************************************************/
void approachTargetWithHand(const Vector &x, const Vector &o)
{
// FILL IN THE CODE
Vector newDof, curDof;
iarm->getDOF(curDof);
newDof = curDof;
newDof[0] = 1;
newDof[1] = 1;
newDof[2] = 1;
iarm->setDOF(newDof, curDof);
Vector xd;
xd = x;
xd[0] = x[0] + 0.05;
xd[1] = x[1] + 0.05;
xd[2] = x[2] + 0.05;
iarm->goToPose(xd, o);
Time::delay(5);
}
/***************************************************/
void roll(const Vector &x, const Vector &o)
{
// FILL IN THE CODE
Vector xd;
xd = x;
xd[0] = x[0] + 0.05;
xd[1] = x[1] - 0.05;
xd[2] = x[2] + 0.05;
iarm->goToPose(xd, o);
iarm->setTrajTime(0.5);
bool done = false;
while(!done){
iarm->checkMotionDone(&done);
Time::delay(0.1);
}
}
/***************************************************/
void look_down()
{
// we ask the controller to keep the vergence
// from now on fixed at 5.0 deg, which is the
// configuration where we calibrated the stereo-vision;
// without that, we cannot retrieve good 3D positions
// with the real robot
if (!simulation)
igaze->blockEyes(5.0);
// FILL IN THE CODE
Vector ang(3);
iarm->getPose(x0, o0);
ang[0] = 0;
ang[1] = -45;
ang[2] = 5;
igaze->lookAtAbsAngles(ang);
bool done = false;
while(!done){
iarm->checkMotionDone(&done);
Time::delay(0.1);
}
}
/***************************************************/
bool make_it_roll(const Vector &cogL, const Vector &cogR)
{
Vector x;
if (simulation)
{
yInfo()<<"detected cogs = ("<<cogL.toString(0,0)<<") ("<<cogR.toString(0,0)<<")";
x=retrieveTarget3D(cogL,cogR);
}
else if (!object.getLocation(x))
return false;
yInfo()<<"retrieved 3D point = ("<<x.toString(3,3)<<")";
fixate(x);
yInfo()<<"fixating at ("<<x.toString(3,3)<<")";
Vector o=computeHandOrientation();
yInfo()<<"computed orientation = ("<<o.toString(3,3)<<")";
approachTargetWithHand(x,o);
yInfo()<<"approached";
roll(x,o);
yInfo()<<"roll!";
return true;
}
/***************************************************/
void home()
{
// FILL IN THE CODE
iarm->goToPose(x0, o0);
bool done = false;
while(!done){
iarm->checkMotionDone(&done);
Time::delay(0.1);
}
Vector ang(3);
ang[0] = 0;
ang[1] = 0;
ang[2] = 5;
igaze->lookAtAbsAngles(ang);
}
public:
/***************************************************/
bool configure(ResourceFinder &rf)
{
string robot=rf.check("robot",Value("icubSim")).asString();
simulation=(robot=="icubSim");
Property optArm;
optArm.put("device","cartesiancontrollerclient");
optArm.put("remote","/"+robot+"/cartesianController/right_arm");
optArm.put("local","/cartesian_client/right_arm");
// let's give the controller some time to warm up
bool ok=false;
double t0=Time::now();
while (Time::now()-t0<10.0)
{
// this might fail if controller
// is not connected to solver yet
if (drvArm.open(optArm))
{
ok=true;
break;
}
Time::delay(1.0);
}
if (!ok)
{
yError()<<"Unable to open the Cartesian Controller";
return false;
}
drvArm.view(iarm);
// FILL IN THE CODE
Property optGaze;
optGaze.put("device","gazecontrollerclient");
optGaze.put("remote","/iKinGazeCtrl");
optGaze.put("local","/tracker/gaze");
if(!drvGaze.open(optGaze))
{
yError()<<"Unable to open the Gaze Controller";
return false;
}
drvGaze.view(igaze);
imgLPortIn.open("/imgL:i");
imgRPortIn.open("/imgR:i");
imgLPortOut.open("/imgL:o");
imgRPortOut.open("/imgR:o");
rpcPort.open("/service");
attach(rpcPort);
updateModule();
return true;
}
/***************************************************/
bool interruptModule()
{
imgLPortIn.interrupt();
imgRPortIn.interrupt();
return true;
}
/***************************************************/
bool close()
{
drvArm.close();
drvGaze.close();
imgLPortIn.close();
imgRPortIn.close();
imgLPortOut.close();
imgRPortOut.close();
rpcPort.close();
return true;
}
/***************************************************/
bool respond(const Bottle &command, Bottle &reply)
{
string cmd=command.get(0).asString();
if (cmd=="help")
{
reply.addVocab32("many");
reply.addString("Available commands:");
reply.addString("- look_down");
reply.addString("- make_it_roll");
reply.addString("- home");
reply.addString("- quit");
}
else if (cmd=="look_down")
{
look_down();
// we assume the robot is not moving now
reply.addString("ack");
reply.addString("Yep! I'm looking down now!");
}
else if (cmd=="make_it_roll")
{
// FILL IN THE CODE
bool go=false; // you need to properly handle this flag
if(okL && okR){
go = true;
}
bool rolled=false;
if (go || !simulation)
rolled=make_it_roll(cogL,cogR);
// we assume the robot is not moving now
if (rolled)
{
reply.addString("ack");
reply.addString("Yeah! I've made it roll like a charm!");
}
else
{
reply.addString("nack");
reply.addString("I don't see any object!");
}
}
else if (cmd=="home")
{
home();
// we assume the robot is not moving now
reply.addString("ack");
reply.addString("I've got the hard work done! Gone home.");
}
else
// the father class already handles the "quit" command
return RFModule::respond(command,reply);
return true;
}
/***************************************************/
double getPeriod()
{
return 0.0; // sync upon incoming images
}
/***************************************************/
bool updateModule()
{
// get fresh images
ImageOf<PixelRgb> *imgL=imgLPortIn.read();
ImageOf<PixelRgb> *imgR=imgRPortIn.read();
// interrupt sequence detected
if ((imgL==NULL) || (imgR==NULL))
return false;
// compute the center-of-mass of pixels of our color
mtx.lock();
okL=getCOG(*imgL,cogL);
okR=getCOG(*imgR,cogR);
mtx.unlock();
PixelRgb color;
color.r=255; color.g=0; color.b=0;
if (okL)
draw::addCircle(*imgL,color,(int)cogL[0],(int)cogL[1],5);
if (okR)
draw::addCircle(*imgR,color,(int)cogR[0],(int)cogR[1],5);
imgLPortOut.prepare()=*imgL;
imgRPortOut.prepare()=*imgR;
imgLPortOut.write();
imgRPortOut.write();
return true;
}
};
/***************************************************/
int main(int argc, char *argv[])
{
Network yarp;
if (!yarp.checkNetwork())
{
yError()<<"YARP doesn't seem to be available";
return 1;
}
ResourceFinder rf;
rf.configure(argc,argv);
CtrlModule mod;
return mod.runModule(rf);
}