-
Notifications
You must be signed in to change notification settings - Fork 1
/
tello.py
74 lines (60 loc) · 2.52 KB
/
tello.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
import socket
import threading
import time
from stats import Stats
class Tello:
def __init__(self):
self.local_ip = ''
self.local_port = 8889
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # socket for sending cmd
self.socket.bind((self.local_ip, self.local_port))
# thread for receiving cmd ack
self.receive_thread = threading.Thread(target=self._receive_thread)
self.receive_thread.daemon = True
self.receive_thread.start()
self.tello_ip = '192.168.10.1'
self.tello_port = 8889
self.tello_adderss = (self.tello_ip, self.tello_port)
self.log = []
self.MAX_TIME_OUT = 15.0
def send_command(self, command):
"""
Send a command to the ip address. Will be blocked until
the last command receives an 'OK'.
If the command fails (either b/c time out or error),
will try to resend the command
:param command: (str) the command to send
:param ip: (str) the ip of Tello
:return: The latest command response
"""
self.log.append(Stats(command, len(self.log)))
self.socket.sendto(command.encode('utf-8'), self.tello_adderss)
print ('sending command: %s to %s' % (command, self.tello_ip))
start = time.time()
while not self.log[-1].got_response():
now = time.time()
diff = now - start
if diff > self.MAX_TIME_OUT:
print ('Max timeout exceeded... command %s' % command)
# TODO: is timeout considered failure or next command still get executed
# now, next one got executed
return
print ('Done!!! sent command: %s to %s' % (command, self.tello_ip))
def _receive_thread(self):
"""Listen to responses from the Tello.
Runs as a thread, sets self.response to whatever the Tello last returned.
"""
while True:
try:
self.response, ip = self.socket.recvfrom(1024)
print('from %s: %s' % (ip, self.response))
self.log[-1].add_response(self.response)
except socket.error:
print("Caught exception socket.error : %s" % exc)
def on_close(self):
pass
# for ip in self.tello_ip_list:
# self.socket.sendto('land'.encode('utf-8'), (ip, 8889))
# self.socket.close()
def get_log(self):
return self.log