-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha4988.py
301 lines (250 loc) · 7.93 KB
/
a4988.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
import RPi.GPIO as GPIO
from time import sleep
import serial
import wiringpi
import sys
ser = serial.Serial("/dev/ttyUSB0", baudrate=9600)
from time import sleep
# wiringpi.wiringPiSetupGpio()
# wiringpi.pinMode(24, 1) # sets GPIO 24 to output
# wiringpi.digitalWrite(24, 1) # sets port 24 to 1 (3V3, on)
data = ''
word = ''
direction =0
number = 0
count = 0
dictionary = {'59004312D': 'move forward','590045F232DC' : 'endtag', '59004312DDD5': 'move forward'}
GPIO.setmode(GPIO.BCM)
#Buttons Setup
#GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
'''
scanner_top_pin = 21
scanner_bottom_pin = 19 # was26
horizontal_top_pin = 16
horizontal_bottom_pin = 20
vertical_top_pin = 13
vertical_bottom_pin= 26 # was19
GPIO.setup(scanner_top_pin, GPIO.IN)
GPIO.setup(scanner_bottom_pin, GPIO.IN)
GPIO.setup(horizontal_top_pin, GPIO.IN)
GPIO.setup(horizontal_bottom_pin, GPIO.IN)
GPIO.setup(vertical_top_pin, GPIO.IN)
GPIO.setup(vertical_bottom_pin, GPIO.IN)'''
#scaenner
stepPin1 = 3
dirPin1 = 2
enablePin1 = 18
sleepPin1 = 4
#Motor Horizontal
stepPin2 = 27
dirPin2 = 17
enablePin2 = 23
sleepPin2 = 22
#Motor Vertical
stepPin3 = 9
dirPin3 = 10
enablePin3 = 24
sleepPin3 = 11
def init():
#Motor Scanner Setup
GPIO.setup(stepPin1, GPIO.OUT)
GPIO.setup(dirPin1, GPIO.OUT)
GPIO.setup(enablePin1, GPIO.OUT)
GPIO.setup(sleepPin1, GPIO.OUT)
GPIO.output(enablePin1, GPIO.LOW)
GPIO.output(sleepPin1, GPIO.LOW)
GPIO.output(dirPin1, GPIO.HIGH)
#Motor Horizontal
GPIO.setup(stepPin2, GPIO.OUT)
GPIO.setup(dirPin2, GPIO.OUT)
GPIO.setup(enablePin2, GPIO.OUT)
GPIO.setup(sleepPin2, GPIO.OUT)
GPIO.output(enablePin2, GPIO.LOW)
GPIO.output(sleepPin2, GPIO.LOW)
GPIO.output(dirPin2, GPIO.HIGH)
#vertical
GPIO.setup(stepPin3, GPIO.OUT)
GPIO.setup(dirPin3, GPIO.OUT)
GPIO.setup(enablePin3, GPIO.OUT)
GPIO.setup(sleepPin3, GPIO.OUT)
GPIO.output(enablePin3, GPIO.LOW)
GPIO.output(sleepPin3, GPIO.LOW)
GPIO.output(dirPin3, GPIO.HIGH)
delay = .0005
#Moving Scanner Motor
def moveScannerUp(num):
#step_count = input("Enter number of steps: ")
step_count = num
GPIO.output(dirPin1, GPIO.LOW)
GPIO.output(sleepPin1, GPIO.HIGH)
for x in range(step_count):
GPIO.output(stepPin1, GPIO.HIGH)
sleep(delay)
GPIO.output(stepPin1, GPIO.LOW)
sleep(delay)
GPIO.output(sleepPin1, GPIO.LOW)
def moveScannerDown(num):
#step_count = input("Enter number of steps: ")
print('inscanner')
step_count = num
GPIO.output(dirPin1, GPIO.HIGH)
GPIO.output(sleepPin1, GPIO.HIGH)
for x in range(step_count):
#readRFID()
GPIO.output(stepPin1, GPIO.HIGH)
sleep(delay)
GPIO.output(stepPin1, GPIO.LOW)
sleep(delay)
GPIO.output(sleepPin1, GPIO.LOW)
def readRFID():
""" with open('/dev/ttyUSB0','r') as tty:
RFID_input = tty.readline()
print(RFID_input)
f = open( 'rfidAttack1.txt', 'a' )
"""
#with serial.Serial("/dev/ttyUSB0", 9600) as ser:
with open('input/rfidAttack1.txt', 'w') as f:
print('before while')
while(ser.in_waiting > 1):
data = ser.readline()
code = str(data);
if(not(code[1].isdigit() or code[1].isalpha())):
f.write(code[2:])
else:
f.write(code[1:])
print data
print('hi')
"""data = serial.read()
print(data)
print('data')
code = ''
if data == '\r':
print(code)
print('printing
c and writing')
f.write( code+'\n' )
code = ''
tag = ""
else:
print('in else')
code = code + data
count = count+1
f = open( 'rfidAttack.txt', 'w' )
f.write(code)
f.close()
if(count == 13):
f.write('\n')
tag[count] = code
count = count+1
#if (len(tag) == 11):
code = code[1:]"""
#Moving Horizontal Motor
def moveHorizontalUp(num):
print"yola"
#step_count = input("Enter number of steps: ")
step_count = num
GPIO.output(dirPin2, GPIO.LOW)
GPIO.output(sleepPin2, GPIO.HIGH)
for x in range(step_count):
GPIO.output(stepPin2, GPIO.HIGH)
sleep(delay)
GPIO.output(stepPin2, GPIO.LOW)
sleep(delay)
print('sleep pin 2')
GPIO.output(sleepPin2, GPIO.LOW)
def moveHorizontalDown(num):
#step_count = input("Enter number of steps: ")
step_count = num
GPIO.output(dirPin2, GPIO.HIGH)
GPIO.output(sleepPin2, GPIO.HIGH)
for x in range(step_count):
GPIO.output(stepPin2, GPIO.HIGH)
sleep(delay)
GPIO.output(stepPin2, GPIO.LOW)
sleep(delay)
GPIO.output(sleepPin2, GPIO.LOW)
#Moving vertical Motor
def moveVerticalUp(num):
#step_count = input("Enter number of steps: ")
## print('in a4988 move vertical up')
step_count = num
GPIO.output(dirPin3, GPIO.LOW)
GPIO.output(sleepPin3, GPIO.HIGH)
f = open('rfidAttack.txt', 'w')
for x in range(step_count):
sleep(delay)
GPIO.output(stepPin3, GPIO.HIGH)
sleep(delay)
GPIO.output(stepPin3, GPIO.LOW)
GPIO.output(sleepPin3, GPIO.LOW)
def moveVerticalDown(num):
#step_count = input("Enter number of steps: ")
step_count = num
GPIO.output(dirPin3, GPIO.HIGH)
GPIO.output(sleepPin3, GPIO.HIGH)
for x in range(step_count):
GPIO.output(stepPin3, GPIO.HIGH)
sleep(delay)
GPIO.output(stepPin3, GPIO.LOW)
sleep(delay)
GPIO.output(sleepPin3, GPIO.LOW)
#GPIO.add_event_detect(scanner_bottom_pin, GPIO.FALLING, callback=stop1, bouncetime=2000)
'''
GPIO.add_event_detect(scanner_top_pin, GPIO.FALLING, callback=stop2, bouncetime=2000)
GPIO.add_event_detect(horizontal_top_pin, GPIO.FALLING, callback=stop3, bouncetime=2000)
GPIO.add_event_detect(horizontal_bottom_pin, GPIO.FALLING, callback=stop4, bouncetime=2000)
GPIO.add_event_detect(vertical_top_pin, GPIO.FALLING, callback=stop5, bouncetime=2000)
GPIO.add_event_detect(vertical_bottom_pin, GPIO.FALLING, callback=stop6, bouncetime=2000)
#add interrupt and handler for start and reset button
'''
#the following method isn't needed once CS is integrated
#def reset(): #motor
# moveScannerUp(10000)
# moveScannerDown(200) #release from pressing the switch
# moveVerticalUp(10000)
# moveVerticalDown(200) #release from pressing the switch
# moveHorizontalLeft(10000)
# moveHorizontalRight(200) #release from pressing the switch
"""
while(1):
#command = raw_input("Enter command: ")
#steps = input("Enter number of steps: ")
command = ''
steps = 0
if command == "move scanner up":
moveScannerUp(steps)
if command == "move scanner down":
moveScannerDown(steps)
if command == "move vertical up":
moveVerticalUp(steps)
if command == "move vertical down":
moveVerticalDown(steps)
if command == "move horizontal up":
moveHorizontalUp(steps)
if command == "move horizontal down":
moveHorizontalDown(steps)
if(number ==0):
moveScannerDown(50000)
number = number+1
data = serial.read()
if data == '\r':
print('in if')
print(code)
code = ''
else:
print('in else')
code = code + data
if (len(code) == 11):
code = code[1:]
# resetReader()
if(len(code)>9 and dictionary[code[1:11]] == 'move forward'):
if(direction == 0): moveVerticalUp(2000)
elif(direction==1): moveHorizontalUp(2000)
elif(direction==2): moveVerticalDown(2000)
else: moveHorizontalDown(2000)
"""
def resetReader():
# wiringpi.digitalWrite(24, 0) # sets port 24 to 0 (0V, off) #low
sleep(10) # wait 10s
# wiringpi.digitalWrite(24, 1) # sets port 24 to 1 (3V3, on) #high
sleep(10) # wait 10s