/
fly.py
179 lines (147 loc) · 4.15 KB
/
fly.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
#!/usr/bin/env python
import RPi.GPIO as GPIO
import libardrone
import LCD1602
import PCF8591 as ADC
import time
import math
TRIG = 11
ECHO = 12
MAX_HEIGHT = 1500
MAX_HAND_DISTANCE = 100
def setup():
global state
global drone
global running
global flying
global battery_level
LCD1602.init(0x27, 1) # init(slave address, background light)
LCD1602.write(0, 0, 'Greeting!!')
LCD1602.write(1, 1, 'Coderdojo:-)')
ADC.setup(0x48) # Setup PCF8591
GPIO.setmode(GPIO.BOARD)
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
drone = libardrone.ARDrone()
drone.set_speed(0.2)
def showDirect(direction):
LCD1602.clear()
LCD1602.write(0, 0, 'AR Drone Demo!!')
LCD1602.write(1, 1, direction)
def loop():
running = True
flying = False
status = '';
current_dist = distance()
dis = -1
alt = 0
while running:
try:
d = direction()
if flying:
dis = distance()
battery_level = drone.navdata.get(0, dict()).get("battery", 0)
if d == 'home':
if flying:
drone.hover()
status = d
elif d == 'pressed':
flying = False
running = False
elif d == 'up':
if flying == False:
flying = True
drone.takeoff()
if flying:
drone.move_up()
dis = 0
elif d == 'down':
drone.move_down()
dis = 0
elif d == 'left':
drone.move_left()
elif d == 'right':
drone.move_right()
else:
print drone.navdata.get(0, dict()).get("battery", 0)
if flying and dis > 0 and dis <= MAX_HAND_DISTANCE and current_dist > 0 and current_dist <= MAX_HAND_DISTANCE:
if dis <= MAX_HAND_DISTANCE:
if dis > current_dist:
alt = drone.navdata.get(0, dict()).get("altitude", 0)
print 'drone up', dis, current_dist, alt, battery_level
drone.move_up()
showDirect('up %d %d' %(dis, alt))
elif dis < current_dist:
alt = drone.navdata.get(0, dict()).get("altitude", 0)
print 'drone down', dis, current_dist, alt, battery_level
drone.move_down()
showDirect('down %d %d' %(dis, alt))
if status != '' and status != d:
showDirect(d)
status = d
current_dist = dis
if flying:
alt = drone.navdata.get(0, dict()).get("altitude", 0)
vx = drone.navdata.get(0, dict()).get("vx", 0)
vy = drone.navdata.get(0, dict()).get("vy", 0)
if alt > MAX_HEIGHT:
drone.land()
flying = False
running = False
if flying and drone.navdata.get(0, dict()).get("battery", 0) < 30:
drone.land()
print "Battery too low landin, Please change"
running = False
time.sleep(0.15)
except:
pass
print "Shutting down...",
print "Battery ", drone.navdata.get(0, dict()).get("battery", 0)
drone.land()
drone.halt()
GPIO.cleanup()
showDirect('landed')
print "Ok."
def destroy():
drone.land()
drone.reset()
GPIO.cleanup()
def distance():
GPIO.output(TRIG, 0)
time.sleep(0.000002)
GPIO.output(TRIG, 1)
time.sleep(0.00001)
GPIO.output(TRIG, 0)
while GPIO.input(ECHO) == 0:
a = 0
time1 = time.time()
while GPIO.input(ECHO) == 1:
a = 1
time2 = time.time()
during = time2 - time1
return math.ceil(during * 340 / 2 * 100)
def direction(): #get joystick result
state = ['home', 'up', 'down', 'left', 'right', 'pressed']
i = 0
if ADC.read(0) <= 5:
i = 1 #up
if ADC.read(0) >= 250:
i = 2 #down
if ADC.read(1) >= 250:
i = 3 #left
if ADC.read(1) <= 5:
i = 4 #right
if ADC.read(2) == 0:
i = 5 # Button pressed
if ADC.read(0) - 125 < 15 and ADC.read(0) - 125 > -15 and ADC.read(1) - 125 < 15 and ADC.read(1) - 125 > -15 and ADC.read(2) == 255:
i = 0
return state[i]
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except SystemExit:
running = False
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
running = False
destroy()