function Q=invkinRR(x, y, L1, L2)
%% cinematica inversa do robot RR_planar

% Contemplar as redundancias retornando a matriz Q=[q1A q1B; q2A q2B]
% sendo a solucao A a do cotovelo em baixo;

Q=zeros(2,2);

% calculo de teta 2

% q2A
Q(2,1)=acos((x^2+y^2-L1^2-L2^2)/(2*L1*L2));
if ~isreal(Q(2,1))
    Q=[];
    return
end
% q2B
Q(2,2)=-acos((x^2+y^2-L1^2-L2^2)/(2*L1*L2));
if ~isreal(Q(2,2))
    Q=[];
    return
end

% calculo de teta 1

% q1A
Q(1,1)=atan2(y*(L1+L2*cos(Q(2,1)))-(x*L2*sin(Q(2,1))) , x*(L1+L2*cos(Q(2,1)))+(y*L2*sin(Q(2,1))));
if ~isreal(Q(1,1))
    Q=[];
    return
end
% q1B
Q(1,2)=atan2(y*(L1+L2*cos(Q(2,2)))-(x*L2*sin(Q(2,2))) , x*(L1+L2*cos(Q(2,2)))+(y*L2*sin(Q(2,2))));
if ~isreal(Q(1,2))
    Q=[];
    return
end

Q=radtodeg(Q);
end
