robobuggy / joystick / joystick.py @ 02df64f0
History | View | Annotate | Download (4.78 KB)
1 | 08263708 | Haley | import pygame |
---|---|---|---|
2 | import serial |
||
3 | |||
4 | # Define some colors
|
||
5 | BLACK = ( 0, 0, 0) |
||
6 | WHITE = ( 255, 255, 255) |
||
7 | |||
8 | # This is a simple class that will help us print to the screen
|
||
9 | # It has nothing to do with the joysticks, just outputing the
|
||
10 | # information.
|
||
11 | class TextPrint: |
||
12 | def __init__(self): |
||
13 | self.reset()
|
||
14 | self.font = pygame.font.Font(None, 20) |
||
15 | |||
16 | def printt(self, screen, textString): |
||
17 | textBitmap = self.font.render(textString, True, BLACK) |
||
18 | screen.blit(textBitmap, [self.x, self.y]) |
||
19 | self.y += self.line_height |
||
20 | |||
21 | def reset(self): |
||
22 | self.x = 10 |
||
23 | self.y = 10 |
||
24 | self.line_height = 15 |
||
25 | |||
26 | def indent(self): |
||
27 | self.x += 10 |
||
28 | |||
29 | def unindent(self): |
||
30 | self.x -= 10 |
||
31 | |||
32 | |||
33 | pygame.init() |
||
34 | |||
35 | # Set the width and height of the screen [width,height]
|
||
36 | size = [500, 700] |
||
37 | screen = pygame.display.set_mode(size) |
||
38 | |||
39 | pygame.display.set_caption("My Game")
|
||
40 | |||
41 | #Loop until the user clicks the close button.
|
||
42 | done = False
|
||
43 | |||
44 | # Used to manage how fast the screen updates
|
||
45 | clock = pygame.time.Clock() |
||
46 | |||
47 | # Initialize the joysticks
|
||
48 | pygame.joystick.init() |
||
49 | |||
50 | # Get ready to print
|
||
51 | textPrint = TextPrint() |
||
52 | |||
53 | # Set starting values
|
||
54 | steeringAngle = 0
|
||
55 | brake = 1
|
||
56 | |||
57 | # Open serial port
|
||
58 | 02df64f0 | Matt Sebek | ser = serial.Serial('COM12', 9600, timeout=5) |
59 | 08263708 | Haley | |
60 | |||
61 | # -------- Main Program Loop -----------
|
||
62 | while done==False: |
||
63 | # EVENT PROCESSING STEP
|
||
64 | for event in pygame.event.get(): # User did something |
||
65 | if event.type == pygame.QUIT: # If user clicked close |
||
66 | done=True # Flag that we are done so we exit this loop |
||
67 | |||
68 | # Possible joystick actions: JOYAXISMOTION JOYBALLMOTION JOYBUTTONDOWN JOYBUTTONUP JOYHATMOTION
|
||
69 | if event.type == pygame.JOYBUTTONDOWN:
|
||
70 | print("Joystick button pressed.")
|
||
71 | if event.type == pygame.JOYBUTTONUP:
|
||
72 | print("Joystick button released.")
|
||
73 | |||
74 | |||
75 | # DRAWING STEP
|
||
76 | # First, clear the screen to white. Don't put other drawing commands
|
||
77 | # above this, or they will be erased with this command.
|
||
78 | screen.fill(WHITE) |
||
79 | textPrint.reset() |
||
80 | |||
81 | # Get count of joysticks
|
||
82 | joystick_count = pygame.joystick.get_count() |
||
83 | |||
84 | textPrint.printt(screen, "Number of joysticks: {}".format(joystick_count) )
|
||
85 | textPrint.indent() |
||
86 | |||
87 | # For each joystick:
|
||
88 | for i in range(joystick_count): |
||
89 | joystick = pygame.joystick.Joystick(i) |
||
90 | joystick.init() |
||
91 | |||
92 | textPrint.printt(screen, "Joystick {}".format(i) )
|
||
93 | textPrint.indent() |
||
94 | |||
95 | # Get the name from the OS for the controller/joystick
|
||
96 | name = joystick.get_name() |
||
97 | textPrint.printt(screen, "Joystick name: {}".format(name) )
|
||
98 | |||
99 | # Usually axis run in pairs, up/down for one, and left/right for
|
||
100 | # the other.
|
||
101 | axes = joystick.get_numaxes() |
||
102 | textPrint.printt(screen, "Number of axes: {}".format(axes) )
|
||
103 | textPrint.indent() |
||
104 | |||
105 | for i in range( axes ): |
||
106 | axis = joystick.get_axis( i ) |
||
107 | textPrint.printt(screen, "Axis {} value: {:>6.3f}".format(i, axis) )
|
||
108 | if i == 0: |
||
109 | steeringAngle = axis |
||
110 | textPrint.unindent() |
||
111 | |||
112 | buttons = joystick.get_numbuttons() |
||
113 | textPrint.printt(screen, "Number of buttons: {}".format(buttons) )
|
||
114 | textPrint.indent() |
||
115 | |||
116 | for i in range( buttons ): |
||
117 | button = joystick.get_button( i ) |
||
118 | textPrint.printt(screen, "Button {:>2} value: {}".format(i,button) )
|
||
119 | if button == 1: |
||
120 | if i == 1: |
||
121 | brake = 0;
|
||
122 | elif i == 3: |
||
123 | brake = 1;
|
||
124 | textPrint.unindent() |
||
125 | |||
126 | # Hat switch. All or nothing for direction, not like joysticks.
|
||
127 | # Value comes back in an array.
|
||
128 | hats = joystick.get_numhats() |
||
129 | textPrint.printt(screen, "Number of hats: {}".format(hats) )
|
||
130 | textPrint.indent() |
||
131 | |||
132 | for i in range( hats ): |
||
133 | hat = joystick.get_hat( i ) |
||
134 | textPrint.printt(screen, "Hat {} value: {}".format(i, str(hat)) ) |
||
135 | textPrint.unindent() |
||
136 | |||
137 | textPrint.unindent() |
||
138 | |||
139 | |||
140 | # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
|
||
141 | |||
142 | # Go ahead and update the screen with what we've drawn.
|
||
143 | pygame.display.flip() |
||
144 | |||
145 | sAngle = (-1.0*(steeringAngle - 1))/2.0 |
||
146 | 02df64f0 | Matt Sebek | sAngle = (sAngle*60)+95 |
147 | 08263708 | Haley | # sAngle = (sAngle*range of motion)+minimum value
|
148 | sAngle = int(sAngle)
|
||
149 | if sAngle > 180: |
||
150 | sAngle = 180
|
||
151 | |||
152 | # send data through xbee
|
||
153 | currentpacket = 'A'+str(sAngle)+'B'+str(brake)+'C' |
||
154 | print currentpacket
|
||
155 | ser.write('A')
|
||
156 | ser.write(str(sAngle))
|
||
157 | ser.write('B')
|
||
158 | ser.write(str(brake))
|
||
159 | ser.write('C')
|
||
160 | |||
161 | # Limit to 20 frames per second
|
||
162 | #clock.tick(20)
|
||
163 | clock.tick(8)
|
||
164 | |||
165 | |||
166 | ser.close() |
||
167 | # Close the window and quit.
|
||
168 | # If you forget this line, the program will 'hang'
|
||
169 | # on exit if running from IDLE.
|
||
170 | pygame.quit () |