PID line following robot using pic complete code


Contents:

  1. Introduction to PIC Microcontroller (PIC 18f4550)
  2. Introduction to PID algorithm 
  3. Sensor selection
  4. PID Controller Algorithms
  5. Code & Tuning
  6. SCHEMETIC and simulations
  7. Code
The basic purpose of this project is to develope the understanding of PID algorithm.Some of the basic things about making robot will not be covered here.For this you have to see the robotics section about making robot and other related stuff lilke motor selection motor driver etc.the core objective is to discuss the implementation of PID control system.



COMPONENTS:


Components that we have use in building a PID base Line following Robots:

  1. ·       PIC microcontroller 18f4550
  2. ·       IR sensor
  3. ·       Chesses and Motor
  4. ·       LM 324
  5. ·       PID algorithm

  1. Introduction to PIC Microcontroller (PIC 18f4550)


      There were two main alternatives regarding the choice of microcontroller: PIC18F family (specifically the PIC18F4550, due to familiarity, and it having the needed features and number of ports), or Arduino (most likely the Nano or UNO, since both have enough processing power and I/O pins for it to be unnecessary to consider the more powerful models). 
          
                  PIC 18f4550:
            The PIC18F4550 is the highest performance USB-equipped model in Microchip, INC. popular PIC18 8-bit microcontroller line. It features (Microchip, Inc. 2009b, Microchip Inc. 2009a):


      32 KB Flash Program Memories.
      A 12 MIPS CPU
      2KB RAM.
      256 byte data EEPROM
   1 x A/E/USART Port
     1 x MSSP (SPI/I2C) Port
      1 x CCP & 1 x ECCP
      1 x 8-bit & 3 x 32-bit Timers
      13 x 10-bit Analog Inputs
       2 x Comparators.
      1 x USB 2.0.
      35 x Digital I/O Pins

This microcontroller has been used extensively. It can be programmed natively in Microchip PIC assembly, or in C18, Microchip’s implementation of the C programming language for use with their PIC18 line. Due to their popularity, there exist a great number of open-source projects developed for the PIC18F line, along with libraries, tutorials, and many articles on coding and developing using these microcontrollers. They are also used to great extent professionally.


2-Sensor selection

IR sensor:

 Depending on the accuracy we have choose IR sensor. The basic concept of IR(infrared) is  obstacle detection by transmitting a IR signal(radiation) in a direction and  a signal is received  at the IR receiver when the IR radiation bounces back  from a surface of the object. For Ir object can be anything the IR LED transmits the IR signal on to the object and the signal is reflected back from the surface of the object. The reflected signals are received by an IR receiver. 



 IR Transmitter:
 IR transmitter is modulation of the information signal with carrier signal, because the receiver modules which are available off-the-shelf are made for a particular carrier frequency. So it is clear that when you chose a particular IR receiver module, you also need to transmit the modulated wave with the same carrier frequency of that of a IR receiver module. Modulating a 38 Khz carrier signal.
IR Receiver:
A IR receiver with readily available off-the-shelf modules.the receiver is designed for 38 kHz carrier signal; hence the IC selected should work for the same frequency. The IC TSOP4838 will serve as a receiver module, which is compatible with both TTL and CMOS logic. This means that we can directly get digital signal from the receiver module and then connect it to the microcontroller. The Implementation of IR receiver is explained using an LED as an indicator.

3- Introduction to PID algorithm: 

The Proportional Integral and Derivative (PID) controler is the control algorithm and mostly used in industrial control.  PID  algorithm allows engineers to operate them in a simple and straightforward way . The name shows PID,The three words  Proportional ,Integral and derivative.


The Proportional Response:

The proportional component  in PID algorithm is the difference of the set point of the system and the process variable. Now this difference is called Error. The proportional gain (Kp) is the ratio between output response and the error signal.

The Integral Response

Integral in PID is the sum of the error term over the time. If the result is a small error the integral component will increase slowly. Now the integral term will continually increase over the time unless and until the error is going to be zero, so the ultimate effect is driving the Steady-State error towards zero. The difference between the process variable and set point is the Steady state error.

The Derivative Response:

 The function of derivative component is to decrease the output if the process variable is going to increase. The derivative is the rate of change of the process variable. The Increase in the derivative time (Td)  will make the system to react strongly and to make changes in the error term.This will increase the overall speed. The very small (Td) the derivative time is used in most practical systems.Derivative Response is sensitive and highly sensitive to the noise and in the process variable. If the feedback is noisy or the control loop rate is very much slower, The control system will be unstable due to derivative response.

4-PID Controller Algorithms:

