wiguna149

Blog For Sharing Knowledge

WEIGHTING METHOD FOR READING LINE FOLLOWER SENSOR

Here are some example for reading line follower sensor. The hardware is 16 pair or infrared-photodiode sensors whith ground multiplexing using eight ADC and two multiplexing channels. This program is created with CodeVision AVR. The front sensors for detecting the line is fourteen from sensor 1 to sensor 14 while the other sensors(sensor 0 and sensor 15) is used for special case.

    #define <mega16.h>
    #define <delay.h>

    #define mplex1    PORTD.6
    #define mplex2    PORTD.7

    //variabel global
    int sensor[17];//variable to save sensor ADC reading
    int bitsensor[17];//variable to save sensor comparation to reference
    int black[17],white[17];//save minimum and maximum value for setting sensor reference
    int bobot[17];//variable needed for weighting
    int bebet[17];//variable needed to save weighting value from sensor reading
    eeprom int ref[17]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//reference value for sensor
    int sensoron;//count number of sensor detecting line       

    void initbobot(){//set weighting value for every sensor
        //bobot[0]=-8;
        bobot[1]=-14;
        bobot[2]=-12;
        bobot[3]=-10;
        bobot[4]=-8;
        bobot[5]=-6;
        bobot[6]=-3;
        bobot[7]=-2;
        bobot[8]=2;
        bobot[9]=4;
        bobot[10]=6;
        bobot[11]=8;
        bobot[12]=10;
        bobot[13]=12;
        bobot[14]=14;
    }                   

    void sensor1on(){
    //activate multiplexer channel 1
        mplex1=1;
        mplex2=0;
    }
    void sensor2on(){
    //activate multiplexer channel 2
        mplex1=0;
        mplex2=1;
    }

    void initset(){//initialization for doing reference setting
        int i;
        for(i=0;i<16;i++){
            black[i]=0;
        }
        for(i=0;i<16;i++){
            white[i]=1023;
        }
    }

    void countbobot(){
    //process the waighting value from bitsensor
        int i;//varible for iteration
        bebet=0;
        sensoron=0;
        for (i=1;i<15;i++){//counting sensor value for weighting
            bebet += (int)bobot[i] * (int)bitsensor[i];//sum of weighting
            sensoron+=(int)bitsensor[i];//number of sensor detecting the line
        }
        if(sensoron==0)bebet=0;
        else bebet=bebet/sensoron;//weighting result of sensors reading
    }

    void scan(){
    //read sensor from ADC and save the value in bitsensor variable
        int i;//variable for iteration
        sensor1on();//activate channel 1,channel 2 off
        delay_us(200);//read all sensor connected in channel 1
        sensor[0]=read_adc(0);//transition delay
        sensor[1]=read_adc(1);
        sensor[2]=read_adc(2);
        sensor[3]=read_adc(3);
        sensor[4]=read_adc(4);
        sensor[5]=read_adc(5);
        sensor[6]=read_adc(6);
        sensor[7]=read_adc(7);
        sensor2on();//activate channel 2, channel 1 off
        delay_us(100); //transition delay
        sensor[8]=read_adc(7);
        sensor[9]=read_adc(6);
        sensor[10]=read_adc(5);
        sensor[11]=read_adc(4);
        sensor[12]=read_adc(3);
        sensor[13]=read_adc(2);
        sensor[14]=read_adc(1);
        sensor[15]=read_adc(0);
        for(i=0;i<16;i++){//initialization for bitsensor variable
            bitsensor[i]=0;
        }
        for(i=0;i<16;i++){//compare readings with reference and save result in bitsensor
            if(sensor[i]>ref[i])bitsensor[i]=1;
            else bitsensor[i]=0;
        }
    }

EXPLAINATION

In this method, every sensors has its weight. the weight shows ther position relative to a set point, in this case the center point. Negative value shows the left sensor. Positive value shows the right sensors. Higher absolute value shows how far the sensor is from the set point. We need to add all the weighting value of all sensors detecting the line. This value is then added with number of sensors detecting the line. This final value gives you the position of your sensors relative to the set point. It is like returnng the average value of weighting to estimate the position from sensor reading.

ADVANTAGE

on old ways we will read sensors and check every case for the reading result. This way is of course working, but you need to know how wide the line you will be detecting and make the case suitable for the line. With weighting method, you can get the position from sensor reading for every wide of line. I have tested my line follower and it can run smoothly in 2 cm and 5 cm wide line.

I will write soon about setting reference automatically via ADC.

Leave a comment