简体   繁体   中英

control servo motor speed by raspberry pi

How to make servo motor reach the desired angle slowly using raspberry pi with python ? I tried this , but it seems not to work in all conditions .

   p = GPIO.PWM(7,50)
   p.start(7.5)
   def servo(angl) :
       try :
               angle_end = angl 
               for i in range (1,100) :
                   angle_step_to_end = (i *angle_end)/100
                   duty_cycle =(((12.5-2.5)/(180-0) * angle_step_to_end)+2.5
                   p.ChangeDutyCycle(duty_cycle)
       except KeyboardInterrupt :
                           p.stop()
                           GPIO.cleanup()

Is there something more real ?!

Typical hobby servos are designed to rotate as fast as possible to the commanded position. To get slow movement, you have to slowly change the commanded position. Without doing a lot of manual calculations, your code appears to be doing that.

Typical values are 1.5 millisecond pulse every 20 ms (7.5%) or so to "center" the servo in its arc of movement. To move to the extremes, typical values are 1.0 milliseconds at one extreme to 2.0 at the other.

Seeing as 7.5 is the midpoint, I propose you simply call p.ChangeDutyCycle() with values that (as smoothly as you want) vary between 5.0% and 10.0%. Remembering of course, that the open-loop control of hobby servos is typically governed by a very cheap potentiometer and that super-precise control is not really attainable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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