试图在Python的Pi 4上进行罐头对抗,并用同时运行的两个传感器的代码束缚

发布于 2025-01-23 07:42:17 字数 2190 浏览 0 评论 0原文

因此,我在饮料行业工作,我决定尝试使用Raspberry Pi4进行罐头反击。它需要在检测罐的GPIO上使用两个工业传感器。

全面披露我只是在搜索大多数代码并阅读终端错误以尝试解决该问题时阅读。我做了一些基本的C#和C ++编程,从而做PLC工作,但这与我现在想做的事情不像我现在要做的事情。只是一些简单的转换和公式陈述。

我已经通过传感器计算出非常基本的代码,

import RPi.GPIO as GPIO
import time

GPIN = 16
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN, GPIO.IN)

counting = 0

while True:
    while GPIO.input(GPIN) == 0:
        time.sleep(0.1)

    counting = counting + 1
    print(counting)


    while GPIO.input(GPIN) == 1:
        time.sleep(0.1)

这在终端中计数。值得注意的是,我需要以略有延迟来计数和关闭状态,以防止意外的双重计数发生。我什至添加了与吉泽尔(Guizero)的GUI,使其在窗口中计数。尽管目前我无法从我记得的工作中复制它,而我愚蠢地没有保存它,因为我试图进入下一阶段app.display()信息。

问题是我需要同时计数2个传感器,一个在罐头拒绝之前,然后再计算一个传感器。因此,我进行了一些阅读,并认为我需要同时运行两个(或3)个循环。由于我有2个需要恒定循环的传感器,而且似乎需要另一个循环来运行和刷新GUI。我变成了线程,并且一直在尝试实施它,因为这似乎是我想要的,但无法制作头部或尾巴。我可以让GUI显示,但是传感器不读取。如果我切换回简单的代码,它将算了。我很难将两者融合在一起。

import threading
from guizero import App, Text, PushButton
import RPi.GPIO as GPIO
import time

GPIN1 = 16
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN1, GPIO.IN)

GPIN2 = 15
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN2, GPIO.IN)


counting1 = 0
counting2 = 0
counting3 = counting1 - counting2

def sensor1():
    global counting1
    while GPIO.input(GPIN1) == 0:
        time.sleep(0.1)                  
    counting1 = counting1 + 1    
    while GPIO.input(GPIN1) == 1:
        time.sleep(0.1)

def sensor2():
    global counting2
    while GPIO.input(GPIN2) == 0:
        time.sleep(0.1)                  
    counting2 = counting2 + 1    
    while GPIO.input(GPIN2) == 1:
        time.sleep(0.1)

x = threading.Thread(target=sensor1)
y = threading.Thread(target=sensor2)
x.start()
y.start()

while True:
        app = App(title="Can Count")
        message = Text(app, text="Total")
        message = Text(app, text=(counting1))
        message = Text(app, text="Rejected")
        message = Text(app, text=(counting3))
        app.display()

我只是有点困难,我敢肯定,我的方式不是最好的方法,朝着正确方向的任何建议,技巧或指针都将不胜感激。我正在尝试将YouTube Python教程倒入侧面,但我仍然很短。

如果我通过X关闭窗口,它可以显示显示更新,它将重新启动窗口并显示更新,但我使用def text():上方的代码和文本尝试了Guizero尝试了一些不同的内容。重复(10,文本)认为这会重新绘制屏幕,​​但这不起作用或打破GUI或代码。

我也知道我叫pushbutton,不要使用它,但是最终目标将简单地重置计数器按钮。

So I work in the beverage industry and I decided to try and make a can counter using a Raspberry PI4. It needs to use two industrial sensors on the GPIO that detect the cans.

Full Disclosure I have just been googling most code and reading when I get errors in terminal to try and fix the issue. I have done some rudimentary C# and C++ programming doing PLC stuff but it's nothing like what i'm trying to do right now. Just some simple statements for conversions and formulas.

I have had it counting via the sensor on a very rudimentary code

import RPi.GPIO as GPIO
import time

GPIN = 16
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN, GPIO.IN)

counting = 0

while True:
    while GPIO.input(GPIN) == 0:
        time.sleep(0.1)

    counting = counting + 1
    print(counting)


    while GPIO.input(GPIN) == 1:
        time.sleep(0.1)

This counts in the terminal. It is of note I need to count the on and off state with a slight delay to keep accidental double counts from happening. I have even added in a GUI with guizero that makes it count in a window. although currently I cannot replicate that from what I remember working and i foolishly didn't save that as i was trying to get to the next stage, but the rough of it was instead of the print(counting) section in the above code I had the app.display() info.

Problem is I need it to count 2 sensors at the same time, one before the can rejector and one after. So I did some reading and figured I needed to run two (or maybe 3) loops at the same time. as I have 2 sensors that need a constant loop, plus it seems like a need another loop that runs and refreshes the GUI. I got turned into threading and have been trying to implement it as that seems like what I want but haven't been able to make heads or tails of it. I can get the GUI to display, but the sensors don't read. If I switch back to my simple code it counts away. Im having trouble meshing the two together.

import threading
from guizero import App, Text, PushButton
import RPi.GPIO as GPIO
import time

GPIN1 = 16
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN1, GPIO.IN)

GPIN2 = 15
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN2, GPIO.IN)


counting1 = 0
counting2 = 0
counting3 = counting1 - counting2

def sensor1():
    global counting1
    while GPIO.input(GPIN1) == 0:
        time.sleep(0.1)                  
    counting1 = counting1 + 1    
    while GPIO.input(GPIN1) == 1:
        time.sleep(0.1)

def sensor2():
    global counting2
    while GPIO.input(GPIN2) == 0:
        time.sleep(0.1)                  
    counting2 = counting2 + 1    
    while GPIO.input(GPIN2) == 1:
        time.sleep(0.1)

x = threading.Thread(target=sensor1)
y = threading.Thread(target=sensor2)
x.start()
y.start()

while True:
        app = App(title="Can Count")
        message = Text(app, text="Total")
        message = Text(app, text=(counting1))
        message = Text(app, text="Rejected")
        message = Text(app, text=(counting3))
        app.display()

I'm just a bit stumped I'm sure my way isn't the best way to do this, any advice, tips or pointers in the right direction would be appreciated. I'm trying to crash course youtube python tutorials on the side but I am still coming up short.

It seems like I can get the display to show updates if i close the window via the x it restarts the window and shows the update but I have tried a few different things with guizero using a def text(): above that code and text.repeat(10, text) thinking this would redraw the screen but that doesn't work or breaks the gui or the code.

Also I know I call PushButton and don't use it, but the end goal will have a simple reset the counter button.. Just haven't got there yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文