Advanced Line Following Robot - Step 5: The Programming

Submitted by Pratheek on February 3, 2010 - 8:31am.



In this page I will explain the final process of building the robot - Programming.


The program for the robot is written in WinAVR and the programmer used to transfer the program to the microcontroller is PonyProg. I used a serial programmer to program the robot. I won't be explaining how to program the robot as this step varies a lot from the software and the hardware programmer you  use. For programming the microcontroller, you can follow the instructions in this page -


The program consists of the following files - 

Lotl.c - This is the most important file. It contains the main function.

Lotl.h - This file contains all the function and variable definitions used by the program.

lcd.c and lcd.h - these files are used to control the LCD. I downloadeed them from .


Note: You will have to disable the JTAG fuse of the microcontroller. The Atmega16 comes shipped with the JTAG feature enabled and this renders some pins of PORTC unusable which are required to control the LCD in this project. Once JTAG is disabled these pins can be used as standard IO pins.


Here is an explanation of the functions used in the program - 


InitPorts() - Initializes all the pins as inputs or outputs. I ususally keep a separate function for initialization as the program becomes more clear and since all the initialization has been done in one place, additions and modifications become simpler.


InitInterrupts() - This function sets the interrupt registers. It has to be called just one at the start of the program.


InitPWM() - Initializes the timer registers for Pulse Width Modulation for controlling the motor speeds. 


PowerLEDOn() and PowerLEDOff() - switch on and off the power LED.



BacklightOn() and BacklightOff() - switch on and off the LCD backlight.



CalcError() - This method reads the sensor values and calculates the current error.


LeftMF() - Drives the left motor forward.


LeftMB() - Drives the left motor backward.


LeftMS() - Stops the left motor.


RightMF() - Drives the right motor forward.


RightMB() - Drives the right motor backward.


RightMS() - Stops the right motor.



Tuning the PID control

This is the most interesting part in building a PID control. In this step one should tune the Kp, Ki and Kd values to get the best results. I cannot give you the values because what works for me will not work for you. The optimum Kp, Ki and Kd values vary a lot from robot to robot. And the best way to determine the optimum values is by trial and error.

First, set all values to 0 and start with tuning the Kp value. First time I just gave an approximate value. Seeing the robot perform will determine what you should do next. If the robot wobbles a lot reduce the Kp value, if the doesn't follow the line (goes straight in curves) increase the Kp value. Tune the Kp value till the robot smoothly follows the line. By now you will have observed that the robot goes with no acceleration on straight lines. Now, tune the Kd term. After tuning the Kd term move to the Ki term. After which, you will see the robot first center over a straight line and then accelerate also.

Note: The optimum Kp, Ki and Kd values vary a lot even from track to track. You can only know these optimum values by testing. 


Time taken for building the robot


Research: 10 -  11 hrs

Designing: 3 - 4 hrs

Mechanics and Electronics: 5 - 6 hrs

Programming: 4 - 5 hrs

Testing: 15 - 20 hrs


Total time taken: 35 - 40 hrs


Possible Upgrades


1. Store the Kp, Ki and Kd values in the microcontroller's EEPROM so that you don't have to set them every time you need to use the robot(I really wanted to do this but did not due to lack of time).


2. Use encoders for wheels this will give you input like your speed, acceleration and even position if possible.


3. Add another two sensors in the front to detect the surface color and implement a algorithm that will detect changes in surface color(eg. black to white) on the go.


4. Use Li - ion or Li - poly batteries. These will improve performance quite a lot owing to their low weight and the power they can provide. Implementing an onboard charger will be really useful.


5. I myself haven't figured out how to do it but implementing an algorithm that will enable the robot map the line it is following will be a great project in itself.






Code V2.2(SOR).zip98.45 KB