Advertisement
Mangus875

Untitled

Nov 28th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. const pi = Math.PI;
  2. const car = (r, theta) => vector(r*Math.cos(theta), r*Math.sin(theta));
  3.  
  4. const validCommands = {
  5. 'GCODE': ['string', 'object'], // insert gcode
  6. 'PATH': ['object'], // list of points to follow
  7. 'GOTO': ['object'], // go to point
  8. 'PEN': ['string'], // "UP" or "DOWN"
  9. 'COMMENT': ['string'], // add gcode comment
  10. };
  11.  
  12. class Command {
  13. constructor(cmd, data, comment="") {
  14. let validCmd = validCommands[cmd];
  15. let validDta = validCmd != undefined ? validCmd.includes(typeof(data)) : undefined;
  16. if (!validCmd) throw(`InvalidCommand: '${cmd}'`);
  17. if (!validDta) throw(`InvalidArgument: Type '${typeof(data)}' for '${cmd}'`);
  18.  
  19. this.cmd = cmd;
  20. this.data = data;
  21. this.comment = comment;
  22. }
  23.  
  24. static toGcode(addSpace=false) {
  25. switch (this.cmd) {
  26. case 'GCODE':
  27. if (typeof(this.data) == 'string') {
  28.  
  29. } else {
  30.  
  31. }
  32. break;
  33.  
  34. case 'PATH':
  35. break;
  36.  
  37. case 'GOTO':
  38. break;
  39.  
  40. case 'PEN':
  41. break;
  42.  
  43. case 'COMMENT':
  44. break;
  45. }
  46. }
  47.  
  48. gcode() {
  49. let code = this.comment != "" ? `; ${this.comment}` : '';
  50.  
  51. switch (this.cmd) {
  52. case 'GCODE':
  53. if (typeof(this.data) == 'string') {
  54. code += this.data;
  55. } else {
  56. this.data.forEach((line, i) => {
  57. code += line;
  58. code += (i < this.data.length-1) ? '\n' : '';
  59. });
  60. }
  61. break;
  62.  
  63. case 'PATH':
  64. break;
  65.  
  66. case 'GOTO':
  67. break;
  68.  
  69. case 'PEN':
  70. break;
  71.  
  72. case 'COMMENT':
  73. break;
  74. }
  75. }
  76. }
  77.  
  78. class Vector {
  79. constructor(x=0, y=0) {
  80. this.x = x;
  81. this.y = y;
  82. }
  83.  
  84. add(vect) {
  85. this.x += vect.x;
  86. this.y += vect.y;
  87. }
  88.  
  89. sub(vect) {
  90. this.x -= vect.x;
  91. this.y -= vect.y;
  92. }
  93.  
  94. mult(scalar) {
  95. this.x *= scalar;
  96. this.y *= scalar;
  97. }
  98. }
  99.  
  100. const addVect = (a, b) => ({
  101. 'X': a.X + b.X,
  102. 'Y': a.Y + b.Y
  103. });
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. const revStr = (str) => str.split('').reverse().join('');
  113. const formatNum = (n, sep=',') => revStr(revStr(n.toString()).match(/.{1,3}/g).join(sep));
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement