Call MATLAB function in appdesigner

조회 수: 4 (최근 30일)
Celia Sanchez-Giron
Celia Sanchez-Giron 2022년 4월 21일
댓글: Jon 2022년 4월 21일
Hi,
I would like to know how can I call my function (inicio) in app designer. This is my function that I wrote directly in appdesigner because I don't know how to write it in MATLAB and call it.
function inicio(app)
app.robotMTH = rigidBodyTree("DataFormat", "Column");
%MODELO ROBOT DH
nJoints = 6;
% a alpha d theta
dhparams = [0 -pi/2 0.290 0;
0.270 0 0 -pi/2;
0.070 -pi/2 0 0;
0 pi/2 0.302 0;
0 -pi/2 0 0;
0 0 0.072 pi];
body0 = rigidBody('body0'); %Crear un objeto de sólido rígido
jnt0 = rigidBodyJoint('jnt0','revolute'); %Crear la articulación.
jnt0.JointAxis = [0, 0, 1]; %Eje de movimiento de la articulación.
%jnt0.PositionLimits = rango (1,:);
jnt0.HomePosition = dhparams(1,4);
tform = trvec2tform([0, 0, 0]);
setFixedTransform(jnt0,tform);
body0.Joint = jnt0;
addBody(app.robotMTH,body0,'base');
n= length (dhparams); %número de cuerpos del robot
bodies = cell(n,1);
joints = cell(n,1);
for i = 1:n
bodies{i} = rigidBody(['body' num2str(i)]);
if i == n % El ultimo cuerpo es fijo
joints{i} = rigidBodyJoint(['jnt' num2str(i)],"fixed");
else
joints{i} = rigidBodyJoint(['jnt' num2str(i)],"revolute");
%joints{i}.PositionLimits = rango (i+1,:);
end
transform = makehgtform('zrotate', dhparams(i,4)) * makehgtform('translate',[dhparams(i,1) 0 dhparams(i,3)])*makehgtform('xrotate', dhparams(i,2));
setFixedTransform(joints{i},transform);
bodies{i}.Joint = joints{i};
if i==1
addBody( app.robotMTH,bodies{i},'body0')
else
%Añadir visual pero se amorfa y no me lo añade en el eje que quiero
addVisual(app.robotMTH.Bodies{i},"Mesh", 'BRAZO7.stl',transform);
addBody( app.robotMTH,bodies{i},bodies{i-1}.Name)
end
end
end
I wrote this in appdesigner but it doesn't work
methods (Access = public)
function appDesignerinicio(app)
Output = app.inicio.Value;
end
end

답변 (2개)

chrisw23
chrisw23 2022년 4월 21일
Your function has no arguments and no return value. So just use app.inicio() to call your function. But it seems that this is only a part of your problem. As the app has a GUI, you probably should call your function within a callback (startup or uicontrol).
  댓글 수: 1
Jon
Jon 2022년 4월 21일
Hi Chris - Sorry, didn't see your post until after I submitted mine but I guess we are aligned in our suggestions to the OP anyhow

댓글을 달려면 로그인하십시오.


Jon
Jon 2022년 4월 21일
It looks like your function just creates a variable called robotMTH and doesn't use anything in the app object to do its calculation. It looks like perhaps njoint and dhParams may vary under different scenarios, so maybe you want them to be arguments to incio.
So I would define a function
function robotMTH = incio(njoint,dhParams)
body0 = rigidBody('body0'); %Crear un objeto de sólido rígido
jnt0 = rigidBodyJoint('jnt0','revolute'); %Crear la articulación.
jnt0.JointAxis = [0, 0, 1]; %Eje de movimiento de la articulación.
%jnt0.PositionLimits = rango (1,:);
jnt0.HomePosition = dhparams(1,4);
tform = trvec2tform([0, 0, 0]);
setFixedTransform(jnt0,tform);
body0.Joint = ...
...
end
and then within the appropriate callback in appdesigner call it using:
%MODELO ROBOT DH
nJoints = 6;
% a alpha d theta
dhparams = [0 -pi/2 0.290 0;
0.270 0 0 -pi/2;
0.070 -pi/2 0 0;
0 pi/2 0.302 0;
0 -pi/2 0 0;
0 0 0.072 pi];
app.robotMTH = incio(nJoints,dhparams) % assign robotMTH to app object
If nJoints and dhparams really never change then you could leave them hard coded in the body of the incio function and just define incio with no input arguments at all

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by