python - RaspberryPi 与 Python:如何停止伺服并重新启动
问题描述
当我在一个动作后停止伺服并想再次启动它时,伺服运动奇怪。伺服似乎超出范围。
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()
伺服应该正常重新启动并进行运动。它认为它可能设置了错误的起始位置并想朝另一个方向移动。
解决方案
或者,您可以使用我的库,它隐藏了大部分 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