subota, 13. listopada 2012.

AVR SERVO PROJECT

AVR SERVO PROJECT  WITH LEGO DUPLO

After this project I can enyoy playing with AVR and with my son at the same time.
Isn't that perfect?

There is a great selection of Lego  Technic or even Mastermind availible. I personally think these are great toys for both education and entertainment. However, I like to make things my way, and also still need DUPLO size for my son (3+)!






EXPLANATION

How and what I did with pictures

STEP 1: Carving a DUPLO Brick and mounting a servo


The material of Lego is actually very nice for modelling. The skalpel or any knife can be used for initial carving. After that centre part can be milled by some kind of grinding tool.


Surely it can easily be done aesthetically better, but it fits perfectly even without a glue. One can also add some glue, while I like it to be more flexible (replacement etc.)

 STEP 2: How to control the servo ?

Servo actuator is not really just the motor. In this case it is a complex system capable of holding the position we want very precisely, and moving arround as we command it. Inside the package there are: Electrical motor, high ratio gearbox, position measuring device and the control circuit. There are three wires coming out of a servo like this: Ground, +5V and signal line.

I did not like the way servos are controlled until I actually tried it. 

One thing is sure - you won't be able to control servo without some kind of external control circuit. Micro-controllers are the most reasonable way of controlling a servo. They work perfectly as a pair.

For all my projects I use AVR. For this project I used both Atmega8 and Atmega32, but any other suitable uC can be used. Take a look at the mess of my control circuit:


It is not completely visible, since I packet it all inside a butter box.

The reason why to use micro-controller is the following: Servos need a PWM signal for setting the angle reference, and with uC you can very easily create PWM signal.

PWM stands for Pulse Width Modulation, and although it might not sound simple, it is not a very complicated concept. In this case, it means that servo will expect to be ticked in 20 ms (miliseconds) intervals at signal line. If not, it will simply not work. The angle is controlled by a duration of these impulses. Typically, a duration of 1 - 2 ms will mean a variation of let's say 180 degrees. For example, if you give an impulse of 1 ms every 20 ms servo will position itself at 100 °. If you give an 1.5 ms impulse it will position at 130 °. This concept is great because it gives you the possibility to virtually change the position every 20 ms. That's the frequency of 50 Hz by the way.

In micro-controller you can do this in two ways: Using PWM with 20 ms period (recommended) or manually using a simple delay functions. PWM method is neat because micro-controllers can do it automatically in the background, and it will not affect the rest of the program.

Here is the basic principle:
 STEP 3: Programming the controller

I do not plan to go to the details of PWM programming, becouse many people already did that. There are some very useful links for this matter at the end of this post.
However, I'll post the C code here only to show how simple it is:



The code is made for: AVR , Clock frequency F_CPU = 1 MHz

If you ignore the complicated setup for registers (you can always just copy it), it is possible to setup the position by simply setting OCR1A. 1180 means 90 ° and 2050 means 180 °. you can also set other values. Usually, OCR1A values need to be adjusted by experimenting for different servo models.

 STEP 4: Applications -- Finally!

Example 1: Brick lifter tractor


Example 2: The ramp


Example 3: The creepy bear


These examples are best seen in action on the video. Check it out here

First two examples are controlled as described. Third one is different, the position is proportional to analog input connected to accelerometer module. That way the hand follows motions of the module.

Anyway, servos are great devices, and except of these examples showing playing with them, they can be used in many serious applications. There are a few models available for under 4 $, and so far I haven't managed to destroy a single one, meaning they are also durable. I recommend trying to use servo to anyone interested.

SOME TECHNICAL DATA:

Servo motor:                                      9G
Servo Stall Torque at 4.8 V:           1.2 kg/cm (16.7 oz/in)
Servo speed at 4.8 V (no load):     0.12 sec / 60 °            
Micro-controller:                              Atmega8, Atmega32


REFERENCES:

EXTREME ELECTRONICS - SERVO MOTOR CONTROL

SOCIETY OF ROBOTS - PWM TUTORIAL

These two references and a basic knowledge in Micro-controllers are all you need to start working with servos.

If you have some questions about overall concept I'd be glad to answer.
vpsus@yahoo.com