How to Build a Robot - Line Follower

Overview

This is part 4 of a series of articles on my experiences building a robot that can do various things. I thought it would be neat to create a robot that was easy to put together with a single soldering iron and was also affordable. I made up the following requirements for my robot:

    Many kits are expensive, so it must be relatively inexpensive.
    It must be easily put together without special equipment.
    It must be easily programmable without a complicated IDE or programmer.
    It must be powerful enough for expandability.
    It should run off a simple power source.
    It should be able to follow a line or a wall, and avoid obstacles.

In this article I'll talk about how to program the robot to be a line follower.


Follow a line?


A line follower is the easiest way to make a robot follow a pre-determined path. You only need a way to move and a sensor to determine if the robot is on the line or not. There have been may algorithms developed to keep the robot on the line. The field of engineering that covers these algorithms is called control theory. For this article, I'm going to make a really simple algorithm. In pseudo code:



                 
is robot to the left of the line?
    turn right
is the robot to the right of the line?
    turn left
is the robot on the line?
    move forward

               


Finding a suitable surface

Many people use black electrical tape on the floor to do robots. This is a huge pain and creates a mess. A white dry erase board is a great surface to try out different line following courses. I found this 2x4' board at Home Depot for $10. You can easily add tracks by drawing with a black marker. The white background and the black marker have enough contrast so that the sensors can easily distinguish the line. Rather than calibrate the sensors for every surface, I'm going to assume that they all read approximately the same for a given color. Then I can compare them relative to one another to determine which sensor is currently reading the darkest value. As long as the line I draw only covers up one sensor at a time, this algorithm will provide a reliable way to determine the position of the robot.


Writing the Program

The code below begins by initializing the robot driver and then waiting 5 seconds. This allows me enough time to put the robot on the track before it starts moving. After that, the robot continuously checks the sensors and determines how to move using the algorithm above. The forward speed can be set using the ROBOT_SPEED define. This is a number out of 255. If the robot moves too fast, the algorithm may not have enough time to correct the motion. I also added a check for if the robot is picked up or is about to fall off the course. If all the sensors are reading 1000, that means the robot is most likely off the ground because none of the light is reflecting back. This is useful if you want the robot to stop while you're moving it around.
Potential Improvements

This algorithm does not use averaging or keep track of error. You'll notice in the video below that sometimes the robot looks like it's shaking back and forth. This is caused by oscillations in the algorithm because the robot overshoots the line. One method to fix this would be to use a PID (proportional-integral-derivative) algorithm. In a nut shell, this type of algorithm keeps track of where the robot was (integral), where it might be going (derivative), and where it currently is (proportional). The algorithm I implemented only cares about what is currently happening to the robot. If you wanted to have a faster robot, you would likely need to remove the oscillations (over-corrections) that are slowing the robot down.



                 
#include "robot.h"
#define ROBOT_SPEED  100

void setup()
{
  Serial.begin(38400);
  Serial.println("Boot");
  rbt_init();
  delay(5000);
}

uint16_t lleft,lmid,lright;
boolean wleft,wmid,wright;
void loop()                  
{
  rbt_sns(&lleft,&lmid,&lright,&wleft,&wmid,&wright);
  Serial.print("Left: ");
  Serial.print(lleft);
  Serial.print("Mid: ");
  Serial.print(lmid);
  Serial.print("Right: ");
  Serial.println(lright);
  //off the line
  if(lleft == 1000 && lmid == 1000 && lright == 1000){
    rbt_move(BRAKE,0);
  }
  //follow track
  else{
    if(lleft > lmid && lleft > lright){
      rbt_move(LEFT,ROBOT_SPEED);
    }
    if(lmid > lleft && lmid > lright){
      rbt_move(FWD,ROBOT_SPEED);
    }
    if(lright > lmid && lright > lleft){
      rbt_move(RIGHT,ROBOT_SPEED);
    }
  }
}

               




Follow a line!



Conclusion

