Guest User

Untitled

a guest
Oct 17th, 2020
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.48 KB | None | 0 0
  1. // This is a component for Linuxcnc HAL
  2. // Copyright 2020 Sam Sokolik <[email protected]>
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. component polygon "Create a spindle synced motion that carves a polygon using eoffset inputs. Assumes positive rotation.";
  18.  
  19. pin in bit enable;
  20. pin in bit enablexslave "enables connecting x postion to radius";
  21. pin in bit enablescan "place at 0,0 in the current coordinate system then enable scan";
  22. pin in bit startscan "move to the edge of shape then start scan and then start motion";
  23. pin in bit enablescanrun "enable this befor enable and enableslave so comp knows to use scanned data";
  24. pin in bit lathemode "switches to lathe mode - material spining";
  25. pin out bit iscaptured "used to only select the center of scanned feature once";
  26. pin out float scancenterx "keeps center of shape to be scanned";
  27. pin out float scancenteryz "keeps center of shape to be scanned";
  28. pin in float xpos "connect to the x axis potion in hal";
  29. pin in float ypos "connect to the y axis positin in hal";
  30. pin in float zpos "connect to the z axis positin in hal";
  31. pin out float xposaten "when componant is enabled - grab current x postion";
  32. pin out float zposaten "when componant is enabled - grab the current z postion";
  33. pin in float zrotratio "what to multiply and add to the z postion for rotation degrees/distance";
  34. pin in unsigned numsides=3 "Number of sides in the polygon";
  35. pin in float inscribedradius=1 "inscribed radius of the polygon";
  36. pin in float dsize "distance from flat to edge of radius";
  37. pin in float cutterdiam "cutter diameter";
  38. pin in float eoffsetscale "scale that eoffset is set to.";
  39. pin in float toolang "angle difference between index and actual tool poition";
  40. pin in float polyang "angle of the polygon";
  41. pin in float spindlepos "Spindle position scaled to 1 per rev";
  42. pin in float cornerrad "Radius of the corner for the polygon";
  43. pin out float spindlerad "Spindle postion in radians";
  44. pin out float polyradius "radious of the polygon at given spindle position";
  45. pin out float tangentang "+/- angle from polygon point for radious";
  46. pin out float knowndist "testing just to make sure it was calculated right";
  47. pin out float testingstuff "testing some math..";
  48. pin out float xoffset "x offset to send to offset componant";
  49. pin out float yoffset "y offset to send to the offset componant";
  50. pin out signed xeoffset "x eoffset output";
  51. pin out signed yeoffset "y eoffset output";
  52. pin out bit isindex "set index enable only once";
  53. pin io bit indexenable "hooked to index enable of encoder";
  54.  
  55. variable float shape[100000];
  56.  
  57. function _;
  58. license "GPL";
  59. ;;
  60. #include <rtapi_math.h>
  61. #define PI 3.14159265358979323846
  62. float spinang;
  63. float scale;
  64. float scaletri;
  65. float spinangoffset;
  66. float polyangoffset;
  67. float radiusslave=0;
  68. float dang=0;
  69. float secdist=0;
  70. float thirddist=0;
  71. float forthdist=0;
  72. float fifthdist=0;
  73. float sixthdist=0;
  74. float seventhdist=0;
  75.  
  76. float scanyz=0;
  77. float scanrad=0;
  78. float scanang=0;
  79. long saveval;
  80.  
  81.  
  82.  
  83. // Find center of the scanned shape
  84. if (enablescan && !iscaptured){
  85. scancenterx=xpos;
  86. if (lathemode){
  87. scancenteryz=zpos;
  88. }else{
  89. scancenteryz=ypos;
  90. }
  91. iscaptured = true;
  92. }
  93.  
  94. if (!enablescan){
  95. iscaptured = false;
  96. }
  97.  
  98. // Scan shape into the array.
  99.  
  100. if (startscan){
  101. if(lathemode){
  102. scanyz=zpos;
  103. }else{
  104. scanyz=ypos;
  105. }
  106. if ((scanyz-scancenteryz) >= 0 && (xpos-scancenterx) >= 0){
  107. scanang=atan((scanyz-scancenteryz)/(xpos-scancenterx));
  108. }
  109.  
  110. if (scanyz-scancenteryz > 0 && xpos-scancenterx < 0){
  111. scanang=(PI/2-fabs(atan((scanyz-scancenteryz)/(xpos-scancenterx))))+PI/2;
  112. }
  113.  
  114. if (scanyz-scancenteryz <= 0 && xpos-scancenterx <= 0){
  115. scanang=atan((scanyz-scancenteryz)/(xpos-scancenterx)) + PI;
  116. }
  117.  
  118. if (scanyz-scancenteryz < 0 && xpos-scancenterx > 0){
  119. scanang=(PI/2-fabs(atan((scanyz-scancenteryz)/(xpos-scancenterx))))+PI+PI/2;
  120. }
  121.  
  122. saveval = (50000/(2*PI)*scanang);
  123. //testing polyradius = saveval;
  124. // spindlerad = scanang;
  125. shape[saveval] = sqrt((xpos-scancenterx)*(xpos-scancenterx) + (scanyz-scancenteryz)*(scanyz-scancenteryz));
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. if (!enable) {
  134. xoffset=0;
  135. yoffset=0;
  136. xeoffset=0;
  137. yeoffset=0;
  138. isindex = false;
  139. xposaten = 0;
  140. zposaten = 0;
  141. return;
  142. }
  143.  
  144. // only set index enable once
  145. if (enable && !isindex) {
  146. isindex = true;
  147. indexenable = true;
  148. zposaten=zpos;
  149. if (enablexslave){
  150. xposaten=xpos;
  151. }
  152. return;
  153. }
  154.  
  155. // wait for spindle index before actually enabling
  156. if (enable && isindex && indexenable){
  157. return;
  158. }
  159.  
  160. //convert spinangoffset to ratio.. for adding to spindle pos - plus a couple rotations
  161. //so the spindle position isn't negative
  162. spinangoffset = (toolang/360-(polyang+(zpos-zposaten)*zrotratio)/360)+2;
  163. polyangoffset = ((polyang+(zpos-zposaten)*zrotratio) /360)*2*PI;
  164.  
  165. //formual I found is for circumscribed - convert to inscribed - makes more sense.
  166.  
  167.  
  168. spinang = ((spindlepos + spinangoffset) - (int)(spindlepos + spinangoffset))*2*PI;
  169.  
  170. //calculate radius based on if x moves
  171. if (enablexslave){
  172. radiusslave=xpos-xposaten;
  173. }
  174.  
  175.  
  176. if(enable&&enablescan){
  177. scale=(inscribedradius-radiusslave);
  178. }else{
  179. scale=(inscribedradius-radiusslave)/cos(PI/numsides);
  180. }
  181.  
  182.  
  183. scaletri=((dsize-inscribedradius)-radiusslave)/cos(PI/3);
  184.  
  185.  
  186. if (dsize > 0){
  187. dang=acos((dsize-inscribedradius)/inscribedradius);
  188. }
  189. if (cornerrad >0){
  190. secdist=sin(PI/numsides)*cornerrad;
  191. thirddist=secdist/tan((((numsides-2)*(PI))/numsides)/2);
  192. tangentang=atan(secdist/(scale-thirddist));
  193. forthdist=cos(PI/numsides)*cornerrad;
  194. knowndist=scale-(thirddist+forthdist);
  195.  
  196. }
  197.  
  198. if (spinang > (PI - dang) && spinang < (PI + dang)){
  199. polyradius = ((cos(PI/3)/cos((fmod(spinang, (2*PI/3))-PI/3)))*scaletri-cutterdiam/2);
  200. }else if ((fmod(spinang, (2*PI/numsides)) < tangentang || 2*PI/numsides - fmod(spinang, (2*PI/numsides)) < tangentang) && cornerrad>0 ){
  201. if(fmod(spinang, (2*PI/numsides)) < tangentang){
  202. fifthdist=cos(fmod(spinang, (2*PI/numsides)))*knowndist;
  203. seventhdist=sin(fmod(spinang, (2*PI/numsides)))*knowndist;
  204. sixthdist=sqrt(cornerrad*cornerrad-seventhdist*seventhdist);
  205. polyradius=(fifthdist+sixthdist)-cutterdiam/2;
  206.  
  207. }else{
  208.  
  209. fifthdist=cos(2*PI/numsides -fmod(spinang, (2*PI/numsides)))*knowndist;
  210. seventhdist=sin(2*PI/numsides -fmod(spinang, (2*PI/numsides)))*knowndist;
  211. sixthdist=sqrt(cornerrad*cornerrad-seventhdist*seventhdist);
  212. polyradius=(fifthdist+sixthdist)-cutterdiam/2;
  213.  
  214.  
  215.  
  216. }
  217.  
  218.  
  219. }else{
  220.  
  221. if(enable&&enablescan){
  222. saveval = (50000/(2*PI)*spinang);
  223. polyradius = shape[saveval] *scale-cutterdiam/2;
  224. }else{
  225. polyradius = ((cos(PI/numsides)/cos((fmod(spinang, (2*PI/numsides))-PI/numsides)))*scale-cutterdiam/2);
  226. }
  227. }
  228.  
  229.  
  230.  
  231. //actual offsets applied - could be used for offset componant.
  232. if (lathemode) {
  233. xoffset = polyradius * -1-(radiusslave);
  234. yoffset = 0;
  235. xeoffset = xoffset / eoffsetscale;
  236. yeoffset = 0;
  237. }else{
  238. xoffset = cos(spinang+polyangoffset)*polyradius * -1-(radiusslave);
  239. yoffset = sin(spinang+polyangoffset)*polyradius *1;
  240.  
  241. //counts for use with eoffset functionality
  242. xeoffset = xoffset / eoffsetscale;
  243. yeoffset = yoffset / eoffsetscale;
  244. }
  245.  
  246. spindlerad = spinang;
Advertisement
Add Comment
Please, Sign In to add comment