Matlab & Arduino "Maximum recursion limit of 500 reached." problem
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello Everyone,
I use matlab and arduino real time. I active my arduino on matlab. When ı run bottom code ı got
Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your available stack space can crash MATLAB and/or your computer.
How should ı do ?
Thank you for your time and attention.
-------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%MATLAB CODE %%%%%%%%%%%%maina.m.txt %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
warning('off','vision:transition:usesOldCoordinates')
%warning control statement that enables u to indicate how u want Matlab
%to act on certain warnings
clear all
clc
answer=1;
arduino=serial('COM4','BaudRate',9600);
fopen(arduino);
faceDetector = vision.CascadeObjectDetector();
%Detects Objects using the Viola-Jones algorithms
%CASCADEObject means passing object on to a succession of other object
%vision.CascadeObjectDetector() is a funtion or a package to detect faces
obj =imaq.VideoDevice('winvideo', 1, 'RGB24_320x240','ROI', [1 1 320 240]);
%imaq.VideoDevice it allows MATLAB to use Video Device of the system
%It also acquires images from the Image Acquisition Device
%YUY2 is the format of the Camera supported by MATLAB
%ROI is Region Of Interest
%Get the input device using image acquisition toolbox,resolution = 640x480 to improve performance
set(obj,'ReturnedColorSpace', 'rgb');
%form is set(obj,name,value);
%sets the named property to the specified value for the Object obj.
%ReturnedColorSpace is a property that specifies the Color Space we want to
%the Toolbox to use when image data returns to Matlab WorkSpace
figure('menubar','none','tag','webcam');
wait=0;
while (wait<600)
wait=wait+1;
frame=step(obj);
%STEP Acquires a single frame from image acquisition Device
%frame is the Variable assined to an image which is either RGB or
%GRAYSCALE
%Acquires a single frame from the VideoDevice System Object,obj.
bbox=step(faceDetector,frame);
wait
if(~isempty(bbox))
bbox
centx=bbox(1) + (bbox(3)/2) ;
centy=bbox(2) - (bbox(4)/2) ;
c1=(centx);
c2=(centy);
c1
c2
fprintf(arduino,'%s',char(centx));
fprintf(arduino,'%s',char(centy));
end
%BBOX=Bounding Box
%step returns a Matrix of M-by-4 where M is some Variable to bbox
%M defines bounding boxes containing the detected objects
%Each row in Matrix has 4 element Vector [x y width height] in pixels
%The objects are detected from Image Named as 'frame'
%detected objects are from face
boxInserter = vision.ShapeInserter('BorderColor','Custom',...
'CustomBorderColor',[255 0 255]);
%It inserts shapes according to matrix dimensions
%BorderColor is to specify the color of Shape by Default is Black
%Here We set it to 'Custom' so we can use 'CustomBorderColor' to specify
%the color of the border by vector representation
videoOut = step(boxInserter, frame,bbox);
%The Step function here returns an image
%Image consists of a Bounding box for the frame
%The BoxInsert er inserts a frame around the image
%Output image is set to variable 'VideoOut'
imshow(videoOut,'border','tight');
%imshow basically displays images
%parameters 'Border','tight' indicates the compells the images to be
%displayed with out a border
f=findobj('tag','webcam');
%
if (isempty(f));
[hueChannel,~,~] = rgb2hsv(frame);
% Display the Hue Channel data and draw the bounding box around the face.
%%figure, imshow(hueChannel), title('Hue channel data');
rectangle('Position',bbox(1,:),'LineWidth',2,'EdgeColor',[1 1 0])
%Creates 2-D rectangle at Position of BBOX with width and Edgecolor
hold off
%Resets to default behaviour
%Clears existing graphs and resets axis properties to their Defaults
noseDetector = vision.CascadeObjectDetector('Nose');
%Detects nose properties from the video frame using Cascade package
%the properties are assigned to a variable noseDetector
faceImage = imcrop(frame,bbox);
%crops the Image 'Frame' with Bounding BOX
%%imshow(faceImage)
%Displays image
noseBBox = step(noseDetector,faceImage);
%Returns NoseBBOX Matrix
noseBBox(1:1) = noseBBox(1:1) + bbox(1:1);
videoInfo = info(obj);
ROI=get(obj,'ROI');
%returns the value of Specified property from the Obj image
VideoSize = [ROI(3) ROI(4)];
videoPlayer = vision.VideoPlayer('Position',[300 300 VideoSize+60]);
%Play video or display image with specified position
tracker = vision.HistogramBasedTracker;
initializeObject(tracker, hueChannel, bbox);
time=0;
while (time<600)
time=time+1;
% Extract the next video frame
frame = step(obj);
time
% RGB -> HSV
[hueChannel,~,~] = rgb2hsv(frame);
% Track using the Hue channel data
bbox = step(tracker, hueChannel);
% Insert a bounding box around the object being tracked
videoOut = step(boxInserter, frame, bbox);
%Insert text coordinates
% Display the annotated video frame using the video player object
step(videoPlayer, videoOut);
pause (.2)
end
time
% Release resources
release(obj);
release(videoPlayer);
%release(vidobj);
close(gcf)
break
end
pause(0.05)
end
fclose(arduino);
%release(vidobj);
release(obj);
%release(videoPlayer);
-----------------------------------------
-------------------------------------------------
////////////////////////////ArduinoCode/////////////////camservo.ino.txt//////////////////////
#include <Servo.h>
// Title: Auto Pan-Tilt Servo/Cam Control
// Subject: This Sketch receives X,Y coordinates from srial then
// moves the camera to center of those coordinates.
#define servomaxx 180 // max degree servo horizontal (x) can turn
#define servomaxy 180 // max degree servo vertical (y) can turn
#define screenmaxx 320 // max screen horizontal (x)resolution
#define screenmaxy 240 // max screen vertical (y) resolution
#define servocenterx 90 // center po#define of x servo
#define servocentery 90 // center po#define of y servo
#define servopinx 9 // digital pin for servo x
#define servopiny 10 // digital servo for pin y
#define baudrate 9600 // com port speed. Must match your C++ setting
#define distancex 1 // x servo rotation steps
#define distancey 2 // y servo rotation steps
int valx = 0; // store x data from serial port
int valy = 0; // store y data from serial port
int posx = 0;
int posy = 0;
int incx = 10; // significant increments of horizontal (x) camera movement
int incy = 10; // significant increments of vertical (y) camera movement
Servo servox;
Servo servoy;
short MSB = 0; // to build 2 byte integer from serial in byte
short LSB = 0; // to build 2 byte integer from serial in byte
int MSBLSB = 0; //to build 2 byte integer from serial in byte
void setup() {
Serial.begin(baudrate); // connect to the serial port
Serial.println("Starting Cam-servo Face tracker");
pinMode(servopinx,OUTPUT); // declare the LED's pin as output
pinMode(servopiny,OUTPUT); // declare the LED's pin as output
servoy.attach(servopiny);
servox.attach(servopinx);
// center servos
servox.write(servocenterx);
delay(200);
servoy.write(servocentery);
delay(200);
}
void loop () {
while(Serial.available() <=0); // wait for incoming serial data
if (Serial.available() >= 4) // wait for 4 bytes.
{
// get X axis 2-byte integer from serial
//MSB = Serial.read();
//delay(5);
LSB = Serial.read();
//MSBLSB=word(MSB, LSB);
valx = int(LSB);
delay(5);
// get Y axis 2-byte integer from serial
//MSB = Serial.read();
//delay(5);
LSB = Serial.read();
//MSBLSB=word(MSB, LSB);
valy = int(LSB);
delay(5);
// read last servos positions
posx = servox.read();
posy = servoy.read();
//Find out if the X component of the face is to the left of the middle of the screen.
if(valx < (screenmaxy/2 - incx)){
if( posx >= 5/*incx*/ ) posx += distancex; //Update the pan position variable to move the servo to the left.
}
//Find out if the X component of the face is to the right of the middle of the screen.
else if(valx > screenmaxy/2 + incx){
if(posx <= 175/*servomaxx-incx*/) posx -=distancex; //Update the pan position variable to move the servo to the right.
}
//Find out if the Y component of the face is below the middle of the screen.
if(valy < (screenmaxx/2 - incy)){
if(posy >= 5)posy -= distancey; //If it is below the middle of the screen, update the tilt position variable to lower the tilt servo.
}
//Find out if the Y component of the face is above the middle of the screen.
else if(valy > (screenmaxx/2 + incy)){
if(posy <= 175)posy += distancey; //Update the tilt position variable to raise the tilt servo.
}
// Servos will rotate accordingly
servox.write(posx);
servoy.write(posy);
}
}
-----------------------------------------------------
10 Kommentare
Walter Roberson
am 29 Jan. 2016
Okay, don't reinstall MATLAB then. Learn to use the debugger instead.
Antworten (2)
Guillaume
am 1 Feb. 2016
Your error message suggests that the built-in serial.length function calls itself recursively. That shouldn't be the case, so maybe you've modified it. If that is the case, reinstalling matlab should solve the issue. Because of the output of which -all length, we know that you don't have any self-written length function that may shadow the built-in length.
The other option is for you to use the debugger to follow your code along and see where it is recursing.
0 Kommentare
Walter Roberson
am 2 Feb. 2016
You are not connecting to your arduino properly. See http://www.mathworks.com/help/supportpkg/arduinoio/ref/arduino.html
0 Kommentare
Siehe auch
Kategorien
Mehr zu Time Series Collections finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!