【发布时间】:2020-09-15 19:07:19
【问题描述】:
到目前为止,这是我的代码:
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
问题是我们需要
- 将 30° 和 45° 分成 100 个步长。那是 theta1 = 0:30/100:30; theta2 = 0:45/100:45;
- 将以下代码键入为 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
- 观察 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
我的代码没有正确编译,我不知道为什么。
【问题讨论】:
-
Matlab 代码未编译。错误在哪里?
-
请将您的代码压缩为minimal reproducible example。逐行阅读不相关的代码是一种痛苦。创建 MRE 的练习会迫使您查看代码并尝试隔离问题,这通常可以帮助您找出问题,甚至无需询问 SO。
-
请不要无礼。我不懂你说什么。您所要做的就是复制和粘贴。
-
“你所要做的就是复制和粘贴”:请阅读How to Ask。你提供的细节越多,其他人就越容易帮助你。当您就错误寻求帮助时,期望您告诉我们错误是什么是合理的。你不明白什么? That Matlab isn't a compiled language? 你认为我评论的哪一部分是粗鲁的?询问更多细节并不粗鲁。也没有问错误是什么。