Matlab (function block in simulink)

1 view (last 30 days)
Salam Alkhawri
Salam Alkhawri on 9 Sep 2019
Answered: Rajani Mishra on 13 Sep 2019
Hi everyone,
First I want to introduce the issue that I have and hope I make it clear. So, my objective is to use a servo and camera using Raspberry Pi to track a green tennis ball. The thing is that I can get the centre position of the ball (x and y) but my real problem is how I use those parameters to make the servo follow the ball. I have tried to write a simple script using the function block in matlab simulink but I dont see it working at all. So, I looked up and I found someone who's using the same thing a camera and servo but the code is in python. I got his idea but I failed to implement his Idea in matlab. The following is what I have written so far and also the simulink tracking of the system and also a link for the python script that I have found. I'm really still new to matlab and any input or help is appreciated. Also, sorry if my English isn't that great
Kind Regards Sal
% Xin is the centre position of the ball
% Xout is the angle that the servo should
function Xout = fcn(Xin)
%intial position of the servo
panAngle=90;
%Move the servo between this range (90-140), and so that the servo moves the camera to the
%left and the camera keeps tracking the centre of the tennis ball. Once the
%ball is at the centre of the camera, servo must stop
if Xin<60 %( The tennis ball here is on the left side of the camera)
panAngle=Xin+panAngle; %(Move the servo to left)
Xout=panAngle;
% maximum range the servo should move to the left
if panAngle>140
Xout=140;
end
%Move the servo between this range (90-30), and so that the servo moves the camera to the
%left and the camera keeps tracking the centre of the tennis ball. Once the
%ball is at the centre of the camera, servo must stop
elseif Xin>100 %( The tennis ball here is on the right side of the camera)
panAngle=Xin-panAngle; %(Move the servo to right)
Xout=panAngle;
%maximum range the servo should move to the right
if panAngle<40
Xout=40;
end
% if object is not detected or outside the range of the camera (160*120)
% (x,y) then the servo shouldn't move or stay at it's last position since last time it saw the tennis ball
else
Xout=PanAngle;
end

Answers (1)

Rajani Mishra
Rajani Mishra on 13 Sep 2019

Community Treasure Hunt

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

Start Hunting!