简体   繁体   English

树莓派线程

[英]raspberry pi threading

I am new to programming. 我是编程新手。 I am jumping in with both feet and building a project which will read a GPIO pin (the machine is a Raspberry Pi) input voltages and ultimately will display on an lcdnumber widget in qt creator. 我用双脚跳了起来,并建立了一个项目,该项目将读取GPIO引脚(该机器是Raspberry Pi)输入电压,最终将显示在qt creator中的lcdnumber小部件上。 For now I'm using a photocell as a surrogate for the o2 cell that will be the final component. 现在,我使用光电管作为o2电池的替代品,这将是最终组件。 Thanks to everyone posting code, I have working code for the photocell which prints voltages just fine. 多亏每个人都发布了代码,我有了光电管的工作代码,它可以很好地打印电压。 In order to get constant and continuous voltages I'm using a while true loop. 为了获得恒定和连续的电压,我使用一会儿真正的循环。 Of course when I run the program, the loop takes over and the other part of my program doesn't run. 当然,当我运行程序时,循环将接管,而程序的另一部分则不会运行。 The other part is a gui with the lcdnumber displays and some push buttons which will ultimately be connected to a digital pot. 另一部分是带有lcdnumber显示屏和一些按钮的gui,这些按钮最终将连接到数字电位计。 The gui runs great, by itself. gui本身运行出色。 The photocell code works great, by itself. 光电管代码本身可以很好地工作。 Combining the two is where I have issues. 将两者结合是我遇到的问题。 From what I've read over the past few days, threading either in python or Qt seems to be the way to go. 根据过去几天的阅读,采用python或Qt进行线程似乎是行之有效的方法。 I've read all of the examples and explanations I could find and I'm stuck. 我已经阅读了所有可以找到的示例和说明,但遇到了麻烦。 The gui pops up and functions, the loop doesn't seem to run. gui弹出并起作用,循环似乎没有运行。 The output of the photocell is printing to the terminal right now, or normally does when I run that code by itself, since I don't know yet how to connect that to the Qt lcd. 光电管的输出现在正在打印到终端,或者通常在我自己运行该代码时执行,因为我还不知道如何将其连接到Qt LCD。

I would very much appreciate it if someone could have a look at my code and let me know where I am going wrong with the threading and anything else you may see since I am still learning. 如果有人可以看一下我的代码,让我知道线程出错的地方以及自从我还在学习的任何其他内容,我将不胜感激。 Any hints as to connecting the photocell signal to the Qt LCD would also be a huge help. 将光电管信号连接到Qt LCD的任何提示也将有很大帮助。

I am using Raspbian (a variant of Debian Linux) and Qt4/Qt Creator 3.2.1 我正在使用Raspbian(Debian Linux的变种)和Qt4 / Qt Creator 3.2.1。

Please let me know what other information you may need or want. 请让我知道您可能需要或想要的其他信息。

import sys
import RPi.GPIO as GPIO
import time
import re
from PyQt4 import QtGui, uic, QtCore
from PyQt4.QtCore import QThread
import spidev
import os
import threading
from threading import Thread

GPIO.setmode(GPIO.BCM) 
GPIO.setwarnings(False)

o2zero = 26
o2span = 19
cozero = 13
cospan = 6
co2zero = 5
co2span = 21

status = "nil"

light_channel = 0
spi = spidev.SpiDev()
spi.open(0,0)

