-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathendurance_calculator.py
307 lines (229 loc) · 9.2 KB
/
endurance_calculator.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
from collections import defaultdict
import math
from scipy.optimize import brentq
def make_dict():
return defaultdict(make_dict)
# Usage:
# tau = defaultdict(make_dict)
# v = 17
# i = 3
# j = 12
# tau[v][i][j] = 44
def Pi(T, Vvert):
return 0.8554*T*(Vvert/2.0 + math.sqrt((Vvert/2.0)**2 + T/(0.3051)**2))
def Pp(T):
return 0.3177*(T**1.5)
def Ppar(Vair):
return 0.0296*(Vair**3)
def Thrust(m, Vair): # MATTERNET M2 Weight without payload: 9.5 Kg
return math.sqrt(((1.5 + m)*9.8 - 0.0279*(Vair*math.cos(10*math.pi/180.0))**2)**2 + (0.0296*Vair**2)**2)
def SpeedFunction(m, Vair, distance, energy_left):
newT = Thrust(m, Vair)
return (distance/float(Vair))*(Pi(newT, 0) + Pp(newT) + Ppar(Vair)) - (energy_left - 0.00001)
def give_endurance(node, vehicle, travel, v, i, j, k, totalTimeij, totalTimejk, speedij, speedjk):
# for [v,i,j,k] in P:
p = vehicle[v].takeoffSpeed
qij = speedij
qjk = speedjk
r = vehicle[v].landingSpeed
TTvij = travel[v][i][j].takeoffTime
FTvij = totalTimeij - travel[v][i][j].takeoffTime - travel[v][i][j].landTime
LTvij = travel[v][i][j].landTime
TTvjk = travel[v][j][k].takeoffTime
FTvjk = totalTimejk - travel[v][j][k].takeoffTime - travel[v][j][k].landTime
LTvjk = travel[v][j][k].landTime
sj = node[j].serviceTimeUAV
mj = (node[j].parcelWtLbs)*0.453592 # Weight in Kg
E = vehicle[v].batteryPower
minimum_time_required = TTvij + FTvij + LTvij + sj + TTvjk + FTvjk + LTvjk
# a) Takeoff from customer i:
newT = Thrust(mj, 0)
Ea = TTvij*(Pi(newT, p) + Pp(newT))
# b) Fly to customer j:
newT = Thrust(mj, qij)
Eb = FTvij*(Pi(newT, 0) + Pp(newT) + Ppar(qij))
# c) Land at customer j:
newT = Thrust(mj, 0)
Ec = LTvij*(Pi(newT, r) + Pp(newT))
# d) Takeoff from customer j:
newT = Thrust(0, 0)
Ed = TTvjk*(Pi(newT, p) + Pp(newT))
# e) Fly to customer k:
newT = Thrust(0, qjk)
Ee = FTvjk*(Pi(newT, 0) + Pp(newT) + Ppar(qjk))
# f) Land at customer k:
newT = Thrust(0, 0)
Ef = LTvjk*(Pi(newT, r) + Pp(newT))
minimum_energy_required = Ea + Eb + Ec + Ed + Ee + Ef
energy_left = E - minimum_energy_required
if energy_left >= 0:
newT = Thrust(0, 0)
PHover = Pi(newT, 0) + Pp(newT)
endurance = minimum_time_required + float(energy_left)/PHover
else:
endurance = -1
return endurance
# return 1200
def modify_speed(oldLaunchTime, oldLandTime, newLaunchTime, newLandTime, v, i, j, k, node, vehicle, travel, totalTimeij, totalTimejk, speedij, speedjk):
oldDuration = oldLandTime - oldLaunchTime - vehicle[v].recoveryTime
newDuration = newLandTime - newLaunchTime - vehicle[v].recoveryTime
# for [v,i,j,k] in P:
p = vehicle[v].takeoffSpeed
r = vehicle[v].landingSpeed
TTvij = travel[v][i][j].takeoffTime
FTvij = totalTimeij - travel[v][i][j].takeoffTime - travel[v][i][j].landTime
LTvij = travel[v][i][j].landTime
TTvjk = travel[v][j][k].takeoffTime
FTvjk = totalTimejk - travel[v][j][k].takeoffTime - travel[v][j][k].landTime
LTvjk = travel[v][j][k].landTime
sj = node[j].serviceTimeUAV
mj = (node[j].parcelWtLbs)*0.453592 # Weight in Kg
E = vehicle[v].batteryPower
# a) Takeoff from customer i:
newT = Thrust(mj, 0)
Ea = TTvij*(Pi(newT, p) + Pp(newT))
# b) Fly to customer j:
newT = Thrust(mj, speedij)
Eb = FTvij*(Pi(newT, 0) + Pp(newT) + Ppar(speedij))
# c) Land at customer j:
newT = Thrust(mj, 0)
Ec = LTvij*(Pi(newT, r) + Pp(newT))
# d) Takeoff from customer j:
newT = Thrust(0, 0)
Ed = TTvjk*(Pi(newT, p) + Pp(newT))
# f) Land at customer k:
newT = Thrust(0, 0)
Ef = LTvjk*(Pi(newT, r) + Pp(newT))
energy_spent = Ea + Eb + Ec + Ed + Ef
energy_left = E - energy_spent
# We have to find the required FTvjk:
newFTvjk = newDuration - totalTimeij - sj - TTvjk - LTvjk
if newFTvjk >= 0:
newspeedjk = speedjk*FTvjk/float(newFTvjk)
changeij = False
else:
newspeedjk = float(vehicle[v].cruiseSpeed)
newFTvjk = speedjk*FTvjk/float(newspeedjk)
changeij = True
# Modify the speed of j -> k leg first:
# See if the speed is more than its maximum possible value:
if newspeedjk > vehicle[v].cruiseSpeed:
newspeedjk = float(vehicle[v].cruiseSpeed)
newFTvjk = speedjk*FTvjk/float(newspeedjk)
changeij = True
# Find the energy requirement associated with flying from j to k with new speed:
newT = Thrust(0, newspeedjk)
Ee = newFTvjk*(Pi(newT, 0) + Pp(newT) + Ppar(newspeedjk))
# See if the drone has enough energy left to fly at this speed:
if Ee > energy_left:
# Find a new speed:
if SpeedFunction(0, speedjk, speedjk*FTvjk, energy_left) < 0:
newspeedjk = brentq(lambda actVar: SpeedFunction(0, actVar, speedjk*FTvjk, energy_left), speedjk, vehicle[v].cruiseSpeed)
elif SpeedFunction(0, speedjk, speedjk*FTvjk, energy_left) < 0.00001:
newspeedjk = float(speedjk)
else:
print("ERROR: Energy calculation wrong. Exiting...")
exit()
newFTvjk = speedjk*FTvjk/float(newspeedjk)
newT = Thrust(0, newspeedjk)
Ee = newFTvjk*(Pi(newT, 0) + Pp(newT) + Ppar(newspeedjk))
changeij = False
# Modify the speed of i -> j leg, if necessary:
newFTvij = float(FTvij)
newspeedij = float(speedij)
if changeij == True:
newFTvij = newDuration - TTvij - LTvij - sj - TTvjk - newFTvjk - LTvjk
if newFTvij >= 0:
newspeedij = speedij*FTvij/float(newFTvij)
else:
newspeedij = float(vehicle[v].cruiseSpeed)
newFTvij = speedij*FTvij/float(newspeedij)
# See if the speed is more than its maximum possible value:
if newspeedij > vehicle[v].cruiseSpeed:
newspeedij = float(vehicle[v].cruiseSpeed)
newFTvij = speedij*FTvij/float(newspeedij)
# Find the energy requirement associated with flying from i to j with new speed:
newT = Thrust(mj, newspeedij)
Eb = newFTvij*(Pi(newT, 0) + Pp(newT) + Ppar(newspeedij))
energy_left = E - (Ea + Ec + Ed + Ee + Ef)
# See if the drone has enough energy left to fly at this speed:
if Eb > energy_left:
# Find a new speed:
if SpeedFunction(mj, speedij, speedij*FTvij, energy_left) < 0:
newspeedij = brentq(lambda actVar: SpeedFunction(mj, actVar, speedij*FTvij, energy_left), speedij, vehicle[v].cruiseSpeed)
elif SpeedFunction(mj, speedij, speedij*FTvij, energy_left) < 0.00001:
newspeedij = float(speedij)
else:
print("ERROR: Energy calculation wrong. Exiting...")
exit()
newFTvij = speedij*FTvij/float(newspeedij)
newT = Thrust(mj, newspeedij)
Eb = newFTvij*(Pi(newT, 0) + Pp(newT) + Ppar(newspeedij))
# As a last check, see if energy consumed is less than energy available:
if Ea + Eb + Ec + Ed + Ee + Ef - E > 0:
print("Energy calculation wrong. Exiting...")
exit()
# Other checks:
if newFTvij <= 0:
print("wrong i->j calculation. Exiting...")
exit()
if newFTvjk <= 0:
print("wrong j->k calculation. Exiting...")
exit()
if (round(newspeedij,5) < round(speedij,5)) or (round(newspeedij,5) > round(vehicle[v].cruiseSpeed,5)):
print("wrong i->j speed calculation. Exiting...")
exit()
if (round(newspeedjk,5) < round(speedjk,5)) or (round(newspeedjk,5) > round(vehicle[v].cruiseSpeed,5)):
print("wrong j->k speed calculation. Exiting...")
exit()
checktprime_at_j = newLaunchTime + TTvij + newFTvij + LTvij
hattprime_at_j = checktprime_at_j + sj
checktprime_at_k = hattprime_at_j + TTvjk + newFTvjk + LTvjk + vehicle[v].recoveryTime
newtotalTimeij = TTvij + newFTvij + LTvij
newtotalTimejk = TTvjk + newFTvjk + LTvjk
return [checktprime_at_j, hattprime_at_j, checktprime_at_k, newtotalTimeij, newtotalTimejk, newspeedij, newspeedjk]
def EndSpecs(node, vehicle, travel, v, i, j, k, distij, distjk, speedij, speedjk, idletime):
# for [v,i,j,k] in P:
p = vehicle[v].takeoffSpeed
qij = speedij
qjk = speedjk
r = vehicle[v].landingSpeed
TTvij = travel[v][i][j].takeoffTime
FTvij = distij/float(speedij)
LTvij = travel[v][i][j].landTime
TTvjk = travel[v][j][k].takeoffTime
FTvjk = distjk/float(speedjk)
LTvjk = travel[v][j][k].landTime
sj = node[j].serviceTimeUAV
mj = (node[j].parcelWtLbs)*0.453592 # Weight in Kg
E = vehicle[v].batteryPower
minimum_time_required = TTvij + FTvij + LTvij + sj + TTvjk + FTvjk + LTvjk
# a) Takeoff from customer i:
newT = Thrust(mj, 0)
Ea = TTvij*(Pi(newT, p) + Pp(newT))
# b) Fly to customer j:
newT = Thrust(mj, qij)
Eb = FTvij*(Pi(newT, 0) + Pp(newT) + Ppar(qij))
# c) Land at customer j:
newT = Thrust(mj, 0)
Ec = LTvij*(Pi(newT, r) + Pp(newT))
# d) Takeoff from customer j:
newT = Thrust(0, 0)
Ed = TTvjk*(Pi(newT, p) + Pp(newT))
# e) Fly to customer k:
newT = Thrust(0, qjk)
Ee = FTvjk*(Pi(newT, 0) + Pp(newT) + Ppar(qjk))
# f) Land at customer k:
newT = Thrust(0, 0)
Ef = LTvjk*(Pi(newT, r) + Pp(newT))
minimum_energy_required = Ea + Eb + Ec + Ed + Ee + Ef
energy_left = E - minimum_energy_required
if energy_left >= 0:
newT = Thrust(0, 0)
PHover = Pi(newT, 0) + Pp(newT)
endurance = minimum_time_required + float(energy_left)/PHover
else:
endurance = -1
enduranceUsed = minimum_time_required + idletime
energyUsed = minimum_energy_required + PHover*idletime
return [endurance, minimum_time_required, enduranceUsed, minimum_energy_required, energyUsed]