You are on page 1of 8

Overview of the servo system

A servo is a device that has a shaft that 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. The servo is an automatic device that uses error-sensing feedback to correct
the performance of a mechanism. The term correctly applies only to systems where the feedback
or error-correction signals help control mechanical position or other parameters. A common type
of servo provides position control. Servos are commonly electrical or partially electronic in
nature, using an electric motor as the primary means of creating mechanical force. Other types of
servos use hydraulics, pneumatics, or magnetic principles. Usually, servos operate on the
principle of negative feedback, where the control input is compared to the actual position of the
mechanical system as measured by some sort of transducer at the output. Any difference between
the actual and wanted values is amplified and used to drive the system in the direction necessary
to reduce or eliminate the error. This procedure is one widely used application of control theory.

There are various types of servo motors, the ones employed here are:

• RC servo motor and

• DC servo motor

RC servo motor:

RC servos are composed of an electric motor mechanically linked to a potentiometer. Pulse-


width modulation (PWM) signals sent to the servo are translated into position commands by
electronics inside the servo. When the servo is commanded to rotate, the motor is powered until
the potentiometer reaches the value corresponding to the commanded position. The servo will
move based on the pulses sent over the control wire, which set the angle of the actuator arm. The
servo expects a pulse every 20 ms in order to gain correct information about the angle. The width
of the servo pulse dictates the range of the servo's angular motion. A servo pulse of 1.5 ms width
will set the servo to its neutral position, or 90°. For example a servo pulse of 1.25 ms could set
the servo to 0° and a pulse of 1.75 ms could set the servo to 180°. The physical limits and
timings of the servo hardware varies between brands and models, but a general servo's angular
motion will travel somewhere in the range of 180° - 210° and the neutral position is almost
always at 1.5 ms .
An insight on AVR Atmega8 microcontroller

Introduction to microcontrollers

A microcontroller is a small computer on a single integrated circuit consisting internally of a


relatively simple CPU, clock, timers, I/O ports, and memory. Program memory in the form of
flash or OTP ROM is also often included on chip, as well as a typically small amount of RAM.
Microcontrollers are designed for small or dedicated applications. Microcontrollers are used in
automatically controlled products and devices.

Why Atmega8?

It is a high performance, low power microcontroller with advanced RISC architecture.


The separate instruction and data buses of the Harvard architecture allow a 14 bit wide
instruction word with a separate 8 bit wide data bus. The two stage pipeline allows all
instructions to execute in single cycle, except for program branches (which require two cycles).
A total of 130 instructions are available. Additionally, a large register set is used to achieve high
performance level. The AtmegaX family is of low cost, high performance, CMOS, fully static 8-
bit microcontroller.

The AtmegaX family has special features to reduce external components, thus reducing
cost, enhancing system reliability and reducing power consumption. The sleep mode offers
power saving. The user can wake the chip from sleep through several external and internal
interrupts, resets. A highly reliable watchdog timer with its own ON-CHIP RC oscillator
provides protection against software lock-up. The device with flash program memory allows the
same device package to be used for prototyping and production. In circuit reprogram ability
allows the code to be updated without the device being removed from the application. This is
useful on the development of many applications where the device may not be easily applicable,
but the prototypes may require code updates as well as in remote applications. (such as rate
information).
The AtmegaX fits perfectly in the applications ranging from high speed automotive and
appliance motor control to low power remote senses, electronic locks, security devices and smart
cards. The Flash/EEPROM technology makes customization of application programs (transmitter
codes, motor speed, motor direction, receiver frequency, security codes etc.) extremely fast and
convenient. The I/O flexibility makes the AtmegaX very versatile even in areas where no
microcontroller use has been considered before (example: timer functions, serial communication,
capture & compare mode and PWM functions ). The serial in system programming features
offers flexibility of customizing after complete assembly and testing. This feature can be used to
serialize a product, store and calibrate data.

Servo motor control with Atmega8:

