-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswarm_through_gate.py
94 lines (75 loc) · 2.47 KB
/
swarm_through_gate.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
import socket
import threading
import time
import drone_flight
# IP and port of Tello
tello1_address = ('192.168.1.63', 8889)
tello2_address = ('192.168.1.69', 8889)
# IP and port of local computer
local1_address = ('', 9010)
local2_address = ('', 9011)
# Create a UDP connection that we'll send the command to
sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind to the local address and port
sock1.bind(local1_address)
sock2.bind(local2_address)
# Send the message to Tello and allow for a delay in seconds
def send(message, delay):
# Try to send the message otherwise print the exception
try:
sock1.sendto(message.encode(), tello1_address)
sock2.sendto(message.encode(), tello2_address)
print("Sending message: " + message)
except Exception as e:
print("Error sending: " + str(e))
# Delay for a user-defined period of time
time.sleep(delay)
# Receive the message from Tello
def receive():
# Continuously loop and listen for incoming messages
while True:
# Try to receive the message otherwise print the exception
try:
response1, ip_address = sock1.recvfrom(128)
response2, ip_address = sock2.recvfrom(128)
print("Received message: from Tello EDU #1: " + response1.decode(encoding='utf-8'))
print("Received message: from Tello EDU #2: " + response2.decode(encoding='utf-8'))
except Exception as e:
# If there's an error close the socket and break out of the loop
sock1.close()
sock2.close()
print("Error receiving: " + str(e))
break
# Create and start a listening thread that runs in the background
# This utilizes our receive functions and will continuously monitor for incoming messages
receiveThread = threading.Thread(target=receive)
receiveThread.daemon = True
receiveThread.start()
# Each leg of the box will be 100 cm. Tello uses cm units by default.
box_leg_distance = 100
# Yaw 90 degrees
yaw_angle = 90
# Yaw clockwise (right)
yaw_direction = "cw"
# Put Tello into command mode
send("command", 3)
# Send the takeoff command
send("takeoff", 8)
center_queue = [sock1, sock2]
for sock in center_queue:
send("forward " + str("50"), 4)
#drone_flight.main()
# Loop and create each leg of the box
for i in range(4):
# Fly forward
send("forward " + str(box_leg_distance), 4)
# Yaw right
send("cw " + str(yaw_angle), 3)
# Land
send("land", 5)
# Print message
print("Mission completed!")
# Close the socket
sock1.close()
sock2.close()