Adafruit_BBIO and continuous servo

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
lzemail
 
Posts: 52
Joined: Tue May 29, 2012 5:00 pm

Adafruit_BBIO and continuous servo

Post by lzemail »

I am trying to control a continuous servo (turn clockwise, counter-clockwise and stop). I am able to make the servo stop and turn it in one direction by using different duty cycles, e.g.
PWM.set_duty_cycle("P9_14", 0) -- stops the servo
PWM.set_duty_cycle("P9_14", 1) -- turns the servo to one direction (other values turns the servo to the same direction)

How do I make the servo to turn the other direction? Where / how the "duty cycle" in BBIO library is calculated to correspond to the pwm parameters, e.g. with a 50 Hz frequency, 50% duty cycle (1.5 ms) will put the servo in neutral (stop) position, etc ? I am not knowledgeable enough to understand the BBIO library source code.

Any help would be appreciated.

User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: Adafruit_BBIO and continuous servo

Post by tdicola »

Duty cycle is actually a percent value from 0.0 to 100.0, including any fractional values inbetween like 25.55, etc. You can figure out the pulse width by looking at both the duty cycle and and frequency of the PWM channel. In particular for a servo signal you want each signal to be 20ms long, so 1/20ms equals a frequency of 50 hertz. The duty cycle will then control what percent of the 20ms signal is held at a high level vs. low level. So for example a duty cycle of 20% would be a signal that's 20% * 20ms = 4ms high and 80% * 20ms = 16ms low.

For a continuous rotation servo you typically want a pulse 1.5ms high for the center/not moving position, so working backward you could compute 1.5ms / 20ms = 7.5% duty cycle. Each direction would then be a pulse longer or shorter than 1.5ms, up to a min and max of 1ms and 2ms. So to go as fast as possible in one direction use a duty cycle of 1.0ms / 20ms = 5%, or as fast as possible in the other direction use 2.0ms / 20ms = 10%. Any value in between 5% - 10% will control the speed of the servo in each direction, and remember 7.5% should be a dead stop. Does that help clarify the usage of duty cycle?

One other thing to be aware of, you might need to invert the polarity of your servo signal on the BeagleBone Black as some folks saw in this thread.. Try sending a duty cycle of 5% to have it move quickly in one direction, and if that doesn't work then try the invert polarity flag when creating the PWM object that I mention in the guide here: https://learn.adafruit.com/controlling- ... on-console

User avatar
lzemail
 
Posts: 52
Joined: Tue May 29, 2012 5:00 pm

Re: Adafruit_BBIO and continuous servo

Post by lzemail »

Thank you for the detailed explanation.
1. Tried duty cycle = 5, 7.5 and 10, plus some other values --- all of them turn the servo in one direction. Zero (0) duty cycle stops the servo.
2. Tried polarity = 1, which makes the servo turns much slower than polarity =0, only in one direction.

Maybe I am doing something wrong. Python code attached.
Thanks.

Code: Select all

# Test continuous rotation servo with Adafruit_BBIO library
# learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black/pwm
# Different duty cycles to control servo: stop, clockwise, counterclockwise

import time
import Adafruit_BBIO.PWM as PWM
#PWM.start(channel, duty, freq=2000, polarity=0)
PWM.start("P9_14", 0, 2000, 0)

try:
    while True:
        duty_cycle = float (raw_input("duty cycle = "))# enter duty cycle
        PWM.set_duty_cycle("P9_14", duty_cycle) 
        time.sleep(1) # servo run for 1 sec
        PWM.set_duty_cycle("P9_14", 0) # stop servo

except KeyboardInterrupt:
    PWM.stop("P9_14")
    PWM.cleanup()

User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: Adafruit_BBIO and continuous servo

Post by tdicola »

Try setting the frequency to 50hz. Right now the code is using 2000hz which is pretty fast for a servo, it equals a pulse width of 0.5ms when servos are really designed for a 20ms pulsewidth (frequency of 50hz). The code would look like:

Code: Select all

# Test continuous rotation servo with Adafruit_BBIO library
# learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black/pwm
# Different duty cycles to control servo: stop, clockwise, counterclockwise

import time
import Adafruit_BBIO.PWM as PWM
#PWM.start(channel, duty, freq=50, polarity=0)
PWM.start("P9_14", 0, 50, 0)

try:
    while True:
        duty_cycle = float (raw_input("duty cycle = "))# enter duty cycle
        PWM.set_duty_cycle("P9_14", duty_cycle) 
        time.sleep(1) # servo run for 1 sec
        PWM.set_duty_cycle("P9_14", 0) # stop servo

except KeyboardInterrupt:
    PWM.stop("P9_14")
    PWM.cleanup()

User avatar
lzemail
 
Posts: 52
Joined: Tue May 29, 2012 5:00 pm

Re: Adafruit_BBIO and continuous servo

Post by lzemail »

That frequency change fixed the problem. Sorry, it's my mistake. I thought the frequency value needs to be millisecond, somehow "2000" looked like "20 ms" for me. Thank you very much for pointing out my mistake. Working code attached.

Code: Select all

 
   # Test continuous rotation servo with Adafruit_BBIO library
    # learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black/pwm
    # Different duty cycles to control servo: stop, clockwise, counterclockwise

    import time
    import Adafruit_BBIO.PWM as PWM
    #PWM.start(channel, duty, freq=50, polarity=0)
    PWM.start("P9_14", 7.5, 50, 0)

    try:
        while True:
            duty_cycle = float (raw_input("duty cycle = "))# enter duty cycle
            PWM.set_duty_cycle("P9_14", duty_cycle)
            time.sleep(1) # servo run for 1 sec
            PWM.set_duty_cycle("P9_14", 7.5) # stop servo

    except KeyboardInterrupt:
        PWM.stop("P9_14")
        PWM.cleanup()

User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: Adafruit_BBIO and continuous servo

Post by tdicola »

No problem, glad to hear it's working now.

Locked
Please be positive and constructive with your questions and comments.

Return to “Beagle Bone & Adafruit Beagle Bone products”