Error implementing PID to a LFR

Discussion in 'The Projects Forum' started by Santosh_16k, Sep 2, 2012.

  1. Santosh_16k

    Thread Starter New Member

    Aug 19, 2011
    19
    0
    I was trying to use PID control in my Line Following Robot but the robot is not following the line as it should. I have used an array of 7 sensors and motors of 12V,2A.The micro controller used is ATmega128. Please tell me where have I gone wrong in the code.The code contains only the proportional part of PID.
    Code ( (Unknown Language)):
    1.  
    2.  
    3. #include<avr/io.h>
    4. #include<util/delay.h>
    5. #include<avr/interrupt.h>
    6.  
    7. #define FREQ 11592000
    8. #define prescaler 1
    9.  
    10. void t0_pwm_init(void);
    11. void t2_pwm_init(void);
    12.  
    13. int OCR_value=0;
    14. int node=0;
    15.  
    16. void motorl(int k)
    17. {
    18.     int duty_cycle;
    19.     duty_cycle = k;
    20.     OCR0=(uint8_t)((duty_cycle*256)/100);
    21.     _delay_ms(1000);
    22. }
    23. void motorr(int k)
    24. {
    25.     int duty_cycle;
    26.     duty_cycle = k;
    27.     OCR2=(uint8_t)((duty_cycle*256)/100);
    28.     _delay_ms(1000);
    29. }
    30. int PID()
    31. {
    32.     int error=0;
    33.     int prior=6;
    34.     if(bit_is_set(PIND,PD0))
    35.     {
    36.         prior=12;
    37.     }
    38.     if(bit_is_set(PIND,PD1))
    39.     {
    40.         prior=10;
    41.     }
    42.     if(bit_is_set(PIND,PD1)&&bit_is_set(PIND,PD0))
    43.     {
    44.         prior=11;
    45.     }
    46.     if(bit_is_set(PIND,PD2))
    47.     {
    48.         prior=8;
    49.     }
    50.     if(bit_is_set(PIND,PD1)&&bit_is_set(PIND,PD2))
    51.     {
    52.         prior=9;
    53.     }
    54.     if(bit_is_set(PIND,PD3))
    55.     {
    56.         prior=6;
    57.     }
    58.     if(bit_is_set(PIND,PD3)&&bit_is_set(PIND,PD2))
    59.     {
    60.         prior=7;
    61.     }
    62.     if(bit_is_set(PIND,PD4))
    63.     {
    64.         prior=4;
    65.     }
    66.     if(bit_is_set(PIND,PD4)&&bit_is_set(PIND,PD3))
    67.     {
    68.         prior=5;
    69.     }
    70.     if(bit_is_set(PIND,PD5))
    71.     {
    72.         prior=2;
    73.     }
    74.     if(bit_is_set(PIND,PD5)&&bit_is_set(PIND,PD4))
    75.     {
    76.         prior=3;
    77.     }
    78.     if(bit_is_set(PIND,PD6))
    79.     {
    80.         prior=0;
    81.     }
    82.     if(bit_is_set(PIND,PD5)&&bit_is_set(PIND,PD6))
    83.     {
    84.         prior=1;
    85.     }
    86.     error=6-prior;
    87.     int correction=4*error;
    88.     return correction;
    89. }
    90. int main()
    91. {
    92.     t0_pwm_init();
    93.     t2_pwm_init();
    94.     DDRD=0x00;
    95.     DDRC=0xFF;
    96.     int t=0;
    97.     int correction=0;
    98.     sei();
    99.     while(1)
    100.     {
    101.         correction=PID();
    102.         t=25-correction;
    103.         motorr(t);
    104.         t=25+correction;
    105.         motorl(t);
    106.     }
    107.     return 0;
    108. }
    109.  
    110. void t0_pwm_init()
    111. {
    112.     TCCR0=(1<<WGM00)|(2<<COM00)|(1<<CS00);
    113.     DDRB|=(1<<PB4);             // selcetOC0 as output pin
    114. }
    115.  
    116. void t2_pwm_init()
    117. {
    118.     TCCR2=(1<<WGM20)|(2<<COM20)|(1<<CS20);
    119.     DDRB|=(1<<PB7);             // selcetOC2 as output pin
    120. }
    121.  
     
  2. crutschow

    Expert

    Mar 14, 2008
    13,003
    3,232
    If you only have the proportional part of the code, how do you expect it to be stable and work? :confused:
     
    Santosh_16k likes this.
  3. Santosh_16k

    Thread Starter New Member

    Aug 19, 2011
    19
    0
    I did not know that all three are essential. I guess that's where I have mistaken!
     
  4. crutschow

    Expert

    Mar 14, 2008
    13,003
    3,232
    Generally all three are needed for the proper response of a feedback control system. In some simple cases you may just need P and I.
     
Loading...