微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MATLAB for Robotics中的动画

如何解决MATLAB for Robotics中的动画

到目前为止,这是我的代码

function P = RobotPosition(T)
    x = T(1,4);
    y = T(2,4);
    z = T(3,4);
    
    P = [x y z];
   

    
function T = RobotConv(theta,d,a,alpha)
    rad = pi/180;
    M_theta = [cos(theta*rad) -sin(theta*rad) 0 0;sin(theta*rad) cos(theta*rad) 0 0;0 0 1 0;0 0 0 1];
    
    M_d = [1 0 0 0;0 1 0 0;0 0 1 d;0 0 0 1];
    M_a = [1 0 0 a;0 1 0 0;0 0 1 0;0 0 0 1];
    M_alpha = [1 0 0 0;0 cos(alpha*rad) -sin(alpha*rad) 0;0 sin(alpha*rad) cos(alpha*rad) 0; 0 0 0 1];
    
    T=M_theta*M_d*M_a*M_alpha;
% command
clc
clear

framemax = 100;
M=moviein(framemax);
set(gcf,'Position',[100 100 640 480]);


%theta1 = 0:30/100:30;
%theta2 = 0:45/100:45;

theta1 = 0:360/100:360;
theta2 = 0:360/100:360;


for k = 1:100+1 % +1 to utilise all angles in theta including the last one
T1=RobotConv(theta1(k),3,0);
T2=RobotConv(theta2(k),2,0);

p0 = [0 0 0];
p1 = RobotPosition(T1);
p2 = RobotPosition(T1*T2);
figure(1)
X = [p0(1) p1(1) p2(1)];
Y = [p0(2) p1(2) p2(2)];
plot(X,Y,'o-')
axis([-1 8 -1 8]);
grid
M(k) = getframe(gcf);


%%
%{
%saving the a gif of the robot
frame = getframe(gca);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if k == 1
imwrite(imind,cm,'robot.gif','gif','Loopcount',inf);
else
imwrite(imind,'WriteMode','append');
end
%}
%%

end

问题是我们需要

  1. 将30°和45°分为100步。那是 theta1 = 0:30/100:30; theta2 = 0:45/100:45;
  2. 将以下代码作为m文件键入
framemax = 100;
M=moviein(framemax);
set(gcf,[100 100 640 480]);
for k = 1:100
 T1=RobotConv(theta1(k),0);
 T2=RobotConv(theta2(k),0);

 p0 = [0 0 0];
 p1 = RobotPosition(T1);
 p2 = RobotPosition(T1*T2);
 figure(1)
 X = [p0(1) p1(1) p2(1)];
 Y = [p0(2) p1(2) p2(2)];
 plot(X,'o-')
 axis([-1 8 -1 8]);
 grid
 M(k) = getframe(gcf);
end
  1. 观察2链接机器人的动画。将θ1和θ2的两个角度都更改为360°。 修改代码以观察整个运动。

附件如下:

function P = RobotPosition(T)
    x = T(1,4);
    P = [x y z];
end

function T = RobotConv(theta,alpha)
    rad = pi/180;
    
    M_theta = [cos(theta*rad) -sin(theta*rad) 0 0;sin(theta*rad) cos(theta*rad) 0 0;0 0 1 0;0 0 0 1];
    M_d = [1 0 0 0;0 1 0 0;0 0 1 d;0 0 0 1];
    M_a = [1 0 0 a;0 1 0 0;0 0 1 0;0 0 0 1];
    M_alpha = [1 0 0 0;0 cos(alpha*rad) -sin(alpha*rad) 0;0 sin(alpha*rad) cos(alpha*rad) 0; 0 0 0 1];
    T=M_theta*M_d*M_a*M_alpha;
end

我的代码无法正确编译,我不确定为什么。

解决方法

每个功能块的末尾都需要一个end语句-相当确定这就是为什么它不会运行的原因,假设第一个代码块是单个文件。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。