我正在运行 Ubuntu 22.0.4 的 Arduino Portenta x8 上制作程序。该程序的一部分有一个线程紧密轮询板上的 GPIO 引脚以读取线性执行器的编码器。我一直在为此使用 python-periphery,但我注意到在读取其他 GPIO 引脚时同时让另一个线程轮询编码器,几秒钟后我收到 TimeoutError: [Errno 110] Connection timed out。如果错误没有明确发生,我注意到有时引脚读数会导致随机值而不是输入的实际信号,就好像它们处于浮动状态一样。
我为测试这个问题写的测试代码如下:
from periphery import GPIO
import threading
def poll(gpio, num):
while True:
print("GPIO {num}: {gpio}".format(num=num, gpio=gpio.read()))
if __name__ =="__main__":
gpio0 = GPIO("/dev/gpiochip5", 0, "in")
gpio1 = GPIO("/dev/gpiochip5", 1, "in")
# creating thread
t1 = threading.Thread(target=poll, args=(gpio0,0,))
t2 = threading.Thread(target=poll, args=(gpio1,1,))
# starting thread 1
t1.start()
# starting thread 2
t2.start()
# wait until thread 1 is completely executed
t1.join()
# wait until thread 2 is completely executed
t2.join()
# both threads completely executed
print("Done!")
完整错误信息如下:
Exception in thread Thread-2 (poll):
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/periphery/gpio_cdev1.py", line 323, in read
Exception in thread Thread-1 (poll):
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/periphery/gpio_cdev1.py", line 323, in read
fcntl.ioctl(self._line_fd, Cdev1GPIO._GPIOHANDLE_GET_LINE_VALUES_IOCTL, data)
TimeoutError: [Errno 110] Connection timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
fcntl.ioctl(self._line_fd, Cdev1GPIO._GPIOHANDLE_GET_LINE_VALUES_IOCTL, data)
File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
TimeoutError: [Errno 110] Connection timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
self.run()
File "/usr/lib/python3.10/threading.py", line 953, in run
File "/usr/lib/python3.10/threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
self._target(*self._args, **self._kwargs)
File "/home/dev/wall_panels/bb_ws/src/spc_unit_tester/spc_unit_tester/polling_test.py", line 6, in poll
File "/home/dev/wall_panels/bb_ws/src/spc_unit_tester/spc_unit_tester/polling_test.py", line 6, in poll
print("GPIO {num}: {gpio}".format(num=num, gpio=gpio.read()))
File "/usr/local/lib/python3.10/dist-packages/periphery/gpio_cdev1.py", line 325, in read
print("GPIO {num}: {gpio}".format(num=num, gpio=gpio.read()))
File "/usr/local/lib/python3.10/dist-packages/periphery/gpio_cdev1.py", line 325, in read
raise GPIOError(e.errno, "Getting line value: " + e.strerror)
periphery.gpio.GPIOError: [Errno 110] Getting line value: Connection timed out
raise GPIOError(e.errno, "Getting line value: " + e.strerror)
periphery.gpio.GPIOError: [Errno 110] Getting line value: Connection timed out
Done!
我尝试在每个循环的底部添加 time.sleep() ,并测试不同的睡眠值。具有较长的延迟(如 1 秒或 0.5 秒)似乎可以修复错误,但较短的延迟(如 0.02 秒(20 毫秒))会导致相同的错误。由于我需要轮询有一个短暂的延迟,以免错过任何编码器信号,我希望找到一些其他方法或解决方法来轮询此编码器信号(执行器供参考:https://www.firgelliauto.com/blogs /tutorials/synchronous-control-of-two-optical-linear-actuators-using-an-arduino).
任何帮助或想法将不胜感激,谢谢!