-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrpmread.py
94 lines (71 loc) · 1.8 KB
/
rpmread.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
#rpmreader v2
# by mausausdruck1. 06.06.19
#
#teilweise angelehnt auf dem Code von
#FireHead RPM-Modul
#03.06.2019
#by Matteo Hagen
### USAGE
# 1. Setup conf
# 2. include modules + rpminit()
# 3. get you RPM via rpmread.rpm as list (left, right)
# 4. if you change your drive direction call rpmread.resetL() or rpmread.resetR()
# (un) commend gpio Setmode in some case!
###
import RPi.GPIO as GPIO
import config as cf
import time
import sys
counterL=0
timerL=0
timerR=0
counterR=0
rpm=[0,0] #rpm L/R
def rpminit():
GPIO.setmode(GPIO.BCM)
GPIO.setup(cf.rpmL, GPIO.IN)
GPIO.add_event_detect(cf.rpmL, GPIO.FALLING, callback=countL)
GPIO.setup(cf.rpmR, GPIO.IN)
GPIO.add_event_detect(cf.rpmR, GPIO.FALLING, callback=countR)
def getTime():
if sys.version_info < (3,7,0):
return time.time()
else:
return time.time_ns() / (10 ** 9) # genauere Zeit ab 3.7 in s float
def countL(var):
global counterL, rpm, timerL
if timerL==0:## init
timerL=getTime()
if counterL >= 2:
dt=getTime()-timerL
ds=counterL/cf.Drehscheibe
rpm[0]=ds/dt * 60 # U pro Minute
counterL=0
timerL=getTime()
else:
counterL=counterL+1
def resetL():
global rpm, timerL, counterL
rpm[0]=0;
timerL=0;
counterL=0;
def countR(var):
global counterR, rpm, timerR
if timerR==0:## init
timerR=getTime()
if counterR >= 2:
dt=getTime()-timerR
ds=counterR/cf.Drehscheibe
rpm[1]=ds/dt * 60 # U pro Minute
counterR=0
timerR=getTime()
else:
counterR=counterR+1
def resetR():
global rpm, timerR, counterR
rpm[1]=0;
timerR=0;
counterR=0;
# Test
#while True:
# print(rpm)