Monday, August 31, 2009

My First Robot

If you have been a regular to amitraj@SASTRA by now you have more than enough expertise to build your first robot . Well the robot we will build is what most people start out building robots with, which is a computer controlled robotic car ( me included ) . As this article is meant for absolute beginners all you experts can skip this one .

What is required

We will be basically modifying a cheap remote controlled chinese car to be computer controlled . So a few electronic components and an RC car is required .So here goes the list

A cheap chinese RC car ( Rs 300 + )
4 6v SPST Relays ( Rs 30 )
4 IN4148 Diodes ( Rs 2 )
4 IN2002 Diodes ( Rs 2 )
4 BC 547 Transistor ( Rs 4 )
4 4.7 K resistors ( RS 1 )
3+ meters ribbon wire ( Rs 15 + )
25 pin D-type male connector ( Rs 10 + )
A battery eliminator

Well that does it for the HW and for the SW we will use the trusty old DOS , yes that is Disk Operating System :) and a turbo C compiler .

The parallel port

Now if you have already read ICE's nice Parallel port tutorial you can skip this part .

A parallel port can send (by varying the voltage levels) 8 bits in parallel (1 byte), simultaneously to a connected device, for example, a printer. The parallel port that you find at the back of your PC is a 25-pin, D-type female connector. 25 pins allow for powerful communication with devices, but we will restrict ourselves to just sending data out of the parallel port.

As mentioned, a parallel port can send 8 bits simultaneously. The pins that are used to do so are 8 pins from pin numbers 2 to 9. You will find the pin numbers etched on the plastic casing of the port. Other pins that we will use are the ground pins (numbers 18, 19, 20, 21, 22, 23, 24 and 25). So many (redundant) ground pins are used for parity checking.

The output pins on the parallel port use TTL (Transistor-Transistor Logic) to pass signals as voltage levels. Voltage levels are either ‘1’ or ‘high’ (a voltage level of 5V) or ‘0’ or ‘low’, signified by 0 volts. This is the theory. In practice, the actual voltages will not be exactly these, but around them. The objective of the interfacing circuitry is to identify these voltage levels on the parallel port and use them for driving our robot. As for the current, the parallel port itself can source only 4.6 mA of current, which is very less, except for say lighting up LEDs. For driving the motors in the robots, we will need more current.This will be provided by a separate power source.

The voltage levels at the parallel port are interpreted as digital signals to make the robot perform specific tasks. Let us see how to do this. Pin number 2 is the 0th bit and progressively pin 9 is the 7th bit of the byte that is sent to the connected device through the parallel port. Hence, a value of 11111111 in binary or 255 in decimal would mean that all pins are at a voltage level of 5V.

Generalizing this, a pin status of 00000001 (only the first data pin, ie, the 2nd pin on the parallel port, or the 0th bit, is active) would be signified by a value 1, or the decimal equivalent of the binary number that corresponds to the desired high–low (one or zero) configuration of the data pins.

Once you have understood the above cocktail of pins and voltages, sending this decimal value using a program is trivial. Although the basics of coding are presented here, higher levels of abstraction of this code have to be used for some really constructive programming.

Consider the following code snippet.
#include
#define port 0x378
#define value 255
void main(void)
{
outportb (port, value) ;
}

Port is the I/ O address of the first parallel port attached on your computer. In the rarest of cases where it might be other than 0x378, do check the printer port I/ O address in the BIOS settings. Value is the decimal value as described in the previous section.

An important point to note is that this setting of value remains the same even after the program has quit, until your PC is rebooted in which case the parallel port is reset. So, any good programmer should, before exiting the program, set the values back to where he started from. For this, you have to obviously read them in when you start the program. That is a completely different topic and we will not cover it in this series.

Now consider a motor inside your robot. A typical DC motor can have three states: clockwise motion, anticlockwise motion and no motion. To generate control signals for these three states we will need 2 bits.

Bit no.0 1
0 0 off
1 0 clockwise motion
0 1 anticlockwise motion
1 1 not needed

So, to create a clockwise motion of the motor we will have to send a value of 1 to the parallel port, similarly a value of 2 for anticlockwise motion. Likewise, the other six unused data bits can be used for controlling three other motors. In our example, we will use only two motors, one for forward/backward motion and other for changing direction.

The circuit


The circuitry is a buffer circuit to drive a relay using +5v volts from the parallel port. The relays, in turn, are connected to the DC motors in the car. The circuit is very basic as shown in the diagram above, and you can put the components together on a PCB.

The parallel port is a very sensitive electronic device that can fail if the wiring is faulty or if there’s a short circuit. In newer motherboards, where the parallel port is built into the board, the fault can domino through the port to your motherboard. We have provided enough safeguards (the two IN4148 diodes), but make sure the circuit is correctly made before you connect it to the parallel port. Also, check the IN4002 diode because this diode protects the circuit from any back EMF generated from the relay. To be doubly sure, you can replace the IN 4148 diodes with 5.1 V Zener diodes.

Vcc in the circuit diagram is the external power supply for the relay. The circuit can handle relays that operate at 24 volts or less having a current rating of 100 mA or less. We use 6 V.

The outputs to be connected to the parallel port can be connected through a 25 PIN D type male plug, which can then be connected to the parallel port. The pins to which the circuit has to be connected on the parallel port have been explained in last month’s article (the article is also on this month’s CD in the Tutorials section).

Once you have created the circuit, the next step is to take the car apart. Do this by opening up all the screws on it. There are two motors, attached to the front and back wheels. Identify the wires powering them and cut them clean. Now remove the rest of the existing circuits off the car and you are ready.

Ready for assembly: The toy car all opened up and wires to motors identified Motors connected up. Inset: reassembling the car

The four relays wired up according to the circuit diagrams provided
Inset right: assembled circuit 1 Inset left: A relay Connecting up to the male plug for the parallel port

Wiring the car

The motors will be connected as shown in the diagram

Functioning of the relay-to-motor connection is very simple. Initially the potentials at point A and B are both 0V and the motor is switched off. When the relay 1 is switched on, the potential at point A changes to Vmm (the voltage required by the motors, 6 volts in our case) and at point B the potentials remains at OV (ground). This dictates the clockwise motion of the motor. Similarly, when relay 2 is switched on (and relay 1 is switched off ), the potentials at A and B exchange and this facilitates the anticlockwise motion of the relay .

By sending a 1 (binary 00000001) , you can make the motor move clockwise and by sending a 2 you can make the motor move anticlockwise, These translate into forward/backward or left /right depending on how you have attached the circuit to motors in the car.

One important thing to remember is that it is imperative that we have enough power for the relay and the motors. Even though the relays and the motors work at the same voltage, a typical 6 V DC adaptor (if you are using 6 volts relay and 6v volts motors) will not be able to drive all of them simultaneously as it can only source 500mA.

You can use an adapter with higher Amp rating (1 Amp) or use two adaptors, one for the relays and another for the motors as they are more easily available.

Basic control

We will now modify the original code snippet we created. Now attach the data pins 0 and 1 to the motor controlling the direction (front wheel) and data pins 2 and 3 to the motor controlling the forward and backward motion (back wheel).

Accordingly, the code now becomes

#include
#define value 1
void main (void )
{
outportb(0x378,value); //activating the first pin for turning left .
sleep(2) ; // sleeping for 2 secs
ouportb(0x378,0) // switch of all motors
}

At this point in the game, every time you want to impart motion to the robot, you will have to run the exe file with a new value ou can easily add to this code the arrow keys interface to control it like you would in a driving game that part is left to you . And this is how it will look finally



!!! That's all Folks !!!

No comments:

Post a Comment