Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. clc
  2. clear all
  3. delete(instrfind({'Port'},{'COM3'}))
  4.  
  5. s= serial('COM3');
  6. set(s,'BaudRate',9600);
  7. fopen(s);
  8.  
  9.  
  10. Initialpoint=[15;0;0];
  11. InitialpointB1=[-7;0;0];
  12. InitialpointB2=[7;0;0];
  13. OP=[0,0,0];
  14.  
  15. %h = line([0;x],[0;y],[0;z]);
  16. %hold off
  17.  
  18. %set(h,'linewidth',5);
  19.  
  20. % Infinite loop
  21. for i=1:550
  22.  
  23. % Reading the serial port
  24. str = fscanf(s);
  25.  
  26. % Finding the letter a for seperation (for first sensor)
  27. indexX1 = find(str == 'X');
  28. % First data obtained
  29. strX1 = str(1:indexX1-1);
  30.  
  31. indexY1 = find(str == 'Y');
  32. % Second data is obtained
  33. strY1 = str(indexX1+1:indexY1-1);
  34.  
  35. indexZ1 = find(str == 'Z');
  36.  
  37. % third data obtained
  38. strZ1 = str(indexY1+1:indexZ1-1);
  39.  
  40. % Finding the letter a for seperation(for second sensor)
  41. indexX2 = find(str == 'A');
  42. % First data obtained
  43. strX2 = str(indexZ1+1:indexX2-1);
  44.  
  45. indexY2 = find(str == 'B');
  46. % Second data is obtained
  47. strY2 = str(indexX2+1:indexY2-1);
  48.  
  49. indexZ2 = find(str == 'C');
  50.  
  51. % third data obtaineed
  52. strZ2 = str(indexY2+1:indexZ2-1);
  53.  
  54.  
  55. % Converting string to double precision value (Gyro)
  56. eulerX1 = str2double(strX1);
  57. % Converting string to double precision value (Acc)
  58. eulerY1 = str2double(strY1);
  59. eulerZ1 = str2double(strZ1);
  60.  
  61. eulerX2 = str2double(strX2);
  62. % Converting string to double precision value (Acc)
  63. eulerY2 = str2double(strY2);
  64. eulerZ2 = str2double(strZ2);
  65.  
  66. x=eulerX1;
  67. y=eulerY1;
  68. z=eulerZ1;
  69.  
  70. e=eulerX2;
  71. f=eulerY2;
  72. g=eulerZ2;
  73.  
  74. AngP2=[x y z];
  75.  
  76.  
  77.  
  78. %Rotation angles ZYX seguidores(deg)
  79. angles=[z -y x];
  80. angles2=[g -f e];
  81. %Rotation angles XYZ(rad)
  82. angles=angles*pi()/180;
  83. angles2=angles2*pi()/180;
  84.  
  85. R=zeros(3,3);
  86. R2=zeros(3,3);
  87.  
  88. %Rotación en Z
  89. alpha=angles(1,1);
  90. %Rotación en Y
  91. beta=angles(1,2);
  92. %Rotación en X
  93. gamma=angles(1,3);
  94.  
  95. %Second sensor Rotación en Z
  96. alpha2=angles2(1,1);
  97. %Rotación en Y
  98. beta2=angles2(1,2);
  99. %Rotación en X
  100. gamma2=angles2(1,3);
  101.  
  102. %Rotation matrices
  103. R(1,1)= cos(alpha)*cos(beta);
  104. R(1,2)= cos(alpha)*sin(beta)*sin(gamma)-sin(alpha)*cos(gamma);
  105. R(1,3)=cos(alpha)*sin(beta)*cos(gamma)+sin(alpha)*sin(gamma);
  106. R(2,1)=sin(alpha)*cos(beta);
  107. R(2,2)=sin(alpha)*sin(beta)*sin(gamma)+cos(alpha)*cos(gamma);
  108. R(2,3)=sin(alpha)*sin(beta)*cos(gamma)-cos(alpha)*sin(gamma);
  109. R(3,1)=-sin(beta);
  110. R(3,2)=cos(beta)*sin(gamma);
  111. R(3,3)=cos(beta)*cos(gamma);
  112.  
  113. R2(1,1)= cos(alpha2)*cos(beta2);
  114. R2(1,2)= cos(alpha2)*sin(beta2)*sin(gamma2)-sin(alpha2)*cos(gamma2);
  115. R2(1,3)=cos(alpha2)*sin(beta2)*cos(gamma2)+sin(alpha2)*sin(gamma2);
  116. R2(2,1)=sin(alpha2)*cos(beta2);
  117. R2(2,2)=sin(alpha2)*sin(beta2)*sin(gamma2)+cos(alpha2)*cos(gamma2);
  118. R2(2,3)=sin(alpha2)*sin(beta2)*cos(gamma2)-cos(alpha2)*sin(gamma2);
  119. R2(3,1)=-sin(beta2);
  120. R2(3,2)=cos(beta2)*sin(gamma2);
  121. R2(3,3)=cos(beta2)*cos(gamma2);
  122.  
  123. Finalpoint=R*Initialpoint;
  124. FinalpointB1=R2*InitialpointB1;
  125. FinalpointB2=R2*InitialpointB2;
  126.  
  127. FinalX=Finalpoint(1,1);
  128. FinalY=Finalpoint(2,1);
  129. FinalZ=Finalpoint(3,1);
  130. FinalXYZ=[FinalX,FinalY,FinalZ];
  131.  
  132. dx=FinalX-FinalpointB1(1,1);
  133. dy=FinalY-FinalpointB1(2,1);
  134. dz=FinalZ-FinalpointB1(3,1);
  135.  
  136. FinalX2=FinalpointB2(1,1);
  137. FinalY2=FinalpointB2(2,1);
  138. FinalZ2=FinalpointB2(3,1);
  139. Final2XYZ=[FinalX2+dx,FinalY2+dy,FinalZ2+dz];
  140.  
  141. %create figures
  142. [a,b,c]=cylinder2P(0.5,20,OP,FinalXYZ);
  143. hold on;
  144. [d,e,f]=cylinder2P(0.5,20,FinalXYZ,Final2XYZ);
  145. grid on;
  146. axis manual;
  147. hold off;
  148. axis([-30 30 -30 30 -30 30]);
  149. view(3);
  150. drawnow;
  151.  
  152. Final2XYZ=Final2XYZ-FinalXYZ;
  153.  
  154. %angle between sensors check
  155. Valor=dot(Final2XYZ,FinalXYZ);
  156. absoluto1=norm(FinalXYZ);
  157. absoluto2=norm(Final2XYZ);
  158.  
  159. Angulo=180-acos(Valor/(absoluto1*absoluto2))*180/pi()
  160.  
  161. pause(0.02);
  162.  
  163.  
  164. end
  165. delete(instrfind({'Port'},{'COM3'}));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement