L'Hexapod: Pulse width modulation for servo position control

Previously published

This article was previously published on lhexapod.com as part of my journey of discovery into robotics and embedded assembly programming. A full index of these articles can be found here.

First some basics: servo motors are what I’ll be using to provide movement for the robot. Each leg will consist of at least three servo motors (1 at the knee and two at the hip). As you’ll see from the wikipedia link above, servo motors are generally controlled by pulse width modulation. In order to control multiple servos you need to generate a continuous PWM control signal for each servo.

Many modern microcontrollers have built in PWM generation capabilities but the number of channels is limited. For example the Atmel AVR ATtiny2313 has 4 PWM channels available. For a hexapod robot with ‘standard design’ legs each utilising 3 servo motors we need 18 channels. If we want to add a movable head (for sensor targeting, or mandibles) then we could add another 3 servos at least, the list goes on. However, we don’t have to use the built in PWM facilities of the microcontroller, we could generate our own PWM signals and use any available I/O pin to output the signal; this means we can generate as many channels as the microprocessor has suitable I/O pins… This method is demonstrated here on a, now obsolete, Atmel AT90S4414. An alternative is to multiplex the available PWM channels to create the required amount of channels, we can do this because hobby servos generally require at most a 2.1ms pulse length with a 50Hz (20ms) refresh. Depending on the clock speed of your microcontroller the PWM signals you can generate are probably much faster than are required for a single servo, so by adding a 74xx138 to the output of each PWM channel from the micro and explicitly switching their channels and the pulse we’re generating during the micro’s PWM cycle we can create a number of PWM channels running at around 50Hz from a single PWM pin on our micro that runs at a faster refresh… Or so the theory goes.

The current plan for servo control is to multiplex an ATtiny2313’s 4 PWM channels using multiplexers into a number of channels. Each micro should be controllable via a TTL serial interface from another controller (or a PC) and, since, potentially we’d need multiple ATtiny’s, the serial link should be transparently chainable (that is each micro should only act on requests directed to it and should ignore all others…).

And then we add another micro that talks to these servo controllers and deals with walking and the required gait to move the legs…

Of course, to get things going, I’m quite happy to use someone else’s servo controller to get things moving, so to speak…