-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiprocessed_wheel_encoder.py
68 lines (53 loc) · 1.44 KB
/
multiprocessed_wheel_encoder.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
from multiprocessing import Process, Value
import RPi.GPIO as GPIO
import time
outputA = 14
outputB = 15
#I do not know which of these are actually supposed to live inside of the process
counter = 0
aState = 0
aLastState = 0
t1 = 0
t2 = 0
deltad = 0.0
deltat = 0.0
calc = 0.0
def gpioSetup(): #uh I sure hope I'm allowed to call this in the other process???
GPIO.setmode(GPIO.BCM)
GPIO.setup(outputA, GPIO.IN)
GPIO.setup(outputB, GPIO.IN)
def readWheel(distanceValue: Value):
#setup:
gpioSetup()
t1 = time.time()
aLastState = GPIO.input(outputA)
while True:
aState = GPIO.input(outputA)
deltad = 1.0 / 8.0
if aState != aLastState:
t2 = time.time() * 1000
if GPIO.input(outputB) != aState:
counter += 1
deltat = t2 - t1
calc = 1000 * deltad / deltat
print(t1)
print(t2)
print("Instantaneous Speed:", calc)
t1 = t2
else:
counter -= 1
aLastState = aState
distanceValue.value = counter
def main():
counter = Value('i', 0.0)
p = Process(target=readWheel, args=counter)
p.start()
try:
while True:
# time.sleep(0.5)
print(counter.value)
except KeyboardInterrupt:
print('"killing" child process')
p.join()
if __name__ == '__main__':
main()