#!/usr/bin/env python
import RPi.GPIO as GPIO
from time import sleep
"""
this is writen for the raspberry pi to get an input from two encoder and print a numberical
count. this will use with a telescope and stellarium
this need lot more work but just a beginner still
"""
class EncoderSetup:
def __init__(self, azm_a=27, azm_b=17, lat_a=20, lat_b=21, counter1=0, counter2=0):
self.azm_a = azm_a
self.azm_b = azm_b
self.lat_a = lat_a
self.lat_b = lat_b
self.counter1 = counter1
self.counter2 = counter2
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.azm_a, GPIO.IN)
GPIO.setup(self.azm_b, GPIO.IN)
GPIO.setup(self.lat_a, GPIO.IN)
GPIO.setup(self.lat_b, GPIO.IN)
class Azimuth(EncoderSetup):
def azm_encoder(self):
Last_RoB_Status = GPIO.input(self.azm_b)
while not GPIO.input(self.azm_a): # starts encoder loop
Current_RoB_Status = GPIO.input(self.azm_b)
dtState = GPIO.input(self.azm_a)
if Current_RoB_Status != Last_RoB_Status:
if dtState != Current_RoB_Status:
self.counter1 += 1
else:
self.counter1 -= 1
Last_RoB_Status = Current_RoB_Status # deboucing
# sleep(0.01)
return self.counter1
class Latitude(EncoderSetup):
def lat_encoder(self):
Last_RoC_Status = GPIO.input(self.lat_a)
while not GPIO.input(self.lat_b):
Current_RoC_Status = GPIO.input(self.lat_a)
dtState2 = GPIO.input(self.lat_b)
if Current_RoC_Status != Last_RoC_Status:
if dtState2 != Current_RoC_Status:
self.counter2 += 1
else:
self.counter2 -= 1
Last_RoC_Status = Current_RoC_Status # deboucing
# sleep(0.01)
return self.counter2
def loop():
eset = EncoderSetup()
azm = Azimuth()
lat = Latitude()
while True:
print ('globalCounter1 = ' + str(azm.azm_encoder()) +
' globalCounter2 = ' + str(lat.lat_encoder()))
"""
this is my problem i get a numerical output with none example
globalcounter1 = none globalcounter2 none
globalcounter1 = 0 globalcounter2 none
globalcounter1 = none globalcounter2 0
globalcounter1 = none globalcounter2 none
globalcounter1 = 4 globalcounter2 -1
globalcounter1 = 5 globalcounter2 none
the second problem is i got to turn lat_encoder first before
i get and out from azm_encoder. i think i need to implement threading
or the the same loop for both lat_encoder and azm_encoder
please help me i little lost at this point
"""
def destroy():
GPIO.cleanup()
if __name__ == '__main__':
try:
loop()
except KeyboardInterrupt:
destroy()
答案 0 :(得分:0)
因为有两个打印语句。第一个是内部功能,第二个是外部功能。当函数没有返回任何时候它返回无值。
我从loop()中删除了print,并替换了return语句所在的位置 示例
打印self.counter1并打印self.counter2
我想知道是否可以为两个编码器使用相同的while循环