function Q = CinInvHip_Liv( P,L )

 %-- L=[l0 l1 l2 l3 l4 l5 l5h l5t l6] -------------------------------------
  l0 = L(1); l1 = L(2); l2 = L(3); l3 = L(4); l4 = L(5); 
  l5 = L(6); l5h = L(7); l6 = L(8);

 %-- Perna livre move-se no mesmo plano da perna de suporte ---------------     
 %-- Junta lateral do tornozelo -------------------------------------------
  xh  = P(1);  yh = P(2);  zh = P(3); 
  ang1 = atan2(-yh+l6/2,(zh-l0-l5-l5h/2));  
  
 %-- Juntas da anca -------------------------------------------------------
  ang7 = 0;
  ang8 = ang1;

 %-- Restriçao cinematica -------------------------------------------------  
  x1  = P(1);  y1 = P(2)+l6/2+l4*sin(ang1);  z1 = P(3)-l5h/2-l5-l4*cos(ang1); 
  x2  = P(4);  z2 = P(6)+l0+l1*cos(-ang1);   y2 = y1+(z2-z1)*sin(ang1);
 %-- Transformaçao de coordenadas : plano do movimento --------------------
  Ye = x2-x1; 
  if (ang1 > pi/4 & ang1 < 3*pi/4) | (ang1 < -pi/4 & ang1 > -3*pi/4)
      Xe = (y2-y1)/sin(ang1);
  else
      Xe = -(z2-z1)/cos(ang1);
  end
 %-- Junta da anca --------------------------------------------------------
  K1 = 2*l3*Ye;
  K2 = 2*l3*Xe;
  K3 = Xe^2+Ye^2+l3^2-l2^2;
  KK_aux = K1^2+K2^2-K3^2;
  if KK_aux < 0, disp('ERROR MSG: function CinInv12R'), pause, return, end
  ang9 = atan2(K1,K2)+atan2(sqrt(K1^2+K2^2-K3^2),K3);
  %-- Ângulo entre -pi e pi -----------------------------------------------
  if ang9 >  pi,    ang9 = ang9-2*pi;   end
  if ang9 < -pi,    ang9 = ang9+2*pi;   end
  
 %-- Junta do joelho ------------------------------------------------------
  K1 = 0;
  K2 = 2*l2*l3;
  K3 = Xe^2+Ye^2-l3^2-l2^2;
  KK_aux = K2^2-K3^2;
  if KK_aux < 0, disp('ERROR MSG: function CinInv12R'), pause, return, end
  ang10 = -atan2(sqrt(K2^2-K3^2),K3);
 
 %-- Juntas do tornozelo --------------------------------------------------
  ang11 = -ang9-ang10;
  ang12 = -ang1;

%-- Vector de juntas ------------------------------------------------------
  Q = [ ang7 ang8 ang9 ang10 ang11 ang12 ];
%-----------------------------------------------------------------------end
