LFR with PID

Discussion in 'Programmer's Corner' started by RRITESH KAKKAR, Aug 7, 2013.

  1. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi,

    I am working on Line follower robot with two sensor connected with variable 20K making voltage divider. directly to ADC input ...it is working fine..
    i want to add PID for gaining more speed and control...
    so, this link tutorial for one sensors..
    how to set offset and other const parameters?

    In the above most link he is using one sensors....i.e. offset 45
    [​IMG]

    but i am using 2 sensors with black line on white the voltage is 0..2 to 0.5 and for black it is more than 2.8V (ranges is 0 to 5V) so, how should i calculate offset or just reading on black line?


    here is the code...
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    4. #define _XTAL_FREQ 20000000  
    5.   int ReadADC(unsigned char ch);
    6.  void main(void)
    7. {     ADCON0=0b01000001;//000 = channel 0, (RA0/AN0)  
    8.   ADCON1=0b10000000;
    9. TRISC=0X00;
    10. unsigned int b;      
    11.   int number;  
    12.   int number1;      
    13.  PR2 = 0b11111001 ;
    14.     T2CON = 0b00000100 ;  
    15.       CCP1CON = 0b00111100;
    16.   CCP2CON = 0b00111100;    
    17.                    PORTC = 0 ;  
    18.     CCPR1L = 145 ;//pwm1  
    19.    CCPR2L = 145 ; //pwm 2  
    20.    __delay_ms(100);
    21.  RC4=1;//forward a
    22. RC3=0; RC0=0;//forward b
    23.  RC6=1;  
    24.  __delay_ms(100);
    25.  RC4=0;
    26. RC3=0;
    27.  RC0=0;
    28.  RC6=0;  
    29.   while(1){
    30. b = ReadADC(2);// compare value  
    31. number = ReadADC(0);     //sensor 1
    32. number1 = ReadADC(1);// sensor 2  
    33. if(number<=b&&number1<=b){
    34.     CCPR1L = 145 ;  
    35.   CCPR2L = 145 ;
    36.   RC4=1;//forward a
    37. RC3=0;
    38.  RC0=0;//forward b
    39.  RC6=1;
    40. }
    41. if(number>=b&&number1<=b){  
    42.    CCPR1L = 155 ;  
    43.    CCPR2L = 155 ;
    44. RC4=0;//forward a
    45. RC3=1;
    46. RC0=0;//forward b
    47. RC6=1;  
    48. }
    49. if(number<=b&&number1>=b){
    50.     CCPR1L = 155 ;    
    51. CCPR2L = 155
    52. RC4=1;//forward a
    53. RC3=0;
    54.  RC0=1;//forward b
    55. RC6=0;  
    56. }
    57.  if(number>b&&number1>b){  
    58.   CCPR1L = 155 ;  
    59.    CCPR2L = 155 ;
    60. RC4=0;//forward a
    61. RC3=0;
    62.  RC0=0;//forward b
    63.  RC6=0;  
    64.  }   } }      
    65.     int ReadADC(unsigned char ch)    
    66.  {     int ret = 0;  
    67.   ADCON0 = 0b10000001 + (ch<<3);    // set channel to read
    68.     __delay_ms(5);  
    69.    GO_DONE = 1;                    // start conversion  
    70.      while(GO_DONE);                    // wait for conversion      
    71.   ret =  (ADRESH & 0x3) << 8;        // get      
    72.   ret +=    ADRESL;                    // result    
    73.  return ret;      
    74.  }
     
  2. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    i have written this for P controller......

    Code ( (Unknown Language)):
    1.  
    2.     #include <htc.h> __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS); #define _XTAL_FREQ 20000000     int ReadADC(unsigned char ch); void main(void){     ADCON0=0b01000001;//000 = channel 0, (RA0/AN0)  ADCON1=0b10000000; TRISC=0X00; unsigned int ch,b;       int number;     int number1;    PR2 = 0b11111001 ;  T2CON = 0b00000100 ;        CCP1CON = 0b00111100;  CCP2CON = 0b00111100;                    PORTC = 0;     int turnr,turnl,kp,tp,offsetr,offsetl,errorl;  int errorr,powerr,powerl;   RC4=1;//forward a RC3=0; RC0=0;//forward b RC6=1; __delay_ms(1000); RC4=0;//forward a RC3=0; RC0=0;//forward b RC6=0;   while(1){ number = ReadADC(0);    //sensor 1 number1 = ReadADC(1);//sensor 2 kp = 1000;                              offsetr = 1.67; //sensor average value on black and wwhite offsetl = 1.57;                             tp = 105;//random set pwm duty cycle      errorr = number - offsetr;        errorl = number1 - offsetl;         turnr = kp * errorr;                         turnr = turnr/100 ;    turnl = kp * errorl;                        turnl= turnl/100;                   powerl = tp + turnr;  //pwm1 change                    powerr = tp - turnl;  //pwm1 change   CCPR2L =  powerl;  //pwm1 change                     CCPR1L =powerr;  //pwm1 change                           if(powerl>0){     RC4=0;//forward a RC3=1; } else{ powerl= powerl* (-1); RC4=1;//forward a RC3=0; //RC0=0;//forward b //RC6=1;} }      if(powerr>0){     RC0=0;//forward b RC6=1; } else{ powerr= powerr* (-1); RC0=1;//forward b RC6=0; //RC0=0;//forward b //RC6=1;} } } }
    3.  
    4.  
     
    Last edited: Aug 8, 2013
  3. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    My recommendation is to declare yourself to be the "project manager" and hire someone to do the engineering work for you.
     
    Brian Griffin likes this.
  4. nigelwright7557

    Senior Member

    May 10, 2008
    487
    71
    You need to look up an equation for PID.
     
Loading...