运行时错误,此GPIO通道已经存在PWM对象

问题描述 投票:0回答:1

是树莓派的新手,在对象检测脚本中运行伺服电机时出现错误

这是代码的一部分,所有导入功能均在代码顶部给出

for i in range (classes.size):
    if(classes[0][i] == 2 and scores[0][i]>0.5):

      servoPIN = 22
      GPIO.setmode(GPIO.BCM)
      GPIO.setup(servoPIN, GPIO.OUT)
      p = GPIO.PWM(servoPIN, 50)  #line 150
      p.start(2.5) # Initialization
      try:

        p.ChangeDutyCycle(5)
        time.sleep(4)
        p.ChangeDutyCycle(10)
        time.sleep(4)
      except KeyboardInterrupt:
        p.stop()
      except:
          #print ("exception")

        GPIO.cleanup()

输出:(电动机打开并立即显示以下错误)

Traceback (most recent call last):
  File "Object_detection_picamera.py", line 150, in <module>
    p = GPIO.PWM(servoPIN, 50) # GPIO 17 for PWM with 50Hz
RuntimeError: A PWM object already exists for this GPIO channel

我不知道为什么会这样,请帮帮我

python raspberry-pi3 gpio
1个回答
0
投票

p = GPIO.PWM(servoPIN,50)#行150

此行永远不会循环PWM通道保持仅在

上初始化

因此只需从循环中删除该行,然后将其放在循环前

© www.soinside.com 2019 - 2024. All rights reserved.