如何将现有的.m文件集成到simulink .mdl文件中

时间:2014-01-10 12:12:34

标签: matlab model simulink robotics robot

我在Matlab使用Peter Corke的机器人工具箱。我有.ma文件用于puma560机器人(它用于机器人轨迹。机器人遵循给定的路径)。当我尝试使用前。机器人工具箱中的“sl_ctorque”simulink文件(它是关于计算扭矩方法),我无法将我的.m文件整合到simulink文件中。我的.m文件如下。所以,如果有人知道如何做这个想法,我会很感激。谢谢!

clear;clc; 
mdl_puma560    %to create puma robot

for type=1:3  % main for loop. It turns 3 times. At first, it sets the path
    %           to x-y plane and draw the robot, at second for y-z plane
    %           and then for x-z plane

  if type==1 

% The path of robot for x-y plane    
path=[0 0 1;0 0 0;0 2 0 ;0.5 1 0 ;1 2 0;1 0 0;1.5 0 1;1.5 0 0;
      1.5 2 0;2.2 2 0;2.5 1.6 0;2.5 0.4 0;2.2 0 0;1.5 0 0;0 0 1];


 elseif type==2   

% Same thing as first part    
path=[-0.5 0 0;0 0 0;0 0 1;0 -0.5 0.5;0 -1 1;0 -1 0;-0.5 -1.2 0;0 -1.2 0;
    0 -1.2 1;0 -1.7 1;0 -2 0.7;0 -2 0.3;0 -1.7 0;0 -1.2 0];


 elseif type==3

 % Same thing as first and second part     
path=[0 -0.5 0;0 0 0;0 0 1;0.5 0 0.5;1 0 1;1 0 0;1.3 -0.5 0;1.3 0 0;
    1.3 0 1;1.7 0 1;2 0 0.7;2 0 0.3;1.7 0 0;1.3 0 0];


  end



% I created a trajectory

p=mstraj(path, [15 15 15], [], [1 0 1], 0.02 , 0.2);

% [15 15 15] means the maximum speed in x,y,z directions.
% [1 0 1] means the initial coordinates
% 0.02 means acceleration time
% 0.2 means smoothness of robot


numrows(p)*0.2;    % 200 ms sample interval
Tp=transl(0.1*p);  % Scale factor of robot
Tp=homtrans( transl(0.4,0,0),Tp);  % Origin of the letter
q=p560.ikine6s(Tp);   % The inverse kinematic


for i=1:length(q)
% q matrix has 280 rows and 6 columns. So this for loop turns 280 times
% At every turns , it plots one part of movement. q(1,:), q(2,:), ...  

    p560.plot(q(i,:))


end

end

1 个答案:

答案 0 :(得分:1)

您需要将m文件编写为函数,然后使用 MATLAB功能块。 MATLAB功能块允许您将MATLAB®功能添加到Simulink®模型,以便部署到桌面和嵌入式处理器。此功能对于编码算法非常有用,这些算法在MATLAB软件的文本语言中比在Simulink产品的图形语言中更好地说明。

enter image description here

然后你可以打开块来粘贴你的功能:

enter image description here

查看示例结帐this page