translate a script function into a matlab function for use in simulink

1 次查看(过去 30 天)
Subject: Translating a function that works using Matlab command into a Matlab function to be used in Simulink
Dear reader,
Greetings! I hope you're doing well
I have written a function that takes an intial condition, [ 0 0 0] , a right wheel ticks vector, and a left wheel ticks vector
the function works perfectly when i call it in matlab, but i want to work in simulink environnement, so i need to translate it into a matlab function, please help me with that.
Here is the function that works in matlab
function [filtered] = filtered_position(init,aa,ab)
w = 150; t2mm = 0.349;
filtered = zeros(length(aa),3);
for k = 1: length(aa)
if aa(k) == ab(k)
th = init(3);
x = (init(1) + aa(k)*t2mm*cos(th));
y = (init(2) + aa(k)*t2mm*sin(th));
% scatter(x,y,'g')
else
alpha(k) = (t2mm*(aa(k)-ab(k)))/w; R(k) = aa(k)/alpha(k);
th = mod(init(3) + alpha(k),2*pi);
cx = init(1) - (R(k)+(w/2))*(sin(init(3)));
cy = init(2) + (R(k)+(w/2))*(cos(init(3)));
x = cx + (R(k)+(w/2))*(sin(th));
y = cy - (R(k)+(w/2))*(cos(th));
% scatter(x,y,'r')
end
% scatter(x,y,'r')
filtered(k,:) = [x,y,th] ;
init = [x,y,th];
end
You can call this function using this example:
[right_left] = [64 64
128 128.5
128.5 128.5
128.5 128.5
64 64.5
64.5 64
128.5 128.5
64 64.5
64.5 64
128.5 128.5
128.5 128.5
128.5 128.5
128.5 128.5
64.5 64]
init = [0,0,0]
[filt] = filtered_position(init,right_left(:,1),right_left(:,2));
plot(filt(:,1),filt(:,2))
This, would give the following result:
and this is what i wanna do using simulink. and a matlab function.

回答(1 个)

BhaTTa
BhaTTa 2024-9-4
@Welid Benchouche, i assume that you want to translate your MATLAB function into a MATLAB Function block that can be used in Simulink.
Please refer to this documentation to know about the implementation of MATLAB Function block.

类别

Help CenterFile Exchange 中查找有关 Simulink 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by