/
robot-view-joystick.py
84 lines (63 loc) · 1.83 KB
/
robot-view-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
# Robot Controller Sample
# Daniel M. Lofaro
# Georgemason University
# http://danlofaro.com
# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://programarcadegames.com/
# http://simpson.edu/computer-science/
import pygame
import ach
import joystruct as jy
JOY_CHAN='joy-chan'
r = ach.Channel(JOY_CHAN)
r.flush()
ref = jy.JOYSTICK_REF()
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
pygame.init()
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Current position
x_coord = 10
y_coord = 10
maxi = 32767.0
# Count the joysticks the computer has
joystick_count = pygame.joystick.get_count()
if joystick_count == 0:
# No joysticks!
print("Error, I didn't find any joysticks.")
else:
# Use joystick #0 and initialize it
my_joystick = pygame.joystick.Joystick(0)
my_joystick.init()
while not done:
# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT
# ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT
# As long as there is a joystick
if joystick_count != 0:
# This gets the position of the axis on the game controller
# It returns a number between -1.0 and +1.0
jl00 = my_joystick.get_axis(0)
jl01 = my_joystick.get_axis(1)
jl10 = my_joystick.get_axis(2)
jl11 = my_joystick.get_axis(3)
for i in range(0,16):
my_joystick.get_button(i)
ref.jl00 = float(jl00)
ref.jl01 = float(jl01)
ref.jl10 = float(jl10)
ref.jl11 = float(jl11)
r.put(ref)
# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT
clock.tick(60)
pygame.quit()