-
Notifications
You must be signed in to change notification settings - Fork 2
/
Joystick.py
138 lines (99 loc) · 3.78 KB
/
Joystick.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
'''
This file contains the functions that the joytick uses.
Last Modified: R. Nance 5/15/2018
#####################DO NOT EDIT BELOW INFORMATION##################################
Originating Branch: Joystick
Originally Created: R. Nance 12/2017
'''
import pygame
from helper import *
#from helper import mapval
xAxisNum = 0
yAxisNum = 1
throttleAxisNum = 2
buttonMap = {
1: 'Zdown',
2: 'Zup',
3: 'Home',
4: 'ChangeMode',
6: 'GetStatus',
7: 'Z Sensitivity Up',
8: 'Z Sensitivity Down',
9: 'ResetHome'
}
class CustomJoystick:
#will need to create a button mapping function that imports text file stuff here. THis is to get a GUI working
#will need to create a button mapping function that imports text file stuff here.
def __init__(self, name, number):
self.name = name
self.joystick = pygame.joystick.Joystick(number)
pygame.init()
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Initialize the joysticks
pygame.joystick.init()
joystick_count = pygame.joystick.get_count()
joystick = pygame.joystick.Joystick(0)
# For each joystick:
for i in range(joystick_count):
joystick = pygame.joystick.Joystick(i)
#print('this is joystick: ', i)
joystick.init()
axes = joystick.get_numaxes()
#print(axes)
def getAxisPosition(self, axisIndex):
joystick = pygame.joystick.Joystick(0)
return joystick.get_axis(axisIndex)
def convertPosition(self, axisIndex):
currPos = self.getAxisPosition(axisIndex)
newPos = (currPos + 1)*127.5
return newPos
############################CODE WRITTTEN BY RYDER#########################################
def getButtons(self):
clock = pygame.time.Clock()
commands = []
for event in pygame.event.get(): # User did something
#print(event)
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
# Possible joystick actions: JOYAXISMOTION JOYBALLMOTION JOYBUTTONDOWN JOYBUTTONUP JOYHATMOTION
if event.type == pygame.JOYBUTTONDOWN:
button = event.button
#commands += button
commands += [buttonMap[button]]
clock.tick(20)
return commands
def getAbsoluteX(self):
pygame.event.get()
#joystick = pygame.joystick.Joystick(0)
return self.joystick.get_axis(xAxisNum)
def getAbsoluteY(self):
pygame.event.get()
#joystick = pygame.joystick.Joystick(0)
return self.joystick.get_axis(yAxisNum)
def getAbsoluteThrottle(self):
pygame.event.get()
#joystick = pygame.joystick.Joystick(0)
return self.joystick.get_axis(throttleAxisNum)
def getAbsolutePosition(self):
pygame.event.get()
position = [round(self.joystick.get_axis(xAxisNum), 3), round(self.joystick.get_axis(yAxisNum), 3)]
return position
def getX(self):
pygame.event.get()
#joystick = pygame.joystick.Joystick(0)
absoluteX = self.getAbsoluteX() + 1
return mapval(absoluteX, 0, 2, 0, 2000)
def getY(self):
pygame.event.get()
#joystick = pygame.joystick.Joystick(0)
absoluteY = self.getAbsoluteY() + 1
return mapval(absoluteY, 0, 2, 0, 2000)
def getThrottle(self):
pygame.event.get()
absoluteThrottle = self.getAbsoluteThrottle()
return mapval(absoluteThrottle, -1, 1, 0, 100)
def getPosition(self):
pygame.event.get()
absolutePosition = [self.getAbsoluteX() + 1, self.getAbsoluteY() + 1]
return map(lambda x: mapval(x, 0, 2, 0, 2000), absolutePosition)