Robot - Line Follower

Saturday, 6 July 2013

Robot - Line Follower

                                                              by  midhun_s

  



I began work on my first robot about two years ago. For no particular reason, I decided to begin with a line following robot and truth be told, my first attempt at building it was a complete failure. Looking back at my efforts, I believe, for a beginner, I was rather too ambitious. The circuit that I had designed had a bunch of unnecessary stuff which I then believed would give my robot an edge over the others. But it never worked and I had to start all over from scratch.

In my second attempt, I managed to get the robot on track. As delighted as I was with my first robot, it was nowhere near where I wanted it to be. I took it to a couple of competitions and not much to my surprise, it failed in both of them. I knew it was time to make some major changes in the design.

In my third attempt, (actually, it wasn’t the third, it was a revision of my second attempt) I updated the firmware and came up with a much more stable and accurate version. It performed well when tested and much to my delight, it finished first in two competitions and second in another. Though I was proud with what I had achieved, I felt that the robot was visually rather unappealing. And you’ll come to know why from its picture given above. It had wires running all over it and I had no other option but to rebuild it. A month or so later, I had the robot all ready and that is the current version of my first robot.

In this instructable I will guide you through the steps that I've followed in building the current version of my robot. It’s one of those robots which belong to the “scratch-built” category. You might find it difficult to find all the parts that I have used. So I insist you to read through the steps that I have followed, and then implement it in your own way with the parts that you've found.

This instructable requires that you are familiar with the following:
• Soldering and related equipments
• Hand tools like screwdrivers, wire cutters and strippers
• Reading schematics and connection diagrams
• C/C++ programming for AVR microcontrollers (optional)

Step 1: Gather the parts

Given below is a list of components that I have used in building this robot.

Electronic Parts
• Atmega32 microcontroller
16x2 alphanumeric LCD
• LM2576 Step-Down Voltage Regulator
L298 Motor Driver Module
Lithium-Ion Rechargeable Battery Pack 11.1V 3000mAh 2C
• 9 x IR Transmitter and Receiver pair (5mm diameter)
• 10K resistor array (9 pin)
• 2N2222 transistor (or any other npn type)
• Resistors and Capacitors
• 2 x Centre Shaft Economy Series DC Motor (100 RPM)
• Switches and Push Buttons

General Parts
• Dot Matrix Prototype PCB
• Plastic Enclosure
• Single-strand Wire
40-pin DIP socket
6 pin FRC right angle male connector
Metal Studs
Plastic Spacers
Nuts and Bolts
Ball Caster Wheel
• 2 x Motor Mounting Clamp
• 2 x Wheel (50mm diameter)
• Cables and Connectors

Tools Required
• Soldering Iron
• Screw Driver
• Wire Cutter and Stripper
• Something to cut and drill plastic

In addition to the above mentioned components, you will also require an AVR programmer and a battery charger.

Now, let’s get started shall we?

Step 2: How many sensors?

The number of sensors to be used actually depends on the complexity of the course which the robot is supposed to cover. If the course contains sharp 90 degree turns, intersections and acute angle turns, you will have to go with at least 4 sensors.

The robot presented here has a total of nine sensors - eight of them arranged in a line and the other in front of the line at the center. Choosing the number 8 has the advantage that you can represent the status of all eight sensors using a single byte with each bit representing the state of one sensor. This will be discussed in detail in later steps.

Step 3: Batteries...Which one to choose?

Batteries come in different varieties and choosing the right one for your application can be a daunting task. Autonomous mobile robots require batteries that last long and are light in weight. Lithium-ion batteries are a good choice. But they are quite costly and require a dedicated charger.
The one shown in the picture is what I have used. It is a Lithium-ion battery pack rated at 11.1V 3000mAh 2C. It has separate cables for charging and discharging. I suggest you read this excellent article on batteries written by ladyada. You can find it here.

Step 4: The Voltage Regulator

The robot need a constant 5V supply for the microcontroller, the sensor array and the LCD. In the first version of my robot, the 7805 linear regulator handled this job. After a couple of trial runs, I noticed that the regulator was getting heated up even with a heatsink attached to it. So in the later version, I decided to use a switching regulator and now it works fine.

