Pioreactor / rpi_hardware_pwm

Access the hardware PWM of a Raspberry Pi
Other
58 stars 13 forks source link

rpi-hardware-pwm not working on RPi5 #14

Open niekas7 opened 5 months ago

niekas7 commented 5 months ago

Just installed a fresh new raspbian OS and set up rpi-hardware-pwm. Python version is 3.11.2. Previously before updates the my code was doing just fine. Everything worked as expected. But now that I updated it the pwm_bcm2835 doesnt show up and if I run my code it gives me an error.

from rpi_hardware_pwm import HardwarePWM
import time

# Initialize the PWM channel on chip  2, channel  1, with a frequency of  50Hz
servo = HardwarePWM(pwm_channel=0, hz=50, chip=2)

def angleToSteps(angle):
    # This function converts an angle to a duty cycle.
    # You might need to adjust the formula based on your specific servo's requirements.
    # The original code uses a correction factor of  0.88, but this might need to be adjusted for your setup.
    correctionFactor =  0.9
    dutycycle =  2.4 + (angle /  18) * correctionFactor
    print(f"angle={angle}, dutycycle={dutycycle}")
    return dutycycle

def moveServo(angle, sleepVal):
    # Change the duty cycle to move the servo to the specified angle
    servo.change_duty_cycle(angleToSteps(angle))
    time.sleep(sleepVal)  # Wait for the servo to move
    servo.change_duty_cycle(0)  # Reset the duty cycle to stop the servo

try:
    servo.start(0.0)
    moveServo(0.0, 1.5)

    isTestAngleInput = True
    isTestAngleRange = False #False

    while isTestAngleInput:
        angle = float(input('Enter angle between  0 &  180 (enter angle<0 to exit): '))
        if angle<0:
            break
        moveServo(angle, 1.5)
        #time.sleep(1)  # Wait for a second before asking for the next angle
    if isTestAngleRange:
        for x in range(180):
            moveServo(x, 0.1)

finally:
    servo.stop()  # Stop the PWM signal
    print("Servo Stop")

The error I get is

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/niekas7/Desktop/zipas/testai/hardwarePWMangle.py", line 5, in <module>
    servo = HardwarePWM(pwm_channel=0, hz=50, chip=2)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/niekas7/.local/lib/python3.11/site-packages/rpi_hardware_pwm/__init__.py", line 62, in __init__
    self.change_frequency(hz)
  File "/home/niekas7/.local/lib/python3.11/site-packages/rpi_hardware_pwm/__init__.py", line 121, in change_frequency
    self.echo(int(per), os.path.join(self.pwm_dir, "period"))
  File "/home/niekas7/.local/lib/python3.11/site-packages/rpi_hardware_pwm/__init__.py", line 78, in echo
    with open(file, "w") as f:
OSError: [Errno 22] Invalid argument
CamDavidsonPilon commented 5 months ago

See #10, specifically https://github.com/Pioreactor/rpi_hardware_pwm/issues/10#issuecomment-1998681462