-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathabsoluteencoder.py
66 lines (56 loc) · 1.42 KB
/
absoluteencoder.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
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
PIN_CLK = 2
PIN_DAT = [3,14]
PIN_CS = 4
delay = 0.0000005
ns = 2 # number of sensors attached
# totally 10 bits to be extracted from SSI signal
bitcount = 16
# pin setup done here
try:
GPIO.setup(PIN_CLK,GPIO.OUT)
GPIO.setup(PIN_DAT[:],GPIO.IN)
GPIO.setup(PIN_CS,GPIO.OUT)
GPIO.output(PIN_CS,1)
GPIO.output(PIN_CLK,1)
except:
print "ERROR. Unable to setup the configuration requested"
#wait some time to start
time.sleep(0.5)
print "GPIO configuration enabled"
def clockup():
GPIO.output(PIN_CLK,1)
def clockdown():
GPIO.output(PIN_CLK,0)
def MSB():
# Most Significant Bit
clockdown()
def readpos():
GPIO.output(PIN_CS,0)
time.sleep(delay*2)
MSB()
data = [0]*ns
for i in range(0,bitcount):
if i<10:
#print i
clockup()
for j in range(0,ns):
data[j]<<=1
data[j]|=GPIO.input(PIN_DAT[j])
clockdown()
else:
for k in range(0,6):
clockup()
clockdown()
GPIO.output(PIN_CS,1)
return data
try:
while(1):
print readpos()
time.sleep(0.001)
#break
finally:
print "cleaning up GPIO"
GPIO.cleanup()