机器人 MATLAB 中的动画



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

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), 0, 3, 0);
T2=RobotConv(theta2(k), 0, 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,cm,'robot.gif','gif','WriteMode','append');
end
%}
%%
end

问题是我们需要

  1. 将 30° 和 45° 分成 100 个步长。那是 θ1 = 0:30/100:30; θ2 = 0:45/100:45;
  2. 键入以下代码作为 m 文件
framemax = 100;
M=moviein(framemax);
set(gcf,'Position',[100 100 640 480]);
for k = 1:100
T1=RobotConv(theta1(k), 0, 3, 0);
T2=RobotConv(theta2(k), 0, 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);
end
  1. 观察 2 连杆机器人的动画。将 θ1 和 θ2 的两个角度更改为 360°。 修改代码以观察整个运动。

附件如下:

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

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;
end

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

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

相关内容

  • 没有找到相关文章

最新更新