# Transformation matrix using DH parameters

I want to write a Matlab function which takes the DH parameters as input and outputs a 4X4 transformation matrix.

The code I have written is :

function  [A]  = compute_dh_matrix(r, alpha, d, theta)

A = eye(4);

% ROTATION FOR X
A(1,1) = cos((theta));
A(2,1) = sin((theta));

% ROTATION FOR Y
A(1,2) = -(sin(theta))*(cos(alpha))*(1);
A(2,2) = (cos(theta))*(cos(alpha));
A(3,2) = (sin(alpha));

% ROTATION FOR Z
A(1,3) = (sin(theta))*(sin(alpha));
A(2,3) = -(cos(theta))*(sin(alpha))*(1);
A(3,3) = (cos(alpha));

% TRANSLATION VECTOR
A(1,4) = (alpha)*(cos(theta));
A(2,4) = (alpha)*(sin(theta));
A(3,4) = d;

end


But when I submit the code for evaluation in an online platform it prompts that variable A has incorrect value.

One of the input data used for evaluation of the code is :

r = 5;
alpha = 0;
d = 3;
theta = pi/2;


The matrix representation I have used to write the code is :

$A =$ \begin{bmatrix}cos(\theta)&&-\sin(\theta)\cos(\alpha)&&\sin(\theta)\sin(\alpha)&&\alpha \cos(\theta)\\\sin(\theta)&&\cos(\theta)\cos(\alpha)&&-\cos(\theta)\sin(\alpha)&&\alpha \sin(\theta)\\0&&\sin(\alpha)&&\cos(\alpha)&&d\\0&&0&&0&&1\end{bmatrix}

A is the transformation matrix generated using DH parameters $\theta$, $\alpha$ and d

$\alpha$ is angle about common normal from old z axis to new z axis

$\theta$ is angle about previous z axis, from old x axis to new x axis

d is offset along previous z axis to the common normal

• Check your formulae for A(1,2) and A(2,3). I assume you intended to negate the values? – sempaiscuba Jul 5 '18 at 15:09
• sorry, I actually missed it while writing the question. I have edited it :) – MSD Jul 5 '18 at 15:12
• I don't think you need radtodeg for $\alpha$ in the translation part. – Petch Puttichai Jul 6 '18 at 10:17
• It's not working even on removing radtodeg – MSD Jul 7 '18 at 11:21

The multiplication factor in the translation terms should be the link length instead of the angle $\alpha$.
That is, the translation should be $(r\cos\theta, r\sin\theta, d)$.