-
Notifications
You must be signed in to change notification settings - Fork 0
/
repeater.py
220 lines (161 loc) · 7.18 KB
/
repeater.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
import numpy as np
from numpy.linalg import norm
import conf
dt=conf.dt # assume time step always 0.1
class Repeater():
"""
Class for the repeater drone
"""
def __init__(self, pos,index,a_max,v_max):
"""
The repeater will have a position and a velocity.
The other variables are needed for the PD controller.
:param pos: initial position of the drone (usually set to the ground station position)
:param index: priority index of the repeater (lower index, higher priority in the swarm).
The repeaters must avoid collision with other repeaters with higher priority
:param a_max: maximum acceleration for the repeater
:param v_max: maximum velocity for the repeater
"""
self.position = pos
self.velocity = np.zeros(2)
self.pos_desired=None
self.a_max=a_max
self.v_max=v_max
#parameters for controller and force field
# Proportional and differential gains
self.kp = conf.kp
self.kd = conf.kd
# repulsive repeater gain, repulsive shaded gain, noise gain
self.G_r = conf.G_r
self.G_s = conf.G_s
self.G_n = conf.G_n
self.error_prev = np.zeros(2)
self.index=index
# For noise in the control
self.percistance_counter = 0
self.noise_direction = np.zeros(2)
def move(self, pos_desired, pos_main_drone, repeaters, boundary_centers):
"""
Updates the position and velocity of the repeater.
:param pos_desired: Desired position to go towards
:param pos_main_drone: Position of the main drone. Needed to avoid collision
:param repeaters: A list with all the repeater drones. To avoid collision
:param boundary_centers: A list with the center coordinates of the boundary
between seen and unseen area
:return:
"""
# Get the control signal (acceleration)
u = self.control(pos_desired, pos_main_drone, repeaters, boundary_centers)
# Make sure it's not too large
if norm(u) > self.a_max:
u = u / norm(u) * self.a_max
# Update velocity
self.velocity += u * dt
# Make sure it's not too large
if norm(self.velocity) > self.v_max:
self.velocity = self.velocity / norm(self.velocity) * self.v_max
# Update position
self.position += self.velocity*dt
def get_repulsive(self, centers, S = 5, R = 3):
"""
:param centers: discretized boundaries of the unaccessible areas.
:param S: distance of force field effect.
:param R: critic disnace. If repeater get closer than R the repulsion is bigger.
:return: total repulsive force from all the obstacles and unseen areas
"""
repulsive = np.zeros(2)
for center in centers:
d = norm(center - self.position)
# If repeater is far away, don't account for it at all
# or if we have priority
if d > S:
continue
# Direction from repeater to self, normalized
direction = (center - self.position) / d
# If very close, large repulsive force
if d <= R:
repulsive += direction * 100
else:
repulsive += ((S - d) / (S - R)) * direction
return repulsive
def get_repulsive_repeaters(self, repeaters, S = 5, R = 3):
"""
Compute repulsive force from the other drones with higher priority
:param repeaters: list of repeaters in the mine
:param S: distance of force field effect.
:param R: critic disnace. If repeater get closer than R the repulsion is bigger.
:return: total repulsive force from all the repeaters
"""
repulsive = np.zeros(2)
# Compute repulsive forces from repeaters
if len(repeaters) > 1: # If more repeaters than this one
for repeater in repeaters: # Look at all repeaters except for this one. Assign indices? d == 0?
d = norm(repeater.position - self.position)
# If repeater is far away, don't account for it at all
# or if we have priority
if d > S or d == 0 or self.index < repeater.index:
continue
# Direction from repeater to self, normalized
direction = (repeater.position - self.position) / d
# If very close, large repulsive force
if d <= R:
repulsive += direction * 100
else:
repulsive += ((S - d) / (S - R)) * direction
return repulsive
def get_repulsive_main(self, pos_main_drone, S = 5, R = 3):
"""
Compute repulsive force from the main drone
:param pos_main_drone: position of the main drone
:param S: distance of force field effect.
:param R: critic disnace. If repeater get closer than R the repulsion is bigger.
:return: repulsive force from the main drone
"""
repulsive = np.zeros(2)
d = norm(pos_main_drone - self.position)
# Direction from main drone to self, normalized
direction = (pos_main_drone - self.position) / d
# If very close, large repulsive force
if d <= R:
repulsive += direction * 100
else:
repulsive += ((S - d) / (S - R)) * direction
return repulsive
def get_noise(self):
"""
Additional noisy repulsion to help get out from local minima
:return:
"""
if self.percistance_counter % 10 == 0:
#The noise is changed every 10 time steps
noise_direction = np.random.normal(0, 1, 2)
self.noise_direction = noise_direction / norm(noise_direction)
self.percistance_counter += 1
return self.noise_direction
def control(self, pos_desired, pos_main_drone, repeaters, boundary_centers):
"""
PD controller to provide a control signal / acceleration
:param pos_desired: Desired position to go towards
:param pos_main_drone: Position of the main drone
:param repeaters: A list with all the repeater drones. To avoid collision
:param boundary_centers: A list with the center coordinates of the boundary
between seen and unseen area
:return: Control signal u
"""
### Repeaters
repulsive_repeaters = self.G_r * self.get_repulsive_repeaters(repeaters)
#### Boundary of non-seen
repulsive_shaded = self.G_s * self.get_repulsive(boundary_centers)
#### Main drone
repulsive_main_drone = self.G_r * self.get_repulsive_main(pos_main_drone)
### Noise
noise = self.G_n * self.get_noise()
# Accumulated repulsive force from obstacles, other drones and unseen area.
repulsive = - repulsive_repeaters - repulsive_shaded - repulsive_main_drone
# PD controller
error = pos_desired - self.position
d_error = (error - self.error_prev) / dt
# Control signal
u = self.kp * error + self.kd * d_error + repulsive + noise
self.error_prev = error
return u