A function within my for-loop is only executing one time, when I need it to continually output updated values.
6 次查看(过去 30 天)
显示 更早的评论
Hello! I have been having issues with a function that I am calling within a for-loop. The function returns values upon the 1st iteration of the for-loop but thereafter is actually skipped all together. Note that the for-loop still runs all the other code within it, but the function inside it is skipped after iteration 1. I have posted my main function below. The troublesome function ( called ServoAngles() ) within the for-loop is in another '.m' file. I will also post that code as well, in case something is amiss there as well. Thank you for any suggestions and suggestions.
What I have done so far to resolve this issue:
1. Used breakpoints to ensure correct values are being passes into ServoAngles() as arguments.
2. Stepped through the loop. The first iteration will go into ServoAngles() and return values; whereas the 2nd-last iteration will not even go into ServoAngles().
3. I watched a quick debugging video suggested in another video by ImageAnalyst but still no success.
%Here I initialize variables
clear;clc;
cam=imaq.VideoDevice('winvideo',1,'YUY2_640x640','ReturnedColorSpace', 'RGB');
minpixelarea=5;
fontSize=14;
numberOfTargets=1;
I = step(cam);
h = imagesc(I);
set(h, 'EraseMode', 'none');
% Ill load an image, store it as ImgSquaresOnBarrier for next line to utilize.
[distToBarrier,pixelToInchs,barrierCentroids]=DistanceToBarrier(ImgSquaresOnBarrier,numberOfTargets);
for i=1:100
try
%Take a picture and filter out all colors except hunter orange. Find centroids. Send info to ServoAngles()
img = step(cam);
[BW]=HunterOrangeMask(img);
BW= bwareafilt(BW, 1);
BW=bwareaopen(BW,minpixelarea);
s = regionprops(BW, 'centroid');
centroids=cat(1, s.Centroid);
size=size(BW);
%This is the line that only returns values one time and is skipped after iteration one.
[theta_X,theta_Y]=ServoAngles(centroids,size,distToBarrier,pixelToInchs)
catch
title("Searching...")
end
end
delete(cam);
%%My function ServoAngles() is below, which is in a seperate .m file.
function [theta_X,theta_Y]=ServoAngles(centroids,size,distToBarrier,pixelToInchs)
centroid_row=centroids(1);
centroid_col=centroids(2);
%Coords of new origin
xshift=round(size(2)/2);
yshift=round(size(1)/2);
%Centroid coords in shifted system with origin at center of the image.
xCoordPixels=centroid_col-xshift;
yCoordPixels=centroid_row-yshift;
%Translate new coords to inches
xCoordInches=xCoordPixels*pixelToInchs;
yCoordInches=yCoordPixels*pixelToInchs;
%Calculate Angle(in degrees) that servos need to point to target based on distToBarrier
theta_X=rad2deg(asin(xCoordInches/distToBarrier));
theta_Y=rad2deg(asin(yCoordInches/distToBarrier));
end
4 个评论
Walter Roberson
2020-7-19
You indicated that for the 2nd and following iterations that ServoAngles is not even called. That could happen if there were an exception inside the loop for some reason. In the place you have
title("Searching...")
for debugging purposes you should
LE = lasterror();
fprintf('Exception on iteration %d, message = "%s"\n', i, LE.message);
采纳的回答
Walter Roberson
2020-7-19
The catch was being invoked. The reason is
size=size(BW);
That replaces the function size() with a variable named size that was not being expected.
We recommend against naming a variable size or sum or length as it is very common to want to use the functions of the same name after having created a variable with those names. And it confuses readers.
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!