-
Notifications
You must be signed in to change notification settings - Fork 8
/
kilobotidassignment.cpp
438 lines (376 loc) · 13.7 KB
/
kilobotidassignment.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
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
#include "kilobotidassignment.h"
#include <QDebug>
KilobotIDAssignment::KilobotIDAssignment(assignmethod method): method(method)
{
}
void KilobotIDAssignment::initialise(bool)
{
if(method==BINARY) {
numofdigits=11;
}
if(method==BASETHREE) {
numofdigits=8;
}
emit clearDrawings();
// calibrate LEDs
emit setTrackingType(LED);
emit getInitialKilobotStates();
QVector<uint8_t> data;
data.resize(9);
// current temp ID
data[0] = method;
kilobot_broadcast msg;
msg.type = 10;
msg.data = data;
emit broadcastMessage(msg);
this->time = 0.0;
this->lastTime = 0.0;
// this->numFound = 0;
this->numSegments = 0;
this->switchSegment = true;
this->stage = START;
qDebug() << "Starting the ID Assignment";
t.start();
// Init Log File operations
if (saveImages){
if (log_file.isOpen()){
log_file.close(); // if it was open I close and re-open it (erasing old content!! )
}
QString log_filename = log_filename_prefix + ".txt";
// QString log_filename = log_filename_prefix + "_" + QDate::currentDate().toString("yyMMdd") + "_" + QTime::currentTime().toString("hhmmss") + ".txt";
log_file.setFileName( log_filename );
if ( !log_file.open(QIODevice::WriteOnly) ) { // open file
qDebug() << "ERROR(!) in opening file" << log_filename;
} else {
qDebug () << "Log file" << log_file.fileName() << "opened.";
log_stream.setDevice(&log_file);
}
}
}
void KilobotIDAssignment::stopExperiment(){
if (log_file.isOpen()){
qDebug() << "Closing file" << log_file.fileName();
log_file.close();
}
this->time = 0;
savedImagesCounter = 0;
}
void KilobotIDAssignment::run()
{
this->time += 0.1; // 100 ms in sec
this->lastTime += 0.1;
if (saveImages) {
if (qRound(this->time*10.0) % 5 == 0) { // every 0.5s
emit saveImage(QString("idsassign_%1.jpg").arg(savedImagesCounter++, 5,10, QChar('0')));
log_stream << this->time << "\t" << (int)this->updatedCol;
for (int kID = 0; kID < this->tempIDs.size(); ++kID){
log_stream << "\t" << allKilos[kID].position.x() << "\t" << allKilos[kID].position.y() << "\t"
<< allKilos[kID].colour << "\t" << (int)this->isAssigned[kID];
for (int d=0; d < allKilos[kID].digits.size(); ++d){
log_stream << "\t" << allKilos[kID].digits[d];
}
// for (int d=allKilos[kID].digits.size(); d < 11; ++d){
// log_stream << "\t -1";
// }
}
log_stream << endl;
this->updatedCol = false;
}
}
// stop broadcasting (replace)
if (t_since > 0) {
--t_since;
} else {
// STOP sending message
kilobot_broadcast msg;
msg.type = 250;
emit broadcastMessage(msg);
}
if (time > 2.0f) {
switch (this->stage) {
case START:
{
//qDebug() << "START" << t.elapsed();
this->lastTime = 0;
this->switchSegment = true;
this->stage = TEST;
// this->numFound = 0;
this->numSegments = 0;
for (int i = 0; i < tempIDs.size(); ++i) {
// reset dupes
if (this->tempIDs[i] == DUPE) this->tempIDs[i] = 0;
}
// get num
kilobot_broadcast msg;
msg.type = 1;
emit broadcastMessage(msg);
t_since = 2;
// reset log files
if (saveImages){
for (int id = 0; id < this->tempIDs.size(); ++id){
allKilos[id].digits.fill(-1);
}
}
break;
}
case TEST:
{
if (this->switchSegment == true) {
qDebug() << "Asking the robots to show new digit (t:" << lastTime << t.elapsed() <<")";
this->switchSegment = false;
// when we have all segments
if (numSegments > (numofdigits-1)) {
for (int i = 0; i < tempIDs.size(); ++i) {
qDebug() << i << "[" << this->isAssigned[i] << "]:" << this->tempIDs[i];
}
this->dupesFound = false;
// work out if we have duplicates, if so fix them
for (int i = 0; i < this->tempIDs.size(); ++i) {
// if we haven't already been identified as a dupe, or assigned
if (tempIDs[i] != DUPE && !this->isAssigned[i]) {
// already compared to ones below
int temp = tempIDs[i];
for (int j = i+1; j < this->tempIDs.size(); ++j) {
if (temp == tempIDs[j] && !this->isAssigned[j]) {
tempIDs[i] = DUPE;
tempIDs[j] = DUPE;
// signal dupes
this->dupesFound = true;
}
}
}
}
this->stage = CONFIRM;
}
// get next seg
kilobot_broadcast msg;
msg.type = 4;
emit broadcastMessage(msg);
t_since = 2;
}
if (lastTime > 2.0f*float(numSegments+1)+0.21f) {
++numSegments;
clearDrawings();
emit updateKilobotStates();
this->switchSegment = true;
}
break;
}
case CONFIRM:
{
// qDebug() << "CONFIRMING" << lastTime;
if((this->confirmationrequestsent==false)){
// get the next tempID to confirm
while( (this->tempIDs[increment] == DUPE || this->isAssigned[increment]) && (increment<tempIDs.size()-1) )
increment++;
// send a confirmation request to the current tempID holder
QVector<uint8_t> data;
data.resize(9);
// current temp ID
data[0] = (this->tempIDs[increment] >> 8)&0xFF;
data[1] = this->tempIDs[increment]&0xFF;
kilobot_broadcast msg;
msg.type = 5;
msg.data = data;
emit broadcastMessage(msg);
t_since=20;
this->confirmationrequestsent=true;
qDebug() << "Confirmation message sent to robot: " << increment;
}
else {
// get the confirmation
if(t_since==0){
//qDebug() << "Confirming robot: " << increment-1;
clearDrawings();
emit updateKilobotStates();
this->confirmationrequestsent=false;
increment++;
}
}
if(increment>=tempIDs.size()){
increment=0;
this->stage = SEND;
}
break;
}
case SEND:
{
if (lastTime > 25.0f) {
// for (int i = 0; i < tempIDs.size(); ++i) {
// qDebug() << i << "[" << this->isAssigned[i] << "]:" << this->tempIDs[i];
// }
for (int id = 0; id < tempIDs.size(); ++id) {
//qDebug() << "SEND" << lastTime;
if (this->tempIDs[id] != DUPE && !this->isAssigned[id]) {
QVector<uint8_t> data;
data.resize(9);
// current temp ID
data[0] = (this->tempIDs[id] >> 8)&0xFF;
data[1] = this->tempIDs[id]&0xFF;
// UID to set
data[2] = (id >> 8)&0xFF;
data[3] = id&0xFF;
kilobot_broadcast msg;
msg.type = 2;
msg.data = data;
emit broadcastMessage(msg);
this->isAssigned[id] = true; // set as assigned
}
}
t_since = tempIDs.size() + 2;
if (dupesFound) {
this->stage = RETRY;
t_move=20;
} else {
this->stage = COMPLETE;
}
}
break;
}
case RETRY:
{
if (t_since <= 0) {
//qDebug() << "RETRY" << lastTime;
kilobot_broadcast msg;
msg.type = 3;
emit broadcastMessage(msg);
t_since = 2;
if(t_move>0){
t_move--;}
else{
this->stage = START;
}
}
break;
}
case COMPLETE:
{
qDebug() << "Successfully assigned a sequentional ID to all the " << tempIDs.size() << " Kilobots on the table." << lastTime;
if (!saveImages){
emit experimentComplete();
}
break;
}
}
}
}
// run once for each kilobot after emitting getInitialKilobotStates() signal
void KilobotIDAssignment::setupInitialKilobotState(Kilobot kilobotCopy)
{
// resize and insert kilobot ID
if (kilobotCopy.getID()+1 > this->tempIDs.size()) {
this->tempIDs.resize(kilobotCopy.getID() + 1);
this->isAssigned.resize(kilobotCopy.getID() + 1);
this->allKilos.resize(kilobotCopy.getID() + 1);
}
this->tempIDs[kilobotCopy.getID()] = DUPE;
this->isAssigned[kilobotCopy.getID()] = false;
if (saveImages){
KiloLog kLog(kilobotCopy.getID(), kilobotCopy.getPosition()*PIXEL_TO_MM, 0, kilobotCopy.getLedColour(),numofdigits);
this->allKilos[kilobotCopy.getID()] = kLog;
updatedCol = true;
}
}
// run once for each kilobot after emitting updateKilobotStates() signal
void KilobotIDAssignment::updateKilobotState(Kilobot kilobotCopy)
{
if (isAssigned[kilobotCopy.getID()]){
// mark with a small green dot the assigned robots
drawCircle(QPointF(kilobotCopy.getPosition().x(),kilobotCopy.getPosition().y()),2, QColor(0,255,0), 2,"");
}
// check colours
if(this->stage==TEST){
if (isAssigned[kilobotCopy.getID()]){
return;
}
lightColour col = kilobotCopy.getLedColour();
// QString colName;
// if (col == OFF) colName = "OFF";
// if (col == RED) colName = "RED";
// if (col == GREEN) colName = "GREEN";
// if (col == BLUE) colName = "BLUE";
// qDebug() << "KB" << kilobotCopy.getID() << "part " << numSegments -1 << " = " << colName << t.elapsed();
int col2 = 0;
if(method==BINARY){
if (col == RED) {
col2 = 0;
}
else if (col == BLUE) {
col2 = 1;
}
else {
//qDebug() << "DETECTION ERROR";
}
this->tempIDs[kilobotCopy.getID()] += int(col2) * binaryMultipliers[numSegments-1];
}
if(method==BASETHREE){
if (col == RED) {
col2 = 0;
}
else if (col == BLUE) {
col2 = 1;
}
else if (col == GREEN) {
col2 = 2;
}
else {
//qDebug() << "DETECTION ERROR";
}
this->tempIDs[kilobotCopy.getID()] += int(col2) * baseThreeMultipliers[numSegments-1];
}
if (saveImages){
kilobot_id kID = kilobotCopy.getID();
kilobot_colour kCol = kilobotCopy.getLedColour();
QPointF kPos = kilobotCopy.getPosition()*PIXEL_TO_MM;
//double kRot = qRadiansToDegrees(qAtan2(-kilobotCopy.getVelocity().y(), kilobotCopy.getVelocity().x()));
this->allKilos[kID].updateAllValues(kID, kPos, 0, kCol);
this->updatedCol = true;
this->allKilos[kID].digits[numSegments-1] = col2;
}
}
if(this->stage==CONFIRM){
if (isAssigned[kilobotCopy.getID()])
return;
else{
lightColour col = kilobotCopy.getLedColour();
if (col == BLUE){
QString colName = "BLUE";
qDebug() << "KB" << kilobotCopy.getID() << "confirms ID" << increment << "with" << colName << "light";
}
if(kilobotCopy.getID()==increment){
if (col != BLUE) {
tempIDs[increment]=DUPE;
this->dupesFound = true;
}
}
else{
if (col == BLUE){
tempIDs[increment]=DUPE;
tempIDs[kilobotCopy.getID()]=DUPE;
this->dupesFound = true;
}
}
}
}
// clearDrawings();
// QColor rgbColor(0,0,0);
// switch (col){
// case OFF:{
// rgbColor.setRgb(0,0,0);
// break;
// }
// case RED:{
// rgbColor.setRgb(255,0,0);
// break;
// }
// case GREEN:{
// rgbColor.setRgb(0,255,0);
// break;
// }
// case BLUE:{
// rgbColor.setRgb(0,0,255);
// break;
// }
// }
// drawCircle(kilobotCopy.getPosition(), 5, rgbColor, 2);
}