# Error using atan2 Inputs must be real.

45 views (last 30 days)
Tony Yu on 23 Jun 2020
Commented: Tony Yu on 23 Jun 2020
Error using atan2
Inputs must be real.
how can i fix this?
clc;
clear all;
a1 = 150; alpha1 = pi/2; d1 = 470;
a2 = 600; alpha2 = 0; d2 = 0;
a3 = 120; alpha3 = pi/2; d3 = 720;
syms c1 c2 c23 c3 c4 c5 c6 s1 c1 s2 s23 s3 s4 s5 s6 R03 R36 th1 th2 th3 th4 th5 th6 Xc Yc Zc R06 ox oz oy d6 r d D r11 r12 r13 r21 r22 r23 r31 r32 r33
c1 = cos(th1)
c2 = cos(th2)
c3 = cos(th3)
s1 = sin(th1)
s2 = sin(th2)
s3 = sin(th3)
c23 = cos(th2+th3)
s23 = sin(th2+th3)
d = a1
d6 = 50
%assume d6 = 5
R = [1 0 0 500;
0 1 0 100;
0 0 1 1500;
0 0 0 1];
r11 = R(1,1)
r12 = R(1,2)
r13 = R(1,3)
r21 = R(2,1)
r22 = R(2,2)
r23 = R(2,3)
r31 = R(3,1)
r32 = R(3,2)
r33 = R(3,3)
ox = R(1,4)
oy = R(2,4)
oz = R(3,4)
Xc = ox - d6*R(1,3)
Yc = oy - d6*R(2,3)
Zc = oz - d6*R(3,3)
D = (Xc^2+Yc^2-d^2+(Zc-d1)^2-a2^2-a3^2)/(2*a2*a3)
%inverse kinematic
th1 = atan2(Xc,Yc)
th2 = atan2(sqrt(Xc^2+Yc^2-d^2),Zc-d1)-atan2(a2+a3*c3,a3*s3)
th3 = atan2 (D,sqrt((1-d^2)))
th4 = atan2(c1*c23*r13+s1*c23*r23+s23r33,-c1*s23*r13-s1*s23*r23+c23*r33)
th5 = atan2(s1*r13-c1*r23,sqrt(1-(s1*r13-c1*r23)^2))
th6 = atan2(-s1*r11+c1*r21, s1*r12-c1*r22)

Walter Roberson on 23 Jun 2020
d is 150 so sqrt(1-d^2) is not going to be real.
That appears to be the only place you are using complex numbers, so I recommend you recheck your formula.
Tony Yu on 23 Jun 2020
oh I was a typo, it should be "D", I updated some parameters but now my th4 is not give me a solution
clc;
clear all;
a1 = 150; alpha1 = pi/2; d1 = 470;
a2 = 600; alpha2 = 0; d2 = 0;
a3 = 120; alpha3 = pi/2; d3 = 720;
syms c1 c2 c3 c4 c5 c6 s1 c1 s2 s3 s4 s5 s6 R03 R36 th1 th2 th3 th4 th5 th6 Xc Yc Zc R06 ox oz oy d6 r d D r11 r12 r13 r21 r22 r23 r31 r32 r33
c1 = cos(th1)
c2 = cos(th2)
c3 = cos(th3)
s1 = sin(th1)
s2 = sin(th2)
s3 = sin(th3)
c23 = cos(th2+th3)
s23 = sin(th2+th3)
d = a1
d6 = 500
%assume d6 = 500
R = [1 0 0 500;
0 1 0 100;
0 0 1 1500;
0 0 0 1];
r11 = R(1,1)
r12 = R(1,2)
r13 = R(1,3)
r21 = R(2,1)
r22 = R(2,2)
r23 = R(2,3)
r31 = R(3,1)
r32 = R(3,2)
r33 = R(3,3)
ox = R(1,4)
oy = R(2,4)
oz = R(3,4)
Xc = ox - d6*R(1,3)
Yc = oy - d6*R(2,3)
Zc = oz - d6*R(3,3)
D = (Xc^2+Yc^2-d^2+(Zc-d1)^2-a2^2-a3^2)/(2*a2*a3)
%inverse kinematic
th1 = atan2(Xc,Yc)
th3 = atan2(D,sqrt(1-D^2))
c3 = cos(th3)
s3 = sin(th3)
th2 = atan2(sqrt(Xc^2+Yc^2-d^2),Zc-d1)-atan2(a2+a3*c3,a3*s3)
c23 = cos(th2+th3)
s23 = sin(th2+th3)
th4 = atan2(c1*c23*r13+s1*c23*r23+s23*r33,-c1*s23*r13-s1*s23*r23+c23*r33)
th5 = atan2(s1*r13-c1*r23,sqrt(1-(s1*r13-c1*r23)^2))
c1 = cos(th1)
s1 = sin(th1)
th6 = atan2(-s1*r11+c1*r21, s1*r12-c1*r22)

Ameer Hamza on 23 Jun 2020
It gives the value of th4, but the output is in the symbolic format. Convert it into a floating-point value by using double(). Change the line like this
th4 = double(atan2(c1*c23*r13+s1*c23*r23+s23*r33,-c1*s23*r13-s1*s23*r23+c23*r33))

#### 1 Comment

Tony Yu on 23 Jun 2020
Thanks, it works!