1 In a case when robot is deviated to left it then pushes to move towards right
2. In ac case of deviation towards right it is forced to move towards left

       The accomplishment of this task gives our robot is following the zigzag path
       Using this method the speed we were giving was much less as error getting by using pid. And this technique was also not reliable for the turns. To apply this algorithm it was better to use the error calculation method after calculating error giving motor its proportional value and them it changes speed and in turn it gets back to the line.
e.g.-the deviation ranges between -3 to +3.


         Kp is the proportionality constant it will give negative value when robot is be towards left of the line then the right motors gets slows down.
This is a good practical approach for many cases. But not suitable because of these reasons

1. It affects accuracy of the robot when the robot is not much effective it gets back to mean position very slowly.

2. To solve this problem we have to increase its proportionality contact but it will then overshoot when robot comes to mean position due to inertia-cross to other side then again overshoot results in deviating result.

PID Controller Algorithms:
PID is a feedback mechanism.
It has three components on which it works that are

1: error
2: time of deviation
3: overshoots

Efficiency of the PID algorithm is based on these three parameters these helps to reach at mean position much faster

 The three constants are set by hit and trial method experimentally.
  1. Kp
  2. Ki
  3. Kd

Using only kp parameter the robot will be applied and give the results as the classical ine following algorithm discussed earlier.
Ki: because of ki robot is forced to follow mean position.
Kd: kd does not allow the robot to perform any deviation



Code:

Given below is the code for measuring the deviation
‘00000001’ this means robot is towards rightmost this code works in a manner that is robot is at right the deviation will be recorded negative

Getting these results PID then sums all the previous deviations and generates an integral called total error. The difference of the deviation is measured by derivative. The error calculated is then send to this code it will give the correction
These lines should run in each iteration.



Tuning:

PID tuning is greatly affected by the floor where it is going to be use and the track where robot is going be run and these physical parameters cannot be calculated mathematically. For example motor inductance, friction centre of mass etc. these quantities can be just guessed and applied by hit and trial method.

 Sensor circuit:
Sensors are the array of 7 IR sensors which are connect with operational amplifier LM324 which converting the analog output of sensor into digital by comparing the threshold level of line then the digital output of opamp are given to microcontroller.

 Main circuit:
The sensor array that we have made in last lab will be connected to PORT B. the motor driver IRFZ44N is a Mosfet  we have us Mosfet due to the fact that it can drive high voltage and current is per requirement we need to have a motor driver that has capability 
to handle high voltage and current. For the accuracy of the robot and requirement of the algorithm we selected IRFZ44N. To control the motion of the motors and to control the speed of the motors a PWM waveform with variable duty cycle is applied on the gate of the IC. The main circuit of the robot is on 18f4550 which is most popular micro controller of pic micro’s having 12 bit ADC resolution 8 analog to digital pins which are used for tuning PID occupied by potentiometers 2 CCP modules which controls PWM given to motors which ultimately controls the speed of robot having 33 I/O pins in which 8 is used for reading data from sensors and 8 are used in 16x2 LCD display interface circuit. Microcontroller take decision on the base is of sensor outputs and after applying PID it controls the motion of robot by changing speed of motors accordingly.

Motor driver:
Our motors are derived by IRFZ44N.
IRFZ44N is a motor driver made up of mosfets. Only code is not sufficient for desired results.
By continuously hit and trials it will effect more. It is not necessary to use all three parameters P,I and D.
If only PI is enough it is not necessary to use derivative part.
PID also requires settling time that is why it will also consume this time in showing accurate results.

5-SCHEMETIC:


PID algorithm implementation



Output:

The robot is successfully following the given arena.
Output is very much accurate and deviations are much reduced to very low level.
Kp=50
Kd=4

In this project our main focus was to cover the PID algorithm implementation.some of the sections about making robot are not included here.

Below is the Code of PIC micro-controller. Enter E-mail address to get complete code file and practice it.

Code:

All rights reserved www.makeitmech.com
PID line following Robot Code:

#include <18f4550.h>
#device adc=10
#fuses NOWDT,PUT,HS,PROTECT
#use delay(clock=20M)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
#include <lcd.c>
\\Variable Initialization 
char c;
float value,pwm1,temp,pwm2,error,preverror,totalerror,kp,ki,kd,filter0;
intpb,speedleft,speedright,filter;
charch;

void main(void)
{
enable_interrupts(INT_RDA);  //Serial Interrupt Enable 
enable_interrupts(GLOBAL);  //Global Interrupt Enable
setup_ccp1(CCP_PWM);   // CCP1 Initialization 
setup_ccp2(CCP_PWM);   // CCP2 Initialization
totalerror=0;    // PID LOOP Variables
error=0;     
preverror=0;
filter0=0;
filter=0;
kp=0;
ki=0;
kd=0;
pwm1=0;
pwm2=0;





Share To:

Post A Comment:

0 comments so far,add yours