Hector Esteban
Hector Esteban

Reputation: 1111

Compute matrix with symbolic variable

I have the following MATLAB script:

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

I would like to obtain the desired array of T_matrix without evaluating the theta's. My objective is that after getting the matrices, derivate each position by each theta in order to compute the Jacobian. So at the end I would like the resulting matrices as a function of the thetas. The problem is that whenever I insert the symbolic variable into the matrix it says:

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)];

Upvotes: 0

Views: 104

Answers (1)

Hector Esteban
Hector Esteban

Reputation: 1111

As Anthony stated, the matrices that my theta is included in, need to be declared as sym as well in order to be able to save symbolic results. Final code:

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

Upvotes: 1

Related Questions