class MyWindow(QtGui.QMainWindow): 

    def __init__(self):
        super(MyWindow, self).__init__()
        uic.loadUi('mainwindow.ui', self)
        self.show()
        QtCore.QObject.connect(self.o2_zero_up,QtCore.SIGNAL("clicked()"), self.o2zeroup) 
        QtCore.QObject.connect(self.o2_zero_down,QtCore.SIGNAL("clicked()"), self.o2zerodown)
        QtCore.QObject.connect(self.o2_span_up,QtCore.SIGNAL("clicked()"), self.o2spanup)
        QtCore.QObject.connect(self.o2_span_down,QtCore.SIGNAL("clicked()"), self.o2spandown)
        QtCore.QObject.connect(self.co_zero_up,QtCore.SIGNAL("clicked()"), self.cozeroup)
        QtCore.QObject.connect(self.co_zero_down,QtCore.SIGNAL("clicked()"), self.cozerodown)
        QtCore.QObject.connect(self.co_span_up,QtCore.SIGNAL("clicked()"), self.cospanup)
        QtCore.QObject.connect(self.co_span_down,QtCore.SIGNAL("clicked()"), self.cospandown)
        QtCore.QObject.connect(self.co2_zero_up,QtCore.SIGNAL("clicked()"), self.co2zeroup)
        QtCore.QObject.connect(self.co2_zero_down,QtCore.SIGNAL("clicked()"), self.co2zerodown)
        QtCore.QObject.connect(self.co2_span_up,QtCore.SIGNAL("clicked()"), self.co2spanup)
        QtCore.QObject.connect(self.co2_span_down,QtCore.SIGNAL("clicked()"), self.co2spandown)  
        QtCore.QObject.connect(self.close_button,QtCore.SIGNAL("clicked()"), self.gpiocleanup) 

    def o2zeroup(self):  
        GPIO.setup(o2zero, GPIO.OUT)   
        GPIO.output(o2zero, 1) 
    def o2zerodown(self): 
        GPIO.setup(o2zero, GPIO.OUT) 
        GPIO.output(o2zero, 0) 

    def o2spanup(self):  
        GPIO.setup(o2span, GPIO.OUT)  
        GPIO.output(o2span, 1) 
    def o2spandown(self): 
        GPIO.setup(o2span, GPIO.OUT) 
        GPIO.output(o2span, 0) 

    def cozeroup(self):  
        GPIO.setup(cozero, GPIO.OUT)  
        GPIO.output(cozero, 1) 
    def cozerodown(self):
        GPIO.setup(cozero, GPIO.OUT)  
        GPIO.output(cozero, 0) 

    def cospanup(self):
        GPIO.setup(cospan, GPIO.OUT)   
        GPIO.output(cospan, 1) 
    def cospandown(self): 
        GPIO.setup(cospan, GPIO.OUT) 
        GPIO.output(cospan, 0)

    def co2zeroup(self):
        GPIO.setup(co2zero, GPIO.OUT)   
        GPIO.output(co2zero, 1) 
    def co2zerodown(self): 
        GPIO.setup(co2zero, GPIO.OUT) 
        GPIO.output(co2zero, 0)

    def co2spanup(self):
        GPIO.setup(co2span, GPIO.OUT)   
        GPIO.output(co2span, 1) 
    def co2spandown(self): 
        GPIO.setup(co2span, GPIO.OUT) 
        GPIO.output(co2span, 0)   

    def gpiocleanup(self): 
        GPIO.cleanup() 

    def closeEvent(self, event): 
        print ("GPIO CleanUP")
        GPIO.cleanup() 
        event.accept()       

class O2_Channel(QtCore.QThread):

    def __init__(self):
        QtCore.QThread.__init__(self)

    def O2_Channel(self):

        ReadChannel(channel)
        adc = spi.xfer2([1,(8+channel)<<4,0])
        data = ((adc[1]&3) << 8) + adc[2]
        return data

        ConvertVolts(data,places)
        volts = (data * 3.3) / float(1023)
        volts = round(volts,places)
        return volts 

    def run(self):

        while True:

            light_level = ReadChannel(light_channel)
            light_volts = ConvertVolts(light_level,2)

            print "--------------------------------------------"
            print("Light: {} ({}V)".format(light_level,light_volts))

            light_channel = 0
            temp_channel  = 1

            delay = .3                         

            time.sleep(delay)

            self.O2_Channel.start()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MyWindow()
    sys.exit(app.exec_())

You must create an instance of your Thread and start it with the start () function from the main thread. 您必须创建线程的实例,并从主线程使用start()函数启动它。 If you want to display the data in your GUI you must do it from the main thread for this I have created a slot and a signal that is emitted when there is a new reading. 如果要在GUI中显示数据,则必须从主线程执行此操作,为此,我创建了一个插槽和一个信号,该信号在出现新读数时发出。

I have also changed the connection style of connection between the signals and the slot to a more modern one. 我也将信号和插槽之间的连接连接方式更改为更现代的方式。

import sys
import RPi.GPIO as GPIO
import time
import re
from PyQt4 import QtGui, uic, QtCore
from PyQt4.QtCore import QThread
import spidev

GPIO.setmode(GPIO.BCM) 
GPIO.setwarnings(False)

o2zero = 26
o2span = 19
cozero = 13
cospan = 6
co2zero = 5
co2span = 21

