forked from MrDaviesKellett/djitellopySim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdjitellopySim.py
More file actions
409 lines (322 loc) · 12 KB
/
Copy pathdjitellopySim.py
File metadata and controls
409 lines (322 loc) · 12 KB
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
import logging
import time
from random import randint, uniform
from math import cos, sin, radians, pi
from threading import Thread, Barrier
from queue import Queue
from typing import List, Callable
from physicsSim import sim
PI = pi
SIMULATION = sim()
class TelloException(Exception):
pass
class Tello:
# Class variables
TELLO_IP = "192.168.10.1" # Tello IP address
CONTROL_UDP_PORT = 8889
STATE_UDP_PORT = 8890
# Set up logger
HANDLER = logging.StreamHandler()
FORMATTER = logging.Formatter(
"[%(levelname)s] %(filename)s - %(lineno)d - %(message)s"
)
HANDLER.setFormatter(FORMATTER)
LOGGER = logging.getLogger("djitellopy simulator")
LOGGER.addHandler(HANDLER)
LOGGER.setLevel(logging.DEBUG)
def __init__(self, host=TELLO_IP, swarm = False):
self.flightPathTaken = []
self.address = (host, Tello.CONTROL_UDP_PORT)
self.drone = {}
self.simulation = SIMULATION
self.drone["scl"] = 0.08
# Set the initial position, scale, and rotation
self.drone["pos"] = [self.simulation.width / 2, self.simulation.height / 2, 1.0]
self.drone["rot"] = 0
# Set the speed of the sprite (in pixels per second)
self.drone["speed"] = 400
self.drone["flip"] = 0
self.drone["led"] = (0, 0, 0)
self.swarm = swarm
self.is_flying = False
self.is_windy = True
self.is_latency = True
# message as given by actual Tello class.
self.LOGGER.info(
"Tello instance was initialized. Host: '{}'. Port: '{}'.".format(
host, Tello.CONTROL_UDP_PORT
)
)
self.simulation.register(self)
if swarm is False:
self.simulation.event_loop()
def _setSwarmPos(self, i):
self.drone["pos"][0] += - 260 + 130 * (i % 4)
self.drone["pos"][1] += - 260 + 130 * (i // 4)
def connect(self, wait_for_state=True):
# Connect to the Tello drone
if wait_for_state:
t = randint(1, 5) / 5
time.sleep(t)
Tello.LOGGER.debug(
"'.connect()' received first state packet after {} seconds".format(t)
)
def simLat(self, min=0.1, max=0.5):
# wait a random amount of time
if self.is_latency == False:
return False
waitTime = uniform(min, max)
time.sleep(waitTime)
def takeoff(self):
Tello.LOGGER.info("sending takoff command to drone")
self.simLat()
current_height = self.drone["pos"][2]
target_height = 1.5
height_diff = target_height - current_height
steps = int(max(abs(height_diff * 100 / self.drone["speed"] * 60), 1))
delta_height = height_diff / steps
self.is_flying = True
for _ in range(steps):
self.drone["pos"][2] += delta_height
time.sleep(0.01)
def land(self):
Tello.LOGGER.info("sending land command to drone")
self.simLat()
# Land the drone by gradually decreasing its z position to 1.0
target_height = 1.0
current_height = self.drone["pos"][2]
height_diff = target_height - current_height
steps = int(max(abs(height_diff * 100 / self.drone["speed"] * 60), 1))
delta_height = height_diff / steps
for _ in range(steps):
self.drone["pos"][2] += delta_height
time.sleep(0.01)
self.is_flying = False
if self.swarm is False:
self.simulation.quit()
def move(self, direction: str, x: int):
Tello.LOGGER.info(
f"sending move command to drone in direction {direction} by {x}"
)
if not self.is_flying:
raise TelloException("Drone is not flying!")
self.simLat()
current_x = self.drone["pos"][0]
current_y = self.drone["pos"][1]
current_z = self.drone["pos"][2]
target_x = current_x
target_y = current_y
target_z = current_z
angle_rad = radians(self.drone["rot"])
match direction:
case "forward":
target_x = current_x + x * sin(angle_rad)
target_y = current_y + x * cos(angle_rad)
case "back":
target_x = current_x - x * sin(angle_rad)
target_y = current_y - x * cos(angle_rad)
case "left":
angle_rad = radians(self.drone["rot"] - 90)
target_x = current_x + x * sin(angle_rad)
target_y = current_y + x * cos(angle_rad)
case "right":
angle_rad = radians(self.drone["rot"] + 90)
target_x = current_x + x * sin(angle_rad)
target_y = current_y + x * cos(angle_rad)
case "up":
x /= 100
target_z = current_z + x
case "down":
x /= 100
target_z = current_z - x
diff_x = target_x - current_x
diff_y = target_y - current_y
diff_z = target_z - current_z
maxDiff = max(abs(diff_x), abs(diff_y), abs(diff_z) * 100)
steps = int(max(maxDiff / self.drone["speed"] * 60, 1))
delta_x = diff_x / steps
delta_y = diff_y / steps
delta_z = diff_z / steps
for _ in range(steps):
self.drone["pos"][0] += delta_x
self.drone["pos"][1] += delta_y
self.drone["pos"][2] += delta_z
time.sleep(0.01)
def rotate(self, direction: str, x: int):
Tello.LOGGER.info(
f"sending rotate command to drone in direction {direction} by {x} degrees"
)
if not self.is_flying:
raise TelloException("Drone is not flying!")
self.simLat()
match direction:
case "cw":
pass
case "ccw":
x = -x
steps = int(max(abs(x / self.drone["speed"] * 60), 1))
delta_x = x / steps
for i in range(steps):
self.drone["rot"] -= delta_x
time.sleep(0.01)
def flip(self, direction: str):
Tello.LOGGER.info(
f"sending flip command to drone in direction {direction}"
)
self.drone["flip"] = 24
def flip_left(self):
self.flip("l")
def flip_right(self):
self.flip("r")
def flip_forward(self):
self.flip("f")
def flip_back(self):
self.flip("b")
def move_forward(self, x):
# Simulate moving the tello forward by `x` amount.
self.move("forward", x)
def move_backward(self, x):
# Simulate moving the tello backward by `x` amount.
self.move("back", x)
def move_left(self, x):
# Simulate moving the tello left by `x` amount.
self.move("left", x)
def move_right(self, x):
# Simulate moving the tello right by `x` amount.
self.move("right", x)
def move_up(self, x):
# Simulate moving the tello up by `x` amount.
self.move("up", x)
def move_down(self, x):
# Simulate moving the tello down by `x` amount.
self.move("down", x)
def rotate_clockwise(self, x):
# Simulate rotating the tello clockwise by `x` amount.
self.rotate("cw", x)
def rotate_counter_clockwise(self, x):
# Simulate rotating the tello clockwise by `x` amount.
self.rotate("ccw", x)
def send_command_without_return(self, cmd):
c = cmd.split()
if c[0] == "EXT" and c[1] == "led":
self.drone["led"] = (int(c[2]), int(c[3]), int(c[4]))
else:
pass
class TelloSwarm:
"""Swarm library for controlling multiple Tellos simultaneously
"""
@staticmethod
def fromFile(path: str):
"""Create TelloSwarm from file. The file should contain one IP address per line.
Arguments:
path: path to the file
"""
with open(path, 'r') as fd:
ips = fd.readlines()
return TelloSwarm.fromIps(ips)
@staticmethod
def fromIps(ips: list):
"""Create TelloSwarm from a list of IP addresses.
Arguments:
ips: list of IP Addresses
"""
if not ips:
raise TelloException("No ips provided")
tellos = []
for ip in ips:
tellos.append(Tello(ip.strip(), swarm = True))
for i in range(len(tellos)):
tellos[i]._setSwarmPos(i)
return TelloSwarm(tellos)
def __init__(self, tellos: List[Tello]):
"""Initialize a TelloSwarm instance
Arguments:
tellos: list of [Tello][tello] instances
"""
self.tellos = tellos
self.barrier = Barrier(len(tellos))
self.funcBarrier = Barrier(len(tellos) + 1)
self.funcQueues = [Queue() for tello in tellos]
self.simulation = SIMULATION
def worker(i):
queue = self.funcQueues[i]
tello = self.tellos[i]
while True:
func = queue.get()
self.funcBarrier.wait()
func(i, tello)
self.funcBarrier.wait()
self.threads = []
for i, _ in enumerate(tellos):
thread = Thread(target=worker, daemon=True, args=(i,))
thread.start()
self.threads.append(thread)
self.simulation.event_loop()
def sequential(self, func: Callable[[int, Tello], None]):
"""Call `func` for each tello sequentially. The function retrieves
two arguments: The index `i` of the current drone and `tello` the
current [Tello][tello] instance.
```python
swarm.parallel(lambda i, tello: tello.land())
```
"""
for i, tello in enumerate(self.tellos):
func(i, tello)
def parallel(self, func: Callable[[int, Tello], None]):
"""Call `func` for each tello in parallel. The function retrieves
two arguments: The index `i` of the current drone and `tello` the
current [Tello][tello] instance.
You can use `swarm.sync()` for syncing between threads.
```python
swarm.parallel(lambda i, tello: tello.move_up(50 + i * 10))
```
"""
for queue in self.funcQueues:
queue.put(func)
self.funcBarrier.wait()
self.funcBarrier.wait()
def sync(self, timeout: float = None):
"""Sync parallel tello threads. The code continues when all threads
have called `swarm.sync`.
```python
def doStuff(i, tello):
tello.move_up(50 + i * 10)
swarm.sync()
if i == 2:
tello.flip_back()
# make all other drones wait for one to complete its flip
swarm.sync()
swarm.parallel(doStuff)
```
"""
return self.barrier.wait(timeout)
def land(self):
for t in self.tellos:
t.land()
self.simulation.quit()
def __getattr__(self, attr):
"""Call a standard tello function in parallel on all tellos.
```python
swarm.command()
swarm.takeoff()
swarm.move_up(50)
```
"""
def callAll(*args, **kwargs):
self.parallel(lambda i, tello: getattr(tello, attr)(*args, **kwargs))
return callAll
def __iter__(self):
"""Iterate over all drones in the swarm.
```python
for tello in swarm:
print(tello.get_battery())
```
"""
return iter(self.tellos)
def __len__(self):
"""Return the amount of tellos in the swarm
```python
print("Tello count: {}".format(len(swarm)))
```
"""
return len(self.tellos)