The servo motor is controlled using PWM pulses. In Atmega8 there are three PWM modes
namely Fast PWM mode, Phase correct PWM mode, Phase and frequency correct frequency
mode. We use the Phase and frequency correct PWM mode to control the servo motor. The
phase and frequency PWM mode (WGM13:0 = 8 or 9-waveform generation mode) provides a
high resolution phase and frequency correct PWM waveform generation option. The phase and
frequency correct PWM mode is based on a dual-slope operation. The counter counts repeatedly
from BOTTOM (0x0000) to TOP and then from TOP to BOTTOM. In non-inverting Compare
Output mode, the Output Compare (OC1x) is cleared on the Compare Match between TCNT1
and OCR1x while up counting, and set on the Compare Match while down counting. In inverting
Compare Output mode, the operation is inverted. The dual-slope operation gives a lower
maximum operation frequency compared to the single-slope operation. However, due to the
symmetric feature of the dual-slope PWM modes, these modes are preferred for motor control
applications. The main difference between the phase correct and the phase and frequency correct
PWM mode is that the time OCR1x Register is updated by the OCR1x Buffer Register. The
PWM resolution for the phase and frequency correct PWM mode can be defined by either ICR1
or OCR1A. The minimum resolution allowed is 2-bit (ICR1 or OCR1A set to 0x0003), and the
maximum resolution is 16-bit (ICR1 or OCR1A set to MAX). The PWM resolution in bits can
be calculated using the following equation:
In phase and frequency correct PWM mode the counter is incremented until the counter value
matches either the value in ICR1 (WGM13:0 = 8), or the value in OCR1A. The counter has then
reached the TOP and changes the count direction. The TCNT1 value will be equal to TOP for
one timer clock cycle. The timing diagram for the phase correct and frequency correct PWM
mode is shown on Figure . The figure shows phase and frequency correct PWM mode when
OCR1A or ICR1 is used to define TOP. The TCNT1 value is in the timing diagram shown as a
histogram for illustrating the dual-slope operation. The diagram includes non-inverted and
inverted PWM outputs. The small horizontal line marks on the TCNT1 slopes represent compare
matches between OCR1x and TCNT1. The OC1x Interrupt Flag will be set when a Compare
Match occurs.

Figure : Phase and frequency correct PWM mode, timing diagram.

The Timer/Counter Overflow Flag (TOV1) is set at the same timer clock cycle as the OCR1x
Registers are updated with the double buffer value (at BOTTOM). When either OCR1A or ICR1
is used for defining the TOP value, the OC1A or ICF1 Flag set when TCNT1 has reached TOP.
The Interrupt Flags can then be used to generate an interrupt each time the counter reaches the
TOP or BOTTOM value. When changing the TOP value the program must ensure that the new
TOP value is higher or equal to the value of all of the Compare Registers. If the TOP value is
lower than any of the Compare Registers, a Compare Match will never occur between the
TCNT1 and the OCR1x. As Figure shows the output generated is, in contrast to the Phase
Correct mode, symmetrical in all periods. Since the OCR1x Registers are updated at BOTTOM,
the length of the rising and the falling slopes will always be equal. This gives symmetrical output
pulses and is therefore frequency correct. Using the ICR1 Register for defining TOP works well
when using fixed TOP values. By using ICR1, the OCR1A Register is free to be used for
generating a PWM output on OC1A. However, if the base PWM frequency is actively changed
by changing the TOP value, using the OCR1A as TOP is clearly a better choice due to its double
buffer feature. In phase and frequency correct PWM mode, the compare units allow generation
of PWM waveforms on the OC1x pins. Setting the COM1x1:0 bits to 2 will produce a non-
inverted PWM and an inverted PWM output can be generated by setting the COM1x1:0 to 3.
The actual OC1x value will only be visible on the port pin if the data direction for the port pin is
set as output (DDR_OC1x). The PWM waveform is generated by setting (or clearing) the OC1x
Register at the Compare Match between OCR1x and TCNT1 when the counter increments, and
clearing (or setting) the OC1x Register at Compare Match between OCR1x and TCNT1 when
the counter decrements. The PWM frequency for the output when using phase and frequency
correct PWM can be calculated by the following equation:

The N variable represents the prescaler divider (1, 8, 64, 256, or 1024). The extreme values for
the OCR1x Register represent special cases when generating a PWM waveform output in the
phase correct PWM mode. If the OCR1x is set equal to BOTTOM the output will be
continuously low and if set equal to TOP the output will be set to high for non-inverted PWM
mode. For inverted PWM the output will have the opposite logic values.
Flowchart:

START

Set control input

Initialize TCCR1x register


for bit resolution

Initialize TCCR1x register for


prescaler

Initialize OCR1x for required


duty cycle

END

You might also like