-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobot.py
497 lines (393 loc) · 16.9 KB
/
Robot.py
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
import numpy as np
import matplotlib.pyplot as plt
import random
import math
from myGlobalEnviroment import *
from abc import ABC, abstractmethod
min_fisic_tollerance_distance = 0.5
class Robot(ABC):
def __init__(self, posX, posY):
self.posX = posX
self.posY = posY
self.deltaX = 0
self.deltaY = 0
self.neighbour = []
self.role = None
self.hashRole = {}
self.formation = None
self.kp = 1
self.error = None
def __str__(self):
string = "Robot with role " + str(self.role) # + " in position " + str(self.getAbsolutePosX()) + "," +str(self.getAbsolutePosY())
return string
def getPosX(self):
return self.posX
def getPosY(self):
return self.posY
def getAbsolutePos(self):
return [self.getAbsolutePosX(), self.getAbsolutePosY()]
def get_error(self):
return self.error
def disconnect(self):
self.neighbour = []
@abstractmethod
def getAbsolutePosX(self):
pass
@abstractmethod
def getAbsolutePosY(self):
pass
def set_desiderd_velx(self, velx):
print("Not avaible with this control law robot")
def set_desiderd_vely(self, velY):
print("Not avaible with this control law robot")
@staticmethod
def makeRandomRobot():
pass
@staticmethod
def connect(robot1, robot2):
robot1.neighbour.append(robot2)
robot2.neighbour.append(robot1)
def setRole(self, integerRole):
self.role = integerRole
@abstractmethod
def calculateControl(self, stepTime):
pass
def updatePosition(self):
self.posX = self.getPosX()+self.deltaX
self.posY = self.getPosY()+self.deltaY
self.deltaX = 0
self.deltaY = 0
@staticmethod
def getName():
raise NotImplementedError
# Pacchetto commander
def makeCommander(self, formation): # si puo' mettere un controllo con una variabile statica sul numero dei comman
self.setFormation(formation)
def setFormation(self, formation):
self.formation = formation
def makeSpanningTree(self):
if self.isCommander():
self.formation.makeSpanningTree(self)
else:
raise PermissionError
def makeCostraint(self):
if self.isCommander():
self.formation.makeFormationCostraint(self)
else:
raise PermissionError
def isCommander(self):
if self.formation:
return True
return False
def change_velocity_and_propagate(self, velX, velY):
print("Not avaible with this control law robot")
# DISPLACEMENT CONTROLLED ROBO
class RobotDisplacement(Robot, ABC):
def __init__(self, posX, posY):
super().__init__(posX, posY)
def getAbsolutePosX(self):
return self.getPosX()
def getAbsolutePosY(self):
return self.getPosY()
def calculateControl(self, stepTime):
self.calculateControlDisplacement(stepTime)
@abstractmethod
def calculateControlDisplacement(self, stepTime):
pass
def desiredDistanceX(self, robot):
return self.hashRole[robot.role][0]
def desiredDistanceY(self, robot):
return self.hashRole[robot.role][1]
class RobotDisplacementSingleIntegrator(RobotDisplacement):
def __init__(self, posX, posY):
super().__init__(posX, posY)
self.kp = 2
def calculateControlDisplacement(self, stepTime):
uX = self.kp * sum((z.getPosX() - self.getPosX() - self.desiredDistanceX(z)) for z in self.neighbour)
uY = self.kp * sum((z.getPosY() - self.getPosY() - self.desiredDistanceY(z)) for z in self.neighbour)
"""
if abs(uX) > setting.limit_ux_si:
uX = setting.limit_ux_si * sign(uX)
if abs(uY) > setting.limit_uy_si:
uY = setting.limit_uy_si * sign(uY)
print("The uX control of the DpSI robot with role " + str(self.role) + " is " + str(uX))
print("The uY control of the DpSI robot with role " + str(self.role) + " is " + str(uY))
"""
self.deltaX = uX * stepTime
self.deltaY = uY * stepTime
self.error = math.sqrt(sum(np.linalg.norm([z.getPosX() - self.getPosX() - self.desiredDistanceX(z), z.getPosY() - self.getPosY() - self.desiredDistanceY(z)])**2 for z in self.neighbour))
@staticmethod
def makeRandomRobot():
posXr = random.randint(0, 50)
posYr = random.randint(0, 50)
robot = RobotDisplacementSingleIntegrator(posXr, posYr)
return robot
@staticmethod
def getName():
return "Robot displacement single integrator"
class RobotDisplacementDoubleIntegrator(RobotDisplacement):
def __init__(self, posX, posY, velX=0, velY=0):
super().__init__(posX, posY)
self.velX = velX
self.velY = velY
self.deltaVelX = 0
self.deltaVelY = 0
self.kp = 2
self.kv = 2
def getVelX(self):
return self.velX
def getVelY(self):
return self.velY
def calculateControlDisplacement(self, stepTime):
uX = self.kp * sum((z.getPosX() - self.getPosX() - self.desiredDistanceX(z)) for z in self.neighbour) + self.kv * sum((z.getVelX() - self.getVelX() + self.desiredVelocityX(z)) for z in self.neighbour)
uY = self.kp * sum((z.getPosY() - self.getPosY() - self.desiredDistanceY(z)) for z in self.neighbour) + self.kv * sum((z.getVelY() - self.getVelY() + self.desiredVelocityY(z)) for z in self.neighbour)
self.deltaX = self.velX * stepTime + (uX * (stepTime**2) / 2)
self.deltaY = self.velY * stepTime + (uY * (stepTime**2) / 2)
self.deltaVelX = uX * stepTime
self.deltaVelY = uY * stepTime
self.error = math.sqrt(sum(np.linalg.norm([z.getPosX() - self.getPosX() - self.desiredDistanceX(z), z.getPosY() - self.getPosY() - self.desiredDistanceY(z)])**2 for z in self.neighbour))
"""
if abs(self.deltaVelX) > setting.limit_ux_si:
self.deltaVelX = setting.limit_ux_si * sign(self.deltaVelX)
if abs(self.deltaVelY) > setting.limit_uy_si:
self.deltaVelY = setting.limit_uy_si * sign(self.deltaVelY)
print("The uX control DpDI of the robot with role " + str(self.role) + " is " + str(uX))
print("The uY control DpDI of the robot with role " + str(self.role) + " is " + str(uY))
"""
def updatePosition(self):
self.posX = self.getPosX() + self.deltaX
self.posY = self.getPosY() + self.deltaY
self.velX = self.getVelX() + self.deltaVelX
self.velY = self.getVelY() + self.deltaVelY
self.deltaX = 0
self.deltaY = 0
self.deltaVelX = 0
self.deltaVelY = 0
def desiredDistanceX(self, robot):
return self.hashRole[robot.role][0][0]
def desiredDistanceY(self, robot):
return self.hashRole[robot.role][0][1]
def desiredVelocityX(self, robot):
return self.hashRole[robot.role][1][0]
def desiredVelocityY(self, robot):
return self.hashRole[robot.role][1][1]
@staticmethod
def makeRandomRobot():
posXr = random.randint(0, 50)
posYr = random.randint(0, 50)
robot = RobotDisplacementDoubleIntegrator(posXr, posYr)
return robot
@staticmethod
def getName():
return "Robot displacement double integrator"
def change_velocity_and_propagate(self, velX, velY):
self.set_desiderd_velx(velX)
self.set_desiderd_vely(velY)
for near in self.neighbour:
near.set_desiderd_velx(velX)
near.set_desiderd_vely(velY)
def set_desiderd_velx(self, velx):
for k1, v in self.hashRole.items():
v[1][0] = int(velx)
def set_desiderd_vely(self, vely):
for k1, v in self.hashRole.items():
v[1][1] = int(vely)
class RobotDisplacementUnicycle(RobotDisplacement):
def __init__(self, posX, posY, startAplha=0):
super().__init__(posX, posY)
self.alpha = startAplha
self.deltaAlpha = 0
self.kp = 2
def calculateControlDisplacement(self, stepTime):
linearux = self.kp * sum((z.getPosX() - self.getPosX() - self.desiredDistanceX(z)) for z in self.neighbour)
linearuy = self.kp * sum((z.getPosY() - self.getPosY() - self.desiredDistanceY(z)) for z in self.neighbour)
l = 1
v = math.cos(-self.alpha) * linearux - math.sin(-self.alpha) * linearuy
w = (math.sin(-self.alpha) * linearux + math.cos(-self.alpha) * linearuy) / l
self.deltaX = v * stepTime * math.cos(self.alpha)
self.deltaY = v * stepTime * math.sin(self.alpha)
self.deltaAlpha = w * stepTime
self.error = math.sqrt(sum(np.linalg.norm([z.getPosX() - self.getPosX() - self.desiredDistanceX(z), z.getPosY() - self.getPosY() - self.desiredDistanceY(z)])**2 for z in self.neighbour))
"""
print("The v control of the DpU robot with role " + str(self.role) + " is " + str(v))
print("The w control of the DpU robot with role " + str(self.role) + " is " + str(w))
"""
def updatePosition(self):
super().updatePosition()
self.alpha += self.deltaAlpha
self.deltaAlpha = 0
print("the new theta is " + str(self.alpha))
@staticmethod
def makeRandomRobot():
posXr = random.randint(0, 50)
posYr = random.randint(0, 50)
theta = random.random()*2* math.pi
robot = RobotDisplacementUnicycle(posXr, posYr, theta)
return robot
@staticmethod
def getName():
return "Robot displacement unicycle"
# DISTANCE CONTROLED ROBOT
class RobotDistance(Robot, ABC):
def __init__(self, startX, startY, theta=random.random() * 2 * math.pi):
super().__init__(0, 0)
self.startX = startX
self.startY = startY
self.theta = theta
def getAbsolutePosX(self):
return self.startX + self.getPosX()*math.cos(self.theta) - self.getPosY()*math.sin(self.theta)
def getAbsolutePosY(self):
return self.startY + self.getPosY()*math.cos(self.theta) + self.getPosX()*math.sin(self.theta)
def calculateControl(self, stepTime):
self.calculateControlDistance(stepTime)
@abstractmethod
def calculateControlDistance(self, stepTime):
pass
class RobotDistanceSingleIntegrator(RobotDistance):
def __init__(self, startX, startY, theta=random.random() * 2 * math.pi):
super().__init__(startX, startY, theta)
self.kp = 1
@staticmethod
def makeRandomRobot():
startXr = random.randint(0, 50)
startYr = random.randint(0, 50)
startTheta = random.random() * 2 * math.pi
robot = RobotDistanceSingleIntegrator(startXr, startYr, startTheta)
return robot
# todo aumentare performance
def calculateControlDistance(self, stepTime):
uX = 0
uY = 0
for n in self.neighbour:
absDistance = abs(myGlobalEnviroment.distanceBetweenRobots(self, n)) # sensore di distanza
if absDistance == 0:
print("min_fisic_tollerance_distance in RobotDistanceSingleIntegrator")
absDistance = min_fisic_tollerance_distance
cos_sin_theta = myGlobalEnviroment.get_relative_cos_sin_theta_between_robots(self, n)
u = self.kp * 4*(absDistance**2 - self.hashRole[n.role]**2)/absDistance - 2*(absDistance**2 - self.hashRole[n.role]**2)**2/absDistance**3
uX += u * cos_sin_theta[0]
uY += u * cos_sin_theta[1]
"""
print("The uX control of the DtSI robot with role " + str(self.role) + " is " + str(uX))
print("The uY control of the DtSI robot with role " + str(self.role) + " is " + str(uY))
"""
self.deltaX = uX * stepTime
self.deltaY = uY * stepTime
self.error = 0
for n in self.neighbour:
self.error += (abs(abs(myGlobalEnviroment.distanceBetweenRobots(self, n))-self.hashRole[n.role]))**2
self.error = math.sqrt(self.error)
@staticmethod
def getName():
return "Robot distance single integrator"
class RobotDistanceDoubleIntegrator(RobotDistance):
def __init__(self, startX, startY, theta=random.random() * 2 * math.pi, velX=0, velY=0):
super().__init__(startX, startY, theta)
self.velX = velX
self.velY = velY
self.deltaVelX = 0
self.deltaVelY = 0
self.kp = 4
self.kv = 12
def getVelX(self):
return self.velX
def getVelY(self):
return self.velY
def updatePosition(self):
self.posX = self.getPosX() + self.deltaX
self.posY = self.getPosY() + self.deltaY
self.velX = self.getVelX() + self.deltaVelX
self.velY = self.getVelY() + self.deltaVelY
self.deltaX = 0
self.deltaY = 0
self.deltaVelX = 0
self.deltaVelY = 0
def calculateControlDistance(self, stepTime):
uX = 0
uY = 0
for n in self.neighbour:
absDistance = abs(myGlobalEnviroment.distanceBetweenRobots(self, n)) # sensore di distanza
if absDistance == 0:
print("min_fisic_tollerance_distance in RobotDistanceDoubleIntegrator")
absDistance = min_fisic_tollerance_distance
cos_sin_theta = myGlobalEnviroment.get_relative_cos_sin_theta_between_robots(self, n)
u = (4 * (absDistance ** 2 - self.desideredDistance(n) ** 2) / absDistance) - (2 * (absDistance ** 2 - self.desideredDistance(n) ** 2) ** 2 / absDistance ** 3)
uX += u * cos_sin_theta[0]
uY += u * cos_sin_theta[1]
uX -= self.Dx()
uY -= self.Dy()
"""
print("The uX control of the DtDI robot with role " + str(self.role) + " is " + str(uX))
print("The uY control of the DtDI robot with role " + str(self.role) + " is " + str(uY))
"""
self.deltaX = self.velX * stepTime + (uX * (stepTime ** 2) / 2)
self.deltaY = self.velY * stepTime + (uY * (stepTime ** 2) / 2)
self.deltaVelX = uX * stepTime
self.deltaVelY = uY * stepTime
self.error = 0
for n in self.neighbour:
self.error += abs(abs(myGlobalEnviroment.distanceBetweenRobots(self, n)) - self.desideredDistance(n)) ** 2
self.error = math.sqrt(self.error)
def Dx(self):
return self.kv * self.getVelX()
def Dy(self):
return self.kv * self.getVelY()
def desideredDistance(self, n):
return self.hashRole[n.role][0][0]
@staticmethod
def makeRandomRobot():
startXr = random.randint(0, 50)
startYr = random.randint(0, 50)
startTheta = random.randint(0, 90)
robot = RobotDistanceDoubleIntegrator(startXr, startYr, startTheta)
return robot
@staticmethod
def getName():
return "Robot distance double integrator"
class RobotDistanceUnicycle(RobotDistance):
def __init__(self, posX, posY, theta=0, startAplha=0):
super().__init__(posX, posY, theta)
self.alpha = startAplha
self.deltaAlpha = 0
self.kp = 1
def updatePosition(self):
super().updatePosition()
self.alpha += self.deltaAlpha
self.deltaAlpha = 0
@staticmethod
def makeRandomRobot():
posXr = random.randint(0, 50)
posYr = random.randint(0, 50)
theta = random.random() * 2 * math.pi
alpha = random.random() * 2 * math.pi
robot = RobotDistanceUnicycle(posXr, posYr, theta=theta, startAplha=alpha)
return robot
def calculateControlDistance(self, stepTime):
linearux = 0
linearuy = 0
for n in self.neighbour:
absDistance = abs(myGlobalEnviroment.distanceBetweenRobots(self, n)) # sensore di distanza
if absDistance == 0:
print("min_fisic_tollerance_distance in RobotDistanceUnycicle")
absDistance = min_fisic_tollerance_distance
cos_sin_theta = myGlobalEnviroment.get_relative_cos_sin_theta_between_robots(self, n)
u = self.kp * (4*(absDistance**2 - self.hashRole[n.role]**2)/absDistance) - (2*(absDistance**2 - self.hashRole[n.role]**2)**2/absDistance**3)
linearux += u * cos_sin_theta[0]
linearuy += u * cos_sin_theta[1]
l = 1
v = math.cos(-self.alpha) * linearux - math.sin(-self.alpha) * linearuy
w = (math.sin(-self.alpha) * linearux + math.cos(-self.alpha) * linearuy) / l
self.deltaX = v * stepTime * math.cos(self.alpha)
self.deltaY = v * stepTime * math.sin(self.alpha)
self.deltaAlpha = w * stepTime
self.error = 0
for n in self.neighbour:
self.error += abs(abs(myGlobalEnviroment.distanceBetweenRobots(self, n)) - self.hashRole[n.role]) ** 2
self.error = math.sqrt(self.error)
"""
print("The v control of the DtU robot with role " + str(self.role) + " is " + str(v))
print("The w control of the DtU robot with role " + str(self.role) + " is " + str(w))
"""
@staticmethod
def getName():
return "Robot distance unicycle"