-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormation.py
More file actions
821 lines (646 loc) · 29.9 KB
/
Formation.py
File metadata and controls
821 lines (646 loc) · 29.9 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
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
from numpy import iinfo
from myGlobalEnviroment import *
from abc import ABC, abstractmethod
from Robot import *
from utils import *
from munkres import Munkres, print_matrix
import numpy
class Formation(ABC):
def __init__(self, numRobots):
self.numRobots = numRobots
def makeFormationCostraint(self, robotCommander):
pass
def setFormationCostraint(self, robotVector):
pass
def makeSpanningTree(self, robotCommander):
robots = []
robotCommander.neighbour.clear()
robotCommander.hashRole.clear()
robots.append(robotCommander)
i = 0
robotCommander.setRole(i)
n = self.numRobots
start_range = 5
ranges = []
for j in range(n):
ranges.append(start_range)
while len(robots) < n:
near_robots_struct = []
for rob in robots:
close_rob = myGlobalEnviroment.thereIsRobotInRange(rob, ranges[rob.role])
for r in close_rob:
near_robots_struct.append([rob, r]) # della forma robot che ha trovato - robot trovato
ranges[rob.role] += 1
for rob_struct in near_robots_struct:
if are_not_connected(robot_list=robots, robot=rob_struct[1]):
rob_struct[1].neighbour.clear()
Robot.connect(rob_struct[0], rob_struct[1])
rob_struct[1].setRole(i + 1)
robotCommander.hashRole.clear()
robots.append(rob_struct[1])
i += 1
@staticmethod
def specifyFormationRobot(posX, posY):
pass
def uncompatibility_number_robots(self, num_robots):
if self.numRobots == num_robots:
return False
print("Incompatible number of robots")
return True
class FormationDisplacement(Formation, ABC):
def make_munkres(self, robotVector):
matrix = self.calc_matrix_cost(robotVector)
m = Munkres()
indexes = m.compute(matrix)
print_matrix(matrix, msg='Lowest cost through this matrix:')
total = 0
for row, column in indexes:
value = matrix[row][column]
total += value
print(f'({row}, {column}) -> {value}')
print(f'total cost: {total}')
self.reset_roles_connections(robotVector, indexes)
def makeFormationCostraint(self, robotCommander):
robotVector = []
list = []
robotVector.append(robotCommander)
list.append(robotCommander)
while list != []:
robot = list.pop()
for elem in robot.neighbour:
if elem not in robotVector:
list.append(elem)
robotVector.append(elem)
self.make_munkres(robotVector)
self.setFormationCostraint(robotVector)
def calc_matrix_cost(self, robotVector):
pass
def reset_roles_connections(self, robotVector, indexes):
i = 0
for robot in robotVector:
robot.setRole(indexes[i][1])
i += 1
def calcolate_baricenter(self, robotVector):
xG = 0
yG = 0
for naer in robotVector:
xG += naer.getAbsolutePosX()
yG += naer.getAbsolutePosY()
xG = xG/self.numRobots
yG = yG / self.numRobots
return [xG, yG]
class SquareFormationDisplacement(FormationDisplacement):
def __init__(self, side):
super().__init__(4)
self.side = side
def calc_matrix_cost(self, robot_vector):
robot_vector = sorted(robot_vector, key=lambda item: item.role)
baricenter = self.calcolate_baricenter(robot_vector)
points = []
punto0 = [baricenter[0] - self.side/2, baricenter[1] - self.side/2]
punto1 = [baricenter[0] - self.side/2, baricenter[1] + self.side/2]
punto2 = [baricenter[0] + self.side/2, baricenter[1] + self.side/2]
punto3 = [baricenter[0] + self.side/2, baricenter[1] - self.side/2]
points.append(punto0)
points.append(punto1)
points.append(punto2)
points.append(punto3)
matrix = []
for robot in robot_vector:
robot_roles_assigment = []
for point in points:
robot_role = distance_between_point(robot.getAbsolutePos(), point)
robot_roles_assigment.append(robot_role)
matrix.append(robot_roles_assigment)
return matrix
class SquareFormationDisplacementSingleIntegrator(SquareFormationDisplacement):
def setFormationCostraint(self, robotVector):
robotVector = sorted(robotVector, key=lambda item: item.role)
robotVector[0].disconnect()
robotVector[1].disconnect()
robotVector[2].disconnect()
robotVector[3].disconnect()
Robot.connect(robotVector[0], robotVector[1])
Robot.connect(robotVector[1], robotVector[2])
Robot.connect(robotVector[2], robotVector[3])
robotVector[0].hashRole[1] = [0, self.side]
robotVector[1].hashRole[0] = [0, -self.side]
robotVector[1].hashRole[2] = [self.side, 0]
robotVector[2].hashRole[1] = [-self.side, 0]
robotVector[2].hashRole[3] = [0, -self.side]
robotVector[3].hashRole[2] = [0, self.side]
def makeFormationRobot(self):
return RobotDisplacementSingleIntegrator.makeRandomRobot()
@staticmethod
def specifyFormationRobot(posX, posY):
return RobotDisplacementSingleIntegrator(posX, posY)
class SquareFormationDisplacementDoubleIntegrator(SquareFormationDisplacement):
def __init__(self, side, desVelX=0, desVelY=0):
super().__init__(4)
self.side = side
self.desVelX = desVelX
self.desVelY = desVelY
def setFormationCostraint(self, robotVector):
robotVector = sorted(robotVector, key=lambda item: item.role)
robotVector[0].disconnect()
robotVector[1].disconnect()
robotVector[2].disconnect()
robotVector[3].disconnect()
Robot.connect(robotVector[0], robotVector[1])
Robot.connect(robotVector[1], robotVector[2])
Robot.connect(robotVector[2], robotVector[3])
robotVector[0].hashRole[1] = [[0, self.side], [self.desVelX, self.desVelY]]
robotVector[1].hashRole[0] = [[0, -self.side], [self.desVelX, self.desVelY]]
robotVector[1].hashRole[2] = [[self.side, 0], [self.desVelX, self.desVelY]]
robotVector[2].hashRole[1] = [[-self.side, 0], [self.desVelX, self.desVelY]]
robotVector[2].hashRole[3] = [[0, -self.side], [self.desVelX, self.desVelY]]
robotVector[3].hashRole[2] = [[0, self.side], [self.desVelX, self.desVelY]]
def makeFormationRobot(self):
return RobotDisplacementDoubleIntegrator.makeRandomRobot()
@staticmethod
def specifyFormationRobot( posX, posY):
return RobotDisplacementDoubleIntegrator(posX, posY)
class SquareFormationDisplacementUnicycle(SquareFormationDisplacementSingleIntegrator):
def makeFormationRobot(self):
return RobotDisplacementUnicycle.makeRandomRobot()
@staticmethod
def specifyFormationRobot(posX, posY):
return RobotDisplacementUnicycle(posX, posY)
class LinearFormationDisplacement(FormationDisplacement):
def __init__(self, side, num_robots):
super().__init__(num_robots)
self.side = side
def calc_matrix_cost(self, robotVector):
baricenter = self.calcolate_baricenter(robotVector)
if len(robotVector)%2 != 0:
starting_point_x = baricenter[0] - math.floor(len(robotVector)/2) * self.side
else:
starting_point_x = baricenter[0] - self.side/2 - (len(robotVector) / 2) * self.side
points = []
for i in range(len(robotVector)):
if i == 0:
point = [starting_point_x, baricenter[1]]
else:
point = [points[i-1][0]+self.side, points[i-1][1]]
points.append(point)
matrix = []
for robot in robotVector:
robot_roles_assigment = []
for point in points:
robot_role = distance_between_point(robot.getAbsolutePos(), point)
robot_roles_assigment.append(robot_role)
matrix.append(robot_roles_assigment)
return matrix
class LinearHorizontalFormationDisplacementSingleIntegrator(LinearFormationDisplacement):
def setFormationCostraint(self, robotVector):
robotVector = sorted(robotVector, key=lambda item: item.role)
for i in range(len(robotVector)):
robotVector[i].disconnect()
for i in range(len(robotVector) - 1):
Robot.connect(robotVector[i], robotVector[i+1])
for i in range(len(robotVector)):
if i == 0:
robotVector[i].hashRole[i + 1] = [self.side, 0]
elif i == (len(robotVector)-1):
robotVector[i].hashRole[i - 1] = [-self.side, 0]
else:
robotVector[i].hashRole[i+1] = [self.side, 0]
robotVector[i].hashRole[i-1] = [-self.side, 0]
def makeFormationRobot(self):
return RobotDisplacementSingleIntegrator.makeRandomRobot()
@staticmethod
def specifyFormationRobot( posX, posY):
return RobotDisplacementSingleIntegrator(posX, posY)
class LinearHorizontalFormationDisplacementDoubleIntegrator(LinearFormationDisplacement):
def __init__(self, side, num_robots, desVelX=0, desVelY=0):
super().__init__(side, num_robots)
self.desVelX = desVelX
self.desVelY = desVelY
def setFormationCostraint(self, robotVector):
robotVector = sorted(robotVector, key=lambda item: item.role)
for i in range(len(robotVector)):
robotVector[i].disconnect()
for i in range(len(robotVector) - 1):
Robot.connect(robotVector[i], robotVector[i+1])
for i in range(len(robotVector)):
if i == 0:
robotVector[i].hashRole[i + 1] = [[self.side, 0], [self.desVelX, self.desVelY]]
elif i == (len(robotVector) - 1):
robotVector[i].hashRole[i - 1] = [[-self.side, 0], [self.desVelX, self.desVelY]]
else:
robotVector[i].hashRole[i + 1] = [[self.side, 0], [self.desVelX, self.desVelY]]
robotVector[i].hashRole[i - 1] = [[-self.side, 0], [self.desVelX, self.desVelY]]
def makeFormationRobot(self):
return RobotDisplacementDoubleIntegrator.makeRandomRobot()
@staticmethod
def specifyFormationRobot(posX, posY):
return RobotDisplacementDoubleIntegrator(posX, posY)
class LinearHorizontalFormationDisplacementUnicycle(LinearHorizontalFormationDisplacementSingleIntegrator):
def makeFormationRobot(self):
return RobotDisplacementUnicycle.makeRandomRobot()
@staticmethod
def specifyFormationRobot(posX, posY):
return RobotDisplacementUnicycle(posX, posY)
class FreeFormationDisplacement(FormationDisplacement, ABC):
def __init__(self, ptsFormation, lines):
num_robots = len(ptsFormation)
super().__init__(num_robots)
self.hash_role = {}
self.ptsFormation = ptsFormation
self.lines = lines # salva le linee per la creazione dei vincoli tra punti
for i in range(num_robots):
self.hash_role[i] = {}
def calcolate_formation_baricenter(self):
xG = 0
yG = 0
for i in self.ptsFormation:
xG += i[1][0]
yG += i[1][1]
xG = xG / self.numRobots
yG = yG / self.numRobots
return [xG, yG]
def calc_matrix_cost(self, robotVector):
robotBaricenter = self.calcolate_baricenter(robotVector)
formationBaricenter = self.calcolate_formation_baricenter()
difference = numpy.subtract(formationBaricenter, robotBaricenter)
points = []
for i in range(len(self.ptsFormation)):
p_i = self.ptsFormation[i][1] - difference
points.append(p_i)
matrix = []
for robot in robotVector:
robot_roles_assigment = []
for point in points:
robot_role = distance_between_point(robot.getAbsolutePos(), point)
robot_roles_assigment.append(robot_role)
matrix.append(robot_roles_assigment)
return matrix
class FreeFormationDisplacementSingleIntegrator(FreeFormationDisplacement):
def __init__(self, ptsFormation, lines):
super().__init__(ptsFormation, lines)
def setFormationCostraint(self, robot_vector):
robot_vector = sorted(robot_vector, key=lambda item: item.role)
for robot in robot_vector: # todo responsabilità del makeSpanningTree
robot.disconnect()
for robot in robot_vector:
for linea in self.lines:
if linea[0] == robot.role:
punto1 = None
punto2 = None
for punto in self.ptsFormation:
if punto[0] == linea[0]:
punto1 = punto
elif punto[0] == linea[1]:
punto2 = punto
dx = punto2[1][0] - punto1[1][0]
dy = punto2[1][1] - punto1[1][1]
robot.hashRole[linea[1]] = [dx, dy]
robot2 = find_robot_by_role(robot_vector, linea[1])
robot2.hashRole[linea[0]] = [-dx, -dy] # setto i vincolo anche nel verso opposto
Robot.connect(robot, robot2)
def makeFormationRobot(self):
return RobotDisplacementSingleIntegrator.makeRandomRobot()
@staticmethod
def specifyFormationRobot( posX, posY):
return RobotDisplacementSingleIntegrator(posX, posY)
class FreeFormationDisplacementDoubleIntegrator(FreeFormationDisplacement):
def __init__(self, ptsFormation, lines, desVelX=0, desVelY=0):
super().__init__(ptsFormation, lines)
self.desVelX = desVelX
self.desVelY = desVelY
def setFormationCostraint(self, robot_vector):
robot_vector = sorted(robot_vector, key=lambda item: item.role)
for robot in robot_vector:
robot.disconnect()
for robot in robot_vector:
for linea in self.lines:
if linea[0] == robot.role:
punto1 = None
punto2 = None
for punto in self.ptsFormation:
if punto[0] == linea[0]:
punto1 = punto
elif punto[0] == linea[1]:
punto2 = punto
dx = punto2[1][0] - punto1[1][0]
dy = punto2[1][1] - punto1[1][1]
robot.hashRole[linea[1]] = [[dx, dy], [self.desVelX, self.desVelY]]
robot2 = find_robot_by_role(robot_vector, linea[1])
robot2.hashRole[linea[0]] = [[-dx, -dy], [self.desVelX, self.desVelY]] # setto i vincolo anche nel verso opposto
Robot.connect(robot, robot2)
def makeFormationRobot(self):
return RobotDisplacementDoubleIntegrator.makeRandomRobot()
@staticmethod
def specifyFormationRobot(posX, posY):
return RobotDisplacementDoubleIntegrator(posX, posY)
class FreeFormationDisplacementUnicycle(FreeFormationDisplacementSingleIntegrator):
def makeFormationRobot(self):
return RobotDisplacementUnicycle.makeRandomRobot()
@staticmethod
def specifyFormationRobot(posX, posY):
return RobotDisplacementUnicycle(posX, posY)
class FormationDistance(Formation, ABC):
def makeFormationCostraint(self, robotCommander):
robotVector = []
list = []
robotVector.append(robotCommander)
list.append(robotCommander)
while list != []:
robot = list.pop()
for elem in robot.neighbour:
if elem not in robotVector:
list.append(elem)
robotVector.append(elem)
self.make_munkres(robotVector)
self.setFormationCostraint(robotVector)
def make_munkres(self, robotVector):
matrixs = self.calc_matrixs_cost(robotVector)
best_idexes = []
min = math.inf
i = 0
for matrix in matrixs:
i += 1
m = Munkres()
indexes = m.compute(matrix)
print_matrix(matrix, msg='Lowest cost through this matrix:')
total = 0
for row, column in indexes:
value = matrix[row][column]
total += value
print(f'({row}, {column}) -> {value}')
print(f'total cost: {total}')
if total < min:
min = total
best_idexes = indexes
#print("the best matrix was the number: " + str(i))
self.reset_roles_connections(robotVector, best_idexes)
def calcolate_baricenter(self, robotVector):
xG = 0
yG = 0
for naer in robotVector:
xG += naer.getAbsolutePosX()
yG += naer.getAbsolutePosY()
xG = xG/self.numRobots
yG = yG / self.numRobots
return [xG, yG]
def calc_matrixs_cost(self, robotVector):
pass
def reset_roles_connections(self, robotVector, indexes):
i = 0
for robot in robotVector:
robot.setRole(indexes[i][1])
i += 1
def set_hash_role(self, robots_vector, self_role, target_role, distance):
robots_vector[self_role].hashRole[target_role] = distance
robots_vector[target_role].hashRole[self_role] = distance
class SquareFormationDistance(FormationDistance):
def __init__(self, side):
super().__init__(4)
self.side = side
def calc_matrixs_cost(self, robotVector):
baricenter = self.calcolate_baricenter(robotVector)
points = []
punto0 = [baricenter[0] - self.side / 2, baricenter[1] - self.side / 2]
punto1 = [baricenter[0] - self.side / 2, baricenter[1] + self.side / 2]
punto2 = [baricenter[0] + self.side / 2, baricenter[1] + self.side / 2]
punto3 = [baricenter[0] + self.side / 2, baricenter[1] - self.side / 2]
points.append(punto0)
points.append(punto1)
points.append(punto2)
points.append(punto3)
return get_matrices_from_points(robotVector, points, baricenter)
class SquareFormationDistanceSingleIntegrator(SquareFormationDistance):
def __init__(self, side):
super().__init__(4)
self.side = side
def setFormationCostraint(self, robotVector):
robotVector = sorted(robotVector, key=lambda item: item.role)
robotVector[0].disconnect()
robotVector[1].disconnect()
robotVector[2].disconnect()
robotVector[3].disconnect()
Robot.connect(robotVector[0], robotVector[1])
Robot.connect(robotVector[0], robotVector[2])
Robot.connect(robotVector[0], robotVector[3])
Robot.connect(robotVector[1], robotVector[2])
Robot.connect(robotVector[1], robotVector[3])
Robot.connect(robotVector[2], robotVector[3])
robotVector[0].hashRole[1] = self.side
robotVector[0].hashRole[2] = self.side*math.sqrt(2)
robotVector[0].hashRole[3] = self.side
robotVector[1].hashRole[0] = self.side
robotVector[1].hashRole[2] = self.side
robotVector[1].hashRole[3] = self.side*math.sqrt(2)
robotVector[2].hashRole[0] = self.side*math.sqrt(2)
robotVector[2].hashRole[1] = self.side
robotVector[2].hashRole[3] = self.side
robotVector[3].hashRole[0] = self.side
robotVector[3].hashRole[1] = self.side*math.sqrt(2)
robotVector[3].hashRole[2] = self.side
def makeFormationRobot(self):
return RobotDistanceSingleIntegrator.makeRandomRobot()
@staticmethod
def specifyFormationRobot(posX, posY):
return RobotDistanceSingleIntegrator(posX, posY)
class SquareFormationDistanceDoubleIntegrator(SquareFormationDistance):
def __init__(self, side, desVelX=0, desVelY=0):
super().__init__(4)
self.side = side
self.desVelX = desVelX
self.desVelY = desVelY
def setFormationCostraint(self, robotVector):
robotVector = sorted(robotVector, key=lambda item: item.role)
robotVector[0].disconnect()
robotVector[1].disconnect()
robotVector[2].disconnect()
robotVector[3].disconnect()
Robot.connect(robotVector[0], robotVector[1])
Robot.connect(robotVector[0], robotVector[2])
Robot.connect(robotVector[0], robotVector[3])
Robot.connect(robotVector[1], robotVector[2])
Robot.connect(robotVector[1], robotVector[3])
Robot.connect(robotVector[2], robotVector[3])
robotVector[0].hashRole[1] = [[self.side], [self.desVelX, self.desVelY]]
robotVector[0].hashRole[2] = [[self.side * math.sqrt(2)], [self.desVelX, self.desVelY]]
robotVector[0].hashRole[3] = [[self.side], [self.desVelX, self.desVelY]]
robotVector[1].hashRole[0] = [[self.side], [self.desVelX, self.desVelY]]
robotVector[1].hashRole[2] = [[self.side], [self.desVelX, self.desVelY]]
robotVector[1].hashRole[3] = [[self.side * math.sqrt(2)], [self.desVelX, self.desVelY]]
robotVector[2].hashRole[0] = [[self.side * math.sqrt(2)], [self.desVelX, self.desVelY]]
robotVector[2].hashRole[1] = [[self.side], [self.desVelX, self.desVelY]]
robotVector[2].hashRole[3] = [[self.side], [self.desVelX, self.desVelY]]
robotVector[3].hashRole[0] = [[self.side], [self.desVelX, self.desVelY]]
robotVector[3].hashRole[1] = [[self.side * math.sqrt(2)], [self.desVelX, self.desVelY]]
robotVector[3].hashRole[2] = [[self.side], [self.desVelX, self.desVelY]]
def makeFormationRobot(self):
return RobotDistanceDoubleIntegrator.makeRandomRobot()
@staticmethod
def specifyFormationRobot(posX, posY):
return RobotDistanceDoubleIntegrator(posX, posY)
class SquareFormationDistanceUnicycle(SquareFormationDistanceSingleIntegrator):
def makeFormationRobot(self):
return RobotDistanceUnicycle.makeRandomRobot()
@staticmethod
def specifyFormationRobot(posX, posY):
return RobotDistanceUnicycle(posX, posY)
class LinearFormationDistance(FormationDistance):
def __init__(self, side, num_robots):
super().__init__(num_robots)
self.side = side
def calc_matrixs_cost(self, robotVector):
baricenter = self.calcolate_baricenter(robotVector)
if len(robotVector) % 2 != 0:
starting_point_x = baricenter[0] - math.floor(len(robotVector) / 2) * self.side
else:
starting_point_x = baricenter[0] - self.side / 2 - (len(robotVector) / 2) * self.side
points = []
for i in range(len(robotVector)):
if i == 0:
point = [starting_point_x, baricenter[1]]
else:
point = [points[i - 1][0] + self.side, points[i - 1][1]]
points.append(point)
return get_matrices_from_points(robotVector, points, baricenter)
class LinearHorizontalFormationDistanceSingleIntegrator(LinearFormationDistance):
def setFormationCostraint(self, robotVector):
robotVector = sorted(robotVector, key=lambda item: item.role)
for i in range(len(robotVector)):
robotVector[i].disconnect()
for i in range(len(robotVector) - 1):
Robot.connect(robotVector[i], robotVector[i + 1])
if i + 2 < len(robotVector):
Robot.connect(robotVector[i], robotVector[i + 2])
if i + 3 < len(robotVector):
Robot.connect(robotVector[i], robotVector[i + 3])
for i in range(len(robotVector) - 1):
self.set_hash_role(robotVector, i, i + 1, self.side)
if i + 2 < len(robotVector):
self.set_hash_role(robotVector, i, i + 2, self.side * 2)
if i + 3 < len(robotVector):
self.set_hash_role(robotVector, i, i + 3, self.side * 3)
def makeFormationRobot(self):
return RobotDistanceSingleIntegrator.makeRandomRobot()
@staticmethod
def specifyFormationRobot(posX, posY):
return RobotDistanceSingleIntegrator(posX, posY)
class LinearHorizontalFormationDistanceDoubleIntegrator(LinearFormationDistance):
def __init__(self, side, num_robots, desVelX=0, desVelY=0):
super().__init__(side=side, num_robots=num_robots)
self.desVelX = desVelX
self.desVelY = desVelY
def setFormationCostraint(self, robotVector):
robotVector = sorted(robotVector, key=lambda item: item.role)
for i in range(len(robotVector)):
robotVector[i].disconnect()
for i in range(len(robotVector) - 1):
Robot.connect(robotVector[i], robotVector[i + 1])
if i + 2 < len(robotVector):
Robot.connect(robotVector[i], robotVector[i + 2])
if i + 3 < len(robotVector):
Robot.connect(robotVector[i], robotVector[i + 3])
for i in range(len(robotVector) - 1):
self.set_hash_role(robotVector, i, i + 1, [[self.side], [self.desVelX, self.desVelY]])
if i + 2 < len(robotVector):
self.set_hash_role(robotVector, i, i + 2, [[self.side * 2], [self.desVelX, self.desVelY]])
if i + 3 < len(robotVector):
self.set_hash_role(robotVector, i, i + 3, [[self.side * 3], [self.desVelX, self.desVelY]])
def makeFormationRobot(self):
return RobotDistanceDoubleIntegrator.makeRandomRobot()
@staticmethod
def specifyFormationRobot(posX, posY):
return RobotDistanceDoubleIntegrator(posX, posY)
class LinearHorizontalFormationDistanceUnicycle(LinearHorizontalFormationDistanceSingleIntegrator):
def makeFormationRobot(self):
return RobotDistanceUnicycle.makeRandomRobot()
@staticmethod
def specifyFormationRobot(posX, posY):
return RobotDistanceUnicycle(posX, posY)
class FreeFormationDistance(FormationDistance, ABC):
def __init__(self, ptsFormation, lines):
num_robots = len(ptsFormation)
super().__init__(num_robots)
self.hash_role = {}
self.ptsFormation = ptsFormation # salva i punti per la conversione tra tipi di free formation
self.lines = lines # salva le connessioni
for i in range(num_robots):
self.hash_role[i] = {}
def calcolate_formation_baricenter(self):
xG = 0
yG = 0
for i in self.ptsFormation:
xG += i[1][0]
yG += i[1][1]
xG = xG / self.numRobots
yG = yG / self.numRobots
return [xG, yG]
def calc_matrixs_cost(self, robotVector):
robotBaricenter = self.calcolate_baricenter(robotVector)
formationBaricenter = self.calcolate_formation_baricenter()
difference = numpy.subtract(formationBaricenter, robotBaricenter)
points = []
for i in range(len(self.ptsFormation)):
p_i = self.ptsFormation[i][1] - difference
points.append(p_i)
return get_matrices_from_points(robotVector, points, robotBaricenter)
class FreeFormationDistanceSingleIntegrator(FreeFormationDistance):
def __init__(self, ptsFormation, lines):
super().__init__(ptsFormation, lines)
def setFormationCostraint(self, robot_vector):
robot_vector = sorted(robot_vector, key=lambda item: item.role)
for robot in robot_vector:
robot.disconnect()
for robot in robot_vector:
for linea in self.lines:
if linea[0] == robot.role:
punto1 = None
punto2 = None
for punto in self.ptsFormation:
if punto[0] == linea[0]:
punto1 = punto
elif punto[0] == linea[1]:
punto2 = punto
distance = distance_between_point(punto1[1], punto2[1])
robot.hashRole[linea[1]] = distance
robot2 = find_robot_by_role(robot_vector, linea[1])
robot2.hashRole[linea[0]] = distance # setto i vincolo anche nel verso opposto
Robot.connect(robot, robot2)
def makeFormationRobot(self):
return RobotDistanceSingleIntegrator.makeRandomRobot()
@staticmethod
def specifyFormationRobot(posX, posY):
return RobotDistanceSingleIntegrator(posX, posY)
class FreeFormationDistanceDoubleIntegrator(FreeFormationDistance):
def __init__(self, ptsFormation, connectLineasFormations, desVelX=0, desVelY=0):
super().__init__(ptsFormation, connectLineasFormations)
self.desVelX = desVelX
self.desVelY = desVelY
def setFormationCostraint(self, robot_vector):
robot_vector = sorted(robot_vector, key=lambda item: item.role)
for robot in robot_vector:
robot.disconnect()
for robot in robot_vector:
for linea in self.lines:
if linea[0] == robot.role:
punto1 = None
punto2 = None
for punto in self.ptsFormation:
if punto[0] == linea[0]:
punto1 = punto
elif punto[0] == linea[1]:
punto2 = punto
distance = distance_between_point(punto1[1], punto2[1])
robot.hashRole[linea[1]] = [[distance], [self.desVelX, self.desVelY]]
robot2 = find_robot_by_role(robot_vector, linea[1])
robot2.hashRole[linea[0]] = [[distance], [self.desVelX, self.desVelY]] # setto i vincolo anche nel verso opposto
Robot.connect(robot, robot2)
def makeFormationRobot(self):
return RobotDistanceDoubleIntegrator.makeRandomRobot()
@staticmethod
def specifyFormationRobot(posX, posY):
return RobotDistanceDoubleIntegrator(posX, posY)
class FreeFormationDistanceUnicycle(FreeFormationDistanceSingleIntegrator):
def makeFormationRobot(self):
return RobotDistanceUnicycle.makeRandomRobot()
@staticmethod
def specifyFormationRobot( posX, posY):
return RobotDistanceUnicycle(posX, posY)