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

조회 수: 3 (최근 30일)
Welid Benchouche
Welid Benchouche 2022년 7월 26일
답변: BhaTTa 2024년 9월 4일
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