当我在一次动作后停止伺服并想要再次启动它时,伺服移动得很奇怪。伺服器似乎移出了范围。
import RPi.GPIO as GPIO
import time
GPIO.setup(17, GPIO.OUT)
p = GPIO.PWM(17, 50)
p.start(2.5)
time.sleep(3)
p.ChangDutyCycle(12.5)
time.sleep(3)
p.ChangDutyCycle(2.5)
time.sleep(3)
p.stop()
p.start(2.5)
# this is not working
p.ChangDutyCycle(12.5)
p.stop()伺服装置应该正常地重新启动并进行移动。它认为它可能设置了错误的起始位置,并希望向另一个方向移动。
发布于 2019-07-22 20:37:09
我解决这个问题不是很优雅,但我没有找到更好的解决方案。我用所有的伺服动作做了一个额外的文件,并在主程序中用os.system("path to file")执行它。在单独的文件中,我初始化了伺服引脚并启动了伺服。在最后,我停止了伺服,只清理了伺服引脚,以避免破坏主程序与其他引脚有关。问题是,您必须将伺服设置回单独文件末尾的起始位置。
发布于 2021-04-03 10:56:07
或者,你可以使用我的库,它隐藏了大部分pwm和GPIO板的复杂性。示例代码:
from RaspberryMotors.motors import servos
s1 = servos.servo(11) #create the servo objects , connected to GPIO board pin #11
s1.setAngleAndWait(45) # move S1 position of 45 degrees
s1.shutdown() #will clean up the GPIO board as well您可以通过以下两个链接中的任何一个查看、下载代码或库:https://github.com/vikramdayal/RaspberryMotors或https://pypi.org/project/RaspberryMotors/#description
发布于 2020-11-26 21:19:48
不能确定它是正确的,但是,在停止后,如果您再次创建PWM,它将正常工作。所以:
p = GPIO.PWM(17, 50)
p.start(2.5)
time.sleep(3)
p.ChangDutyCycle(12.5)
p.stop()
# create it again
p = GPIO.PWM(17, 50)
p.start(12.5)
time.sleep(3)
p.ChangDutyCycle(4)
p.stop()https://stackoverflow.com/questions/56847946
复制相似问题