Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const pi = Math.PI;
- const car = (r, theta) => vector(r*Math.cos(theta), r*Math.sin(theta));
- const validCommands = {
- 'GCODE': ['string', 'object'], // insert gcode
- 'PATH': ['object'], // list of points to follow
- 'GOTO': ['object'], // go to point
- 'PEN': ['string'], // "UP" or "DOWN"
- 'COMMENT': ['string'], // add gcode comment
- };
- class Command {
- constructor(cmd, data, comment="") {
- let validCmd = validCommands[cmd];
- let validDta = validCmd != undefined ? validCmd.includes(typeof(data)) : undefined;
- if (!validCmd) throw(`InvalidCommand: '${cmd}'`);
- if (!validDta) throw(`InvalidArgument: Type '${typeof(data)}' for '${cmd}'`);
- this.cmd = cmd;
- this.data = data;
- this.comment = comment;
- }
- static toGcode(addSpace=false) {
- switch (this.cmd) {
- case 'GCODE':
- if (typeof(this.data) == 'string') {
- } else {
- }
- break;
- case 'PATH':
- break;
- case 'GOTO':
- break;
- case 'PEN':
- break;
- case 'COMMENT':
- break;
- }
- }
- gcode() {
- let code = this.comment != "" ? `; ${this.comment}` : '';
- switch (this.cmd) {
- case 'GCODE':
- if (typeof(this.data) == 'string') {
- code += this.data;
- } else {
- this.data.forEach((line, i) => {
- code += line;
- code += (i < this.data.length-1) ? '\n' : '';
- });
- }
- break;
- case 'PATH':
- break;
- case 'GOTO':
- break;
- case 'PEN':
- break;
- case 'COMMENT':
- break;
- }
- }
- }
- class Vector {
- constructor(x=0, y=0) {
- this.x = x;
- this.y = y;
- }
- add(vect) {
- this.x += vect.x;
- this.y += vect.y;
- }
- sub(vect) {
- this.x -= vect.x;
- this.y -= vect.y;
- }
- mult(scalar) {
- this.x *= scalar;
- this.y *= scalar;
- }
- }
- const addVect = (a, b) => ({
- 'X': a.X + b.X,
- 'Y': a.Y + b.Y
- });
- const revStr = (str) => str.split('').reverse().join('');
- const formatNum = (n, sep=',') => revStr(revStr(n.toString()).match(/.{1,3}/g).join(sep));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement