/
PiProgram.py
executable file
·389 lines (331 loc) · 10.6 KB
/
PiProgram.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/usr/bin/python
import pygame
from pygame.locals import *
import os
import wheel
import sys, select, tty, termios, bluetooth, time, re
from evdev import InputDevice, categorize, ecodes # Device Input
from lib import xbox_read # Controller Lib
import random
# =====> Base Class
class Base():
def getMacAddress(self):
# This function accesses a file and gets the mac address
try:
# Opens the file, only read properties
with open("macFile","r+") as macFile:
# Reads and strips mac files data
mc = macFile.readline().rstrip("\n")
# Checks if what is in the file is a mac format
if re.match("[0-9a-f]{2}([-:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", mc.lower()):
# returns the mac or nothing
return mc
else: return ''
except IOError:
print "[...] MAC address File doesn't seem to Exist [...]"
return ''
def mainMenu(self):
# Menu function;
# Menu options
menu = {}
menu['1']=": Keyboard with GUI"
menu['2']=": Wheel"
menu['3']=": Xbox Controller"
menu['4']=": Playstation3 Controller"
menu['p']=": Pre-set Figures"
menu['r']=": Reconnected"
menu['q']=": Quit"
print "[...] Menu [...]\n"
# Sets some menu options
options=menu.keys()
options.sort()
# Goes thought every option and prints it out line by line.
for entry in options: print "\t" + entry, menu[entry]
def __init__(self):
# This __init function is the
# Clears the terminal
os.system(['clear','cls'][os.name == 'nt'])
# Calls the Car Class
carClass = Car()
# Gets Mac Address
macaddr = self.getMacAddress()
print "[...] Connecting to the Car : " + macaddr + " [...]"
# Tests if the car works
if Car().test(macaddr):
print "[...]\033[92m Connection Successful \033[0m[...]"
# If the test function works, setup the proper connection
carClass.connecting(macaddr)
else:
print "[...]\033[91m Connection Failed \033[0m [...]"
# Tests if the car is inrange
if carClass.inRange(macaddr):
print "[...]\033[91m Car is in range \033[0m[...]"
else:
print "[...]\033[91m Car is NOT in range \033[0m[...]"
# Loops until it breaks
while True:
self.mainMenu()
# Waits untill input
selection=raw_input("\nPlease Select: ")
# Clears termin
os.system(['clear','cls'][os.name == 'nt'])
# If the input matches any of the if statements below it will run that function or code.
if selection == '1':
print '[...] Keyboard [...]'
carClass.keyboard()
elif selection == '2':
print '[...] Wheel [...]'
carClass.wheelHandler()
elif selection == '3':
print '[...] Xbox Controller [...]'
carClass.controllerXbox()
elif selection == '4':
print '[...] Playstation3 Controller [...]'
carClass.controllerPs3()
elif selection == 'p':
carClass.subPreMenu()
elif selection == 'r':
if Car().test(self.getMacAddress()):
print "[...]\033[92m Connection Successful \033[0m[...]"
carClass.connecting(self.getMacAddress())
else:
print "[...]\033[91m Connection Failed or Already Connected \033[0m [...]"
elif selection == 'q':
break
else:
print "Unknown Option Selected!"
time.sleep(1)
# =====> Car Class
class Car:
# Global variables:
x = 1
y = 1
last = 0
speed = 8
maxSpeed = 15
minSpeed = 0
speedTimeStamp = time.time()
wheelClass = wheel.WheelClass()
def moveX(self, st):
# Checks in the variable sent is
if(st >= 0 or st <= 2):
# Sets X axes to the set value
self.x = st
def moveY(self, st):
# Checks in the variable sent is
if(st >= 0 or st <= 2):
# Sets X axes to the set value
self.y = st
def moveXY(self, xBit, yBit, spd, tim):
# Sets: X, Y, speed and time it's doing it for
self.x = xBit
self.y = yBit
self.move(spd)
time.sleep(tim)
def move(self, spd):
# Move/drive function
# Array of number according to the location of
arra = [[5,1,6], [3,0,4], [7,2,8]]
# Creates a char according to the data sent. [ (16 x direction) + speed ]
ch = (16 * arra[self.y][self.x]) + spd
# Error checking to make sure it's not out side of the bounds of the char variable.
if self.last != ch and (ch >= 0 or ch <= 255) :
# Send the var over bluetooth adaptor
self.sock.send(chr(ch))
self.last = ch
def setSpeed(self, spd):
# Sets the speed of the car
# added up a temp var for error checking
tmp = self.speed + spd
# Checks that its not over or under the max/min speed
if(tmp <= self.maxSpeed and tmp >= self.minSpeed):
# Gets current time
currentTime = time.time()
# Checks according of time passed so doesn't change to quickly
if(float(currentTime - self.speedTimeStamp) >= .2):
# Sets the global speed to new speed
self.speed = tmp
# Sets new time
self.speedTimeStamp = currentTime
print "New Speed: " + str(self.speed)
def keyboard(self):
# loop around each key press
self.wheelClass.launchGUI()
# Shows what are the different buttons.
print "\nForward: Up Arrow\nBackwards: Down Arrow\nLeft: Left Arrow\nRight: Right Arrow\n\nSpeed up: +/plus Button\nSpeed Down: -/minus Button\n\nExit: Escape Button\n\nCurrent Speed: "+str(self.speed)
while True:
# Starts pulling keyboard inputs from pygame
pygame.event.pump()
# Sets keyboard inputs to a variable
self.pressed = pygame.key.get_pressed()
# Get events
event = pygame.event.poll()
# if either the up/down button is pressed, set the Y axes to
if self.pressed[K_UP]: self.moveY(0)
elif self.pressed[K_DOWN]: self.moveY(2)
else: self.moveY(1)
# if either the left/right button is pressed, set the X axes to
if self.pressed[K_LEFT]: self.moveX(0)
elif self.pressed[K_RIGHT]: self.moveX(2)
else: self.moveX(1)
# if either the plus/minus buttons are pressed, increment/decrement the speed
if self.pressed[K_EQUALS]: self.setSpeed(1)
elif self.pressed[K_MINUS]: self.setSpeed(-1)
# Will run the move function, move with set the X/Y vars and move the car accordingly
self.move(self.speed)
# If the escape key is pressed, exit
if self.pressed[K_ESCAPE]: break
if event.type == pygame.MOUSEBUTTONDOWN: #and event.button == LEFT:
self.mouse_click_handler(event.pos)
os.system(['clear','cls'][os.name == 'nt'])
def controllerXbox(self):
# loop around xbox events
while True:
self.speed = 0
# pygame.event.pump()
# if self.pressed[K_ESCAPE]: break
for event in xbox_read.event_stream(deadzone=12000):
# if either the up/down button is pressed, set the Y axes to
if (event.key == 'RT' or event.key == 'LT'):
if event.key == 'RT' and event.value > 1:
self.speed = int(event.value)/17
self.moveY(0)
elif event.key == 'LT' and event.value > 1:
self.speed = int(event.value)/17
self.moveY(2)
else: self.moveY(1)
# if either the left/right button is pressed, set the X axes to
if(event.key == 'dl' or event.key == 'dr'):
if event.key == 'dl' and event.value == 1: self.moveX(0)
elif event.key == 'dr' and event.value == 1: self.moveX(2)
else: self.moveX(1)
# Runs the move function
self.move(self.speed)
#if(event.key == 'guide'): break
os.system(['clear','cls'][os.name == 'nt'])
def controllerPs3(self):
print '[...] Ps3 Controller [...]'
def wheelHandler(self):
while True:
# Starts pulling keyboard inputs from pygame
pygame.event.pump()
# Sets keyboard inputs to a variable
self.pressed = pygame.key.get_pressed()
direction,turning,speed=self.wheelClass.getMov()
self.y = direction
self.x = turning
self.move(speed)
if self.pressed[K_ESCAPE]: break
def connecting(self,bdr_addr):
# Connects the Pi and Car
try:
self.sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
self.sock.connect((bdr_addr, 1))
return sock
except: return ''
def test(self,mac):
# Tests the connection between the Pi and Car
try:
testSock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
testSock.connect((mac, 1))
testSock.close()
time.sleep(.5)
return True
except:
return False
def inRange(self, mac):
# Function, tests if car is in range and connected
devices = bluetooth.discover_devices()
for bdaddr in devices:
if mac == bdaddr:
return True
return False
def subPreMenu(self):
# Clears terminal
os.system(['clear','cls'][os.name == 'nt'])
menu = {}
# Sets sub menu
menu['1']=": Circle"
menu['2']=": Random*"
menu['3']=": Three Point Turn"
menu['4']=": Figure of Eight"
menu['5']=": Cruise"
menu['q']=": Quit"
while True:
print "[...] Pre-defined Figures [...]\n"
# Sorts menu
options=menu.keys()
options.sort()
# Prints menu
for entry in options:
print "\t" + entry, menu[entry]
selection=raw_input("\nPlease Select: ")
os.system(['clear','cls'][os.name == 'nt'])
if selection == '1':
print '[...] Circle [...]'
self.circle()
elif selection == '2':
print '[...] Random [...]'
self.randomMoves()
elif selection == '3':
print '[...] Three Point Turn [...]'
self.threePointTurn()
elif selection == '4':
print '[...] Figure of Eigth [...]'
self.eigth()
elif selection == '5':
print '[...] Cruise [...]'
elif selection == 'q':
break
else:
print "Unknown Option Selected!"
time.sleep(1)
def threePointTurn(self):
self.moveXY(1, 2, 8, .4) # backwards
self.moveXY(2, 2, 8, 1.6) # backwards, right
self.moveXY(0, 0, 8, 1.8) # forward, left
self.moveXY(1, 0, 8, .5) # forward
self.moveXY(1,1,0,0)
def circle(self):
self.moveXY(2, 0, 6, 16) # forward right
self.moveXY(1,1,0,0) # stop
def eigth(self):
self.moveXY(2, 0, 8, 7.2) # forward right
self.moveXY(1, 0, 8, .1) # forward
self.moveXY(0, 0, 8, 7.2) # forward left
self.moveXY(1, 0, 8, .1) # forward
self.moveXY(1,1,0,0) # stop
def randomMoves(self):
# Spazz out for 12 seconds, making random move every 3 seconds
for x in range(1,5): #running 4 times
dirX = random.choice([0,1,2])
dirY = random.choice([0,2]) #not using 1 as it would just turn wheels
speed = random.randint(5,10)
self.moveXY(dirX,dirY,speed,3)
def cruise(self):
#Cruising for 10 seconds
self.moveXY(1,0,9,10)
##### Accelerometer #####
def axelmeter(self, speed):
self.progbar.set_fraction(speed/11.0)
def mouse_click_handler(self, pos):
if pos[0] > 110 and pos[0] < 190:
if pos[1] >110 and pos[1] < 140:
print "Do 8 function"
self.eigth()
if pos[1] >160 and pos[1] < 190:
self.randomMoves()
if pos[0] > 210 and pos[0] < 290:
if pos[1] >110 and pos[1] < 140:
print "Circle"
self.circle()
if pos[1] >160 and pos[1] < 190:
self.threePointTurn()
if pos[0] >10 and pos[0] <90:
if pos[1] >110 and pos[1] < 140:
self.cruise()
if pos[1] >160 and pos[1] < 190:
sys.exit(0)
if __name__ == "__main__":
# Start of the program
Base()