-
Notifications
You must be signed in to change notification settings - Fork 0
/
swarm_controller.py
230 lines (189 loc) · 10.5 KB
/
swarm_controller.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
import random
import time
from enum import Enum
import math
from layer import *
from geopy.distance import great_circle
from point import Point
class State(Enum):
normal = 1
coherence = 2
avoidance = 3
class CoherenceState(Enum):
normal = 1
lost = 2
class SwarmController(Layer):
def __init__(self, next, config, data_store, telemetry):
Layer.__init__(self, next)
self.state = State.normal
self.data_store = data_store
self.telemetry = telemetry
self.avoidance_target = None
self.aggregation_timer = time.time()
self.target = None
self.coherence_state = CoherenceState.normal
self.coherence_timer = None
self.avoidance_radius = config.getint('avoidance_radius')
self.aggregation_timeout = config.getint('aggregation_timeout')
self.target_radius = config.getint('target_radius')
self.radio_radius = config.getint('radio_radius')
self.drone_timeout = config.getint('drone_timeout')
self.cohesion_degree = config.getint('cohesion_degree')/100
self.critical_avoidance_range = config.getint('critical_avoidance_range')
def execute_layer(self, current_output):
# print("SWARM STATE: " + str(self.state))
if self.state == State.normal:
# 1. Check if avoidance needed
if self.avoidance_needed():
return self.perform_avoidance(current_output)
# 2. Check if coherence needed
elif self.coherence_needed():
return self.perform_coherence(current_output)
# 3. Continue to searching layer
else:
return self.perform_normal(current_output)
elif self.state == State.coherence:
if self.avoidance_needed():
return self.perform_avoidance(current_output)
elif self.coherence_complete():
# If coherence complete return to normal; otherwise continue with coherence
self.aggregation_timer = time.time()
return self.perform_normal(current_output)
else:
return self.perform_coherence(current_output)
elif self.state == State.avoidance:
# If avoidance complete return to normal; otherwise continue with avoidance
if self.avoidance_complete():
# print("AVOIDANCE COMPLETE")
self.aggregation_timer = time.time()
return self.perform_normal(current_output)
else:
return self.perform_avoidance(current_output)
# Checks whether an avoidance move is necessary in the current state
def avoidance_needed(self):
current_position = self.telemetry.get_location()
position_of_closest = self.data_store.get_position_of_drone_closest_to(current_position,
timeout=self.drone_timeout)
if position_of_closest is not None:
# print("Distance to closest: " + str(current_position.distance_to(position_of_closest)))
return current_position.altitude == position_of_closest.altitude and \
current_position.distance_to(position_of_closest) < self.avoidance_radius
else:
return False
# Returns an action that needs to be taken for avoidance
def perform_avoidance(self, current_output):
if self.state != State.avoidance:
print("AVOIDANCE INITIATED")
# If avoidance was just initiated, we need to calculate which way to avoid to
self.state = State.avoidance
current_position = self.telemetry.get_location()
position_of_closest = self.data_store.get_position_of_drone_closest_to(current_position,
timeout=self.drone_timeout)
avoidance_latitude = current_position.latitude - (position_of_closest.latitude - current_position.latitude)
avoidance_longitude = current_position.longitude - (
position_of_closest.longitude - current_position.longitude)
avoidance_altitude = current_position.altitude
self.target = Point(
latitude=avoidance_latitude,
longitude=avoidance_longitude,
altitude=avoidance_altitude)
bearing_to_target = current_position.bearing_to_point(self.target)
distance_to_avoidance_target = self.target.distance_to(current_position)
# If the avoidance target is too close we instead move to a random direction
if distance_to_avoidance_target < 5:
# print('CRITICAL AVOIDANCE DETECTED')
self.target = Point(great_circle(meters=self.critical_avoidance_range).destination(current_position, random.uniform(0,360)))
else:
self.target = Point(great_circle(meters=distance_to_avoidance_target).destination(current_position, (bearing_to_target+10) % 360))
self.target.altitude = current_position.altitude
# print("AVOIDANCE TARGET: " + str(self.target) +
# "CURRENT POSITION: " + str(current_position) +
# "DISTANCE: " + str(distance_to_avoidance_target))
self.aggregation_timer = time.time()
current_output.move = self.target
if hasattr(current_output.move, 'simple_string'):
current_output.move_info = "AVOIDANCE MOVE: " + current_output.move.simple_string()
else:
current_output.move_info = "AVOIDANCE MOVE"
return current_output
def avoidance_complete(self):
return self.telemetry.get_location().distance_to(self.target) < self.target_radius
def coherence_needed(self):
# The requirements for coherence are an extension of the very rudimentary requirements specified in the omega
# algorithm to accomodate the more complex function of the swarm.
# The idea is that if the aggregation timer runs out, we check whether the center
# of mass would be within radio range if another aggregation timer ran out
# return time.time() - self.aggregation_timer > self.aggregation_timeout
if time.time() - self.aggregation_timer > self.aggregation_timeout:
current_position = self.telemetry.get_location()
position_of_closest = self.data_store.get_position_of_drone_closest_to(current_position,
timeout=self.drone_timeout)
# center_of_mass = self.compute_neighbour_mass_center()
if position_of_closest is not None:
distance_to_closest = position_of_closest.distance_to(current_position)
if distance_to_closest < self.radio_radius*0.5:
self.aggregation_timer = time.time()
else:
return True
else:
return True
else:
return False
def perform_coherence(self, current_output):
if self.state != State.coherence:
print("Coherence initiated")
self.coherence_state = CoherenceState.normal
self.state = State.coherence
self.coherence_timer = time.time()
if self.coherence_needed():
current_position = self.telemetry.get_location()
center_of_mass = self.compute_neighbour_mass_center()
if center_of_mass is not None:
# We move only partially towards the center of mass
self.target = Point(
latitude=current_position.latitude +
(center_of_mass.latitude - current_position.latitude) * self.cohesion_degree,
longitude=current_position.longitude +
(center_of_mass.longitude - current_position.longitude) * self.cohesion_degree,
altitude=current_position.altitude +
(center_of_mass.altitude - current_position.altitude) * self.cohesion_degree)
else:
# If no neighbours in range, move towards the mass center of recently seen drones first, otherwise go home
print("FRIENDS LOST")
if self.coherence_state == CoherenceState.normal:
if not hasattr(self.target, "distance_to"):
self.target = Point(self.target)
if time.time() - self.coherence_timer > 10:
self.coherence_state = CoherenceState.lost
else:
center_of_mass_wider = self.compute_neighbour_mass_center(self.radio_radius*2)
self.target = center_of_mass_wider
if self.coherence_state == CoherenceState.lost:
initial_position = self.telemetry.get_initial_location()
bearing_to_initial = current_position.bearing_to_point(initial_position)
towards_home = current_position.point_at_vector(self.radio_radius/2, bearing_to_initial)
if towards_home.distance_to(initial_position) > current_position.distance_to(initial_position):
self.target = initial_position
else:
self.target = towards_home
# print("COHERENCE INITIATED TOWARDS: " + str(self.target) +
# "CURRENT POSITION: " + str(current_position) +
# "DISTANCE: " + str(self.target.distance_to(current_position)))
current_output.move = self.target
if hasattr(current_output.move, 'simple_string'):
current_output.move_info = "COHERENCE MOVE: " + current_output.move.simple_string()
else:
current_output.move_info = "COHERENCE MOVE"
return current_output
def compute_neighbour_mass_center(self, custom_radius=0):
current_position = self.telemetry.get_location()
if custom_radius == 0:
return self.data_store.compute_neighbour_mass_center(current_position, self.radio_radius, self.drone_timeout)
else:
return self.data_store.compute_neighbour_mass_center(current_position, custom_radius, self.drone_timeout)
def coherence_complete(self):
return self.telemetry.get_location().distance_to(self.target) < self.target_radius
def perform_normal(self, current_output):
if self.state != State.normal:
self.state = State.normal
return Layer.execute_layer(self, current_output)