I want to use a matrix and a function to then create another function
1 次查看(过去 30 天)
显示 更早的评论
I currently have a matrix that has known values that I then want to use to create a second matrix using a function loop. The matrix within the function loop has four variables that coincide with the variables from the above matrix DH. How do I use each row of the DH matrix to be a value to then insert into the Ai matrix? (Colum 1 = theta, Column 2 = d, etc.) I need this to then output 6 different matrices A1,A2,etc.
clear
clc
close all
theta1 = -50;
theta2 = 30;
theta3 = 15;
theta4 = -75;
theta5 = -45;
theta6 = -60;
DH = [theta1, 486.5, 150, -90;...
theta2+90, 0, -700, 0;...
theta3, 0, 0, 90;...
theta4, 600, 0, -90;...
theta5, 0, 0, 90;...
theta6, 65, 0, 0];
function Ai = getHomogeneousTransform(theta, d, a, alpha)
Ai = [cos(theata), -sin(theta)*cos(theta), sin(theta)*sin(alpha),a*cos(theta);...
sin(theta),cos(theta)*cos(alpha),-cos(theta)*sin(alpha),a*sin(theta);...
0, sin(alpha), cos(alpha), d;...
0,0,0,1];
end
1 个评论
Stephen23
2022-11-29
"How do I use each row of the DH matrix to be a value to then insert into the Ai matrix?"
Use a loop and indexing.
"I need this to then output 6 different matrices A1,A2,etc."
Numbering variable names like that forces you into writing slow, complex, inefficient, buggy code. Best avoided.
The simple and very efficient approach is to use indexing. You should use indexing.
回答(1 个)
Torsten
2022-11-28
theta1 = -50;
theta2 = 30;
theta3 = 15;
theta4 = -75;
theta5 = -45;
theta6 = -60;
DH = [theta1, 486.5, 150, -90;...
theta2+90, 0, -700, 0;...
theta3, 0, 0, 90;...
theta4, 600, 0, -90;...
theta5, 0, 0, 90;...
theta6, 65, 0, 0];
A = cell(size(DH,1),1);
for i = 1:size(DH,1)
A{i} = getHomogeneousTransform(DH(i,1)*pi/180, DH(i,2), DH(i,3), DH(i,4)*pi/180);
A{i}
end
function Ai = getHomogeneousTransform(theta, d, a, alpha)
Ai = [cos(theta),-sin(theta)*cos(theta),sin(theta)*sin(alpha),a*cos(theta);...
sin(theta),cos(theta)*cos(alpha),-cos(theta)*sin(alpha),a*sin(theta);...
0,sin(alpha),cos(alpha),d;...
0,0,0,1];
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!