利用霍尔增量式编码器测速
- 根据AB相谁领先判断正负
- 根据AB相单位时间内上升沿个数测速
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
49import RPi.GPIO as GPIO
import time
class Encoder:
def __init__(self, pinA, pinB):
self.t = 0.1
GPIO.setmode(GPIO.BOARD)
self.pinA = pinA
self.pinB = pinB
self.last_A=1
self.direction=1
self.counter = 0
GPIO.setup(pinA, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(pinB, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pinA, GPIO.RISING, callback=self.callback)
GPIO.add_event_detect(pinB, GPIO.RISING, callback=self.callback)
def callback(self, channel):
self.counter += 1
def getCounter(self):
return self.counter
def getRPM(self):
counterA = self.counter
time.sleep(self.t)
counterB = self.counter
self.counter = 0
rpm = (counterB - counterA) / (1320 / 2)
return rpm
def getV(self):
t=time.time()
while time.time()-t<0.1:
self.judgeV()
rpm = self.getRPM()
v = rpm * 0.09 * 3.1415926927 * (1 / self.t)*direction
return v
def judgeV(self):
A=GPIO.input(self.pinA)
B=GPIO.input(self.pinB)
if A!=self.last_A:
if A==B:
self.direction=1
else:
self.direction=-1
self.last_A=A