简体   繁体   English

RaspberryPi 与 Python:如何停止伺服并重新启动

[英]RaspberryPi with Python: How to stop Servo and Start again

When i stop the servo after an action and want to start it again the servo is moving strange.当我在一个动作后停止伺服并想再次启动它时,伺服移动很奇怪。 The servo seems to move out of range.伺服似乎超出了范围。

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()

The servo should normally start again and make the movement.伺服应正常重新启动并进行运动。 It think it maybe sets the start position wrong and wants to move in the other direction.它认为它可能设置了错误的起始位置并想朝另一个方向移动。

Alternately, you can use my library which hides most of the pwm and GPIO board complexity.或者,您可以使用我的库,它隐藏了大部分 pwm 和 GPIO 板的复杂性。 Sample code:示例代码:

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

You can view download the code or the library via any of the two links: https://github.com/vikramdayal/RaspberryMotors or https://pypi.org/project/RaspberryMotors/#description您可以通过以下两个链接中的任何一个查看下载代码或库: https : //github.com/vikramdayal/RaspberryMotorshttps://pypi.org/project/RaspberryMotors/#description

I solved the problem not very elegant, but I didn't found any better solution.我解决的问题不是很优雅,但我没有找到更好的解决方案。 I made an extra file with all the servo moves and executed it with os.system("path to file") in the main program.我制作了一个包含所有伺服运动的额外文件,并在os.system("path to file")使用os.system("path to file")执行它。 In the separate file I initialized the servo pins and started the servo.在单独的文件中,我初始化了伺服引脚并启动了伺服。 At the end I stopped the servo and cleaned only the servo pins to not break the main program with other pins involved.最后,我停止了伺服并只清理了伺服引脚,以免与其他引脚一起破坏主程序。 The problem is that you have to set the servos back to the start position at the end of the separate file.问题是您必须将伺服器设置回单独文件末尾的起始位置。

Not sure it is correct, but, after the stop if you create again the PWM it works.不确定它是否正确,但是,如果您再次创建 PWM,它会在停止后工作。 So:所以:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM