User avatar
texsquirreljr
Posts: 11
Joined: Tue Jun 22, 2021 2:03 am
Location: Houston, TX
Contact: Website

Python: Robotics Servos Won't Reverse, Only Go One Direction and Stop There

Fri Sep 24, 2021 11:54 pm

I'm trying to get some Servos to work:
http://magicduino.com/Images/ItemsMedia/File/7203.pdf
And for some reason I can't get them to reverse with this script:

Code: Select all

#Okay, so a duty cycle of 2 is full charge clockwise, while 12 is full charge counter clockwise.
#As they get closer to each other they slow down. With the servo I'm currently testing 7 seems to be the part where clock wise hits close to stopping.
#Only 0 seems to get you to absolute stillness.

#When messing with the arm servos, it will only go one way, but functions like a clock and only goes 120 deg.

import RPi.GPIO as GPIO
import time

servoPin = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPin, GPIO.OUT)

p = GPIO.PWM(servoPin, 50) # GPIO 17 for PWM with 50Hz
p.start(0) # Initialization #0 seems to stop and hold it
try:
    while True:
        dutyCycle = input("Type new duty cycle as decimal number...\n") #Min Duty cycle seems to be 1.65 but is virtually identical to 2. Min is fastest with 6 being a relative slowest while not just grinding.
        p.ChangeDutyCycle(float(dutyCycle))
        input("Press enter to stop...\n")
        p.ChangeDutyCycle(0)
except KeyboardInterrupt:
    print("\nStopping and Cleaning...\n")
    p.stop()
    GPIO.cleanup()
1 through 15 seem to be all the different positions, but it only will only let me go down.
I can tell it to go to 5 then 4 then 3 etc.
but I can't tell it to go back up to 5 or 15.
Going past the scale doesn't help either as I tried a little incremental script to see where it actually moves (to see if it at least jiggled anywhere else from 1 to 100) and nothing got it to move.
I've tested with different servos and it seems to work with them, in fact the notes in the code I've included mention these and what works for them.
What am I doing wrong?
I am texsquirreljr, son of TexSquirrel and MrsTexSquirrel.
---------------------------------------------------------------------------------------------
<><

ame
Posts: 3742
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Python: Robotics Servos Won't Reverse, Only Go One Direction and Stop There

Sat Sep 25, 2021 1:14 am

Don't forget, servos use the pulse width to set the output arm position. A width of 1.5ms is typically the "centre" position, with 1ms and 2ms being the two extremes of motion.

In your code you are setting the frequency to 50Hz, which is fine, then you can calculate the duty cycle, which is a percentage, which will give you the pulse width you need.

At 50Hz the period is 20ms. Therefore a duty cycle of 7.5% will give you 1.5ms (centre), 10% will give you 2ms, and 5% will give you 1ms. Outside of that range, or even at the extreme ends of the range, your motor might start grinding. Try 1.2ms and 1.8ms as the range. For any given servo the response might be different to the typical range, and might not be centred.

You could try the pigpio library. I think it's better for servos.

User avatar
texsquirreljr
Posts: 11
Joined: Tue Jun 22, 2021 2:03 am
Location: Houston, TX
Contact: Website

Re: Python: Robotics Servos Won't Reverse, Only Go One Direction and Stop There

Sat Sep 25, 2021 1:30 am

I figured out that it was the servos I was using. All but two were made by the kit's company, only those two work back and forth.

For once it was an actual hardware malfunction and/or defect.
Last edited by texsquirreljr on Sat Sep 25, 2021 4:43 am, edited 1 time in total.
I am texsquirreljr, son of TexSquirrel and MrsTexSquirrel.
---------------------------------------------------------------------------------------------
<><

ame
Posts: 3742
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Python: Robotics Servos Won't Reverse, Only Go One Direction and Stop There

Sat Sep 25, 2021 1:35 am

Ok. Cool.

Most servos work the same, so if you pick up a couple from a local store, or Amazon, eBay, or Ali, they should behave.

Have fun!

User avatar
texsquirreljr
Posts: 11
Joined: Tue Jun 22, 2021 2:03 am
Location: Houston, TX
Contact: Website

Re: Python: Robotics Servos Won't Reverse, Only Go One Direction and Stop There

Sat Sep 25, 2021 1:43 am

ame wrote:
Sat Sep 25, 2021 1:14 am
You could try the pigpio library. I think it's better for servos.
What does it do better? I’m trying to bring my robotics home.
I am texsquirreljr, son of TexSquirrel and MrsTexSquirrel.
---------------------------------------------------------------------------------------------
<><

ame
Posts: 3742
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Python: Robotics Servos Won't Reverse, Only Go One Direction and Stop There

Sat Sep 25, 2021 1:56 am

Well it used to be that RPi.GPIO could only do PWM on the hardware PWM pin (a single pin, gpio 18), although it does software PWM on any pin now. At the time I wanted to drive several servos, so I switched to pigpio. The syntax is better too.

GPIOzero also handles servos.

It really depends on what else you want to do at the same time whether a particular library fits best, although it is worth familiarising yourself with all the options.

Return to “Troubleshooting”