Code covered by the BSD License  

Highlights from
Automata Engineer 2010 NITK

image thumbnail

Automata Engineer 2010 NITK

by

 

29 Oct 2010 (Updated )

This code solves the problem statement of Automata Engineer 2010 NITK.

real4 - Copy.m
%Initialising video preview
vid=videoinput('winvideo',1,'YUY2_640x480');
preview(vid); pause(3);

%Initialising output serial port
ser=serial ('COM10','BaudRate',9600);
fopen(ser);

%test to check whether the set up is right
user_entry = input('test 1');

%initialization of bot location. It is intially assumed to be 6, however 
%the actual bot location can be obtained using the function botloc(prop)
bot_loc=6;

%image aquisition
img=getsnapshot(vid);
im=ycbcr2rgb(img);

%get arena(this includes filtering and converting the rgb image of the 
%          arena to a bw image which can be analysed easily  )
gray=rgb2gray(im);
%caliberate1(trial and error.. important)
%niblack function is used for local thresholding
imt=niblack(gray,0.1,121);
imbw=im2bw(imt);
%se=strel('square',3);
%im_er=imerode(imbw,se);
are = medfilt2(imbw,[3 3]);
imshow(are)
user_entry = input('test 2');
%end of get arena

%determines the orientation of the bot, left,right,up or down. return value
%is an integer which represents the orientation 
o=get_ore(~are);

%get wall- This function filters in the red wall present on the arena
imred=im(:,:,1);
imgrn=im(:,:,2);
imwall=imred-imgrn;
%direct caliberation
wall=im2bw(imwall,0.2545);
imshow(wall)
%user_entry = input('test 3');
%   0.3922=100/255... quite optimum for red regions with pixel value 100
%end of get wall

%wallhormat and wallvermat return the presence of walls in matrix format
%wallhormat- returns the walls oriented horizontally
%wallvermat- returns the walls oriented vertically
wallhormat(are,wall)
wallvermat(are,wall)

%image properties
im_eroded=imerode(are,strel('square',3));
are3=imrotate(im_eroded,10);
%test 1 should be =31
[wall_lab,test]=bwlabel(are3,4);
%imtool(wall_lab)
%user_entry = input('test 4');
prop=regionprops(wall_lab,'Centroid','Area');
%end of image properties 

%getadmat is used to get the adjecency matrix representing the arena
adj_mat=getadmat(are,wall);

%assuming the food is placed only 5 times
for i=1:5
    %reading and processing the food image
    food=imread('foo2.jpg');
    foodloc=food_loc(food)
    user_entry = input('test 5');
    
    %getting the shortest path
    [dist, path] = graphshortestpath(sparse(adj_mat), bot_loc, foodloc);
    %dist
    path
    %user_entry = input('test 6');
    
    %returns the string of motion to be executed by the bot as LFRLL...
    %left forward right left left and so on
    [ mot, o ] = motion( path, dist, o );
    mot=char(mot);
    
    %REAL TIME STARTS NOWWWWWW:
    %The part of the code from here on processes the arena real time and
    %gives directions to the bot and also corrects its path something goes
    %wrong.
    
    %pic=getsnapshot(vid);
    %picrgb=ycbcr2rgb(pic);
    %picpro=get_are(picrgb);
    
    
    
    ii=1;
    jj=1;
    sz=size(mot,2);
    for ii=1:sz
        %temp=mot(i);
    
        p3=prop(path(jj)).Centroid;
        p4=prop(path(jj+1)).Centroid;
        
        %p2=lcen;
        %p1=scen;
    
        pic=getsnapshot(vid);
        picrgb=ycbcr2rgb(pic);
        picpro=get_are(picrgb);
        [lcen scen]=get_cir(~picpro);
        cenbot=(lcen+scen)/2;
        %ang1=angle2Points(p4,p3);
        ang1=angle2Points(cenbot,p4);
        ang2=angle2Points(scen,lcen);
        ang=mod((ang2-ang1+2*pi),2*pi)*180/pi;
        dis = sqrt( (cenbot(1)-p4(1)).^2 +  (cenbot(2)-p4(2)).^2 );
    
        if mot(ii)=='L'
            disp('l');
            %fwrite(ser,'l');
            %pause(3);
            while(ang>15 && ang<345)
                if(ang>15 && ang<180)
                fwrite(ser,'L');
                else fwrite(ser,'R');
                end
                
                pic=getsnapshot(vid);
                picrgb=ycbcr2rgb(pic);
                picpro=get_are(picrgb);
                [lcen scen]=get_cir(~picpro);
                cenbot=(lcen+scen)/2;
                %ang1=angle2Points(p4,p3);
                ang1=angle2Points(cenbot,p4);
                ang2=angle2Points(scen,lcen);
                ang=mod((ang2-ang1+2*pi),2*pi)*180/pi;
            end
        end
    
    
        if mot(ii)=='R'
            disp('r');
            %pause(3);
            while(ang>15 && ang<345)
                if(ang>180 && ang<345)
                fwrite(ser,'R');
                else fwrite(ser,'L');
                end
                pic=getsnapshot(vid);
                picrgb=ycbcr2rgb(pic);
                picpro=get_are(picrgb);
                [lcen scen]=get_cir(~picpro);
                cenbot=(lcen+scen)/2;
                %ang1=angle2Points(p4,p3);
                ang1=angle2Points(cenbot,p4);
                ang2=angle2Points(scen,lcen);
                ang=mod((ang2-ang1+2*pi),2*pi)*180/pi;    
            end
        end
    
        if mot(ii)=='F'
            disp('f');
            %pause(5);
            %jj=jj+1;
            while(dis>10)
                disp('F');
                    if(ang<345 && ang>180)
                    fwrite(ser,'R');
                    elseif(ang>15 && ang<180)
                    fwrite(ser,'L');
                    else
                    fwrite(ser,'F');
                    end
                pic=getsnapshot(vid);
                picrgb=ycbcr2rgb(pic);
                picpro=get_are(picrgb);
                [lcen scen]=get_cir(~picpro);
                cenbot=(lcen+scen)/2;
                %ang1=angle2Points(p4,p3);
                ang1=angle2Points(cenbot,p4);
                ang2=angle2Points(scen,lcen);
                ang=mod((ang2-ang1+2*pi),2*pi)*180/pi;
                dis = sqrt( (cenbot(1)-p4(1)).^2 +  (cenbot(2)-p4(2)).^2 );
            end
            jj=jj+1;
        end
    end
    bot_loc=foodloc;
    user_entry = input('I AM STILL HUNGRY!!!! PLACE MORE FOOD AND ENTER');
end

Contact us