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 test.m
vid=videoinput('winvideo',1,'YUY2_640x480');
preview(vid); pause(3);
ser=serial ('COM10','BaudRate',9600);
fopen(ser);
user_entry = input('test 1');

%initialization
bot_loc=6;

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

%get arena
gray=rgb2gray(im);
%caliberate1(trial and error.. important)
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
o=get_ore(~are);
%get wall
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(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 
adj_mat=getadmat(are,wall);

for i=1:5
    food=imread('foo2.jpg');
    foodloc=food_loc(food)
    user_entry = input('test 5');
    [dist, path] = graphshortestpath(sparse(adj_mat), bot_loc, foodloc)
    %dist
    path
    %user_entry = input('test 6');
    [ mot, o ] = motion( path, dist, o );
    mot=char(mot)
    %REAL TIME STARTS NOWWWWWW
    %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