@@ -7,7 +7,8 @@ var Planner = require('./planner.js'), // Plans and Trajectories
7
7
CollisionDetector = require ( './algorithms/collision/collision-detector.js' ) ,
8
8
Trajectory = require ( './planner.js' ) . Trajectory ;
9
9
/*----------------------------Constants------------------------------*/
10
- const LOG = false ;
10
+ var LOG = false ;
11
+ const DEBUG = false
11
12
const DSAFE = 200 ;
12
13
13
14
/*----------------------------CollisionSystem-------------------------------*/
@@ -21,8 +22,9 @@ const DSAFE = 200;
21
22
* @param id
22
23
* @param plan
23
24
*/
24
- function CollisionSystem ( messenger , gps , id ) {
25
-
25
+ function CollisionSystem ( messenger , gps , id , log ) {
26
+
27
+ LOG = log ;
26
28
this . messenger = messenger ;
27
29
this . gps = gps ;
28
30
this . id = id ;
@@ -61,7 +63,7 @@ CollisionSystem.prototype.handleHIA = function( message, from ) {
61
63
62
64
//console.log("MYPOS: ",myLocation);
63
65
//console.log("THEIRPOS: ",message);
64
- console . log ( "DISTANCE from" , this . id , "to" , from . id , d ) ;
66
+ if ( LOG ) console . log ( "DISTANCE from" , this . id , "to" , from . id , d ) ;
65
67
66
68
// Assume that collision avoidance with from is alreadhy
67
69
// being taken care of if from is in cordlinks already
@@ -72,7 +74,7 @@ CollisionSystem.prototype.handleHIA = function( message, from ) {
72
74
// If I have larger address, I send roll assign
73
75
if ( from . id < this . id ) {
74
76
75
- console . log ( this . id , "NEW LINK CREATED WITH" , from . id ) ;
77
+ if ( DEBUG ) console . log ( this . id , "NEW LINK CREATED WITH" , from . id ) ;
76
78
77
79
// randomly generate 0 or 1
78
80
// 0 is "you are coordinator I am partner"
@@ -124,12 +126,12 @@ CollisionSystem.prototype.handleHIA = function( message, from ) {
124
126
*/
125
127
CollisionSystem . prototype . handleRollAssignment = function ( message , from ) {
126
128
127
- console . log ( this . id , "RECIEVED ROLL ASSIGNMENT FROM" , from . id ) ;
129
+ if ( DEBUG ) console . log ( this . id , "RECIEVED ROLL ASSIGNMENT FROM" , from . id ) ;
128
130
129
131
// If we don't have a coord link with this guy
130
132
if ( ! this . coordLinks . get ( from . id ) ) {
131
133
// Create a new cord link with this guy
132
- console . log ( this . id , "NEW LINK ESTABLISHED WITH" , from . id ) ;
134
+ if ( DEBUG ) console . log ( this . id , "NEW LINK ESTABLISHED WITH" , from . id ) ;
133
135
this . coordLinks . set ( from . id , new CoordLink ( from , message . roll ) ) ;
134
136
}
135
137
@@ -158,13 +160,13 @@ CollisionSystem.prototype.handlePlanRequest = function( message, from ) {
158
160
// Get a coordlink if we have one
159
161
var clink = this . coordLinks . get ( from . id ) ;
160
162
161
- console . log ( this . id , "HANDLING PLAN REQUEST FOR PLAN" , message . scheduleid , "FROM" , from . id ) ;
163
+ if ( DEBUG ) console . log ( this . id , "HANDLING PLAN REQUEST FOR PLAN" , message . scheduleid , "FROM" , from . id ) ;
162
164
//console.log("CURRENT ID", clink.scheduleid );
163
165
164
166
// If we have a clink with this guy and we have not already recieved this plan request
165
167
if ( clink && clink . scheduleid < message . scheduleid ) {
166
168
167
- console . log ( this . id , "ORIGINAL PLAN REQUEST!" ) ;
169
+ if ( DEBUG ) console . log ( this . id , "ORIGINAL PLAN REQUEST!" ) ;
168
170
169
171
// Get the next segments to be send in the schedule
170
172
var nextSegs = this . gps . getNextSegments ( ) ;
@@ -191,12 +193,11 @@ CollisionSystem.prototype.handlePlanResponse = function( message, from ) {
191
193
// Get a coordlink if we have one
192
194
var clink = this . coordLinks . get ( from . id ) ;
193
195
194
- console . log ( this . id , "HANDLING PLAN RESPONSE FOR PLAN" , message . scheduleid , "FROM" , from . id ) ;
195
- //console.log("CURRENT ID", clink.scheduleid );
196
+ if ( DEBUG ) console . log ( this . id , "HANDLING PLAN RESPONSE FOR PLAN" , message . scheduleid , "FROM" , from . id ) ;
196
197
197
198
// If we have a coord link with this guy and we have not already recieved this plan response
198
199
if ( this . coordLinks . get ( from . id ) && clink . scheduleid < message . scheduleid ) {
199
- console . log ( this . id , "NEW PLAN RECIVED" ) ;
200
+ if ( DEBUG ) console . log ( this . id , "NEW PLAN RECIVED" ) ;
200
201
201
202
// Update the schedule id
202
203
clink . scheduleid = message . scheduleid ;
@@ -232,9 +233,11 @@ CollisionSystem.prototype.handlePlanResponse = function( message, from ) {
232
233
}
233
234
234
235
235
- console . log ( this . id , "MYPLAN " , myPlan ) ;
236
- console . log ( this . id , "THEIRPLAN" , from . id , theirPlan ) ;
237
- console . log ( this . id , "COLLLLLLISION" , collision ) ;
236
+ if ( DEBUG ) {
237
+ console . log ( this . id , "MYPLAN " , myPlan ) ;
238
+ console . log ( this . id , "THEIRPLAN" , from . id , theirPlan ) ;
239
+ console . log ( this . id , "COLLLLLLISION" , collision ) ;
240
+ }
238
241
239
242
// If we have a collision
240
243
if ( collision ) {
@@ -284,7 +287,7 @@ CollisionSystem.prototype.handlePlanResponse = function( message, from ) {
284
287
285
288
// When we get out of the collision sphere let our parner know
286
289
this . gps . when ( col . isect1 , col . trajectory1 . trajectory , dcol , function ( ) {
287
- console . log ( "TO2:" , from ) ;
290
+ if ( DEBUG ) console . log ( "TO2:" , from ) ;
288
291
my . messenger . sendLocal ( [ permission ] , from ) ;
289
292
//my.cleanupCoordLink(fr.id);
290
293
} ) ;
@@ -322,16 +325,18 @@ CollisionSystem.prototype.removeFromSegPerm = function( granter, trajectory ){
322
325
if ( waitingOn . length == 0 ) {
323
326
this . gps . unpause ( ) ;
324
327
} else {
325
- console . log ( "UGGGGGG REMOVED LINK AND NOW SEGPERM IS" , waitingOn ) ;
328
+ if ( DEBUG ) console . log ( "UGGGGGG REMOVED LINK AND NOW SEGPERM IS" , waitingOn ) ;
326
329
}
327
330
}
328
331
}
329
332
}
330
333
return ;
331
334
}
332
335
333
- console . log ( "GRANTER:" , granter ) ;
334
- console . log ( "ALWAYS STRING:" , trajectory ) ;
336
+ if ( DEBUG ) {
337
+ console . log ( "GRANTER:" , granter ) ;
338
+ console . log ( "ALWAYS STRING:" , trajectory ) ;
339
+ }
335
340
336
341
// Get this permissions for the sent trajectory
337
342
var waitingOn = this . segPermissions . get ( trajectory ) ;
@@ -350,7 +355,7 @@ CollisionSystem.prototype.removeFromSegPerm = function( granter, trajectory ){
350
355
if ( waitingOn . length === 0 ) {
351
356
this . gps . unpause ( ) ;
352
357
} else {
353
- console . log ( "UGGGGGG REMOVED LINK AND NOW SEGPERM IS" , waitingOn ) ;
358
+ if ( DEBUG ) console . log ( "UGGGGGG REMOVED LINK AND NOW SEGPERM IS" , waitingOn ) ;
354
359
}
355
360
}
356
361
}
@@ -387,7 +392,7 @@ CollisionSystem.prototype.handleScheduleReply = function( message, from ) {
387
392
var collision = message . collision ;
388
393
var collisions = message . collisions ;
389
394
390
- console . log ( this . id , "Recieved Schedule from:" , from . id , schedule ) ;
395
+ if ( DEBUG ) console . log ( this . id , "Recieved Schedule from:" , from . id , schedule ) ;
391
396
392
397
// myPlan is a map from trajseg -> 0 || 1 || 2
393
398
for ( var trajSeg of schedule ) {
@@ -398,8 +403,11 @@ CollisionSystem.prototype.handleScheduleReply = function( message, from ) {
398
403
399
404
// If the action is 1
400
405
if ( act === 1 ) {
401
- console . log ( this . id , "NEED TO UPDATE SEGPERM OF " , mySeg ) ;
402
- console . log ( this . id , "MY SEGPERMS ARE: " , this . segPermissions ) ;
406
+
407
+ if ( DEBUG ) {
408
+ console . log ( this . id , "NEED TO UPDATE SEGPERM OF " , mySeg ) ;
409
+ console . log ( this . id , "MY SEGPERMS ARE: " , this . segPermissions ) ;
410
+ }
403
411
404
412
// Get the seg permissions for this trajectory and update it
405
413
// TODO WHY IS THIS NESSISARY
@@ -432,14 +440,16 @@ CollisionSystem.prototype.handleScheduleReply = function( message, from ) {
432
440
//var fr = from;
433
441
434
442
this . gps . when ( collision . isect2 , collision . trajectory2 . trajectory , dcol , function ( ) {
435
- console . log ( "TO:" , from ) ;
443
+ if ( DEBUG ) console . log ( "TO:" , from ) ;
436
444
my . messenger . sendLocal ( [ permission ] , from ) ;
437
445
//my.cleanupCoordLink( fr.id );
438
446
} ) ;
439
447
}
440
448
}
441
- console . log ( this . id , "HERE IS WHAT WE HAVE BEEN WAITING FOR!! A MODIFIED PLAN!!!!!" ) ;
442
- console . log ( this . segPermissions ) ;
449
+ if ( DEBUG ) {
450
+ console . log ( this . id , "HERE IS WHAT WE HAVE BEEN WAITING FOR!! A MODIFIED PLAN!!!!!" ) ;
451
+ console . log ( this . segPermissions ) ;
452
+ }
443
453
}
444
454
}
445
455
@@ -514,33 +524,48 @@ CollisionSystem.prototype.registerMessages = function() {
514
524
var messenger = this . messenger ;
515
525
var my = this ;
516
526
517
- /* this.gps.on("PlanComplete",function(){
527
+ this . gps . on ( "PlanComplete" , function ( ) {
518
528
// Itterate over all the coordlinks and remove them
519
529
for ( var clink of my . coordLinks ) {
530
+
531
+
532
+ var gp = new CMessages . GrantPermission ( ) ;
533
+ messenger . sendLocal ( [ gp ] , clink [ 1 ] . linkPartner ) ;
520
534
521
- // Grant permission to everyone waiting for me!
522
- var gp = new CMessages.GrantPermission();
523
- messenger.sendLocal( [gp], clink[1].linkPartner );
535
+
536
+ //Itterate over all the trajectories in their plan
537
+ /*if( clink[1].schedule !== undefined ){
538
+ for( var traj of clink[1].schedule ) {
539
+
540
+ // If this trajectory is a partner waiting on me
541
+ if( traj[1] === 2 ) {
542
+
543
+ // Grant permission to everyone waiting for me!
544
+ var gp = new CMessages.GrantPermission( traj[0] );
545
+ messenger.sendLocal( [gp], clink[1].linkPartner );
546
+ }
524
547
548
+ }
549
+ }*/
525
550
// Clean up my partner
526
551
my . cleanupCoordLink ( clink [ 1 ] . linkPartner . id ) ;
527
552
}
528
- });*/
553
+ } ) ;
529
554
530
555
this . gps . on ( "TrajectoryChange" , function ( traj ) {
531
556
532
- console . log ( "CHANGED TRAJ TO" , traj ) ;
557
+ if ( DEBUG ) console . log ( "CHANGED TRAJ TO" , traj ) ;
533
558
534
559
// If the trajectory that we are about to execute
535
560
// has a non zero permissions length then pause
536
561
var waitingOn = my . segPermissions . get ( Trajectory . stringify ( traj ) ) ;
537
562
538
- console . log ( "WAITINGON FOR THIS SEG" , waitingOn ) ;
563
+ if ( DEBUG ) console . log ( "WAITINGON FOR THIS SEG" , waitingOn ) ;
539
564
540
565
// If we are waiting on this segment
541
566
if ( waitingOn . length > 0 ) {
542
567
// Pause the gps
543
- console . log ( "SHOULD PAUSE GPS NOW!!!!!!!!!!!!!!!!!!!!!!" ) ;
568
+ if ( DEBUG ) console . log ( "SHOULD PAUSE GPS NOW!!!!!!!!!!!!!!!!!!!!!!" ) ;
544
569
my . gps . pause ( ) ;
545
570
}
546
571
@@ -553,12 +578,12 @@ CollisionSystem.prototype.registerMessages = function() {
553
578
// Get the c link out
554
579
link = links [ 1 ] ;
555
580
556
- console . log ( "LINKS SCHEDULE:" , link . schedule ) ;
581
+ if ( DEBUG ) console . log ( "LINKS SCHEDULE:" , link . schedule ) ;
557
582
558
583
// Check to see if this link contains the trajectory
559
584
if ( link . schedule && link . schedule . get ( Trajectory . stringify ( traj ) ) !== undefined ) {
560
585
561
- console . log ( "WE HAVE A SCHEDULE WITH THIS TRAJECTORY" ) ;
586
+ if ( DEBUG ) console . log ( "WE HAVE A SCHEDULE WITH THIS TRAJECTORY" ) ;
562
587
563
588
// Update the schedule steps
564
589
link . steps ++ ;
@@ -567,7 +592,7 @@ CollisionSystem.prototype.registerMessages = function() {
567
592
// TODO 2 ??
568
593
if ( link . steps === 3 ) {
569
594
570
- console . log ( my . id , "UPDATING LINK" ) ;
595
+ if ( DEBUG ) console . log ( my . id , "UPDATING LINK" ) ;
571
596
572
597
// Reset the steps
573
598
link . steps = 0 ;
@@ -605,7 +630,7 @@ CollisionSystem.prototype.registerMessages = function() {
605
630
606
631
CollisionSystem . prototype . cleanupCoordLink = function ( linkpartnerid ) {
607
632
608
- console . log ( "cleaningupcordlinkwith " , linkpartnerid ) ;
633
+ if ( DEBUG ) console . log ( "cleaningupcordlinkwith " , linkpartnerid ) ;
609
634
610
635
// Remove this link partner from all segments waiting on him
611
636
this . removeFromSegPerm ( linkpartnerid , "all" ) ;
@@ -620,8 +645,8 @@ CollisionSystem.prototype.cleanupCoordLink = function(linkpartnerid){
620
645
* @param amcoord
621
646
*/
622
647
function CoordLink ( linkPartner , amcoord ) {
623
- var schedule ; // my current schedule
624
- var partnerPlan ; // don't let the name fool you, this is your plan if you are the parner
648
+ var schedule ; // my current schedule
649
+ var partnerPlan ; // don't let the name fool you, this is your plan if you are the parner
625
650
this . linkPartner = linkPartner ; // my link partner
626
651
this . amcoord = amcoord ; // 0 if not, 1 if am
627
652
this . scheduleid = 0 ; // The id of the schedule we are working on
0 commit comments