Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. // This is a component for Linuxcnc HAL
  2. // Copyright 2020 Sam Sokolik <samcoinc@gmail.com>
  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 float xpos "connect to the x axis potion in hal";
  22. pin out float xposaten "when componant is enabled - grab current x postion";
  23. pin in unsigned numsides=3 "Number of sides in the polygon";
  24. pin in float inscribedradius=1 "inscribed radius of the polygon";
  25. pin in float cutterdiam "cutter diameter";
  26. pin in float eoffsetscale "scale that eoffset is set to.";
  27. pin in float toolang "angle difference between index and actual tool poition";
  28. pin in float polyang "angle of the polygon";
  29. pin in float spindlepos "Spindle position scaled to 1 per rev";
  30. pin out float spindlerad "Spindle postion in radians";
  31. pin out float polyradius "radious of the polygon at given spindle position";
  32. pin out float xoffset "x offset to send to offset componant";
  33. pin out float yoffset "y offset to send to the offset componant";
  34. pin out signed xeoffset "x eoffset output";
  35. pin out signed yeoffset "y eoffset output";
  36. pin out bit isindex "set index enable only once";
  37. pin io bit indexenable "hooked to index enable of encoder";
  38.  
  39. function _;
  40. license "GPL";
  41. ;;
  42. #include <rtapi_math.h>
  43. #define PI 3.14159265358979323846
  44. float spinang;
  45. float scale;
  46. float spinangoffset;
  47. float polyangoffset;
  48. float radiusslave=0;
  49.  
  50. if (!enable) {
  51. xoffset=0;
  52. yoffset=0;
  53. xeoffset=0;
  54. yeoffset=0;
  55. isindex = false;
  56. xposaten = 0;
  57. return;
  58. }
  59.  
  60. // only set index enable once
  61. if (enable && !isindex) {
  62. isindex = true;
  63. indexenable = true;
  64. if (enablexslave){
  65. xposaten=xpos;
  66. }
  67. return;
  68. }
  69.  
  70. // wait for spindle index before actually enabling
  71. if (enable && isindex && indexenable){
  72. return;
  73. }
  74.  
  75. //convert spinangoffset to ratio.. for adding to spindle pos - plus a couple rotations
  76. //so the spindle position isn't negative
  77. spinangoffset = (toolang/360-polyang/360)+2;
  78. polyangoffset = (polyang/360)*2*PI;
  79.  
  80. //formual I found is for circumscribed - convert to inscribed - makes more sense.
  81.  
  82.  
  83. spinang = ((spindlepos + spinangoffset) - (int)(spindlepos + spinangoffset))*2*PI;
  84.  
  85. //calculate radius based on if x moves
  86. if (enablexslave){
  87. radiusslave=xpos-xposaten;
  88. }
  89. scale=(inscribedradius-radiusslave)/cos(PI/numsides);
  90. polyradius = ((cos(PI/numsides)/cos((fmod(spinang, (2*PI/numsides))-PI/numsides)))*scale-cutterdiam/2);
  91.  
  92.  
  93.  
  94.  
  95. //actual offsets applied - could be used for offset componant.
  96. xoffset = cos(spinang+polyangoffset)*polyradius * -1-(radiusslave);
  97. yoffset = sin(spinang+polyangoffset)*polyradius;
  98.  
  99. //counts for use with eoffset functionality
  100. xeoffset = xoffset / eoffsetscale;
  101. yeoffset = yoffset / eoffsetscale;
  102.  
  103. spindlerad = spinang;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement