16 Channel Servo Driver - travel speed

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
d0ctr
 
Posts: 1
Joined: Sat Jan 12, 2013 6:42 pm

16 Channel Servo Driver - travel speed

Post by d0ctr »

Hey guys,

I just got the 16 Channel Servo Driver a couple days ago, everything's working fine.. I'm on the way to replace my custom build SPI Servo Drivers on my humanoid robot with this I2C one because I decided to let a RPi run the bot via Python, etc. this just makes everything easier, no compiling/flashing/etc. anymore. Just code, run the .py file and done.

But I'm stuck with two things, also I'm new to python.

#1: pwm.setPWM(servoChannel, 0, endPosition)
I didn't really study the library, but what the heck is the second argument (where I placed the 0) for? For me it looks like an offset?

#2: Travel speed
As I'm new to python and to adafruits lib, is there any function to control the speed of travel? For example I want to let the servo travel to it's end position in about 30 seconds, how could I do it? If I have to write the function does anybody have an idea for me? I don't want the whole code! I want to do this on my own but I need an idea how to start. I appreciate every help!


Cheers
d0ctr

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: 16 Channel Servo Driver - travel speed

Post by adafruit_support_bill »

The library documentation is here: http://learn.adafruit.com/adafruit-16-c ... -reference

To control the servo sweep speed, you need to write a loop that incrementally updates the servo position over time. For example, to sweep 180 degrees in 30 seconds, you could write a loop that increments the position by 3 degrees every 500 milliseconds.

bennakhi
 
Posts: 6
Joined: Sat Nov 09, 2013 2:33 am

Re: 16 Channel Servo Driver - travel speed

Post by bennakhi »

adafruit_support_bill wrote:The library documentation is here: http://learn.adafruit.com/adafruit-16-c ... -reference

To control the servo sweep speed, you need to write a loop that incrementally updates the servo position over time. For example, to sweep 180 degrees in 30 seconds, you could write a loop that increments the position by 3 degrees every 500 milliseconds.
could you post a python code example ?

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: 16 Channel Servo Driver - travel speed

Post by adafruit_support_mike »

This script will sweep the control pulses from the lowest to highest value and back again:

Code: Select all

#!/usr/bin/python

from Adafruit_PWM_Servo_Driver import PWM
import time

# ===========================================================================
# Example Code
# ===========================================================================

# Initialise the PWM device using the default address
pwm = PWM(0x40, debug=True)

servoMin  = 150  # Min pulse length out of 4096
servoMax  = 600  # Max pulse length out of 4096
sweepTime = 30   # We want the sweep to last 30 seconds
stepSize  = 10   # We'll change the pulse by a small amount each time

steps     = (servoMax - servoMin) / stepSize    # Calculate the number of steps we'll need
stepTime  = sweepTime / float( steps )          # Calculate the delay between changes

#  The calculation for stepTime looks a bit strange because of the way Python
#  does math.  If you divide an integer by another integer, it will round the
#  result down to the nearest integer.  If you want a floating point value, you
#  need to include a floating point number in the calculation.  The `float()`
#  function forces the intepreter to treat `steps` as a floating point value.

pwm.setPWMFreq(60)                                # Set frequency to 60 Hz

for t in range( 0, 2*steps ):                     # Make the cycle long enough for a sweep up and back
    if t < steps:
        n = t                                     # For the first half of the cycle, count up
    else:
        n = (2 * steps) - t                       # For the second half of the cycle, count down

    pwm.setPWM( 0, 0, servoMin + (n * stepSize) ) # Make the pulse width depend on the count
    time.sleep( stepTime )                        # Wait before taking the next step

bennakhi
 
Posts: 6
Joined: Sat Nov 09, 2013 2:33 am

Re: 16 Channel Servo Driver - travel speed

Post by bennakhi »

adafruit_support_mike wrote:This script will sweep the control pulses from the lowest to highest value and back again:

Code: Select all

#!/usr/bin/python

from Adafruit_PWM_Servo_Driver import PWM
import time

# ===========================================================================
# Example Code
# ===========================================================================

# Initialise the PWM device using the default address
pwm = PWM(0x40, debug=True)

servoMin  = 150  # Min pulse length out of 4096
servoMax  = 600  # Max pulse length out of 4096
sweepTime = 30   # We want the sweep to last 30 seconds
stepSize  = 10   # We'll change the pulse by a small amount each time

steps     = (servoMax - servoMin) / stepSize    # Calculate the number of steps we'll need
stepTime  = sweepTime / float( steps )          # Calculate the delay between changes

#  The calculation for stepTime looks a bit strange because of the way Python
#  does math.  If you divide an integer by another integer, it will round the
#  result down to the nearest integer.  If you want a floating point value, you
#  need to include a floating point number in the calculation.  The `float()`
#  function forces the intepreter to treat `steps` as a floating point value.

pwm.setPWMFreq(60)                                # Set frequency to 60 Hz

for t in range( 0, 2*steps ):                     # Make the cycle long enough for a sweep up and back
    if t < steps:
        n = t                                     # For the first half of the cycle, count up
    else:
        n = (2 * steps) - t                       # For the second half of the cycle, count down

    pwm.setPWM( 0, 0, servoMin + (n * stepSize) ) # Make the pulse width depend on the count
    time.sleep( stepTime )                        # Wait before taking the next step
thanks for your fast response

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”