Trying to make loop works.

I need to make a loop keep running until a condition in the code is met to break the loop.
I was thinking of using a while loop like this:
while (Y<60);
motorX_down;
end
while(Y>60);
motorX_up
end
if (Y==60);
motor_stop;
end
I use this code to control my motors according to the feedback from the image processing. My objective is to make the camera move so that its center is the same as the detected object coordinate. I had the image processing part done and can get the detected object coordinate in X and Y.
Then arduino will move the motor to adjust the camera.
Now my frame size 160x120 with X:80 and Y:60 as the center.
motorX_up will make the camera move up. motorX_down will make the camera move down. motor_stop will stop the camera.
So far i'm only trying to make the camera move vertically only to test it.
But when I run the code, the motor move only down :( So maybe I thought I had the while loop wrong.

Respuestas (1)

Honglei Chen
Honglei Chen el 5 de Mzo. de 2013

1 voto

Because you never update Y in your first loop so it keeps going down. You need to update Y value in the loop, for example
Y = Some_function_to_generate_Y;
while Y~=60
if Y < 60
motorX_down;
else
motorX_up;
end
Y = Some_function_to_generate_Y;
end

5 comentarios

Adrian Dronca
Adrian Dronca el 5 de Mzo. de 2013
Y is a variable not a function. It must be initialized before using it.
Aliff
Aliff el 5 de Mzo. de 2013
Editada: Aliff el 5 de Mzo. de 2013
Ok so this is the whole code and value X and Y is generated at the for loop
close all;clear all;clc;
a=arduino('COM14');
a.pinMode(2,'OUTPUT');
a.pinMode(3,'OUTPUT');
a.pinMode(4,'OUTPUT');
a.pinMode(5,'OUTPUT');
a.pinMode(9,'OUTPUT');
a.pinMode(10,'OUTPUT');
vid=videoinput('winvideo',1,'YUY2_160X120');
set(vid,'TriggerRepeat',Inf);
vid.returnedcolorspace='rgb';
vid.FrameGrabInterval=2;
start(vid) %start the video aquisition here
while(vid.FramesAcquired<=200)
data1=getdata(vid,1);
C=imrotate(data1,90);
data=imcomplement(C);
diff_im = imsubtract(data(:,:,3), rgb2gray(data));
diff_im=medfilt2(diff_im,[1 2]);
diff_im=im2bw(diff_im,0.26);
diff_im=bwareaopen(diff_im,50);
bw=bwlabel(diff_im,8);
stats=regionprops(bw,'BoundingBox','Centroid');
imshow(C)
hold on
This is a loop to bound the yellow objects in a rectangular box and along with its center coordinate
for object=1:length(stats)
bb=stats(object).BoundingBox;
bc=stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','Y','LineWidth',2);
plot(bc(1),bc(2),'-m+');
a=text(bc(1)+15,bc(2), strcat('X:', num2str(round(bc(1))), 'Y:', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
X=num2str(round(bc(1)));
Y=num2str(round(bc(2)));
Honglei Chen
Honglei Chen el 5 de Mzo. de 2013
I can't comment on what you are trying to do here, but your while loop is on vid.FrameAcquired. Once you do
data1 = getdata(vid,1);
I think you need to increase the vid.FrameAcquired by 1. Otherwise your count never goes over 200.
Aliff
Aliff el 5 de Mzo. de 2013
Editada: Aliff el 5 de Mzo. de 2013
The while loop is okay except for that. But from this code here X and Y can only be generated with this code:
for object=1:length(stats)
bb=stats(object).BoundingBox;
bc=stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','Y','LineWidth',2);
plot(bc(1),bc(2),'-m+');
a=text(bc(1)+15,bc(2), strcat('X:', num2str(round(bc(1))), 'Y:', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
X=num2str(round(bc(1)));
Y=num2str(round(bc(2)));
end
This for loop always update its center coordinate, so maybe I could use the code to move the motors inside this loop?
Honglei Chen
Honglei Chen el 5 de Mzo. de 2013
Sounds reasonable but you are really the best person to answer that as I don't know your algorithm.

Iniciar sesión para comentar.

Categorías

Más información sobre Arduino Hardware en Centro de ayuda y File Exchange.

Preguntada:

el 5 de Mzo. de 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by