-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAutonomous1.2.1.py
223 lines (176 loc) · 5.3 KB
/
Autonomous1.2.1.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
#!/usr/bin/env python
#special thanks to kamalraghav81 for helping me out with classes and threading!
import RPi.GPIO as GPIO
import threading, queue, random
import sys, tty, termios
from pygame import *
from time import *
init()
screen = display.set_mode((640, 480))
display.set_caption('The amazing key presser!')
forward_pin = 5
reverse_pin = 6
left_pin = 24
right_pin = 23
light_pin = 21
headlight_toggle = "OFF"
GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
GPIO.setup([forward_pin, reverse_pin, left_pin, right_pin, light_pin], GPIO.OUT)
q = queue.Queue(maxsize=10)
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class RemoteControl:
"""
Motion control interface for RC car
"""
def __init__(self):
"""
send preset commands to remote
"""
print("Setup/Initialize.")
global q
def forward(self):
"""
Move forward
GPIO commands to move forward
"""
print("Move Forward")
GPIO.output(forward_pin, GPIO.LOW)
GPIO.output(reverse_pin, GPIO.HIGH)
def reverse(self):
"""
Move forward
GPIO commands to move forward
"""
print("Go Back")
GPIO.output(forward_pin, GPIO.HIGH)
GPIO.output(reverse_pin, GPIO.LOW)
def right(self):
"""
Move forward
GPIO commands to move forward
"""
print("Turn Right")
GPIO.output(left_pin, GPIO.HIGH)
GPIO.output(right_pin, GPIO.LOW)
def left(self):
"""
Move forward
GPIO commands to move forward
"""
print("Turn Left")
GPIO.output(left_pin, GPIO.LOW)
GPIO.output(right_pin, GPIO.HIGH)
def forward_left(self):
print("Go Forward-Left")
GPIO.output(left_pin, GPIO.LOW)
GPIO.output(right_pin, GPIO.HIGH)
GPIO.output(forward_pin, GPIO.LOW)
GPIO.output(reverse_pin, GPIO.HIGH)
def forward_right(self):
print("Go Forward-Right")
GPIO.output(left_pin, GPIO.HIGH)
GPIO.output(right_pin, GPIO.LOW)
GPIO.output(forward_pin, GPIO.LOW)
GPIO.output(reverse_pin, GPIO.HIGH)
def reverse_left(self):
print("Go Reverse-Left")
GPIO.output(left_pin, GPIO.LOW)
GPIO.output(right_pin, GPIO.HIGH)
GPIO.output(forward_pin, GPIO.HIGH)
GPIO.output(reverse_pin, GPIO.LOW)
def reverse_right(self):
print("Go Reverse-Right")
GPIO.output(forward_pin, GPIO.HIGH)
GPIO.output(reverse_pin, GPIO.LOW)
GPIO.output(left_pin, GPIO.HIGH)
GPIO.output(right_pin, GPIO.LOW)
def preset(self):
"""
send preset data
"""
GPIO.output(left_pin, GPIO.HIGH)
GPIO.output(right_pin, GPIO.HIGH)
GPIO.output(forward_pin, GPIO.HIGH)
GPIO.output(reverse_pin, GPIO.HIGH)
print("Sending default command")
def run(self):
"""
run parallel thread to send commands
"""
thread = threading.Thread(target=worker)
thread.start()
def worker():
print("thread at work")
remote = RemoteControl()
while True:
if q.empty():
#sleep(.1)
remote.preset()
else:
new_key = q.get()
q.task_done()
if new_key == 'a':
remote.left()
#sleep(.1)
elif new_key == 'd':
remote.right()
#sleep(.1)
elif new_key == 'w':
remote.forward()
#sleep(.1)
elif new_key == 's':
remote.reverse()
#sleep(.1)
elif new_key == 'aw':
remote.forward_left()
elif new_key == 'dw':
remote.forward_right()
elif new_key == 'as':
remote.reverse_left()
elif new_key == 'sd':
remote.reverse_right
if __name__ == "__main__":
remote = RemoteControl()
remote.run()
while True:
#rand = random.uniform(0, 5)
keys = ([key.get_pressed()[K_a],key.get_pressed()[K_d],
key.get_pressed()[K_w], key.get_pressed()[K_s]])
if key.get_pressed()[K_l] == 1:
if headlight_toggle == "ON":
print("headlights off")
headlight_toggle = "OFF"
GPIO.output(headlight_pin, GPIO.LOW)
else:
print("headlights on")
headlight_toggle = "ON"
GPIO.output(headlight_pin, GPIO.HIGH)
print(keys)
if keys == [1,0,0,0]:
q.put('a')
elif keys == [0,1,0,0]:
q.put('d')
elif keys == [1,0,1,0]:
q.put('aw')
elif keys == [0,1,1,0]:
q.put('dw')
elif keys == [0,0,0,1]:
q.put('s')
elif keys == [0,0,1,0]:
q.put('w')
elif keys == [0,1,0,1]:
q.put('sd')
elif keys == [1,0,0,1]:
q.put('as')
event.pump()
print('More to come, soon!!!')