Skip to content

Commit 7cdc298

Browse files
committed
Tello-Python SampleCode v1.0.0
1 parent 30e994e commit 7cdc298

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+7466
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.pyc
2+
*.DS_Store
3+
*.dat
4+
*.caffemodel

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Sample code is offered under MIT License (See below).
2+
3+
Copyright 2018 Ryze
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a
6+
copy of this software and associated documentation files (the "Software"),
7+
to deal in the Software without restriction, including without limitation
8+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
and/or sell copies of the Software, and to permit persons to whom the
10+
Software is furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included
13+
in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Tello-Python
2+
3+
##Introduction
4+
5+
This is a collection of python modules that interact with the Ryze Tello drone.
6+
7+
##Project Description
8+
9+
This toolkit contains three sample programs based on tello sdk and python2.7,including Single_Tello_Test, Tello_Video, and Tello_Video (With_Pose_Recognition). There is also a program file named tello_state.py.
10+
11+
- **Single_Tello_Test**
12+
13+
In Single_Tello_Test,You can design a series of command combinations by writing a txt script to let tello execute a series of actions you have designed. This program can also be used as a command set test tool for tello.
14+
15+
- **Tello_Video**
16+
17+
In Tello_Video,You can receive the video stream data from tello, decode the video through the h264 decoding library, and display it on a GUI interface based on Tkinter and PIL. In addition, it also supports a control panel that can operate tello. This sample code provides an example of receiving and processing and getting the correct video data. The source code of the h264 decoding library is also provided in the package, which can be used for your reference.
18+
19+
- **Tello_Video(With_Pose_Recognition)**
20+
21+
Tello_Video(With_Pose_Recognition) is an application version modified from Tello_Video.It uses the decoded video data,and everytime extract a single frame image for pose recognition operation , and binds the specific posture and aircraft control commands to realize the pose control of Tello.This code is mainly used as an application case for utilizing the decoded video data of tello for image processing.
22+
23+
- **Tello_state.py**
24+
25+
Tello_state.py can read the various status data of tello, and can be used as a tool to debug and view the status of tello.
26+
27+
## Environmental configuration
28+
29+
The sample codes above are based on python2.7.There is no need to install additional third-party libraries for running Single_Tello_Test and tello_state.py.For Tello_Video and Tello_Video (With_Pose_Recognition), you need to install a series of third-party libraries. Therefore, in these two folders, a one-click installation script (based on windows32/64, linux and macos) is provided, which can facilitate you with installing all relevant dependencies.
30+
31+
Specific to the content and description of each package, you can refer to the readme file in the related folder.
32+
33+
34+
##Contact Information
35+
36+
If you have any questions about this sample code and the installation, please feel free to contact me. You can communicate with me by sending e-mail to sdk@ryzerobotics.com.

Single_Tello_Test/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#Single_Tello_Test
2+
## Step1
3+
Write the command set to be run in command.txt, for example::
4+
```
5+
command
6+
takeoff
7+
land
8+
```
9+
## Step2
10+
The script will automatically send a command to Tello. After receiving the reply from the previous command, the next command will be sent immediately.
11+
To add a delay, you can use the Delay command and the script will automatically delay. The unit of delay is seconds, which can be given to decimals.
12+
```
13+
delay 5
14+
```
15+
## Step3
16+
Run the script
17+
```
18+
python tello_test.py command.txt
19+
```
20+
The command window will type each instruction and its reply. After the execution is finished, the commands will be stored in the log folder to name the test end time.

Single_Tello_Test/command.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
command
2+
takeoff
3+
delay 5
4+
land

Single_Tello_Test/stats.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from datetime import datetime
2+
3+
class Stats:
4+
def __init__(self, command, id):
5+
self.command = command
6+
self.response = None
7+
self.id = id
8+
9+
self.start_time = datetime.now()
10+
self.end_time = None
11+
self.duration = None
12+
13+
def add_response(self, response):
14+
self.response = response
15+
self.end_time = datetime.now()
16+
self.duration = self.get_duration()
17+
# self.print_stats()
18+
19+
def get_duration(self):
20+
diff = self.end_time - self.start_time
21+
return diff.total_seconds()
22+
23+
def print_stats(self):
24+
print '\nid: %s' % self.id
25+
print 'command: %s' % self.command
26+
print 'response: %s' % self.response
27+
print 'start time: %s' % self.start_time
28+
print 'end_time: %s' % self.end_time
29+
print 'duration: %s\n' % self.duration
30+
31+
def got_response(self):
32+
if self.response is None:
33+
return False
34+
else:
35+
return True
36+
37+
def return_stats(self):
38+
str = ''
39+
str += '\nid: %s\n' % self.id
40+
str += 'command: %s\n' % self.command
41+
str += 'response: %s\n' % self.response
42+
str += 'start time: %s\n' % self.start_time
43+
str += 'end_time: %s\n' % self.end_time
44+
str += 'duration: %s\n' % self.duration
45+
return str

