概要
それまで動いていたgpiozero
のプログラムが動かなくなったので修正した.
環境
- Debian GNU/Linux 12
- Python 3.11.2
- gpiozero 2.0
raspi@raspi:~ $ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 12 (bookworm)
Release: 12
Codename: bookworm
raspi@raspi:~ $ python --version
Python 3.11.2
raspi@raspi:~ $ pip list | grep gpiozero
gpiozero 2.0
問題
下のようなプログラムを作成し,実行した.
PWM_org.py
import gpiozero
import sys
from time import sleep
pin = gpiozero.PWMOutputDevice(pin=17, frequency=1000)
while True:
try:
for i in range(0, 11, 1):
pin.value = i / 10
sleep(1)
except:
pin.close()
sys.exit()
すると下のようなエラーが発生した.
raspi@raspi:~ $ python3 PWM_org.py
/usr/lib/python3/dist-packages/gpiozero/devices.py:295: PinFactoryFallback: Falling back from lgpio: 'can not open gpiochip'
warnings.warn(
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 408, in pin
pin = self.pins[info]
~~~~~~~~~^^^^^^
KeyError: PinInfo(number=11, name='GPIO17', names=frozenset({17, '17', 'BOARD11', 'J8:11', 'BCM17', 'WPI0', 'GPIO17'}), pull='', row=6, col=1, interfaces=frozenset({'', 'gpio', 'spi', 'dpi', 'uart'}))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/raspi/PWM_org.py", line 5, in <module>
pin = gpiozero.PWMOutputDevice(pin=17, frequency=1000)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 103, in __call__
self = super().__call__(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/gpiozero/output_devices.py", line 392, in __init__
super().__init__(pin, active_high=active_high, initial_value=None,
File "/usr/lib/python3/dist-packages/gpiozero/output_devices.py", line 74, in __init__
super().__init__(pin, pin_factory=pin_factory)
File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 75, in __init__
super().__init__(*args, **kwargs)
File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 549, in __init__
pin = self.pin_factory.pin(pin)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 410, in pin
pin = self.pin_class(self, info)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 101, in __init__
GPIO.setup(self._number, GPIO.IN, self.GPIO_PULL_UPS[self._pull])
RuntimeError: Cannot determine SOC peripheral base address
Falling back from lgpio: 'can not open gpiochip'
このエラーが気になる.
調べたこと
調べてみると同じ現象が発生している模様.
https://raspberrypi.stackexchange.com/questions/148946/lgpio-can-not-open-gpiochip-lgpio-seems-unable-to-open-gpiochip-devices
下のコードを追加することで解決できるとのこと.
import gpiozero.pins.lgpio
import lgpio
def __patched_init(self, chip=None):
gpiozero.pins.lgpio.LGPIOFactory.__bases__[0].__init__(self)
chip = 0
self._handle = lgpio.gpiochip_open(chip)
self._chip = chip
self.pin_class = gpiozero.pins.lgpio.LGPIOPin
gpiozero.pins.lgpio.LGPIOFactory.__init__ = __patched_init
(上記コードは https://github.com/gpiozero/gpiozero/issues/1166 より引用)
結論
PWM_org.py
を下のように変更すればよい.
PWM_mod.py
import gpiozero
import sys
from time import sleep
# 追加したコード(ここから)
import gpiozero.pins.lgpio
import lgpio
def __patched_init(self, chip=None):
gpiozero.pins.lgpio.LGPIOFactory.__bases__[0].__init__(self)
chip = 0
self._handle = lgpio.gpiochip_open(chip)
self._chip = chip
self.pin_class = gpiozero.pins.lgpio.LGPIOPin
gpiozero.pins.lgpio.LGPIOFactory.__init__ = __patched_init
# 追加したコード(ここまで)
pin = gpiozero.PWMOutputDevice(pin=17, frequency=1000)
while True:
try:
for i in range(0, 11, 1):
pin.value = i / 10
sleep(1)
except:
pin.close()
sys.exit()