SELECTING THE COMPONENTS
From the datasheet of LM2576 switching regulator (Page 12, Figure 4), you will find the value of the inductor that is to be used. It depends on the maximum input voltage and maximum output current. In my instance, the maximum input voltage is about 12V and maximum output current does not exceed 200mA. So I must have chosen a 680uH inductor. But unfortunately I couldn’t find one. All I got was a 390uH and it worked just fine.

The value of the input and output capacitors aren’t that critical. Any value above 100uF, 25V should work. The diode should be a fast recovery diode – preferably a Schottky. The one that I have used is 1N5822. The heatsink is not necessary, however I have added one as a measure of safety.

Given below are the schematic and board layout files of the voltage regulator designed in Cadsoft Eagle. You can also download the pdf files from below. You will have to get it ready on PCB or you can make it on a perfboard.

Step 5: The Sensor Array

The sensor array has a total of nine IR emitter-detector pairs. Eight of these are positioned in a line with a spacing of 18mm in between. The spacing is so chosen that the robot can cover tracks of width between 20mm and 40mm without having the need to change the program. The spacing between an emitter and the corresponding receiver is 10mm.

The sensor array circuit can be considered as two parts-one for the emitter array and the other for the detectors. Let's see each of these in detail.

DESIGNING THE IR EMITTER ARRAY
When you open the schematic file, you will notice that the IR emitters are connected in a series-parallel form. I obtained this design from LED series/parallel array wizard. All I had to do was enter the specs of the LED and the array (given below) and the wizard showed me the arrangement that consumes the least amount of power.

Source voltage = 5V
Diode forward voltage = 1.5V
Diode forward current  = 5mA
No. of LEDs in your array = 9

The transistor shown in the picture is used to turn the IR emitters ON and OFF. I have used a 2N2222 but you can use any other n-p-n transistor. The emitters are turned ON only when a reading is to be taken. This method reduces the total power consumed by the sensor array and most importantly, the effect of ambient light on sensor values is also reduced. This is accomplished by taking two readings - one with the emitters ON and the other with the emitters OFF. Subtracting the sensor values obtained in two cases, we will obtain a value that is independent of ambient light.

DESIGNING THE IR DETECTOR ARRAY
The IR detector circuit is essentially a potential divider with the IR detector connected in series with a resistor. The eight sensor array is read using the ADC and the ninth sensor is read using Pin 29 (PC7) of Atmega32. For the eight detectors, I have used a 10K resistor array and for the ninth detector I have used a separate 10K resistor.

Step 6: The Motor Driver

The motor driver is a small circuitry that controls the power supplied to the motor based on the input from a microcontroller.
The module that I have used is based on the L298 motor driver IC. It can drive two motors independently. The operating voltage is between 8V and 48V and the module can handle a maximum current of 2A per motor.

PIN FUNCTIONALITY
- I1 and I2 are logic input pins corresponding to output pins OP1 and OP2. These output pins are connected to the left motor.
- I3 and I4 are logic input pins corresponding to output pins OP3 and OP4. These output pins are connected to the right motor.
- EA is the enable input for OP1 and OP2.
- EB is the enable input for OP3 and OP4.
- VCC and GND are the supply and ground terminals for driving the motor.
- 5V and GN are the supply input to the logic pins.

CONNECTIONS
- EA, EB and 5V pins are connected to 5V supply.
- VCC is connected to battery output.
- I1 is connected to Pin 19 (PWM pin)
- I2 is connected to Pin 20
- I3 is connected to Pin 18 (PWM pin)
- I4 is connected to Pin 21

SPEED AND DIRECTION CONTROL
The microcontroller is programmed to generate a square wave of 500Hz frequency on pins 18 and 19. The dutycycle of these signals can be controlled independently.

When a logic 0 is written to Pin 20 and if dutycycle on Pin 19 is 100%, the left motor rotates in forward direction at full speed. When a logic 1 is written to Pin 21 and if dutycycle on Pin 18 is 0%, the left motor rotates in the opposite direction at full speed.

