forked from rtv/stagectrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contribcircle.cc
860 lines (700 loc) · 23.7 KB
/
contribcircle.cc
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctime>
#include <list>
#include <deque>
#include <algorithm>
#include "stage.hh"
using namespace Stg;
using namespace std;
enum RobotState
{
UNKNOWN = 0,
CHARGING = 1,
SEARCHING = 2,
INGROUP = 3,
HOMING = 4
};
enum MessageType
{
ADDROBOT = 0,
REMOVEROBOT = 1,
IAMCOMMING = 2,
IAMLEAVING = 3,
WELCOME = 4,
BYE = 5
};
/* Minimal Message Structure */
struct RobotMessage {
MessageType type;
unsigned int sender;
unsigned int receiver;
double t;
unsigned int robotid;
unsigned int relaycount;
};
typedef struct
{
ModelPosition* position;
//ModelRanger* ranger; //Sonar Sensor
ModelFiducial* fiducial;
ModelFiducial::Fiducial* closest;
radians_t closest_bearing;
meters_t closest_range;
radians_t closest_heading_error;
list<RobotMessage> inbox;
RobotState state;
list<unsigned int> teammates;
deque<double> d2c; //This one is used to check if robot is in group
double energy;
Stg::Pose charger;
} robot_t;
// Define const double XXX = YYYY; Here for global access during control
static int NUMBEROFROBOTS = 36; //For Performance analysis as well as home point calculation
static list<Stg::Pose> HomePositions;
static bool isGlobalInitDone = false;
static int loggerID; // The first robot who become active in Stage, logs everything
static int NUMBEROFSEARCHINGROBOTS = 0; //For Performance analysis
static double timeOrigin;
static bool bEnabled = true; //Behaviour Enabled
static bool energyEnabled = true; //Enerrgy Consumption
static bool leaderEnabled = false;
static bool perfectMode = false; //TODO: this must be used with --noenergy
static int globalRelayCount = 3;
static double robotCameraFOV = 60.0;
static double robotCameraDepth = 1.0;
static long int totalMessageCounterNaive = (NUMBEROFROBOTS * (NUMBEROFROBOTS - 1)) / 2;
static long int totalMessageCounterMy = 0;
const double FATTMAX = 1.0;
const double FREPMAX = -1.0;
const double FWATT = 1.0;
const double RMIN = 0.0;
static list<RobotMessage> MessageList;
// This is aligned with RobotStates
const Stg::Color StateColors[5] = {Color("white"), Color("grey"), Color("blue"), Color("green"), Color("cyan")};
// Local Functions
double getCurrentTimeStamp()
{
timeval tt;
gettimeofday(&tt, NULL);
double sec = (double) tt.tv_sec;
double usec = (double) tt.tv_usec;
return sec + (usec / 1.0e6);
}
void setRobotState(robot_t* r, RobotState s)
{
/* For analysis */
if ((r->state != INGROUP) && (s == INGROUP))
{
totalMessageCounterNaive += NUMBEROFROBOTS;
NUMBEROFSEARCHINGROBOTS++;
}
else if ((r->state == INGROUP) && (s != INGROUP))
{
totalMessageCounterNaive += NUMBEROFROBOTS;
NUMBEROFSEARCHINGROBOTS--;
}
/* end */
r->state = s;
r->position->SetColor(StateColors[s]);
r->inbox.clear();
r->d2c.clear();
}
// No Broadcast support yet ...
void sendMessage(MessageType type, int sender, int receiver, unsigned int rid, unsigned int rc)
{
// if (MessageList.size() > MAX_MESSAGE_LIST_SIZE)
// return;
RobotMessage msg;
msg.type = type;
msg.t = getCurrentTimeStamp();
msg.sender = sender;
msg.receiver = receiver;
msg.robotid = rid;
msg.relaycount = rc;
/*
* Do not insert duplicate messages
* Further consideration must taken into account:
* Timestamp
* RelayCount
*
* It is a naive implementation, might become source of undesired behavior
*
*/
bool found = false;
list<RobotMessage>::iterator i;
RobotMessage stored;
for (i = MessageList.begin(); (i != MessageList.end()); ++i)
{
stored = (*i);
if (
(msg.type == stored.type) &&
(msg.sender == stored.sender) &&
(msg.receiver == stored.receiver) &&
(msg.robotid == stored.robotid)
)
{
found = true;
break;
}
}
if (!found)
{
totalMessageCounterMy++;
MessageList.push_back(msg);
}
}
void updateMyInbox(robot_t* robot)
{
list<RobotMessage>::iterator i;
RobotMessage msg;
i = MessageList.begin();
while (i != MessageList.end())
{
msg = (*i);
if (msg.receiver == robot->position->GetId())
{
robot->inbox.push_back(msg);
MessageList.erase(i++);
}
else
{
++i;
}
}
}
/* Golden!*/
double calc_r(int N)
{
if (N <= 1) return 0.0;
double theta = (2 * 3.1415) / double(N);
double intr = (1.0 - cos(robotCameraFOV - theta)) / (1.0 - cos(theta));
//TODO: extreme cases
if (intr < 0)
{
return 0;
}
else
{
return (robotCameraDepth - 0.5) * sqrt(intr);
}
}
//void updateRobotBelief(robot_t robot, )
double sigmoid(double x)
{
return 1.0 / (1.0 + exp(-1.0 * x));
}
class CCVis : public Visualizer
{
public:
robot_t& rob;
int res;
vector<Stg::Pose> vertx;
CCVis( robot_t& rob )
: Visualizer( "Make Circle", "vis_make_circle" ), rob(rob), res(8)
{
}
virtual ~CCVis()
{
}
virtual void Visualize( Model* mod, Camera* cam )
{
if (rob.state != INGROUP) return;
glPointSize( 8.0 );
glPushMatrix();
//glRotatef( -rtod(mod->GetGlobalPose().a), 0,0,1 );
GLdouble x = robotCameraDepth;
GLdouble y = 0.0;
GLdouble a = robotCameraFOV / 2.0;
GLdouble dtheta = robotCameraFOV / double(res);
GLdouble vx, vy;
GLdouble theta = -a;
while (theta <= a)
{
vx = (cos(theta) * x) + (-sin(theta) * y);
vy = (sin(theta) * x) + ( cos(theta) * y);
vertx.push_back(*(new Pose(vx,vy,0.0,0.0)));
theta += dtheta;
}
mod->PushColor(1.0, 0.0, 0.0, 0.15);
glBegin( GL_POLYGON );
glVertex2f( 0.0, 0.0 );
for (int i = 0; i < vertx.size(); ++i)
{
glVertex2f( vertx.at(i).x, vertx.at(i).y);
}
glEnd();
glPopMatrix();
}
};
// forward declare
//int RangerUpdate( ModelRanger* mod, robot_t* robot );
int FiducialUpdate( ModelFiducial* fid, robot_t* robot );
// Stage calls this when the model starts up
extern "C" int Init( Model* mod, CtrlArgs* args )
{
srand ( time(NULL) );
robot_t* robot = new robot_t;
robot->position = (ModelPosition*)mod;
if (isGlobalInitDone == false)
{
loggerID = robot->position->GetId();
timeOrigin = getCurrentTimeStamp();
printf("\nDoing one time init process, logger robot is %d \n", loggerID);
// Very Experimental
NUMBEROFROBOTS = (int) robot->position->GetWorld()->GetChildren().size() - 2;
printf("[CLC] Number of Robots in the world file: %d\n", NUMBEROFROBOTS);
Stg::Pose center(0.0, -14.0, 0.0, 0.0);
Stg::Pose p;
for (int i = 0; i < NUMBEROFROBOTS; i++)
{
p = center;
p.x = (i - int(NUMBEROFROBOTS / 2)) * 1.2;
HomePositions.push_front(p);
}
isGlobalInitDone = true;
if (args->cmdline.find("--disabled") != std::string::npos)
{
printf("[ARG] Usage of knowledge disabled.\n");
bEnabled = false;
}
else
{
printf("[ARG] Usage of knowledge enabled.\n");
}
if (args->cmdline.find("--perfect") != std::string::npos)
{
printf("[ARG] Perfect mode activated.\n");
perfectMode = true;
}
else
{
printf("[ARG] Perfect mode is de-active.\n");
}
if (args->cmdline.find("--noenergy") != std::string::npos)
{
printf("[ARG] Usage of energy disabled.\n");
energyEnabled = false;
}
else
{
printf("[ARG] Usage of energy enabled.\n");
}
if (args->cmdline.find("--leader") != std::string::npos)
{
printf("[ARG] One robot will start as INGROUP.\n");
leaderEnabled = true;
}
else
{
printf("[ARG] No robot will start as INGROUP.\n");
}
size_t found = args->cmdline.find("--relaycount");
if (found != std::string::npos)
{
size_t nextdash = args->cmdline.find("--", found+12);
size_t endsearch = (nextdash == std::string::npos) ? args->cmdline.length() : nextdash;
globalRelayCount = atoi(args->cmdline.substr(found+12, endsearch).c_str());
}
if (globalRelayCount <= 0)
{
printf("[ERROR] Invalid Relay Count! \n");
exit(1);
}
else
{
printf("[ARG] Relay Count : %d \n", globalRelayCount);
}
found = args->cmdline.find("--fov");
if (found != std::string::npos)
{
size_t nextdash = args->cmdline.find("--", found+5);
size_t endsearch = (nextdash == std::string::npos) ? args->cmdline.length() : nextdash;
robotCameraFOV = atof(args->cmdline.substr(found+5, endsearch).c_str());
}
if ((robotCameraFOV <= 0.0) || (robotCameraFOV > 180.0))
{
printf("[ERROR] Invalid FOV! (0.0 < FOV <= 180.0)\n");
exit(1);
}
else
{
printf("[ARG] Robot FOV (d) : %.2f \n", robotCameraFOV);
robotCameraFOV = Stg::dtor(robotCameraFOV);
}
found = args->cmdline.find("--depth");
if (found != std::string::npos)
{
size_t nextdash = args->cmdline.find("--", found+7);
size_t endsearch = (nextdash == std::string::npos) ? args->cmdline.length() : nextdash;
robotCameraDepth = atof(args->cmdline.substr(found+7, endsearch).c_str());
}
if ((robotCameraDepth <= 0.0) || (robotCameraDepth >= 10.0))
{
printf("[ERROR] Invalid Camera Depth! (0.0 < Depth < 10.0)\n");
exit(1);
}
else
{
printf("[ARG] Robot Camera Depth (m) : %.2f \n", robotCameraDepth);
}
}
// subscribe to the ranger, which we use for navigating
// robot->ranger = (ModelRanger*)mod->GetUnusedModelOfType( "ranger" );
// assert( robot->ranger );
// ask Stage to call into our ranger update function
//robot->ranger->AddCallback( Model::CB_UPDATE, (model_callback_t)RangerUpdate, robot );
robot->fiducial = (ModelFiducial*)mod->GetUnusedModelOfType( "fiducial" ) ;
assert( robot->fiducial );
robot->fiducial->AddCallback( Model::CB_UPDATE, (model_callback_t)FiducialUpdate, robot );
// Per robot init
robot->position->SetFiducialReturn(robot->position->GetId());
robot->charger = HomePositions.front();
HomePositions.pop_front();
robot->position->SetPose(robot->charger);
robot->teammates.push_front(robot->position->GetId());
setRobotState(robot, (perfectMode) ? INGROUP : SEARCHING);
if ((robot->position->GetId() == loggerID) && (leaderEnabled))
{
setRobotState(robot, INGROUP);
}
robot->energy = 1000.0 + (robot->position->GetId() * 200.0);
robot->position->AddVisualizer( new CCVis(*robot), true);
robot->fiducial->Subscribe();
//robot->ranger->Subscribe();
robot->position->Subscribe();
//printf("I am damn robot %d and my root id is %d \n", robot->position->GetId(), robot->position->Root()->GetId());
return 0; //ok
}
//int RangerUpdate( ModelRanger* rgr, robot_t* robot )
//{
// return 0;
//}
int FiducialUpdate( ModelFiducial* fid, robot_t* robot)
{
// find the closest teammate
double dist = 1e6; // big
robot->closest = NULL;
FOR_EACH( it, fid->GetFiducials() )
{
ModelFiducial::Fiducial* other = &(*it);
if( other->range < dist )
{
dist = other->range;
robot->closest = other;
}
}
/* This is some shared knowledge, let's say it's global now */
static Pose attCenter = Pose(0.0, 0.0, 0.0, 0.0);
static double movementX = 0.0;//1e-4;
double att_dx = attCenter.x - robot->position->GetPose().x;
double att_dy = attCenter.y - robot->position->GetPose().y;
double att_d = sqrt( (att_dx * att_dx) + (att_dy * att_dy) );
robot->d2c.push_front(att_d);
if (robot->d2c.size() > 100)
{
robot->d2c.pop_back();
}
updateMyInbox(robot);
//printf("I am robot %d, I think there are %d bots in the world. [In: %4d][E: %4.2f] \n", robot->position->GetId(), robot->teammates.size(), robot->inbox.size(), robot->energy);
if (robot->position->GetId() == loggerID)
{
fprintf(stderr, "\n%ld,%ld,%ld,%d,", (unsigned long int) (robot->position->GetWorld()->SimTimeNow() /1000.0), totalMessageCounterNaive, totalMessageCounterMy, NUMBEROFSEARCHINGROBOTS);
// std::set<Stg::Model*>::iterator modit;
// std::set<Stg::Model*> allModels = robot->position->GetWorld()->GetAllModels();
// for (modit = allModels.begin(); modit !=allModels.end(); ++ modit )
// {
// Stg::Model* mod = *(modit);
// Stg::ModelPosition* rPos = (Stg::ModelPosition*) mod;
// printf("*** Robot %d @ <%6.4f, %6.4f>\n", rPos->GetId(), rPos->GetPose().x, rPos->GetPose().y);
// }
}
if (robot->state == INGROUP)
{
fprintf(stderr, "%d,", (int) robot->teammates.size());
}
if (robot->state == SEARCHING)
{
// Check if any WELCOME message received
list<RobotMessage>::iterator mit;
bool updated = false;
mit = robot->inbox.begin();
while (mit != robot->inbox.end())
{
RobotMessage msg = *(mit);
//printf("From: %2d To: %2d Says: %2d \n", msg.sender, msg.receiver, msg.robotid);
if (msg.type == WELCOME)
{
updated = true;
robot->teammates.push_back(msg.robotid);
}
robot->inbox.erase(mit++);
}
if (updated) // Hurray! Welcome Message Received
{
setRobotState(robot, INGROUP);
robot->teammates.sort();
robot->teammates.unique();
}
else if (robot->d2c.size() > 80) // Check if you are in group based on differences over recent distances to center
{
double meand = 0.0;
deque<double>::iterator qit;
deque<double>::iterator prev;
double diff;
bool first = true;
for (qit = robot->d2c.begin(); qit != robot->d2c.end(); ++qit)
{
if (!first)
{
diff = fabs((*prev) - (*qit));
meand += diff;
}
else
{
first = false;
}
prev = qit;
}
meand /= robot->d2c.size();
if (meand < 0.01)
{
setRobotState(robot, INGROUP);
FOR_EACH( it, fid->GetFiducials() )
{
ModelFiducial::Fiducial* other = &(*it);
robot->teammates.push_back(other->id);
}
}
}
// Still Searching? AKA : No Welcome Message
if (robot->state == SEARCHING)
{
// Send Messages to all other neighbors
FOR_EACH( it, fid->GetFiducials() )
{
ModelFiducial::Fiducial* other = &(*it);
sendMessage(IAMCOMMING, robot->position->GetId(), other->id, 0, 0); // The value is not important
}
}
}
else if (robot->state == INGROUP)
{
//Naive energy consumption model (note only in INGROUP mode)
if (energyEnabled == true)
{
robot->energy -= 2.5;
}
// Step 1: Look for incomming messages and take appropriate update
list<RobotMessage>::iterator mit;
// first check JOINING and LEAVING bots
bool joinleave = false;
list<unsigned int> welcomeacklist;
list<unsigned int> byeacklist;
mit = robot->inbox.begin();
while (mit != robot->inbox.end())
{
RobotMessage msg = *mit;
if (msg.type == IAMCOMMING)
{
joinleave = true;
robot->teammates.push_back(msg.sender);
welcomeacklist.push_back(msg.sender);
robot->inbox.erase(mit++);
}
else if (msg.type == IAMLEAVING)
{
joinleave = true;
robot->teammates.remove(msg.sender);
byeacklist.push_back(msg.sender);
sendMessage(BYE, robot->position->GetId(), msg.sender, 0, 0); // Reply Back
robot->inbox.erase(mit++);
}
else
{
++mit;
}
}
list<unsigned int>::iterator iit;
list<unsigned int>::iterator tit; //Not only iterator names are confusing, this one is also a little bit naughty ;)
for (iit = welcomeacklist.begin(); iit != welcomeacklist.end(); ++iit)
{
for (tit = robot->teammates.begin(); tit != robot->teammates.end(); ++tit)
{
sendMessage(WELCOME, robot->position->GetId(), *iit, *tit, 0); // Reply Back
}
}
// Create a new list of nearby robots that are not in welcome or bye lists
list<unsigned int> inGroupNeighbors;
FOR_EACH( it, fid->GetFiducials() )
{
ModelFiducial::Fiducial* other = &(*it);
bool found = false;
for (iit = welcomeacklist.begin(); (iit != welcomeacklist.end()) /* && (!found) */; ++iit)
{
if (other->id == *iit) found = true;
}
for (iit = byeacklist.begin(); (iit != byeacklist.end()) /* && (!found) */; ++iit)
{
if (other->id == *iit) found = true;
}
if (!found) inGroupNeighbors.push_back(other->id);
}
// Update your belief using INGROUP SYNC messages
mit = robot->inbox.begin();
while (mit != robot->inbox.end())
{
RobotMessage msg = *mit;
if ((msg.type == ADDROBOT) || (msg.type == REMOVEROBOT))
{
if (msg.type == ADDROBOT)
{
robot->teammates.push_back(msg.robotid);
}
else if (msg.type == REMOVEROBOT)
{
robot->teammates.remove(msg.robotid);
}
//inGroupNeighbors.remove(msg.sender);
if (msg.relaycount > 0)
{
list<unsigned int>::iterator nit;
for (nit = inGroupNeighbors.begin(); nit != inGroupNeighbors.end(); ++nit)
{
sendMessage(msg.type, robot->position->GetId(), *nit, msg.robotid, msg.relaycount-1);
}
}
}
robot->inbox.erase(mit++);
}
robot->teammates.sort();
robot->teammates.unique();
list<unsigned int>::iterator nit;
for (nit = inGroupNeighbors.begin(); nit != inGroupNeighbors.end(); ++nit)
{
for (tit = welcomeacklist.begin(); tit != welcomeacklist.end(); ++tit)
{
sendMessage(ADDROBOT, robot->position->GetId(), *nit, *tit, globalRelayCount);
}
for (tit = byeacklist.begin(); tit != byeacklist.end(); ++tit)
{
sendMessage(REMOVEROBOT, robot->position->GetId(), *nit, *tit, globalRelayCount);
}
}
// Step 3: Check State Trnasitions for itself
if (robot->energy < 500.0)
{
FOR_EACH( it, fid->GetFiducials() )
{
ModelFiducial::Fiducial* other = &(*it);
sendMessage(IAMLEAVING, robot->position->GetId(), other->id, 0, 0); // The value is not important
}
robot->teammates.clear();
robot->position->Stop();
setRobotState(robot, HOMING);
}
}
else if (robot->state == HOMING)
{
double h_dx = robot->charger.x - robot->position->GetPose().x;
double h_dy = robot->charger.y - robot->position->GetPose().y;
double h_d = sqrt( (h_dx * h_dx) + (h_dy * h_dy) );
if (h_d < 0.1)
{
setRobotState(robot,CHARGING);
}
else
{
double h_force = FATTMAX * 2.0 * (sigmoid(h_d) - 0.5);
double h_theta = atan2(h_dy, h_dx);
double h_force_x = h_force * cos(h_theta);
double h_force_y = h_force * sin(h_theta);
radians_t theta = robot->position->GetPose().a;
double vx_robot = ( cos(theta) * h_force_x) + (sin(theta) * h_force_y);
double vy_robot = (-sin(theta) * h_force_x) + (cos(theta) * h_force_y);
robot->position->SetSpeed(vx_robot, vy_robot, 0.0);
}
}
else if (robot->state == CHARGING)
{
if (robot->energy < robot->position->GetId() * 1000.0)
{
robot->energy += 10.0;
robot->position->SetSpeed(0.0, 0.0, 0.0);
}
else
{
setRobotState(robot, SEARCHING);
}
}
/* Actions Based on State - No State Transition Here */
if ((robot->state == SEARCHING) || (robot->state == INGROUP))
{
attCenter.x += movementX;
if (fabs(attCenter.x) > 8.0)
{
movementX = -1.0 * movementX;
}
double att_force;
/* This function must be calculated via Computation Geometery Methods*/
//double rmin_adaptive = RMIN + ((bEnabled) ? ((double) robot->teammates.size() / 4.0) : 0.0);
double rmin_adaptive;
if (perfectMode)
{
rmin_adaptive = RMIN + calc_r(NUMBEROFROBOTS);
}
else
{
rmin_adaptive = RMIN + ((bEnabled) ? calc_r(robot->teammates.size()) : 0.0);
}
// if (robot->position->GetId() == loggerID)
// {
// printf("%d %6.4f\n", robot->teammates.size(), calc_r(robot->teammates.size()));
// }
//rmin_adaptive = 12.0;
if (att_d < rmin_adaptive)
{
att_force = -FATTMAX;
}
else
{
att_force = FATTMAX * 2.0 * (sigmoid(att_d - rmin_adaptive) - 0.5);
}
double att_theta = atan2(att_dy, att_dx);
double att_force_x = att_force * cos(att_theta);
double att_force_y = att_force * sin(att_theta);
double rep_force = 0.0;
double rep_force_x = 0.0;
double rep_force_y = 0.0;
double sum_force_x = att_force_x;
double sum_force_y = att_force_y;
FOR_EACH( it, fid->GetFiducials() )
{
ModelFiducial::Fiducial* other = &(*it);
double rep_d = other->range;
double rep_theta = normalize(other->bearing + robot->position->GetPose().a);
rep_force = FREPMAX * (1.0 - (2.0 * (sigmoid(rep_d) - 0.5)) );
rep_force_x = rep_force * cos(rep_theta);
rep_force_y = rep_force * sin(rep_theta);
sum_force_x += rep_force_x;
sum_force_y += rep_force_y;
}
// Forward Kinematic Model (Point Robot)
double vx_global = sum_force_x;
double vy_global = sum_force_y;
//printf("Target Speed of %d : %6.4f %6.4f \n", robot->position->GetId(), vx, vy);
radians_t theta = robot->position->GetPose().a;
double vx_robot = ( cos(theta) * vx_global) + (sin(theta) * vy_global);
double vy_robot = (-sin(theta) * vx_global) + (cos(theta) * vy_global);
/* The Angular Velocity Module Calculation */
double w_robot = 0.0;
radians_t r_heading = robot->position->GetPose().a;
radians_t angle_error = normalize ( normalize(att_theta + 3.1415) - r_heading);
//radians_t angle_error = normalize(att_theta + 3.1415) - r_heading;
w_robot = FWATT * 2.0 * (sigmoid(angle_error) - 0.5);
robot->position->SetSpeed( vx_robot, vy_robot, w_robot);
}
return 0;
}