CSE 466 Final Project - Accelerometer Driven Car
by Kelvin Lau and Mike Quinn

Home | Accelerometer Use | Servo Use | Building Car | Problems Faced
Stepper Motor | THE CODE | THE SCHEMATIC

Controlling the Servo

A Servo is a small device that has an output shaft. This shaft can be positioned to specific angular positions by sending the servo a coded signal. As long as the coded signal exists on the input line, the servo will maintain the angular position of the shaft. As the coded signal changes, the angular position of the shaft changes. In practice, servos are used in radio controlled airplanes to position control surfaces like the elevators and rudders. They are also used in radio controlled cars, puppets, and of course, robots.  A picture of a servo is shown below on the right. 

How did we communicate the angle at which the servo should turn? The control wire is used to communicate the angle. The angle is determined by the duration of a pulse that is applied to the control wire. This is called Pulse Coded Modulation. The servo expects to see a pulse every 20 milliseconds (.02 seconds). The length of the pulse will determine how far the motor turns. A 1.5 millisecond pulse, for example, will make the motor turn to the 90 degree position (often called the neutral position). If the pulse is shorter than 1.5 ms, then the motor will turn the shaft to closer to 0 degress. If the pulse is longer than 1.5ms, the shaft turns closer to 180 degress.

Being able to control the servo to .05 milliseconds of pulse looked like it might be one of our bigger challenges.  Doing a little math we determined that we would need an interrupt routine that ran at 20 kHz.  This would give us about .0479 milliseconds between interrupts.  To get this interrupt routine to run at this frequency we used the Timer 2 interrupt and had it overflow every 128 clock cycles.  Since Timer 2 is a 16-bit timer we needed to needed to reload the timer with TH2 = 0xFF and TL2 = 0x80 after every interrupt.  Our needed servo pulse range, when tested, was more along the range of 0.75 milliseconds to 2.25 milliseconds to get the full 180 degrees of angle.

To take a closer look at the actual code for delivering this Pulse Coded Modulation signal, view the adjust_settings function of THE CODE.