Matlab ball follower code not working ( suppose to show correct command) for simulation only
1 view (last 30 days)
Show older comments
clear;
clc
vid=videoinput('winvideo',2');%PLEASE CHECK THE RESOLUTION BEFORE TESTING
set(vid,'FramesPerTrigger',1);
set(vid, 'ReturnedColorspace', 'rgb')
%preview(vid);
pause(10);
rgb_image = getsnapshot(vid);
[a b c]= size(rgb_image);
y=a;
x=b;
x1=x/2-120;
x2=x/2+120;
y1=y/2-30;
y2=y/2+30;
%ser=serial('COM1');
%fopen(ser);
while(1)
rgb_image = getsnapshot(vid);
flushdata(vid);
rbar=0;
cbar=0;
e=0;
fR=rgb_image(:,:,1);fG=rgb_image(:,:,2);fB=rgb_image(:,:,3);
I = imsubtract(fR(:,:,1), rgb2gray(rgb_image));
% I=((fR>=80) & (fG<=70) & (fB<=70));%PLEASE SET THE THRESHOLD VALUES BEFORE TESTING
se=strel('disk',20);
B=imopen(I,se);
final=imclose(B,se);
[L,n]=bwlabel(final);
imshow(rgb_image);% THIS LINE IS NEEDED ONLY FOR TESTING SIMULATION
hold on % THIS LINE IS NEEDED ONLY FOR TESTING SIMULATION
for k=1:n
[r,c]=find(L==k);
rbar=mean(r);
cbar=mean(c);
plot(cbar,rbar,'Marker','*','MarkerEdgeColor','B','MarkerSize',20)% THIS LINE IS NEEDED ONLY FOR TESTING SIMULATION
e=(((cbar>=x1)*2*2*2) + ((cbar<=x2)*2*2) + ((rbar>=y1)*2) + (rbar<=y2))
end
hold off
switch (e)
case 5
disp('left');
case 6
disp('left');
case 7
disp('left');
case 9
disp('right');
case 10
disp('right');
case 11
disp('right');
case 13
disp('forward');
case 14
disp('back');
otherwise
disp('stop');
end
end
%fclose(ser);
if true
% code
end
0 Comments
Answers (1)
Image Analyst
on 23 Mar 2014
Usually what most people do when their code is "not working" is to use the debugger . Go here to learn how: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/ If you do that it will be much faster for you than debugging via the Answers forum where we have to go back and forth multiple times, with long delays in between, asking questions like "What exactly does 'not working' mean?" Once you learn to debug yourself, you can rapidly solve these questions without us.
0 Comments
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!