-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcCarDriver.py
155 lines (120 loc) · 2.86 KB
/
rcCarDriver.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
import serial
import pygame
from pygame.locals import *
import os
import sys
import time
FWD = "FWD"
REV = "REV"
LEFT = "LFT"
RIGHT = "RGHT"
STOP = "STP"
QUIT = "QT"
COMMANDS = {
"STP":0,
"RGHT":1,
"LFT":2,
"REV":4,
"REVRGHT":5,
"REVLFT":6,
"FWD":8,
"FWDRGHT":9,
"FWDLFT":10,
"QT":9999
}
PREVIOUS_COMMAND = STOP
def processInput(event):
newCommand = STOP
if (event.type == pygame.QUIT):
newCommand = QUIT
elif (event.type == KEYDOWN):
keyinput = pygame.key.get_pressed()
#complex orders
if keyinput[pygame.K_q]:
newCommand = QUIT
elif keyinput[pygame.K_UP] and keyinput[pygame.K_RIGHT]:
newCommand = FWD+RIGHT
elif keyinput[pygame.K_UP] and keyinput[pygame.K_LEFT]:
newCommand = FWD+LEFT
elif keyinput[pygame.K_DOWN] and keyinput[pygame.K_RIGHT]:
newCommand = REV+RIGHT
elif keyinput[pygame.K_DOWN] and keyinput[pygame.K_LEFT]:
newCommand = REV+LEFT
#simple orders
elif keyinput[pygame.K_UP]:
newCommand = FWD
elif keyinput[pygame.K_DOWN]:
newCommand = REV
elif keyinput[pygame.K_RIGHT]:
newCommand = RIGHT
elif keyinput[pygame.K_LEFT]:
newCommand = LEFT
elif event.type == pygame.KEYUP:
#up-right
if (PREVIOUS_COMMAND == FWD+RIGHT):
if event.key == pygame.K_RIGHT:
newCommand = FWD
elif event.key == pygame.K_UP:
newCommand = RIGHT
#up-left
elif (PREVIOUS_COMMAND == FWD+LEFT):
if event.key == pygame.K_LEFT:
newCommand = FWD
elif event.key == pygame.K_UP:
newCommand = LEFT
#back-right
elif (PREVIOUS_COMMAND == REV+RIGHT):
if event.key == pygame.K_RIGHT:
newCommand = REV
elif event.key == pygame.K_DOWN:
newCommand = RIGHT
#back-left
elif (PREVIOUS_COMMAND == REV+LEFT):
if event.key == pygame.K_RIGHT:
newCommand = REV
elif event.key == pygame.K_DOWN:
newCommand = LEFT
return newCommand
def getCommand():
global PREVIOUS_COMMAND
newCommand = STOP
event = pygame.event.wait()
if event:
newCommand = processInput(event)
PREVIOUS_COMMAND = newCommand
return newCommand
else:
return None
def getCommand2():
global PREVIOUS_COMMAND
newCommand = STOP
for event in pygame.event.get():
newCommand = processInput()
PREVIOUS_COMMAND = newCommand
return newCommand
def main():
os.system('clear')
print("Starting driver pgm")
pygame.init()
pygame.display.set_mode((1,1))
ser = serial.Serial('/dev/ttyACM1',9600)
# timeout=1);
command = STOP
while True:
commandKey = getCommand()
print("Iteration")
#time.sleep(1)
if commandKey is None:
pass
elif (commandKey and (commandKey != QUIT)):
command = COMMANDS.get(commandKey,None)
fCommand = '{0:#0{1}x}'.format(command,4)
print(commandKey,":",fCommand,":",command)
ser.write(bytes(fCommand))
else:
break
pygame.quit()
sys.exit()
ser.close()
if __name__ == '__main__':
main()