forked from palmerjoshua/RoverPylot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
KeyboardRover20.py
191 lines (139 loc) · 3.66 KB
/
KeyboardRover20.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
'''
CONTROLS:
WASD - Drive
SPACE - Take Picture
J - Camera Up
K - Camera Down
U - Toggle Infrared
I - Toggle Lights
'''
from datetime import date
from random import choice
from string import ascii_lowercase, ascii_uppercase
import StringIO
import pygame
from pygame.locals import *
from rover import Rover20
class KeyboardRover20(Rover20):
def __init__(self):
Rover20.__init__(self)
self.quit = False
self.lightsAreOn = False
self.stealthIsOn = False
# each integer may be -1, 0, or 1
self.treads = [0,0]
# window must be open and in focus for pygame to take input
self.windowSize = [640, 480]
# used to only refresh the video and not the unused pixels
self.imageRect = (160,120,320,240)
# Live video frames per second
self.fps = 48
# stores what the camera currently sees
self.currentImage = None
self.displayCaption = "Keyboard Rover 2.0"
pygame.init()
pygame.display.init()
pygame.display.set_caption(self.displayCaption)
self.screen = pygame.display.set_mode(self.windowSize)
self.clock = pygame.time.Clock()
# automagically called by Rover20, acts as main loop
def processVideo(self, jpegbytes, timestamp_10msec):
if not self.quit:
self.currentImage = StringIO.StringIO(jpegbytes)
self.refreshVideo()
self.parseControls()
self.setTreads(self.treads[0], self.treads[1])
def parseControls(self):
for event in pygame.event.get():
if event.type == QUIT:
self.quit = True
elif event.type == KEYDOWN:
# camera
if event.key in (K_j, K_k, K_SPACE):
self.updateCameraState(event.key)
# drive
elif event.key in (K_w, K_a, K_s, K_d):
self.updateTreadState(event.key)
# infrared
elif event.key is K_u:
self.stealthIsOn = not self.stealthIsOn
if self.stealthIsOn:
self.turnStealthOn()
else:
self.turnStealthOff()
# lights
elif event.key is K_i:
self.lightsAreOn = not self.lightsAreOn
if self.lightsAreOn:
self.turnLightsOn()
else:
self.turnLightsOff()
elif event.type == KEYUP:
# drive
if event.key in (K_w, K_a, K_s, K_d):
self.updateTreadState()
# camera
elif event.key in (K_j, K_k):
self.updateCameraState()
else:
pass
else:
pass
# live video feed
def refreshVideo(self):
#load image, update display
image = pygame.image.load(self.currentImage).convert()
self.currentImage.close()
self.screen.blit(image, (160, 120))
pygame.display.update(self.imageRect)
#limit fps
self.clock.tick(self.fps)
# move rover
def updateTreadState(self, key=None):
if key is None:
self.treads = [0,0]
elif key is K_w:
self.treads = [1,1]
elif key is K_s:
self.treads = [-1,-1]
elif key is K_a:
self.treads = [-1,1]
elif key is K_d:
self.treads = [1,-1]
else:
pass
# move camera and take pictures
def updateCameraState(self, key=None):
# stationary
if key is None:
self.moveCameraVertical(0)
# up
elif key is pygame.K_j:
self.moveCameraVertical(1)
# down
elif key is pygame.K_k:
self.moveCameraVertical(-1)
# take picture
elif key is pygame.K_SPACE:
# FIX THIS
pass
#self.takePicture(self.newPictureName())
else:
pass
# save jpegbytes to file
def takePicture(self, fname):
with open(fname, 'w') as fd:
fd.write(self.currentImage.getvalue())
# today's date plus a random string of letters
def newPictureName(self):
todaysDate = str(date.today())
uniqueKey = ''.join(choice(ascii_lowercase + ascii_uppercase) \
for _ in range(4))
return todaysDate+'_'+uniqueKey+'.jpg'
def main():
rover = KeyboardRover20()
while not rover.quit:
pass
rover.close()
if __name__ == '__main__':
main()