简体   繁体   English

在 raspberry pi 中使用 python 进行线程化以获取 gpio 输入

[英]Threading using python in raspberry pi for gpio inputs

Doubt in Threading in gpio pins raspberry pi对 gpio 引脚 raspberry pi 中线程的怀疑

my self working on encoders of differential drive robot, i need to read the speed and podition of wheels using two encoders simultaneously.我自己在差动驱动机器人的编码器上工作,我需要同时使用两个编码器读取轮子的速度和位置。 I have used threading library in python even though i started both the functions only the function which has been initiated at first runs while the second doesn't.我在 python 中使用了线程库,尽管我只启动了两个函数 function,它在第一次运行时启动,而第二次运行时没有启动。 I am not sure what the resson is, is there anything to be noted while using threading in gpio pins of raspberry pi 3b, if so please help and suggest a solution.我不确定resson是什么,在raspberry pi 3b的gpio引脚中使用线程时是否有任何需要注意的地方,如果有请帮助并提出解决方案。

Thanks in advance提前致谢

from threading import Thread
import RPi.GPIO as GPIO
import time
import datetime 

def encoder_right(t1):
  
   ...
        
def encoder_left(t1):
    
    
   ...

t1 = datetime.datetime.now()

Thread1 = Thread(target = encoder_right(t1),daemon = True)

Thread2 = Thread(target = encoder_left(t1),daemon = True)

Thread1.start()

Thread2.start()

Thread1.join()

Thread1.join()

When you create a new threading.Thread object, you need to pass a function to the target parameter.当你新建一个threading.Thread object时,你需要传递一个function给target参数。 In your code, instead of passing the function object, you are calling the function. That is, when you write:在您的代码中,不是传递 function object,而是调用function。也就是说,当您编写:

Thread1 = Thread(target=encoder_right(t1), daemon=True)

You are calling encoder_right(t1) ;您正在调用encoder_right(t1) your code never gets beyond this point because you've entered an infinite loop.你的代码永远不会超出这一点,因为你已经进入了一个无限循环。 You never create a thread and you never reach the lines after this code.您永远不会创建线程,也永远不会到达此代码之后的行。

You want to write instead:你想写:

Thread1 = Thread(target=encoder_right, args=(t1,), daemon=True)
Thread2 = Thread(target=encoder_left, args=(t1,), daemon=True)

This way you're passing the function object in the target parameter, and the Thread object will take care of starting the function in a new thread.这样,您将在target参数中传递 function object, Thread object 将负责在新线程中启动 function。


Unrelated to your question, but you only need to call GPIO.setmode(GPIO.BCM) and GPIO.setwarnings(False) once in your program;与您的问题无关,但您只需要在程序中调用GPIO.setmode(GPIO.BCM)GPIO.setwarnings(False)一次; you don't need to call that in each thread.您不需要在每个线程中调用它。 That is, you could write instead:也就是说,您可以这样写:

def encoder_right(t1):
    ...


def encoder_left(t1):
    ...

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

t1 = datetime.datetime.now()

Thread1 = Thread(target=encoder_right, args=(t1,), daemon=True)
Thread2 = Thread(target=encoder_left, args=(t1,), daemon=True)

Thread1.start()
Thread2.start()

Thread1.join()
Thread2.join()

Also.还。 I would avoid giving variables the same name as your functions (eg,, in your function encoder_left you also have a variable named encoder_left ).我会避免为变量提供与您的函数相同的名称(例如,在您的 function encoder_left中,您还有一个名为encoder_left的变量)。 At some point this is going to cause a surprising problem when you try to reference the function and find that you're actually referencing an integer value instead.在某些时候,当您尝试引用 function 并发现您实际上引用的是 integer 值时,这会导致令人惊讶的问题。

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

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