-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.py
172 lines (145 loc) · 3.94 KB
/
car.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
#car.py
#Drive L298N
#Date: 2013-2-2
#Author: JimmyWang
#Email: JimmyWang.tj@gmail.com
from time import sleep
import wiringpi
import threading
import time
import sys
import random
from evdev import InputDevice, list_devices, categorize, ecodes
direction = 'Pause' # Forward, Pause, Right, Left, Backward, BackRight, BackLeft
mouse_click = False
stop_listen = False
enableA = 1
enableB = 6
h1 = 15
h2 = 16
h3 = 11
h4 = 10
obsA = 14
obsB = 7
class MouseListener(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global direction
global stop_listen
global mouse_click
dev = InputDevice('/dev/input/event1')
print(dev)
for event in dev.read_loop():
#print event
mouse_click = True
if event.code == 272 and event.type == 1 and event.value == 0 :
print 'Left press'
if direction == 'Forward':
direction = 'Left'
elif direction == 'Right':
direction = 'Forward'
if event.code == 273 and event.type == 1 and event.value == 0 :
print 'Right press'
if direction == 'Forward':
direction = 'Right'
elif direction == 'Left':
direction = 'Forward'
if event.code == 274 and event.type == 1 and event.value == 0 :
print 'Middle press'
if direction == 'Forward' or direction == 'Right' or direction == 'left' or direction == 'Backward':
direction = 'Pause'
else:
direction = 'Forward'
if event.code == 8 and event.type == 2 and event.value == 1 :
print 'scroll up'
if direction == 'Pause':
direction = 'Forward'
elif direction == 'Backward':
direction = 'Pause'
if event.code == 8 and event.type == 2 and event.value == -1 :
print 'scroll down'
if direction == 'Pause':
direction = 'Backward'
elif direction == 'Forward':
direction = 'Pause'
if stop_listen == True:
break
def startMouse():
thread = MouseListener()
thread.start()
return thread
def forward():
wiringpi.softPwmWrite(enableA, 255)
wiringpi.digitalWrite(h1, 0)
wiringpi.digitalWrite(h2, 1)
wiringpi.softPwmWrite(enableB, 235)
wiringpi.digitalWrite(h3, 0)
wiringpi.digitalWrite(h4, 1)
def backward():
wiringpi.softPwmWrite(enableA, 255)
wiringpi.digitalWrite(h1, 1)
wiringpi.digitalWrite(h2, 0)
wiringpi.softPwmWrite(enableB, 255)
wiringpi.digitalWrite(h3, 1)
wiringpi.digitalWrite(h4, 0)
def stop():
wiringpi.softPwmWrite(enableA, 0)
wiringpi.softPwmWrite(enableB, 0)
def turnRight():
wiringpi.softPwmWrite(enableA, 255)
wiringpi.digitalWrite(h1, 0)
wiringpi.digitalWrite(h2, 1)
wiringpi.softPwmWrite(enableB, 255)
wiringpi.digitalWrite(h3, 1)
wiringpi.digitalWrite(h4, 0)
def turnLeft():
wiringpi.softPwmWrite(enableA, 255)
wiringpi.digitalWrite(h1, 1)
wiringpi.digitalWrite(h2, 0)
wiringpi.softPwmWrite(enableB, 255)
wiringpi.digitalWrite(h3, 0)
wiringpi.digitalWrite(h4, 1)
wiringpi.wiringPiSetup()
wiringpi.pinMode(enableA, 1)
wiringpi.pinMode(h1, 1)
wiringpi.pinMode(h2, 1)
wiringpi.pinMode(enableB, 1)
wiringpi.pinMode(h3, 1)
wiringpi.pinMode(h4, 1)
wiringpi.softPwmCreate(enableA, 0, 255)
wiringpi.softPwmCreate(enableB, 0, 255)
wiringpi.pinMode(obsA, wiringpi.INPUT)
wiringpi.pinMode(obsB, wiringpi.INPUT)
mthread = startMouse()
while True:
valueA = wiringpi.digitalRead(obsA)
valueB = wiringpi.digitalRead(obsB)
#print 'obsA:', valueA, 'obsB:', valueB
if valueA == 0 or valueB == 0:
if direction == 'Pause':
stop()
else:
backward()
sleep(0.5)
turnLeft()
sleep(0.5)
direction = 'Forward'
else:
if direction == 'Forward':
forward()
elif direction == 'Pause':
stop()
elif direction == 'Left':
turnLeft()
direction = 'Forward'
elif direction == 'Right':
turnRight()
direction = 'Forward'
elif direction == 'Backward':
backward()
try:
sleep(0.1)
except KeyboardInterrupt:
stop_listen = True
raise