-
Notifications
You must be signed in to change notification settings - Fork 1
/
snaptain_control_xbox.py
94 lines (85 loc) · 2.03 KB
/
snaptain_control_xbox.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
#!/usr/bin/python3
import threading
import time
import socket
import Xbox.xbox as xbox
from snaptain_control import ControlMessage, Drone
DEADZONE = 256
class CommsThread(threading.Thread):
def __init__(self, drone):
super().__init__()
self.drone = drone
def run(self):
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
self.stop = False
while not self.stop:
m = drone.next_message()
sock.sendto(m.to_proto(), ("172.16.10.1", 8080))
#print(m)
time.sleep(.02)
def interrupt(self):
self.stop = True
self.join()
drone = Drone()
joy = xbox.Joystick()
comms = CommsThread(drone)
comms.start()
takeoff = False
stop = False
trim = False
speed = False
flip = False
try:
while 1:
(roll, pitch) = joy.rightStick(DEADZONE)
(yaw, climb) = joy.leftStick(DEADZONE)
if roll == 0 and yaw == 0:
coordinated = joy.rightTrigger() - joy.leftTrigger()
roll = coordinated
yaw = 0.7*coordinated
drone.set_pitch_roll_vec(pitch, roll)
drone.set_yaw_climb_vec(yaw, climb)
if joy.leftThumbstick() and joy.rightThumbstick():
if not takeoff:
drone.takeoff()
takeoff = True
else:
takeoff = False
if not trim:
if joy.dpadUp():
drone.trim_forward()
trim = True
elif joy.dpadDown():
drone.trim_aft()
trim = True
elif joy.dpadLeft():
drone.trim_left()
trim = True
elif joy.dpadRight():
drone.trim_right()
trim = True
elif not joy.dpadUp() and not joy.dpadDown() and not joy.dpadLeft() and not joy.dpadRight():
trim = False
if joy.leftBumper():
if not speed:
drone.next_speed()
speed = True
else:
speed = False
if joy.A():
if not flip:
drone.flip()
flip = True
else:
flip = False
if joy.Start():
if not stop:
drone.stop()
stop = True
else:
stop = False
time.sleep(0.02)
except KeyboardInterrupt:
pass
comms.interrupt()
joy.close()