So the logic level at pins 20 and 21 determines the direction of rotation and dutycycle on Pins 18 and 19 determines the speed of rotation.

Step 7: Wiring the "Brain"


control board_2.jpg

DSC03757.JPGNow for those who have had little experience with perfboards, this might seem as a difficult task but believe me it is a lot simpler. All you have to do is wire the standalone circuit (shown in the picture) and then add some berg connectors. If you haven't used a perfboard before, I would suggest you watch this excellent video - Circuit Skills: Perfboard Prototyping.

Referring to the pin diagram of Atmega32, the connections to be made are given below:

Pins 33 to 40 - sensor array (analog pins)
Pins 31 to 28 - sensor array (gnd, vcc, ninth sensor and emitter ON/OFF pins)

Pin 25 - Top Push Button Pin
Pin 24 - Right Push Button Pin
Pin 23 - Middle Push Button Pin
Pin 22 - Left Push Button Pin

Pins 18 to 21 - motor control pins

Pins 1 to 3 - LCD Control Pins (RS, R/W and Enable)
Pins 14 to 17 - LCD Data pins (DB4 to DB7)

Pins 6 to 11 - ISP header

 

 

 

 

 

 

 

 

 

 

 

 

 

 Step 8: Charging and Programming Headers

The right angled 6-pin programming header is connected to the microcontroller as shown in the picture. The charging header provides external access to charge the battery. It is connected to the bottom left portion of the main circuit. It is made out of berg strips, a 4-pin connector and a small piece of perfboard. Then portions are cut out from the enclosure to accommodate charging and programming headers.

Step 9: Attaching the LCD and the Push Buttons

To connect the LCD to the main circuit, I made a breakout board. The board is built on perfboard and it has got a 10K pot for contrast adjustment. The circuit can be wired in a perfboard as shown in the picture above. The connections for the push buttons are a lot simpler. I have used a 5-pin relimate connector to connect the push button to the microcontroller circuit. The LCD and the push buttons are then attached to the enclosure cover.

Step 10: Attaching the Battery and the Switch

The battery and the switch are connected as shown in the picture above. When the switch is turned ON, the battery will be connected to the supply pins of the motor driver and also to the input pins of the voltage regulator. The battery is charged with the switch in OFF position.

Step 11: Attaching the Motors and the Wheels

I first attached the motor mount and the wheel to both the motors. The shaft of the motor is 6mm in diameter and has an M3 thread hole for attaching the wheels. The motors are then attached to either end of a pipe extender so that the axis of the motor shafts are perfectly aligned. The whole system is attached to the bottom of the enclosure. Metal studs of 8mm length provide the necessary spacing between the wheel and the enclosure. The wires from the motors are taken out through the pipe extender and are given to the enclosure.

Step 12: Fixing the Caster Wheel

The caster wheel is mounted on a plastic base which I cut out to the required shape. The caster wheel is mounted to the base using metal studs, bolts and nuts. The plastic base is then attached to the enclosure by means of metal studs and bolts.

Step 13: Mounting the Sensor Array

The sensor array is mounted to the same base as to which the caster wheel is attached. Cables connected to the sensor array pins are taken inside the enclosure and is connected to the microcontroller.

Step 14: Connecting the Motor Driver

To connect the motor driver with the main circuit I have used perfboard and berg connectors. The logic pins on the motor driver and the main circuit are connected by a 'bridge'. The wires of both the motors are then connected to the motor driver.

Step 15: Connecting the Voltage Regulator

The voltage regulator is connected as shown in the picture above. The input to the regulator is taken from the VCC and GND pins of the motor driver. The 5V output cable is connected to the microcontroller circuit.

Step 16: Let's Complete the Hardware

The charging cable is connected as shown above. Now all that's left to do is to connect the LCD and the push buttons. The cables from the LCD and push buttons are attached as shown in the picture. And finally, the cover of the enclosure is attached.

Step 17: Setting Up the Programmer

Now that the hardware is all ready, let's move on to the programming part. But before starting out, you have to be ready with the following stuff.

• AVR programmer - the one that I have used is USBasp. If you have already got another programmer for AVR you can use that one too.

