Clear Filters
Clear Filters

How can I pass output of a function in another function

3 views (last 30 days)
I have 2 function and one script.
The script basically calls 'BallDetection' function and stores the output in 3 matrices as Zvec,Avec,Dvec.Now I want to pass the values of Avec and Dvec in a second function called MyUpdatedEKFFun.
So how do I do that is there any other method you can suggest will be very helpful.
Thanks.
  4 Comments
Tipu Sultan
Tipu Sultan on 16 May 2019
The script is as follows:
D = 'C:\Users\Tipu_Standard\Desktop\Khepera_updated_pics';
S = dir(fullfile(D,'original*.jpg')); % pattern to match filenames.
N = numel(S);%Calculate number of elements in S
%C = cell(1,N,N);%Create ecll array here 1 by N cell array of of empty matrices
Zvec = zeros(1,10);
Dvec = zeros(1,10);
Avec = zeros(1,10);
for k = 1:N
F = fullfile(D,S(k).name);
I = imread(F);
%C{:,:,k} = BallDetection(I);
%[Zvec(k),Dvec(k),Avec(k)] = fun1(I);
[Zvec(k),Dvec(k),Avec(k)] = BallDetection(I);
end
BallDetection function is as follows:
function [Z,range,Angle] = BallDetection(I)
%function [z,distance,angle] = fun1(I)
%function [Zvec,Dvec,Avec] = fun1(I)
B=rgb2gray(I);
A=adapthisteq(B);
% Display the original image
figure,imshow(A);
distance=0;
angle=0;
rmin=8;
rmax=30;
% Find all the circles with radius >= 8 and radius <= 30
[centersdark,radiidark] = imfindcircles(A,[rmin rmax],'Objectpolarity','dark','Sensitivity',0.85);
k=numel(radiidark);
viscircles(centersdark,radiidark ,'EdgeColor','b');
if(k==0)
z=0;
else
z=1;
p=2*radiidark;
f=2.1;
w=62;
h=144;
s=2.88;
%distance=(f*w*h)/(p*s);
distance=(f*w*h)/(p*s);
a=(192/2)-centersdark(1);
b=144-centersdark(2);
%angle=atan2(a,b)*(180/pi);
angle=atan2(a,b)*(180/pi);
end
%tri = [z,distance,angle]
Z = z;
range = distance;
Angle = angle;
end
MyUpdatedEKFFun is as follows:
function [X,Y,R,A] = MyUpdatedEKFFun(Avec,Dvec,t)
%% Function to implement Extended Kalman Filter
% Initialize S,Esimator Vector,Pre allocate memory to diferent partial derivatives
S = [100 0 0 0 0 0;
0 100 0 0 0 0;
0 0 100 0 0 0;
0 0 0 100 0 0;
0 0 0 0 100 0;
0 0 0 0 0 100];
prev_S = S;
Big_lambda = eye(3);
a=0;
b=0;
c=0;
p=0;
q=0;
s=0;
est_vec=[ a ; b; c; p; q; s];
%theta = [ 31 24 18] ;
%t= [ 1 1.1 1.2 ];
%r= [330 364 379];
%% Initialize angles,range,time
% Intialize angles,range and time from the input given to the function when
% called the function
%theta = [theta1 theta2 theta3 theta4 theta5 theta6 theta7 theta8 ]; % theta defines the angles measured from the 'BallDetection' function
%t= [ t1 t2 t3 t4 t5 t6 t7 t8 ]; % t defines the time of each image captured
%r= [r1 r2 r3 r4 r5 r6 r7 r8 ]; % r defines distance measured from the 'BallDetection' function
theta=Avec;
tm=t;
r=Dvec;
%% Input Measurement vector
% Create a matrix with the angles,range,time as input measurement vector
x = [ r; theta ; tm]; % Input Measurement vector
%dif_x=zeros(2,3);
%% Preallocation various matrices
% Preallocate memory and create measurement vector
dif_a = arrayfun(@(t) [t^2 t 1 0 0 0; 0 0 0 t^2 t 1],t,'UniformOutput',false); % New Measurement vector
%time=2;
time=1.1;
dif_x = zeros(2, 3, 3); % Pre-allocation
w = zeros(2, 1, 3); % Pre-allocation
%pred_x = zeros(3, 3, 3);
W = zeros(2, 2); % Pre-allocation
y = zeros(2, 1, 3); % Pre-allocation
K = zeros(6, 2, 3); % Pre-allocation
%% Time update (Prediction)
% Repeat until the predefined condition is met and update estimator vector,Kalman gain,'S'.
for i=1:9
%% Calculte various matrices for a given iteration
% Calculate different partial dervatives,noise,kalman gain,measurement
% vector,new estimator vector
dif_x(:, :, i) = [-cos(theta(i)), r(i).*sin(theta(i)), 2*a.*t(i)+b;...
-sin(theta(i)), -r(i).*cos(theta(i)), 2*p.*t(i)+q];
W(:,:,i) = dif_x(:,:,i) * Big_lambda(i) * dif_x(:,:,i)';
pred_x = x(:,i) + [ 21.15; 0.06; 0.08] % Predicted Input Measurement Vector
w(:,:,i) = dif_x(:,:,i) * (x(:,i) - pred_x); % Measurement noise vector
y(:,:,i) = dif_a{i} * est_vec + w(:,:,i); % New Measurement vector
K(:,:,i) = prev_S*dif_a{i}'/((W(:,:,i) + dif_a{i}*prev_S*dif_a{i}')); % Kalman Gain
%% Update
% Update until a precondition is met
S_new = (eye(6) - (K(:,:,i) * dif_a{i})) * prev_S; % Upadte 'S'
prev_S = S_new;
est_vec_new = est_vec + K(:,:,i)*(y(:,:,i)-dif_a{i}*est_vec); % Measue new estimator vector
cond = abs(est_vec_new - est_vec);
if cond < 0.003 % Check the condition
break
end
est_vec = est_vec_new; % Update the estimator vector
a=est_vec(1,1); % Time rate of change of velocity
b=est_vec(2,1); % Velocity corresponding direction
c=est_vec(3,1); % Initial displacement with respect to S5
p=est_vec(4,1); % Time rate of change of velocity
q=est_vec(5,1); % Velocity corresponding direction
s=est_vec(6,1); % Initial displacement with respect to S3
%X(i) = a*time.^2+b*time+c; % To calculate x co-ordinate for t>=3
%Y(i) = p*time.^2+q*time+s; % To calculate y co-ordinate for t>=3
%figure,plot(t,meas_equa1)
%new_t=[t,time];
%figure,plot(r_cosTheta,r_sineTheta)
%% Co-ordinate determenation
% Calculate X,Y co-ordinates for t>=time
X(i) = a*time.^2+b*time+c % To calculate x co-ordinate for t>=time
Y(i) = p*time.^2+q*time+s % To calculate y co-ordinate for t>=time
R(i) = (X(i)^2+Y(i)^2) % To calculate next Range
A(i) = pred_x(2,1) + atan(Y(i)/X(i)) - 5 % To calculate new theta
time = time + 0.1
%plot(pred_x,R)
%hold on
plot(theta,r,'--bo')
hold on
plot(A,R,'--ro')
end
end
Thanks in advance.

Sign in to comment.

Answers (0)

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!