如何删除没有贯彻线程我的期望输出

问题描述:

#!/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

我解决了第一个问题。这是怎么回事

因为有两个打印语句。首先是内部功能,其次是外部功能。当函数不返回任何东西时,它返回None值。

我删除从环的print()和更换其中return语句是 例如

打印self.counter1和打印self.counter2

我不知道是否可以使用同一个while循环这两个编码器