• USB cable - you will need it to connect the programmer with your PC or laptop.

winAVR - this is a software bundle that comes with a compiler, a programmer and a debugger for the AVR series of microcontrollers. You will have to download and install it.

• The winAVR comes with an editor - the Programmer's Notepad. But the one that I have used is Notepad++ Portable with winAVR plug-in installed in it. You can download it from below.

Now the programmer is connected as shown in the picture. Before uploading the program, we will have to configure the AVR. This is done by writing a pair of bytes called fusebits. This is discussed in detail in the next step.

You can download all the libraries and the program from below.

Step 18: Writing the Fusebits

Fusebits are a pair of bytes that configures several parameters of an AVR microcontroller. It decides the frequency at which the AVR operates, the start-up delay required, and many more. I have calculated the required fuses using the Engbedded AVR Fuse Calculator.

I have attached an MS-DOS Batch file which you can download. To write the fusebits, connect the programmer to the AVR and run this batch file. You should be able to see the DOS screen shown above.

In the next few steps, we will see some of the important commands used in the program. There are a total of four libraries, one each for the LCD, the sensor, the motors and the push buttons.

Step 19: Push Button Library Commands

The three functions used in the <pushbutton.h> library are:

init_pushbutton() - activates internal pull-up on push button pins. Call this function once before using any other function of this library.

get_single_button_press() - checks whether any button is pressed. If it detects any keypress, it waits for that particular key to be released and then returns the corresponding keycode.

wait_for_button_press() - waits until a key is pressed. Upon detecting a keypress, it waits for that particular key to be released. Then it returns the corresponding keycode.

Step 20: Motor Library Commands

Two functions are available under the header file <motors.h>

init_motors() - this function sets the motor driver control pins to output mode. Speed control is achieved by Pulse Width Modulation (PWM) technique. This function should be called once before using the function given below.

set_motors() - sets the speed and direction of the two motors. Speed is any value between +250 and -250. A negative value indicates that the direction of rotation is backwards.

set_motors(250, 250) – move forward at full speed
set_motors(0, 0) – stop the motors
set_motors(-125, -125) – move backward at half the full speed

Step 21: LCD Library Commands

The library <lcd.h> has got a lot of functions but only a few are required for displaying data. They are given below.

init_lcd() - sets lcd data and control pins to outpout mode and initializes the LCD. This function should be called once prior to using any other function in this library.

print_string() - prints a text at the specified location.
print_string(2, 2, "Hello World") displays "Hello World" (without the double quotes) starting from the 2nd position of the 2nd row of the LCD.

print_integer() - prints an integer of specified number of digits at the specified location.
print_integer(2, 1, number, 3) displays the value present in 'number' starting from the 1st position of 2nd row. The value can have a maximum of 3 digits.

clear_screen() - clears the display.

Step 22: Sensor Library Commands

The functions used in the <sensors.h> library are,

init_sensors() - initializes 10-bit ADC of Atmega32 and also sets the IR emitter control pin in output mode. This function should be called before using any other function in this library.

emitters_on() - turns the IR emitters ON.

emitters_off() - turns the IR emitters OFF.

get_sensors_binary() - reads the state of all 8 sensors and return the value as a byte.

count_binary() - takes a byte as argument and returns the number of bits that are set to 1. This is used along with get_sensors_binary()  to find out the number of sensors that are over the line.

get_front_sensor() - reads the status of the 9th sensor. This function returns a '1' if the sensor is over the line, otherwise it returns a '0'.

The function calibrate_sensors() will be discussed separately. The remaining functions are not used in the main program. I will leave it to you to find out what their purpose is.

Step 23: Algorithm...that's what matters!

The algorithm is developed keeping in mind the following rules:
• at any intersection, if there exists a forward path, then the robot should take that path and if it doesn't exist, then the robot is free to choose any path.
• if there is any discontinuity in the line, then the robot has to move forward until it detects the line.
• there will not be any dead ends in the track.

Given below are three steps which is repeatedly executed when the robot moves over the line.

