-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjoystickControl.py
executable file
·210 lines (162 loc) · 6.06 KB
/
joystickControl.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/local/bin/python2.7-32
"""
@brief Joystick control for the QuickBot.
@description This program is used to drive the QuickBot via a joystick (actually gamepad).
Currently setup for tank drive.
@author Rowland O'Flaherty (rowlandoflaherty.com)
@date 12/10/2013
@note Does not work with 64-bit python
This code was modified from http://www.pygame.org/docs/ref/joystick.html
@version: 1.0
@copyright: Copyright (C) 2014, Georgia Tech Research Corporation see the LICENSE file included with this software (see LINENSE file)
"""
import numpy as np
import pygame
import socket
import sys
# Parameters
sendFlag = True
pwmMinVal = 45
pwmMaxVal = 100
axisMinVal = 0.2
# Get input arguments
HOST = "192.168.1.101"
PORT = 5005
if len(sys.argv) > 2:
print 'Invalid number of command line arguments.'
print 'Proper syntax:'
print '>> joystickControl.py robotIP'
print 'Example:'
print '>> QuickBotRun.py ', HOST
sys.exit()
if len(sys.argv) == 2:
HOST = sys.argv[1]
if sendFlag:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Helper functions
def mapAxisToPWM(axisVal):
# Axis: -1 -> PWM: pwmMaxVal
# Axis: -axisMinVal -> pwmMinVal
if np.abs(axisVal) < axisMinVal:
pwm = 0
else:
scaling = (pwmMaxVal - pwmMinVal) / (1.0 - axisMinVal)
pwm = -scaling * (axisVal - np.sign(axisVal)*axisMinVal) - np.sign(axisVal)*pwmMinVal
return int(pwm)
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
# This is a simple class that will help us print to the screen
# It has nothing to do with the joysticks, just outputing the
# information.
class TextPrint:
def __init__(self):
self.reset()
self.font = pygame.font.Font(None, 20)
def printScreen(self, screen, textString):
textBitmap = self.font.render(textString, True, BLACK)
screen.blit(textBitmap, [self.x, self.y])
self.y += self.line_height
def reset(self):
self.x = 10
self.y = 10
self.line_height = 15
def indent(self):
self.x += 10
def unindent(self):
self.x -= 10
pygame.init()
# Set the width and height of the screen [width,height]
size = [500, 700]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("QuickBot Joystick Control")
#Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Initialize the joysticks
pygame.joystick.init()
# Get ready to print
textPrint = TextPrint()
# -------- Main Program Loop -----------
while done==False:
# EVENT PROCESSING STEP
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done=True
# Possible joystick actions: JOYAXISMOTION JOYBALLMOTION JOYBUTTONDOWN JOYBUTTONUP JOYHATMOTION
# if event.type == pygame.JOYBUTTONDOWN:
# print("Joystick button pressed.")
# if event.type == pygame.JOYBUTTONUP:
# print("Joystick button released.")
# DRAWING STEP
# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
screen.fill(WHITE)
textPrint.reset()
# Get count of joysticks
joystick_count = pygame.joystick.get_count()
textPrint.printScreen(screen, "Number of joysticks: {}".format(joystick_count) )
textPrint.indent()
axis = [0]*4
# For each joystick:
for i in range(joystick_count):
joystick = pygame.joystick.Joystick(i)
joystick.init()
textPrint.printScreen(screen, "Joystick {}".format(i) )
textPrint.indent()
# Get the name from the OS for the controller/joystick
name = joystick.get_name()
textPrint.printScreen(screen, "Joystick name: {}".format(name) )
# Usually axis run in pairs, up/down for one, and left/right for
# the other.
axes = joystick.get_numaxes()
textPrint.printScreen(screen, "Number of axes: {}".format(axes) )
textPrint.indent()
if i == 0:
for i in range( axes ):
axis[i] = joystick.get_axis( i )
textPrint.printScreen(screen, "Axis {} value: {:>6.3f}".format(i, axis[i]) )
pwm = mapAxisToPWM(axis[i])
if i == 1:
pwm_left = pwm
if i == 3:
pwm_right = pwm
textPrint.unindent()
textPrint.unindent()
cmdStr = "QuickBot Command:"
textPrint.printScreen(screen, cmdStr)
cmdStr = "$PWM=" + str(pwm_left) + "," + str(pwm_right) + "*\n"
textPrint.printScreen(screen, cmdStr)
textPrint.unindent()
if sendFlag:
sock.sendto(cmdStr, (HOST, PORT))
# buttons = joystick.get_numbuttons()
# textPrint.printScreen(screen, "Number of buttons: {}".format(buttons) )
# textPrint.indent()
# for i in range( buttons ):
# button = joystick.get_button( i )
# textPrint.printScreen(screen, "Button {:>2} value: {}".format(i,button) )
# textPrint.unindent()
# # Hat switch. All or nothing for direction, not like joysticks.
# # Value comes back in an array.
# hats = joystick.get_numhats()
# textPrint.printScreen(screen, "Number of hats: {}".format(hats) )
# textPrint.indent()
# for i in range( hats ):
# hat = joystick.get_hat( i )
# textPrint.printScreen(screen, "Hat {} value: {}".format(i, str(hat)) )
textPrint.unindent()
textPrint.unindent()
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 20 frames per second
clock.tick(20)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit ()