Raspberry pi servo doesn't stop

2019-07-24 23:58发布

So I'm trying to use a servo (Doman s0306d) with the pi camera, tried running this script I found to test the motor, it starts running but doesn't stop unless I manually unplug it from the breadboard.

import RPi.GPIO as IO        # calling for header file for GPIO’s of PI
import time                           # calling for time to provide delays in program
IO.setwarnings(False)          # do not show any warnings
IO.setmode (IO.BCM)            # programming the GPIO by BCM pin numbers. (like PIN29 as‘GPIO5’)
IO.setup(19,IO.OUT)             # initialize GPIO19 as an output
p = IO.PWM(19,50)              # GPIO19 as PWM output, with 50Hz frequency
p.start(7.5)                             # generate PWM signal with 7.5% duty cycle
time.sleep(4)
for x in range(0,5):                                                       # execute loop forever                                    
        p.ChangeDutyCycle(7.5)                   # change duty cycle for getting the servo position to 90º
        time.sleep(1)                                      # sleep for 1 second
        p.ChangeDutyCycle(12.5)                  # change duty cycle for getting the servo position to 180º
        time.sleep(1)                                     # sleep for 1 second
        p.ChangeDutyCycle(2.5)                  # change duty cycle for getting the servo position to 0º
        time.sleep(1)                                    # sleep for 1 second
p.ChangeDutyCycle(0)
p.stop()
IO.cleanup()

Any ideas ? Thank you.

1条回答
欢心
2楼-- · 2019-07-25 00:19

[EDIT] The servo you are using is a "continuous" servo - so you need to give it the zero speed or "STOP" setting pulse width of 1500us (according to the website http://www.domanrchobby.com/content/?150.html). I haven't used PWM on the pi but if the percentages are of the 50Hz pulse rate (20ms interval) then that should be your 7.5% centre value. You need to make sure the servo gets that pulse before your code exits.

[Original] You are setting the duty cycle to 0 when you exit, which will probably mean the servo doesn't get any pulses. Some servos will stop sometime after they don't get pulses, but some servos (particularly digital servos, but not all) will continue to try achieve the setting from the last pulse they received. Suggest you leave the setting at the mid range 7.5 which you know the servo can achieve and delay for a little while before cleaning up.

查看更多
登录 后发表回答