Step1
The sensor readings are taken and stored in a byte with each bit representing the status of one particular sensor. If a bit is 1, then the corresponding sensor is over the line and if the bit is 0, then it is not on the line. The following examples will make it clear.

If the value of the byte is 00011000, only the two sensors in the middle are over the line and if the value of the byte is 10000000, only the left most sensor is over the line.

Step2
A count of the number of sensors that are over the line is taken. The count may be any value between 0 and 8.

Step3
For the count obtained, the sensor reading is compared with a set of values. If a compare match is found, then the speed and direction of both motors are adjusted accordingly.

Given below are some of the values with which the sensor reading is compared. Given next to each value is the action that is to be taken.

0000000 - no line detected. Continue previous action.

10000000 - turn sharply to the left.
00000001 - turn sharply to the right.

11000000 - turn left
01100000 - turn left
00110000 - turn left
00011000 - move straight forward at full speed
00001100 - turn right
00000110 - turn right
00000011 - turn right

11100000 - turn left
01110000 - turn left
00111000 - turn left
00011100 - turn right
00001110 - turn right
00000111 - turn right
10011000 - acute angle turn towards left
00011001 - acute angle turn towards right

11110000 - 90 degree turn towards left
00001111 - 90 degree turn towards right
11001100 or 11011000 - acute angle turn towards left
00110011 or 00011011 - acute angle turn towards right
11111000 - 90 degree turn towards left
00011111 - 90 degree turn towards right
11011100 - acute angle turn towards left
00111011 - acute angle turn towards right

11111100 - 90 degree turn towards left
00111111 - 90 degree turn towards right

11111110 or 01111111 or 11111111 - turn either left or right

In the next step, we will see how the robot reacts when it detects an intersection or a sharp turn.

Step 24: Line Detection and Response

Picture 1 shows the two most common sections which the robot will encounter. When the robot is over a straight line, both the motors are driven at full speed. But when the robot detects a turn, say to the right as shown in the picture, the right motor is slowed down so that the robot turns right. The amount by which the motor is slowed down depends on the degree of the turn.

Picture 2 shows six different sections that are tough to handle. In the coming steps, we will perform a detailed analysis on each of the six different sections based on the algorithm that I have used.

Picture 3 is a flowchart that depicts the series of events that takes place when a turn is detected.

Step 25: Sharp 90 Degree Turn

When the binary value read is 1111000 and when sensor 9 is not over the line, the robot detects a 90 degree turn to the left. So, the robot moves forward until the sensors 4 and 5 lose the line. Then, counter-clockwise rotation is performed until either sensor 4 or sensor 5 detects the line.

Step 26: Acute Angle Turn

When the binary value read is 1001100, the robot detects an acute angle turn to the left. So the robot moves forward until sensors 4 and 5 lose the line. Then counter-clockwise rotation is performed until either sensor 4 or sensor 5 detects the line.

Step 27: 90 Degree Intersection

The binary value read is 11111111, and sensor 9 detects the line. So the turn is skipped and the robot moves forward.

Step 28: T- Shaped Section

The binary value read is 11111111, and sensor 9 does not detect the line. So the robot moves forward until sensors 4 and 5 lose the line and then performs rotation in any specified direction.

Step 29: Discontinuity in Line

When the robot detects a discontinuity, the binary value read is 00000000. So the robot continues its previous action. The previous action was that corresponding to the binary value 00011000 and it is to move forward. So the robot moves forward until it detects the line again.

Step 30: Acute Angle Intersection

When the binary value read is 10011000, the robot detects an acute angle turn towards the left. So it moves forward until sensors 4 and 5 lose the line. In doing so, sensor 7 detects the line before sensors 4 and 5 lose the line. So, the robot skips the turn and moves forward.

Step 31: Let's Upload The Code

To upload the program, first we need to set up the hardware. For this, connect the programmer to the robot and the USB cable from the programmer to your PC or laptop. The program has to be compiled before it can be uploaded.

To compile the code, open the main.c file in Notepad++ Portable. Select  Macro and click on winAVR Compile. Alternately, you can type Ctrl+R to compile. After compiling the code, the console window will be as shown above.

