用符号变量计算矩阵

问题描述 投票:0回答:1

我有以下MATLAB脚本:

var_theta = sym('theta', [1,7]);
matrix_DH = computeDH(var_theta);
T_matrix = zeros(4,4,7);
for i = 1:7
    T_matrix(:,:,i) = computeT(matrix_DH(i,1), matrix_DH(i,2), matrix_DH(i,3), matrix_DH(i,4));
end

function matrixT = computeT(alpha, d, r, theta)
  matrixT = zeros(4,4);
  matrixT(1,:) = [cos(theta) -1*sin(theta) 0 r]; % first row 
  matrixT(2,:) = [sin(theta)*cos(alpha) cos(theta)*cos(alpha) -sin(alpha) -d*sin(alpha)]; % 2n row  
  matrixT(3,:) = [sin(theta)*sin(alpha) cos(theta)*sin(alpha) cos(alpha) d*cos(alpha)]; % 3rd row  
  matrixT(4,:) = [0 0 0 1]; % last row
end

function DH = computeDH(theta)
   DH = zeros(7,4);
   L = 0.4;
   M = 0.39;
   DH(1,:) = [0.5*pi 0 0 theta(1,1)];
   DH(2,:) = [-0.5*pi 0 0 theta(2)];
   DH(3,:) = [-0.5*pi L 0 theta(3)];
   DH(4,:) = [0.5*pi 0 0 theta(4)];
   DH(5,:) = [0.5*pi M 0 theta(5)];
   DH(6,:) = [-0.5*pi 0 0 theta(6)];
   DH(7,:) = [0 0 0 theta(7)];
end

我想在不评估theta的情况下获得所需的T_matrix数组。我的目标是在获得矩阵之后,通过每个theta推导出每个位置以便计算雅可比矩阵。所以最后我希望得到的矩阵作为thetas的函数。问题是每当我将符号变量插入矩阵时,它会说:

The following error occurred converting from sym to double:
Unable to convert expression into double array.

Error in computeT_robotics>computeDH (line 21)
   DH(1,:) = [0.5*pi, 0, 0, theta(1)];
matlab matrix double symbolic-math
1个回答
0
投票

正如安东尼所说,我的theta所包含的矩阵也需要声明为sym,以便能够保存符号结果。最终代码:

var_theta = sym('theta', [1,7]);
matrix_DH = computeDH(var_theta);
T_matrix = sym('T_matrix',[4 4 7]);
for i = 1:7
    T_matrix(:,:,i) = computeT(matrix_DH(i,1), matrix_DH(i,2), matrix_DH(i,3), matrix_DH(i,4));
end

function matrixT = computeT(alpha, d, r, theta)
  matrixT = sym('matrixT', [4 4]);
  matrixT(1,:) = [cos(theta) -1*sin(theta) 0 r]; % first row 
  matrixT(2,:) = [sin(theta)*cos(alpha) cos(theta)*cos(alpha) -sin(alpha) -d*sin(alpha)]; % 2n row  
  matrixT(3,:) = [sin(theta)*sin(alpha) cos(theta)*sin(alpha) cos(alpha) d*cos(alpha)]; % 3rd row  
  matrixT(4,:) = [0 0 0 1]; % last row
end

function DH = computeDH(theta)
   DH = sym('DH',[7 4]);
   L = 0.4;
   M = 0.39;
   DH(1,:) = [0.5*pi 0 0 theta(1,1)];
   DH(2,:) = [-0.5*pi 0 0 theta(2)];
   DH(3,:) = [-0.5*pi L 0 theta(3)];
   DH(4,:) = [0.5*pi 0 0 theta(4)];
   DH(5,:) = [0.5*pi M 0 theta(5)];
   DH(6,:) = [-0.5*pi 0 0 theta(6)];
   DH(7,:) = [0 0 0 theta(7)];
end
© www.soinside.com 2019 - 2024. All rights reserved.