forked from legonigel/wii-drone-on
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
executable file
·144 lines (129 loc) · 3.31 KB
/
demo.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
#!/usr/bin/env python
# Python AR.Drone 2.0
#
# Copyright (C) 2013 Quadeare <[email protected]>
# Copyright (C) 2016 LegoNigel <[email protected]>
# Twitter : @quadeare
from pygame import *
import pygame
import libardrone
import threading
import time
from subprocess import call
import signal
import sys
class video(threading.Thread):
"""Video class to launch media flux"""
def __init__(self, nom = ''):
threading.Thread.__init__(self)
self.process = None
def run(self):
print "Video"
call(["ffplay", "tcp://192.168.1.1:5555/"])
def stop(self):
call(["killall", "ffplay"])
if self.process is not None:
self.process.terminate()
self.process = None
class controle(threading.Thread):
"""Control class (to control the drone)"""
def __init__(self, nom = ''):
threading.Thread.__init__(self)
self._stopevent = threading.Event( )
def stop(self):
self._stopevent.set( )
def run(self):
"""We call pygame (to use controler)"""
pygame.init()
"""Launch drone class"""
drone = libardrone.ARDrone(True)
clock = pygame.time.Clock()
running = True
screen = pygame.display.set_mode((640, 360))
"""Set up and init joystick"""
#j=joystick.Joystick(0)
#j.init()
print "running"
move_forward = 0
move_backward = 0
move_left = 0
move_right = 0
turn = 0
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
drone.hover()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
drone.reset()
running = False
# takeoff / land
elif event.key == pygame.K_RETURN:
drone.takeoff()
elif event.key == pygame.K_SPACE:
drone.land()
# emergency
elif event.key == pygame.K_BACKSPACE:
drone.reset()
# forward / backward
elif event.key == pygame.K_w:
drone.move_forward()
elif event.key == pygame.K_s:
drone.move_backward()
# left / right
elif event.key == pygame.K_a:
drone.move_left()
elif event.key == pygame.K_d:
drone.move_right()
# up / down
elif event.key == pygame.K_UP:
drone.move_up()
elif event.key == pygame.K_DOWN:
drone.move_down()
# turn left / turn right
elif event.key == pygame.K_LEFT:
drone.turn_left()
elif event.key == pygame.K_RIGHT:
drone.turn_right()
# speed
elif event.key == pygame.K_1:
drone.speed = 0.1
elif event.key == pygame.K_2:
drone.speed = 0.2
elif event.key == pygame.K_3:
drone.speed = 0.3
elif event.key == pygame.K_4:
drone.speed = 0.4
elif event.key == pygame.K_5:
drone.speed = 0.5
elif event.key == pygame.K_6:
drone.speed = 0.6
elif event.key == pygame.K_7:
drone.speed = 0.7
elif event.key == pygame.K_8:
drone.speed = 0.8
elif event.key == pygame.K_9:
drone.speed = 0.9
elif event.key == pygame.K_0:
drone.speed = 1.0
clock.tick(1000)
print "Shutting down...",
drone.reset()
drone.halt()
print "Ok."
quit()
if __name__ == '__main__':
try:
# Controle
controle = controle('Thread Controle')
controle.start()
time.sleep(5)
# Video
video = video('Thread Video')
video.start()
except (KeyboardInterrupt, SystemExit):
controle.stop()
video.stop()
sys.exit()