status = "nil"

light_channel = 0
spi = spidev.SpiDev()
spi.open(0,0)

class MyWindow(QtGui.QMainWindow): 

    def __init__(self):
        super(MyWindow, self).__init__()
        uic.loadUi('mainwindow.ui', self)

        self.o2_zero_upclicked.connect(self.o2zeroup) 
        self.o2_zero_down.clicked.connect(self.o2zerodown)
        self.o2_span_up.clicked.connect(self.o2spanup)
        self.o2_span_down.clicked.connect(self.o2spandown)
        self.co_zero_up.clicked.connect(self.cozeroup)
        self.co_zero_down.clicked.connect(self.cozerodown)
        self.co_span_up.clicked.connect(self.cospanup)
        self.co_span_down.clicked.connect(self.cospandown)
        self.co2_zero_up.clicked.connect(self.co2zeroup)
        self.co2_zero_down.clicked.connect( self.co2zerodown)
        self.co2_span_up.clicked.connect(self.co2spanup)
        self.co2_span_down.clicked.connect(self.co2spandown)  
        self.close_button.clicked.connect(self.gpiocleanup) 

        self.thread = O2_Channel()
        self.thread.changedValue.connect(self.onChangeValue)
        self.thread.start()

        self.show()

    def onChangeValue(self, values):
        light_level, light_volts = values

        print "--------------------------------------------"
        print("Light: {} ({}V)".format(light_level,light_volts))


    def o2zeroup(self):  
        GPIO.setup(o2zero, GPIO.OUT)   
        GPIO.output(o2zero, 1) 
    def o2zerodown(self): 
        GPIO.setup(o2zero, GPIO.OUT) 
        GPIO.output(o2zero, 0) 

    def o2spanup(self):  
        GPIO.setup(o2span, GPIO.OUT)  
        GPIO.output(o2span, 1) 
    def o2spandown(self): 
        GPIO.setup(o2span, GPIO.OUT) 
        GPIO.output(o2span, 0) 

    def cozeroup(self):  
        GPIO.setup(cozero, GPIO.OUT)  
        GPIO.output(cozero, 1) 
    def cozerodown(self):
        GPIO.setup(cozero, GPIO.OUT)  
        GPIO.output(cozero, 0) 

    def cospanup(self):
        GPIO.setup(cospan, GPIO.OUT)   
        GPIO.output(cospan, 1) 
    def cospandown(self): 
        GPIO.setup(cospan, GPIO.OUT) 
        GPIO.output(cospan, 0)

    def co2zeroup(self):
        GPIO.setup(co2zero, GPIO.OUT)   
        GPIO.output(co2zero, 1) 
    def co2zerodown(self): 
        GPIO.setup(co2zero, GPIO.OUT) 
        GPIO.output(co2zero, 0)

    def co2spanup(self):
        GPIO.setup(co2span, GPIO.OUT)   
        GPIO.output(co2span, 1) 
    def co2spandown(self): 
        GPIO.setup(co2span, GPIO.OUT) 
        GPIO.output(co2span, 0)   

    def gpiocleanup(self): 
        GPIO.cleanup() 

    def closeEvent(self, event):
        self.thread.stop()
        self.thread.quit()
        self.thread.wait()
        self.thread.deleteLater() 
        print ("GPIO CleanUP")
        GPIO.cleanup() 
        event.accept()   

def ReadChannel(channel):
    adc = spi.xfer2([1,(8+channel)<<4,0])
    data = ((adc[1]&3) << 8) + adc[2]
    return data

def ConvertVolts(data, places):
    volts = (data * 3.3) / float(1023)
    volts = round(volts,places)
    return volts    

class O2_Channel(QtCore.QThread):
    changedValue = QtCore.pyqtSignal(tuple)
    def __init__(self):
        QtCore.QThread.__init__(self)
        self.mRunning = True

    def run(self):
        while self.mRunning:

            light_level = ReadChannel(light_channel)
            light_volts = ConvertVolts(light_level,2)

            print "--------------------------------------------"
            print("Light: {} ({}V)".format(light_level,light_volts))
            self.changedValue.emit((light_level, light_volts))
            light_channel = 0
            temp_channel  = 1
            delay = .3                         
            time.sleep(delay)

    def stop(self):
        self.mRunning = False

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MyWindow()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM