vsergeev / python-periphery

A pure Python 2/3 library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux.
MIT License
519 stars 139 forks source link

Question about PWM channel and pin #24

Closed CharlesCCC closed 4 years ago

CharlesCCC commented 5 years ago

Here I was trying to use this library for the Coral EdgeTPU board, I'm having such difficulty to get the PWM to work. I have post a question here.

Here is my testing code:

from periphery import PWM
import time

# Open PWM channel 0, pin 0
pwm = PWM(0,0)

# Set frequency to 1 kHz
pwm.frequency = 50
# Set duty cycle to 75%
pwm.duty_cycle = 0.02

pwm.enable()

print(pwm.period)
print(pwm.frequency)
print(pwm.enabled)

# Change duty cycle to 50%

pwm.duty_cycle = 0.05

pwm.close()

Problem is this part:

# Open PWM channel 0, pin 0
pwm = PWM(0,0)

I can see output when running PWM(0,0) PWM(0,1) PWM(0,2)

but I get the error messsage when trying to run the following:

PWM(1,1)

PWM(2,2)

mendel@elusive-jet:/sys/class/pwm$ sudo python3 /usr/lib/python3/dist-packages/edgetpuvision/testPWM.py
OSError: [Errno 19] No such device

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/periphery/pwm.py", line 69, in _open
    f_export.write("%d\n" % pin)
OSError: [Errno 19] No such device

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/edgetpuvision/testPWM.py", line 5, in <module>
    pwm = PWM(1,1)
  File "/usr/local/lib/python3.5/dist-packages/periphery/pwm.py", line 44, in __init__
    self._open(channel, pin)
  File "/usr/local/lib/python3.5/dist-packages/periphery/pwm.py", line 71, in _open
    raise PWMError(e.errno, "Exporting PWM pin: " + e.strerror)
periphery.pwm.PWMError: [Errno 19] Exporting PWM pin: No such device

Based off the document from both Coral and the library site: https://coral.withgoogle.com/tutorials/devboard-gpio/

https://github.com/vsergeev/python-periphery

The

PWM(1,1)

PWM(2,2)

should have worked without issue, I can see the following directories existed:

"\sys\class\pwm\pwmchip0"
"\sys\class\pwm\pwmchip1"
"\sys\class\pwm\pwmchip2"

In the python-periphery source code https://github.com/vsergeev/python-periphery/blob/master/periphery/pwm.py

it should getting the path as following:

PWM(1,1) ===> /sys/class/pwm/pwmchip1/pwm1 if pwm1 not exists, then it should call the export to generate it.

So, My main question are:

Thank you in advance.

CharlesCCC commented 5 years ago

wrong documentation, please check #26

vsergeev commented 5 years ago

See latest comment on #26.