In this article I showed the process of writing a control algorithm to follow a line. A line follower is a neat way to learn about control theory and watch a robot navigate a course completely autonomously! In the next article, I'll make the robot navigate around a floor and avoid bumping into obstacles and walls.
Previous
Next Post »
My photo

Hi, I`m Sostenes, Electrical Technician and PLC`S Programmer.
Everyday I`m exploring the world of Electrical to find better solution for Automation. I believe everyday can become a Electrician with the right learning materials.
My goal with BLOG is to help you learn Electrical.
Related Posts Plugin for WordPress, Blogger...

Label

KITAIFA NEWS KIMATAIFA MICHEZO BURUDANI SIASA TECHNICAL ARTICLES f HAPA KAZI TU. LEKULE TV EDITORIALS ARTICLES DC DIGITAL ROBOTICS SEMICONDUCTORS MAKALA GENERATOR GALLERY AC EXPERIMENTS MANUFACTURING-ENGINEERING MAGAZETI REFERENCE FUNDAMENTAL OF ELECTRICITY ELECTRONICS IOT ELECTRICAL ENGINEER MEASUREMENT VIDEO ZANZIBAR YETU TRANSDUCER & SENSOR MITINDO RENEWABLE ENERGY ARDUINO AUTOMOBILE SYNCHRONOUS GENERATOR ELECTRICAL DISTRIBUTION CABLES DIGITAL ELECTRONICS AUTOMOTIVE PROTECTION SOLAR TEARDOWN DIODE AND CIRCUITS BASIC ELECTRICAL ELECTRONICS MOTOR SWITCHES CIRCUIT BREAKERS CIRCUITS THEORY MICROCONTROLLER PANEL BUILDING ELECTRONICS DEVICES MIRACLES SWITCHGEAR ANALOG MOBILE DEVICES CAMERA TECHNOLOGY GENERATION BATTERIES FREE CIRCUITS INDUSTRIAL AUTOMATION SPECIAL MACHINES WEARABLES COMMUNICATION ELECTRICAL SAFETY ENERGY EFFIDIENCY-BUILDING DRONE NUCLEAR ENERGY CONTROL SYSTEM FILTER`S SMATRPHONE BIOGAS POWER TANZIA BELT CONVEYOR MATERIAL HANDLING RELAY ELECTRICAL INSTRUMENTS PLC`S TRANSFORMER AC CIRCUITS CIRCUIT SCHEMATIC SYMBOLS DDISCRETE SEMICONDUCTOR CIRCUITS WIND POWER C.B DEVICES DC CIRCUITS DIODES AND RECTIFIERS FUSE SPECIAL TRANSFORMER THERMAL POWER PLANT cartoon CELL CHEMISTRY EARTHING SYSTEM ELECTRIC LAMP FUNDAMENTAL OF ELECTRICITY 2 BIPOLAR JUNCTION TRANSISTOR ENERGY SOURCE 555 TIMER CIRCUITS AUTOCAD C PROGRAMMING HYDRO POWER LOGIC GATES OPERATIONAL AMPLIFIER`S SOLID-STATE DEVICE THEORRY DEFECE & MILITARY FLUORESCENT LAMP HOME AUTOMATION INDUSTRIAL ROBOTICS ANDROID COMPUTER ELECTRICAL DRIVES GROUNDING SYSTEM BLUETOOTH CALCULUS REFERENCE DC METERING CIRCUITS DC NETWORK ANALYSIS ELECTRICAL SAFETY TIPS ELECTRICIAN SCHOOL ELECTRON TUBES FUNDAMENTAL OF ELECTRICITY 1 INDUCTION MACHINES INSULATIONS ALGEBRA REFERENCE HMI[Human Interface Machines] INDUCTION MOTOR KARNAUGH MAPPING USEUL EQUIATIONS AND CONVERSION FACTOR ANALOG INTEGRATED CIRCUITS BASIC CONCEPTS AND TEST EQUIPMENTS DIGITAL COMMUNICATION DIGITAL-ANALOG CONVERSION ELECTRICAL SOFTWARE GAS TURBINE ILLUMINATION OHM`S LAW POWER ELECTRONICS THYRISTOR USB AUDIO BOOLEAN ALGEBRA DIGITAL INTEGRATED CIRCUITS FUNDAMENTAL OF ELECTRICITY 3 PHYSICS OF CONDUCTORS AND INSULATORS SPECIAL MOTOR STEAM POWER PLANTS TESTING TRANSMISION LINE C-BISCUIT CAPACITORS COMBINATION LOGIC FUNCTION COMPLEX NUMBERS ELECTRICAL LAWS HMI[HUMANI INTERFACE MACHINES INVERTER LADDER DIAGRAM MULTIVIBRATORS RC AND L/R TIME CONSTANTS SCADA SERIES AND PARALLEL CIRCUITS USING THE SPICE CIRCUIT SIMULATION PROGRAM AMPLIFIERS AND ACTIVE DEVICES BASIC CONCEPTS OF ELECTRICITY CONDUCTOR AND INSULATORS TABLES CONDUITS FITTING AND SUPPORTS CONTROL MOTION ELECTRICAL INSTRUMENTATION SIGNALS ELECTRICAL TOOLS INDUCTORS LiDAR MAGNETISM AND ELECTROMAGNETISM PLYPHASE AC CIRCUITS RECLOSER SAFE LIVING WITH GAS AND LPG SAFETY CLOTHING STEPPER MOTOR SYNCHRONOUS MOTOR AC METRING CIRCUITS BASIC AC THEORY BECOME AN ELECTRICIAN BINARY ARITHMETIC BUSHING DIGITAL STORAGE MEMROY ELECTRICIAN JOBS HEAT ENGINES HOME THEATER INPECTIONS LIGHT SABER MOSFET NUMERATION SYSTEM POWER FACTORS REACTANCE AND IMPEDANCE INDUCTIVE RESONANCE SCIENTIFIC NOTATION AND METRIC PREFIXES SULFURIC ACID TROUBLESHOOTING TROUBLESHOOTING-THEORY & PRACTICE 12C BUS APPLE APPS & SOFTWARE BATTERIES AND POWER SYSTEMS ELECTROMECHANICAL RELAYS ENERGY EFFICIENCY-LIGHT INDUSTRIAL SAFETY EQUIPMENTS MEGGER MXED-FREQUENCY AC SIGNALS PRINCIPLE OF DIGITAL COMPUTING QUESTIONS REACTANCE AND IMPEDANCE-CAPATIVE RECTIFIER AND CONVERTERS SEQUENTIAL CIRCUITS SERRIES-PARALLEL COMBINATION CIRCUITS SHIFT REGISTERS BUILDING SERVICES COMPRESSOR CRANES DC MOTOR DRIVES DIVIDER CIRCUIT AND KIRCHHOFF`S LAW ELECTRICAL DISTRIBUTION EQUIPMENTS 1 ELECTRICAL DISTRIBUTION EQUIPMENTS B ELECTRICAL TOOL KIT ELECTRICIAN JOB DESCRIPTION LAPTOP THERMOCOUPLE TRIGONOMENTRY REFERENCE UART WIRELESS BIOMASS CONTACTOR ELECTRIC ILLUMINATION ELECTRICAL SAFETY TRAINING FILTER DESIGN HARDWARE INDUSTRIAL DRIVES JUNCTION FIELD-EFFECT TRANSISTORS NUCLEAR POWER VALVE WWE oscilloscope 3D TECHNOLOGIES COLOR CODES ELECTRIC TRACTION FLEXIBLE ELECTRONICS FLUKE GEARMOTORS INTRODUCTION LASSER MATERIAL PID PUMP SCIENCE SEAL ELECTRICIAN CAREER ELECTRICITY SUPPLY AND DISTRIBUTION FEATURED MUSIC NEUTRAL PERIODIC TABLES OF THE ELEMENTS POLYPHASE AC CIRCUITS PROJECTS REATORS SATELLITE STAR DELTA VIBRATION WATERPROOF