generated from MartinKierkegaard/TelloDroneAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Drone.py
241 lines (198 loc) · 6.7 KB
/
Drone.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
import socket
import sys
import time
class Drone(object):
""" In this class der wil be 3 regions with commands and information that the drone will have.
The 4 regions are StartUp, Commands, Mission Pad and Information Messages.
The start region includes det connection to the wifi and drone and the functions: sendMessage and printInfo.
The commands includes the commands that the drone can perform.
The mission pad includes the commands for the mission pad.
The information messages includes the messages about battery, flytime, serial number and SDK informations."""
#region Startup/Setup
# Constructor
def __init__(self, ip, port):
self.TelloIp = ip
print("ip: " + ip)
self.TelloPort = port
self.Host = ""
self.HostPort = 9000
self.locaddr = (self.Host, self.HostPort)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.tello_address = ("192.168.10.1", 8889)
self.sock.bind(self.locaddr)
# Connection
def connect(self, wait):
print("Connect")
result = self.sendMessage("command")
print(result)
time.sleep(wait)
# Sending messages/commands to the drone
def sendMessage(self, TelloMessage):
print("TelloMessage " + TelloMessage + " end")
msg = TelloMessage.encode(encoding="utf-8")
sent = self.sock.sendto(msg, self.tello_address)
data, server = self.sock.recvfrom(1518)
print(data.decode(encoding="utf-8"))
return "from sendmessage " + TelloMessage + " end "
# Print out information about location
def printInfo(self, wait):
print("Hello Drone at : " + self.TelloIp)
time.sleep(wait)
# set wifi password for Tello drone
def wifi_ssid_pass(self, ssid, password, wait):
print("wifi_ssid_pass")
result = self.sendMessage("wifi " + "ssid" + " " + "password")
time.sleep(wait)
# update Wifi password for the Tello drone
def ap_ssid_pass(self, ssid, password, wait):
print("ap_ssid_pass")
result = self.sendMessage("ap " + "ssid" + " " + "password")
time.sleep(wait)
# Remote controller settings
def rcabcd(self, a, b, c, d, wait):
print("rcabcd")
result = self.sendMessage(
"rc " + " " + a + " " + b + " " + c + " " + d
)
time.sleep(wait)
#endregion
#region Commands
# TakeOff the ground
def takeOff(self, wait):
print("takeOff")
result = self.sendMessage("takeoff")
time.sleep(wait)
# Landing the drone
def land(self, wait):
print("land")
result = self.sendMessage("land")
time.sleep(wait)
# End the drone
def end(self, wait):
print("end")
self.sock.close()
time.sleep(wait)
# Turn clockwise
def cw(self, x, wait):
print("cw")
result = self.sendMessage("cw " + x)
time.sleep(wait)
# Turn counter clockwise
def ccw(self, x, wait):
print("ccw")
result = self.sendMessage("ccw " + x)
time.sleep(wait)
# Fly up
def up(self, x, wait):
print("up")
result = self.sendMessage("up " + x)
time.sleep(wait)
# Fly down
def down(self, x, wait):
print("down")
result = self.sendMessage("down " + x)
time.sleep(wait)
# Turn left
def left(self, x, wait):
print("left")
result = self.sendMessage("left " + x)
time.sleep(wait)
# Turn right
def right(self, x, wait):
print("left")
result = self.sendMessage("left " + x)
time.sleep(wait)
# Move forward
def forward(self, x, wait):
print("forward")
result = self.sendMessage("forward " + x)
time.sleep(wait)
# Move backwards
def back(self, x, wait):
print("back")
result = self.sendMessage("back " + x)
time.sleep(wait)
# Flip in given direction
def flip(self, x, wait):
print("flip")
result = self.sendMessage("flip " + x)
time.sleep(wait)
# Hover
def stop(self, wait):
print("stop")
result = self.sendMessage("stop")
time.sleep(wait)
# Set Speed
def speed(self, x, wait):
print("speed")
result = self.sendMessage("speed " + x)
# Fly at a curve (drifting)
def curve(self, x1, y1, z1, x2, y2, z2, speed, wait):
print("curve")
result = self.sendMessage(
"curve " + x1 + " " + y1 + " " + z1 + " " + x2 +
" " + y2 + " " + z2 + " " + speed
)
time.sleep(wait)
#endregion
#region Missionpad
""" A Mission pad is used together with a Tello Drone.
The Tello Drone uses its camera to detect the Mission Pads ID and
executes this Mission pads programmed commands. """
# Fly at a curve, with mission pad (drifting)
def curve_pad(self, x1, y1, z1, x2, y2, z2, speed, mid, wait):
print("curve")
result = self.sendMessage("curve " + x1 + " ", + y1 + " " + z1 + " " + x2 + " " + y2 + " " +z2 + " " + speed + " " + mid)
time.sleep(wait)
# Fly to the XYZ coordinates of the mission pad
def goXYZSpeed(self, x, y, z, speed, wait):
print("goXYZSpeed")
result = self.sendMessage("go " + x + " " + y + " " + z + " " + speed)
time.sleep(wait)
# Mission pad detection
def mdirection(self, x, wait):
print("mdirection")
result = self.sendMessage("mdirection" + x)
time.sleep(wait)
return result
# Turn off mission pad detection
def moff(self, wait):
print("moff")
result = self.sendMessage("moff")
time.sleep(wait)
return result
# Fly from mission pad 1 to mission pad 2
def jump(self, x, y, z, speed, yaw, mid1, mid2, wait):
print("jump")
result = self.sendMessage(
"jump" + " " + x + " " + y + " " + z + " "
+ speed + " " + yaw + " " + mid1 + " " + mid2
)
#endregion
#region informations messages
# Give a battery message
def battery(self, wait):
result = self.sendMessage("battery?")
time.sleep(wait)
return result
# Flytime
def time(self, wait):
result = self.sendMessage("time?")
time.sleep(wait)
return result
# Trello SDK version
def sdk(self, wait):
result = self.sendMessage("sdk?")
time.sleep(wait)
return result
# Trello serial number
def sn(self, wait):
result = self.sendMessage("sn?")
time.sleep(wait)
return result
#endregion
#region Step
def stepUp(self, up, forward, wait):
result = self.sendMessage("up " + 19 + ", forward " + 24)
time.sleep(wait)
#endregion