2017-03-17 27 views
-2
#!/usr/bin/env python 
import RPi.GPIO as GPIO 
from time import sleep 

樹莓初始化自我和主要varible

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) 

我的目標

""" 
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 
""" 

組GPIO這個循環得到的狀態和b比較,得到方向,計數和去抖

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 

這個循環得到的狀態和b進行比較,獲得方向,數量和去抖

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 
""" 

重設覆盆子上的gpio

def destroy(): 
    GPIO.cleanup() 

啓動程序

if __name__ == '__main__': 

使用調用循環開始

刪除追溯錯誤信息

try: 
     loop() 
    except KeyboardInterrupt: 
     destroy() 
+0

你應該真的編輯你的問題,使Python代碼的縮進是有效的語法。 – barny

回答

0

我解決了第一個問題。這是怎麼回事

因爲有兩個打印語句。首先是內部功能,其次是外部功能。當函數不返回任何東西時,它返回None值。

我刪除從環的print()和更換其中return語句是 例如

打印self.counter1和打印self.counter2

我不知道是否可以使用同一個while循環這兩個編碼器