-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvehicle.py
315 lines (263 loc) · 11.4 KB
/
vehicle.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
import pygame as pg
from utils import Aircraft, random_color, limit, constrain
from constants import *
from math import cos, sin, atan2, pi
import random
import copy
vec2 = pg.math.Vector2
class Vehicle():
def __init__(self, x,y, behavior, window):
"""
idealized vehicle representing a drone
:param x and y: represents inicial target
:param behavior: State Machine
:param window: pygame screen were it will be draw
"""
self.debug = False # debug lines is Off
# Variables used to move drone
self.location = vec2(random.uniform(0,SCREEN_WIDTH),random.uniform(0,SCREEN_HEIGHT)) # Random position in screen
self.velocity = vec2(0.1,0) # Inicial speed
self.target = vec2(x,y)
self.acceleration = vec2(0,0)
self.radius = SIZE_DRONE # Drone Size
self.desired = vec2()
self.memory_location = [] # To draw track
self.rotation = atan2(self.location.y,self.location.x) # inicital rotation
# Arbitrary values
self.max_speed = FORWARD_SPEED
self.max_force = SEEK_FORCE
self.angular_speed = ANGULAR_SPEED
# Picks a random color for target, is used to differentiate visually during simulation
self.color_target = random_color()
# Variables related to State Machine
self.behavior = behavior
self.window = window # tela em que esta acontecendo a simulaçao
self.theta = 0 # variavel para o eight somada no seek_around
self.count = 0
# Variables to draw drone using Sprites
self.drone = Aircraft()
self.all_sprites = pg.sprite.Group()
self.all_sprites.add(self.drone)
def update(self):
"""
Standart Euler integration
Updates bahavior tree
"""
# updates behavior in machine state
self.behavior.update(self)
# Updates velocity at every step and limits it to max_speed
self.velocity += self.acceleration * 1
self.velocity = limit(self.velocity, self.max_speed)
# updates position
self.location += self.velocity
# Prevents it from crazy spinning due to very low noise speeds
if self.velocity.length() > 0.5:
self.rotation = atan2(self.velocity.y,self.velocity.x)
# Constrains position to limits of screen
self.location = constrain(self.location,SCREEN_WIDTH,SCREEN_HEIGHT)
self.acceleration *= 0
# Memory of positions to draw Track
self.memory_location.append((self.location.x,self.location.y))
# size of track
if len(self.memory_location) > SIZE_TRACK:
self.memory_location.pop(0)
def applyForce(self, force):
"""
Applies vetor force to vehicle
Newton's second law -> F=m.a
You can divide by mass
"""
self.acceleration += force/MASS
def seek(self, target):
"""
Seek Steering force Algorithm
"""
try:
self.desired = (target - self.location).normalize()*self.max_speed
except: # if you try to normalize a null vector it will catch
self.desired = (target - self.location)*self.max_speed
# Calculates steering force
steer = self.desired - self.velocity
# Limit the magnitude of the steering force.
steer = limit(steer,self.max_force)
# Applies steering force to drone
self.applyForce(steer)
# Draws current target being seeked
pg.draw.circle(self.window, self.color_target ,target ,5, 0)
def arrive(self, target):
"""
Arrive Steering Behavior
"""
# Calculates vector desired
self.desired = (target - self.location)
# get the distance to the target
d = self.desired.magnitude()
try:
dist = copy.deepcopy(self.desired.normalize()) # obtem direção
except: # If the magnitude of desired is zero it cant be normalized
dist = copy.deepcopy(self.desired)
r = RADIUS_TARGET
# Modulates the force
if d < r : # close to target it will reduce velocty till stops
# interpolation
dist *= self.max_speed*(1 + 1/r*(d-r))
else:
dist *= self.max_speed
# Steering force
steer = dist - self.velocity
#Limit the magnitude of the steering force.
steer = limit(steer, self.max_force)
# apply force to the vehicle
self.applyForce(steer)
# Simulates Wind - random Noise
wind = vec2(random.uniform(-0.15,0.15) , random.uniform(-0.15,0.15) )
self.applyForce(wind)
# Draws current target as a point
pg.draw.circle(self.window, self.color_target ,target ,5, 0)
def stay_at(self, center, r = RADIUS_TARGET):
"""
Drone Behavior - it will orbit a given target (center)
"""
posToCenter = center - self.location
#ok
if self.debug == True:
pg.draw.line(self.window,BLACK, self.location ,center,1)
# se o veiculo se encontra mais longue q o raio de rotaçao
if posToCenter.length() > r :
self.seek(center)
#self.target =copy.deepcopy(center)
else: # se ele esta dentro do raio de rotaçao
# reinicia forças
centerToPerimeter = posToCenter.normalize()*(-1*r )
#ok
pg.draw.line(self.window,(0,0,255),center,center+centerToPerimeter,5 )
posToPerimeter = centerToPerimeter + posToCenter
#pg.draw.line(window,(255,0,0),center,center+posToPerimeter,5 )
print(f'distancia até perimetro {posToPerimeter.length()}')
# new target is on the radius
# theta is the angle of the vector center to perimeter
theta = atan2(centerToPerimeter.y, centerToPerimeter.x)
theta += self.angular_speed
new_target = vec2(0,0)
# new target
new_target.x += r * cos(theta)
new_target.y += r * sin(theta)
new_target += center
if self.debug == True:
pg.draw.line(self.window,(0,255,0), center, new_target ,5)# verde é o target
pg.draw.line(self.window,BLACK, self.location, new_target, 2 )
self.seek(new_target)
def seek_around(self, center, radius_target = RADIUS_TARGET):
"""
Drone Behavior - it will orbit a given target (center) with prevision
:param center: position of target to orbite
:param radius_target: distance till center, default = RADIUS_TARGET from constants
"""
# Calculating the max speed
self.angular_speed = FORWARD_SPEED / radius_target
# future positiom
hop_ahead = HOP_AHEAD #o quanto se ve a frente
fut_pos = self.velocity.normalize()*(hop_ahead)
fut_pos += self.location
if self.debug == True:
pg.draw.line(self.window,(0,255,50),self.location,fut_pos,5)
#print(f'center: {center}')
posToCenter = center - fut_pos
# line from drone to center
if self.debug == True:
pg.draw.line(self.window,BLACK, self.location ,center,1)
# se o veiculo se encontra mais longue q o raio de rotaçao
if posToCenter.length() > radius_target:
self.seek(center)
#self.target =copy.deepcopy(center)
else: # se ele esta dentro do raio de rotaçao
# reinicia forças
centerToPerimeter = posToCenter.normalize()*(-1*radius_target)
#ok
if self.debug == True:
pg.draw.line(self.window,(0,0,255),center,center+centerToPerimeter,5 )
posToPerimeter = centerToPerimeter + posToCenter
#pg.draw.line(window,(255,0,0),center,center+posToPerimeter,5 )
#print(f'distancia até perimetro {posToPerimeter.length()}')
# new target is on the radius
# theta is the angle of the vector center to perimeter
self.theta = atan2(centerToPerimeter.y, centerToPerimeter.x)
self.theta += self.angular_speed
new_target = vec2(0,0)
# new target
new_target.x += radius_target * cos(self.theta)
new_target.y += radius_target * sin(self.theta)
new_target += center
if self.debug == True:
pg.draw.line(self.window,(0,255,0), center, new_target ,5)# verde é o target
pg.draw.line(self.window,BLACK, self.location, new_target, 2 )
self.seek(new_target)
def get_position(self):
return self.location
def set_target(self, target):
self.target = target
def get_target(self):
try:
return self.target
except:
return None
def set_debug(self):
"""
Method to view debug lines . Assists the developer.
"""
self.debug = not self.debug
def get_debug(self):
return str(self.debug)
def check_collision(self, all_positions, index):
"""
This method checks if the drone is colliding with another
drone during simulation it receives all the positions from all drones
"""
# gets all positions of simultaneos drones
aux = 0
for p in all_positions:
# compares current position to all the drones
# aux != index -> avoids the auto-collision check
if ((self.location - p.location).length() < AVOID_DISTANCE) and aux != index:
self.velocity *= - 20 # arbitrary factor that defines how strong is the impact
#self.applyForce(vec2(-self.max_force,-self.max_force))
break
aux+=1
def draw(self, window):
"""
Defines shape of vehicle and draw it to screen
"""
# draws track
if len(self.memory_location) >= 2:
pg.draw.lines(self.window, self.color_target, False, self.memory_location, 1)
# Drawing drone's outer circle as a hitbox?
if self.debug == True:
pg.draw.circle(self.window, (100, 100, 100), self.location, AVOID_DISTANCE, 1)
# usar sprite para desenhar drone
self.all_sprites.draw(self.window)
self.all_sprites.update(self.location,self.rotation)
#---- these methods were not used in this project
def collision_avoidance(self, all_positions, index):
"""
Not working yet
"""
aux = 0
for p in all_positions:
if ((self.location - p.location).length() < AVOID_DISTANCE) and (aux != index):
desired = vec2(self.max_speed, self.velocity.y)
steer = desired - self.velocity
steer = limit(steer, self.max_force)
self.applyForce(steer)
print(f'Alerta de colisão drone {index} com drone {aux}')
break
aux +=1
def bouncing(self):
"""
Bouncing Behavior
NOT USED
"""
if self.location.x + self.radius > SCREEN_WIDTH or self.location.x - self.radius< 0:
self.velocity.x *= -1
if self.location.y + self.radius> SCREEN_HEIGHT or self.location.y- self.radius < 0:
self.velocity.y *= -1
self.location += self.velocity