如何删除none并实现线程到我想要的输出

时间:2017-03-17 18:24:07

标签: multithreading python-2.7 class raspberry-pi

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

在raspberry init self和main varible

上设置gpio
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)

此循环获得a和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

此循环获得a和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() 

1 个答案:

答案 0 :(得分:0)

我解决了第一个问题。这发生了什么

因为有两个打印语句。第一个是内部功能,第二个是外部功能。当函数没有返回任何时候它返回无值。

我从loop()中删除了print,并替换了return语句所在的位置 示例

打印self.counter1并打印self.counter2

我想知道是否可以为两个编码器使用相同的while循环