16 September 2006

Servo Motor Control


Servos

Servos are DC motors with built in gearing and feedback control loop circuitry. And no motor drivers required. They are extremely popular with robot, RC plane, and RC boat builders. Most servo motors can rotate about 90 to 180 degrees. Some rotate through a full 360 degrees or more.

However, servos are unable to continually rotate, meaning they can't be used for driving wheels, unless they are modified (how to modify), but their precision positioning makes them ideal for robot legs and arms, rack and pinion steering, and sensor scanners to name a few. Since servos are fully self contained, the velocity and angle control loops are very easy to impliment, while prices remain very affordable. To use a servo, simply connect the black wire to ground, the red to a 4.8-6V source, and the yellow/white wire to a signal generator (such as from your microcontroller). Vary the square wave pulse width from 1-2 ms and your servo is now position/velocity controlled.


PWM

Pulse width modulation (PWM) is a powerful technique for controlling analog circuits with a processor's digital outputs. PWM is employed in a wide variety of applications, ranging from measurement and communications to power control and conversion. The general concept is to simply send an ordinary logic square wave to your servo at a specific wave length, and your servo goes to a particular angle (or velocity if your servo is modified). The wavelength directly maps to servo angle.The standard time vs angle is represented in this chart:

Figure: Pulse for controlling a Servo motor


Programmable Counter Array (PCA)

The PCA is a special modules in Philips P89V51RD2 which includes a special 16-bit Timer that has five 16-bit capture/compare modules associated with it. Each of the modules can be programmed to operate in one of four modes: rising and/or falling edge capture, software timer, high-speed output, or pulse width modulator. Each module has a pin associated with it in port 1.
Module 0 is connected to P1.3 (CEX0), module 1 to P1.4 (CEX1), etc. Registers CH and CL contain current value of the free running up counting 16-bit PCA timer. The PCA timer is a common time base for all five modules and can be programmed to run at: 1/6 the oscillator frequency, 1/2 the oscillator frequency, the Timer 0 overflow, or the input on the ECI pin (P1.2). The timer count source is determined from the CPS1 and CPS0 bits in the CMOD SFR.

In the CMOD SFR there are three additional bits associated with the PCA. They are CIDL which allows the PCA to stop during idle mode, WDTE which enables or disables the Watchdog function on module 4, and ECF which when set causes an interrupt and the PCA overflow flag CF (in the CCON SFR) to be set when the PCA timer overflows. The Watchdog timer function is implemented in module 4 of PCA. Here, we are interested only PWM mode.


8051 Pulse width modulator mode

All of the PCA modules can be used as PWM outputs. Output frequency depends on the source for the PCA timer. All of the modules will have the same frequency of output because they all share one and only PCA timer. The duty cycle of each module is independently variable using the module's capture register CCAPnL.When the value of the PCA CL SFR is less than the value in the module's CCAPnL SFR the output will be low, when it is equal to or greater than the output will be high. When CL overflows from FF to 00, CCAPnL is reloaded with the value in CCAPnH. this allows updating the PWM without glitches. The PWM and ECOM bits in the module's CCAPMn register must be set to enable the PWM mode. For more details see P89V51RD2 datasheet.

This is an example how to control servos with 8051 by using PWM. The schematic is shown below. I use P1.4 (CEX1) to control the left servo and P1.2 (CEX2) to control the right servo. Here, I use GWS servo motor model S03T STD. I need three states of duty cycle:

  • 20 ms to Stop the servo

  • 1 ms to Rotate Clockwise

  • 2 ms to Rotate Counter-clockwise


Calculation for duty cycle (for XTAL 18.432 MHz with 6 Clock/Machine cycle)

  • Initial PWM Period = 20mS (18.432MHz /6-Cycle Mode)

  • Initial PCA Count From Timer0 Overflow

  • 1 Cycle of Timer0 = (1/18.432MHz)x6 = 0.326 uS

  • Timer0 AutoReload = 240 Cycle = 78.125 uS

  • 1 Cycle PCA = [(1/18.432MHz)x6]x240 = 78.125 uS

  • Period 20mS of PCA = 20ms/78.125us = 256 (CL Reload)

  • CL (20mS) = 256 Cycle Auto Reload

  • Load CCAPxH (1.0mS) = 256-13 = 243 (243,244,...,255 = 13 Cycle)

  • Load CCAPxH (2.0mS) = 255-26 = 230 (230,231,...,255 = 26 Cycle)

Schematic: Control RC Servos motors with 8051 PWM


Datasheet

- P89V51RD2 [pdf]


Source Code (For SDCC)

- pwm_servos.h

- test_servos.c


Related Links

- What is a Servo?

- Tutorial on how to modify a servo for full 360 degree rotation

- Servo Tutorial for Robotics

- Pulse-width modulation (Wiki)

- Introduction to Pulse Width Modulation (PWM)

6 comments:

Raj said...

That is a nice and informative post on Servo Motors.
Web hosting india

MaHieRa said...

hi..

i have look at the program to design servo motor...but i can't undrsnt the program...
can u explain to me???

sabareesh said...

HEY YOU HAVE ATTACHED A RESISTANCE AT THE SIGNAL WIRE OF SERVO MOTOR..WHAT IS THE VALUE OF THAT RESISTANCE..?PLEASE HELP ME..!

Unknown said...

hi itz very intersting.., can u please mail me the connection PWM with microcontroller..,especially PWM circuit

THANK YOU

Unknown said...

hi.., can you please send me pwm ciruit
thank you

Osama Zain said...

Thanks admin
you have good post and nice blog
and i want to visit and subscribe to my blog STD Symptoms and Gamma Benzene Hexachloride (GBH)
thanks a gain admin ,,,