-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.py
More file actions
executable file
·67 lines (57 loc) · 2.03 KB
/
Copy pathcontrol.py
File metadata and controls
executable file
·67 lines (57 loc) · 2.03 KB
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
import sys
from twisted.internet import reactor
from twisted.python import log
from twisted.internet import threads
import pygame
import pygame.joystick
import pygame.event
class Joystick(object):
axis = [ 0, 0, 0, 0 ]
button = [ False, ] * 16
button_toggle = [ False, ] * 16
def __init__(self):
log.msg("Initializing pygame...")
pygame.init()
if pygame.joystick.get_count() < 1:
log.err("No joysticks were found, exiting")
sys.exit(100)
self.joy = pygame.joystick.Joystick(0)
self.joy.init()
log.msg(u"Using joystick %s" % self.joy.get_name())
reactor.callLater(0, self._loop)
def _loop(self):
def cb(event):
if event.type == pygame.JOYBUTTONDOWN and event.button == 8:
reactor.stop()
if event.type == pygame.JOYAXISMOTION:
self.axis[event.axis] = event.value
if event.type == pygame.JOYBUTTONDOWN:
self.button[event.button] = True
if event.type == pygame.JOYBUTTONUP:
self.button[event.button] = False
self.button_toggle[event.button] = not self.button_toggle[event.button]
reactor.callLater(0, self._loop)
def eb(err):
log.err(err)
d = threads.deferToThread(pygame.event.wait)
d.addCallbacks(cb, eb)
def get_throttle(self):
if abs(self.axis[1]) < 0.1:
mode = "RELEASE"
elif self.axis[1] < 0:
mode = "FORWARD"
else:
mode = "BACKWARD"
return (int(abs(self.axis[1]*255)), mode)
def get_turn(self):
if abs(self.axis[0]) < 0.5:
mode = "RELEASE"
elif self.axis[0] < 0:
mode = "LEFT"
else:
mode = "RIGHT"
return mode
def get_camerapan(self):
return (int((1 + self.axis[2]) * 127), self.button[5])
def get_camera(self):
return self.button_toggle[0]