Let's Make Robots!

Biped Howto: Moving servos

+++ UPDATE (12-march-2014): Rename title as "Biped Howto".

Hi, I will try to explain, in a very simplistic manner, the way in which servo motors can be controlled.

Servo motors are controlled by a PWM signal, in which the active part of the signal (known as duty cycle) define the amount of degrees the servo will rotate.

PWM signals use to have a period between 15ms and 25ms. In my case, this period is 20msDepending on the servo type, and after a bit of calibrationproportional relation between the target angle  and the PWM duty cycle can be found  

Once this relation is found, I can program a macro conversion between both variables:


The main problem controlling this kind of robots, is the high number of servos. In my case there are 10 degrees of freedomwhich implies 10 servo motorsBy default, a single microprocessor doesn't have enough peripherals to manage all these servos, and you must use an external driver. In my case, I found the part PCA9685 in Adafruit (https://www.adafruit.com/products/815). 

 This controller can manage up to 16  PWM channels independently through an I2C bus channel... a perfect solution! 


You can download the attached file <PCA9685_ServoDrv.rar> with the source code of this driver. You can use it with your mbed board or port it to your own platform. 


PCA9685_ServoDrv.zip5.28 KB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Yes, all of you ate right. But I've just only want to tell the way in which I solve the way to drive those 10 servos in a simplistic manner just using one I2C channel, available in common microcontrollers.

"Duty cycle..."

Pick that nit!

Comeon guys you're being a little picky aren't you. Anyone who has the money for digital servos should also have the wits to figure out they are controlled different to analog servos.
I don't see any problem describing the signal conditioning for them as pwm. Google servo control and your going to get whacked with a gazillion sites describing it as pwm. Just because your duty doesn't cover from 0-100 percent in a servo doesn't mean it isn't pwm.
The unfortunate part is a lot of microcontrollers can't use the hardware pwm correctly to drive servos so the signal is bitbanged using timers plus there's never enough hardware pwm pins.
And this is why I believe lord_bot has stated that they cannot drive enough servos. I can't see a controller that's driving 48 servos or even 16 servos having much time for anything else.
It's a personal choice in the end, use an extra microcontroller as a servo controller or buy a chip like the PCA9685.

Yes, servo control can technically be called PWM but I don't think it's helpful to do so. You use PWM for your motor control and pulse lengths to control your servos.

IMO, calling the servo control pulse PWM adds to newcomers' confusion. The term PWM obscures the need for pulse lengths to be precisely controlled. I've seen lots of weird servo control strategies with PWM being sent at frequencies well outside the norm. These strategies happened to work with the servos being used but if the person were to use a different servo it wouldn't.

I've seen a lot of servo lice from this nit not being picked soon enough.

It doesn't take much money to buy a digital servo. And they can be controlled just the same as an analog servo.

And since when did wits come with money? :-)


Duane, I think the pictures and his text clearly point out  the range of duty for the pulse width and off time and the variability between brands. If somebody chooses then to try values wildly different from those he suggests then maybe they have too much money?  ; )
I don't think the article is intended to be a how to use a servo type tip but rather how he has calculated the different angles in his legs project.

Ok so digital servos aren't expensive perhaps, but I was thinking along the lines of dynamixels using the serial connection.
In any case if you were to weigh how many analog servos are used in projects here against digital it would be overwhelmingly analog.

Point taken on wits != money or money != wits.

I'd avoid calling the pulses used to drive servos "PWM".

The "duty cycle" is usually defined as the percent of time the signal is high. This isn't the case with servos.

In some analog servos, the position can be controlled by the duty cycle of the signal but some digital servos can be driven at a higher rate than 50Hz. In these cases the "duty cycle" and "pulse length" will have little in common.

With a digital servo it's possible to have a signal with 10% duty cycle and a signal with 80% duty cycle both drive the servo to the same position. It's length of the high pulse that is important not the overall percentage of high time vs low time.

So with servos "pulse length" is the frequently used term used when describing the pulse to the servo.

I also think it would be a good idea to point out you're just learning this yourself and this is what you're figured out so far. As OddBot said, there are lots of microcontrollers capable of driving many servos.

As I've stated many times, a Propeller can drive 32 servos without additional components. Add some latch ICs and a Propeller can drive 144 servos (from a single cog). 

I was trying to work out if this is supposed to be a question or a tip.

Your statement is not correct. You say:

"By default, a single microprocessor doesn't have enough peripherals to manage all these servos" and yet a standard Arduino can drive 12 servos. The Arduino mega can driver 48 servos. You simply chose the wrong processor for the project.

Nit picking aside, one thing you need to be weary of is temperature. Analog servos (most common type) will have different center positions and ranges at different temperatures. If you find this affects your robot greatly then you may need to use a temperature sensor to allow your software to compensate.