Now to upload the code, go to  Macro and select winAVR Program (or type Ctrl+U). Observe the console window.

Now that the program is uploaded,  the next step is to calibrate the line sensors.

Step 32: Line Sensor Calibration



For the robot to detect the line accurately, its sensors should be calibrated. The method that I have used is the median-filter technique. It is explained in detail below. The calibration code can be obtained from the calibrate_sensors() function under the header file <sensors.h>.

The array of eight sensors is placed over the bright surface (here it’s the white line). When the calibration command is received, a set of 100 readings is taken. For each sensor, the median of their 100 readings represents its 'max value'. Next, the sensor array is placed over the dark surface (here it’s the black background) and the same procedure is repeated. This time, the median represents the 'min value'. The threshold value of each sensor is determined from its ‘max value’ and ‘min value’ using the formula given below.

threshold = min value + (max value - min value) /2

If the value read by a sensor is greater than its threshold value, then that sensor is over the bright surface, otherwise it is over the dark surface.
The minimum, maximum and threshold values of each sensor are then written to the EEPROM memory of Atmega32 so that calibration values can be retrieved every time when the robot is turned on.

Note: The ninth sensor cannot be calibrated by software as it is connected to an input pin of Atmega32. The output from the ninth sensor is either a logic 1 or a logic 0 and it depends on the value of the resistor used along with its detector. I have picked a 10K resistor for this. But I recommend that you use a potentiometer instead.

Step 33: Testing the Line Sensors

To test the line sensors, first turn ON the switch. Now select Test (press the right push button). Then place the sensors over different sections of the track and observe the sensor readings.

Step 34: Optimizing the Code

In the main.c file, there is a function by the name follow_line(). It is shown in the picture above. This function adjusts the speed of both the motors based on the value read by the line senors. For your robot to smoothly follow the line, you will have to modify the values of speed that I have used in this function.

Step 35: A Few Notes...

There are some parameters that you can change depending upon the nature of the track where you test your robot. They are given below.

LINE COLOR
If the color of the line is dark (usually black) when compared to that of the background (usually white), you will have to edit the <sensors.h> library as shown in the picture above.

MOTOR SPEED
There are two parameters that define the speed at which the robot moves. MAXSPEED determines the maximum speed of the robot on a scale from 0 to 250. You can change this value to set the desired speed of the robot. ROTATE_SPEED determines the speed with which the robot takes a turn. These two parameters can be adjusted in the main.c file.

That's it. After 35 steps. Now all you got to do is build one on your own and explore the tracks. Good Luck!
For ore project view this link

7 comments:

  1. Hi there! Would you mind if I share your blog with my zynga
    group? There's a lot of people that I think would really enjoy your content. Please let me know. Many thanks

    Feel free to visit my site :: buy backlinks

    ReplyDelete
    Replies
    1. ya of-course u can share..i would feel very great 4 that, i saw ur site its nice giving a good knowledge

      Delete
  2. Currently it sounds like BlogEngine is the best blogging
    platform out there right now. (from what I've read)
    Is that what you are using on your blog?

    Also visit my web page - free online auto insurance quote

    ReplyDelete
  3. Great article, totally what I needed.

    Feel free to visit my web site: gutter cleaning sydney - pureroofgutterrepairssydney.net.au,

    ReplyDelete
  4. I wanted to thank you for this good read!! I absolutely loved every little bit of it.
    I have got you book marked to check out new things you post…

    Also visit my page gutter cleaning sydney - http://pureroofgutterrepairssydney.net.au/,

    ReplyDelete
  5. Howdy this is somewhat of off topic but I was wanting to know
    if blogs use WYSIWYG editors or if you have to manually code
    with HTML. I'm starting a blog soon but have no coding knowledge so I wanted to get guidance from someone with experience.
    Any help would be greatly appreciated!

    my weblog sydney window cleaning

    ReplyDelete
  6. When I originally commented I appear to have clicked on the -Notify me when new
    comments are added- checkbox and now every time a comment is added I receive 4 emails with the same comment.

    There has to be an easy method you are able to remove me
    from that service? Cheers!

    My blog post breast Augmentation sydney

    ReplyDelete