-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobot.py
217 lines (172 loc) · 5.7 KB
/
robot.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
211
212
213
214
215
216
217
import random, pygame, sys
from pygame.locals import *
import RPi.GPIO as GPIO
import os
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
pin1 = 2
pin2 = 3
in1 = 4
in2 = 17
in3 = 27
in4 = 22
GPIO.setup(pin1,GPIO.OUT)
GPIO.setup(pin2,GPIO.OUT)
GPIO.setup(in1,GPIO.OUT)
GPIO.setup(in2,GPIO.OUT)
GPIO.setup(in4,GPIO.OUT)
GPIO.setup(in3,GPIO.OUT)
#PUMP
def pump():
GPIO.output(pin2, GPIO.HIGH)
print "pump running"
GPIO.output(pin1, GPIO.LOW)
#WHEELS
def backward():
GPIO.output(in1, GPIO.HIGH)
GPIO.output(in3, GPIO.HIGH)
print "fbackward running motor "
GPIO.output(in2, GPIO.LOW)
GPIO.output(in4, GPIO.LOW)
def forward():
GPIO.output(in2, GPIO.HIGH)
GPIO.output(in4, GPIO.HIGH)
print "forward running motor"
GPIO.output(in1, GPIO.LOW)
GPIO.output(in3, GPIO.LOW)
def right():
GPIO.output(in2, GPIO.HIGH)
GPIO.output(in4, GPIO.LOW)
print "right running motor"
GPIO.output(in1, GPIO.LOW)
GPIO.output(in3, GPIO.LOW)
def left():
GPIO.output(in1, GPIO.LOW)
GPIO.output(in3, GPIO.LOW)
print "left running motor "
GPIO.output(in2, GPIO.LOW)
GPIO.output(in4, GPIO.HIGH)
def clean():
GPIO.cleanup()
def stop():
print "Now stop"
GPIO.output(in2, GPIO.LOW)
GPIO.output(in4, GPIO.LOW)
GPIO.output(in1, GPIO.LOW)
GPIO.output(in3, GPIO.LOW)
GPIO.output(pin1, GPIO.LOW)
GPIO.output(pin2, GPIO.LOW)
##########################################################
FPS = 15
WINDOWWIDTH = 640
WINDOWHEIGHT = 320
CELLSIZE = 20
assert WINDOWWIDTH % CELLSIZE == 0, "Window width must be a multiple of cell size."
assert WINDOWHEIGHT % CELLSIZE == 0, "Window height must be a multiple of cell size."
CELLWIDTH = int(WINDOWWIDTH / CELLSIZE)
CELLHEIGHT = int(WINDOWHEIGHT / CELLSIZE)
# R G B
WHITE = (255, 255, 255)
BLACK = ( 0, 0, 0)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
DARKGREEN = ( 0, 155, 0)
DARKGRAY = ( 40, 40, 40)
BGCOLOR = WHITE
UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'
HEAD = 0 # syntactic sugar: index of the worm's head
def main():
global FPSCLOCK, DISPLAYSURF, BASICFONT
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
pygame.display.set_caption('Robot Controller')
showStartScreen()
while True:
runGame()
showGameOverScreen()
def runGame():
DISPLAYSURF.fill(BGCOLOR)
pygame.display.update()
while True: # main game loop
for event in pygame.event.get(): # event handling loop
if event.type == QUIT:
terminate()
elif event.type == KEYDOWN:
if (event.key == K_p):
pump()
elif (event.key == K_a):
left()
elif (event.key == K_d):
right()
elif (event.key == K_w):
forward()
elif (event.key == K_s):
backward()
elif (event.key == K_SPACE):
stop()
elif event.key == K_ESCAPE:
stop()
terminate()
elif event.type == KEYUP:
DISPLAYSURF.fill(WHITE)
pygame.display.update()
stop()
def drawPressKeyMsg():
pressKeySurf = BASICFONT.render('Press a key to start', True, DARKGRAY)
pressKeyRect = pressKeySurf.get_rect()
pressKeyRect.topleft = (WINDOWWIDTH - 200, WINDOWHEIGHT - 30)
DISPLAYSURF.blit(pressKeySurf, pressKeyRect)
# KRT 14/06/2012 rewrite event detection to deal with mouse use
def checkForKeyPress():
for event in pygame.event.get():
if event.type == QUIT: #event is quit
terminate()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE: #event is escape key
terminate()
else:
return event.key #key found return with it
# no quit or key events in queue so return None
return None
def showStartScreen():
titleFont = pygame.font.Font('freesansbold.ttf', 100)
titleSurf1 = titleFont.render('CONTROLLER', True, WHITE, DARKGREEN)
titleSurf2 = titleFont.render('Robot', True, GREEN)
degrees1 = 0
degrees2 = 0
#KRT 14/06/2012 rewrite event detection to deal with mouse use
pygame.event.get() #clear out event queue
while True:
DISPLAYSURF.fill(BGCOLOR)
rotatedSurf1 = pygame.transform.rotate(titleSurf1, degrees1)
rotatedRect1 = rotatedSurf1.get_rect()
rotatedRect1.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)
DISPLAYSURF.blit(rotatedSurf1, rotatedRect1)
rotatedSurf2 = pygame.transform.rotate(titleSurf2, degrees2)
rotatedRect2 = rotatedSurf2.get_rect()
rotatedRect2.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)
DISPLAYSURF.blit(rotatedSurf2, rotatedRect2)
drawPressKeyMsg()
#KRT 14/06/2012 rewrite event detection to deal with mouse use
if checkForKeyPress():
return
pygame.display.update()
FPSCLOCK.tick(FPS)
degrees1 += 3 # rotate by 3 degrees each frame
degrees2 += 7 # rotate by 7 degrees each frame
def terminate():
pygame.quit()
sys.exit()
def drawGrid():
for x in range(0, WINDOWWIDTH, CELLSIZE): # draw vertical lines
pygame.draw.line(DISPLAYSURF, DARKGRAY, (x, 0), (x, WINDOWHEIGHT))
for y in range(0, WINDOWHEIGHT, CELLSIZE): # draw horizontal lines
pygame.draw.line(DISPLAYSURF, DARKGRAY, (0, y), (WINDOWWIDTH, y))
if __name__ == '__main__':
main()