Single_Tello_Test/tello.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import socket
2+
import threading
3+
import time
4+
from stats import Stats
5+
6+
class Tello:
7+
def __init__(self):
8+
self.local_ip = ''
9+
self.local_port = 8889
10+
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # socket for sending cmd
11+
self.socket.bind((self.local_ip, self.local_port))
12+
13+
# thread for receiving cmd ack
14+
self.receive_thread = threading.Thread(target=self._receive_thread)
15+
self.receive_thread.daemon = True
16+
self.receive_thread.start()
17+
18+
self.tello_ip = '192.168.10.1'
19+
self.tello_port = 8889
20+
self.tello_adderss = (self.tello_ip, self.tello_port)
21+
self.log = []
22+
23+
self.MAX_TIME_OUT = 15.0
24+
25+
def send_command(self, command):
26+
"""
27+
Send a command to the ip address. Will be blocked until
28+
the last command receives an 'OK'.
29+
If the command fails (either b/c time out or error),
30+
will try to resend the command
31+
:param command: (str) the command to send
32+
:param ip: (str) the ip of Tello
33+
:return: The latest command response
34+
"""
35+
self.log.append(Stats(command, len(self.log)))
36+
37+
self.socket.sendto(command.encode('utf-8'), self.tello_adderss)
38+
print 'sending command: %s to %s' % (command, self.tello_ip)
39+
40+
start = time.time()
41+
while not self.log[-1].got_response():
42+
now = time.time()
43+
diff = now - start
44+
if diff > self.MAX_TIME_OUT:
45+
print 'Max timeout exceeded... command %s' % command
46+
# TODO: is timeout considered failure or next command still get executed
47+
# now, next one got executed
48+
return
49+
print 'Done!!! sent command: %s to %s' % (command, self.tello_ip)
50+
51+
def _receive_thread(self):
52+
"""Listen to responses from the Tello.
53+
54+
Runs as a thread, sets self.response to whatever the Tello last returned.
55+
56+
"""
57+
while True:
58+
try:
59+
self.response, ip = self.socket.recvfrom(1024)
60+
print('from %s: %s' % (ip, self.response))
61+
62+
self.log[-1].add_response(self.response)
63+
except socket.error, exc:
64+
print "Caught exception socket.error : %s" % exc
65+
66+
def on_close(self):
67+
pass
68+
# for ip in self.tello_ip_list:
69+
# self.socket.sendto('land'.encode('utf-8'), (ip, 8889))
70+
# self.socket.close()
71+
72+
def get_log(self):
73+
return self.log
74+

Single_Tello_Test/tello_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from tello import Tello
2+
import sys
3+
from datetime import datetime
4+
import time
5+
6+
start_time = str(datetime.now())
7+
8+
file_name = sys.argv[1]
9+
10+
f = open(file_name, "r")
11+
commands = f.readlines()
12+
13+
tello = Tello()
14+
for command in commands:
15+
if command != '' and command != '\n':
16+
command = command.rstrip()
17+
18+
if command.find('delay') != -1:
19+
sec = float(command.partition('delay')[2])
20+
print 'delay %s' % sec
21+
time.sleep(sec)
22+
pass
23+
else:
24+
tello.send_command(command)
25+
26+
log = tello.get_log()
27+
28+
out = open('log/' + start_time + '.txt', 'w')
29+
for stat in log:
30+
stat.print_stats()
31+
str = stat.return_stats()
32+
out.write(str)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Sample code is offered under MIT License (See below).
2+
3+
Copyright 2018 Ryze
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a
6+
copy of this software and associated documentation files (the "Software"),
7+
to deal in the Software without restriction, including without limitation
8+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
and/or sell copies of the Software, and to permit persons to whom the
10+
Software is furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included
13+
in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 commit comments

Comments
 (0)