Public
Edited
May 9, 2023
Importers
Insert cell
Insert cell
function Op(operation, x, y, z, f) {
this.op = operation;
this.args = {
x: x,
y: y,
z: z,
f: f
};
}
Insert cell
Insert cell
function lowerSBP(toolPath) {
let IR = [];

toolPath.forEach(function (instruction) {
if (!instruction || instruction[0] == "''") {
return;
}
let tokens = instruction.trim().split(",");
let opcode = tokens[0];
let newPosition;
if (opcode === "M2" || opcode === "J2") {
newPosition = new Op(
"move",
parseFloat(tokens[1]),
parseFloat(tokens[2]),
0,
null
);
} else if (opcode === "M3" || opcode === "J3") {
newPosition = new Op(
"move",
parseFloat(tokens[1]),
parseFloat(tokens[2]),
parseFloat(tokens[3]),
null
);
} else if (opcode === "MZ" || opcode === "JZ") {
newPosition = new Op("move", 0, 0, parseFloat(tokens[1]), null);
} else if (opcode === "MX" || opcode === "JX") {
newPosition = new Op("move", parseFloat(tokens[1]), 0, 0, null);
} else if (opcode === "MY" || opcode === "JY") {
newPosition = new Op("move", 0, parseFloat(tokens[1]), 0, null);
} else {
return;
}
IR.push(newPosition);
});
return IR;
}
Insert cell
function lowerGCode(gcodeArray) {
let IR = [];

let opcodeRe = /(G[0-9]+|M[0-9]+)/;
let opXRe = /X(-?[0-9]+.[0-9]+)/;
let opYRe = /Y(-?[0-9]+.[0-9]+)/;
let opZRe = /Z(-?[0-9]+.[0-9]+)/;
let opFRe = /F(-?[0-9]+.[0-9]+)/;
let findOpcode = (instruction, argRe) => {
let maybeArgResults = instruction.match(argRe);
if (!maybeArgResults) {
return "";
}
return maybeArgResults[0];
};
let findArg = (instruction, argRe) => {
let maybeArgResults = instruction.match(argRe);
if (!maybeArgResults || maybeArgResults.length < 2) {
return null;
}
return parseFloat(maybeArgResults[1]) || null;
};

gcodeArray.forEach(function (instruction) {
if (!instruction || instruction[0] == "''") {
return;
}
let tokens = instruction.trim().split(" ");

let newPosition;
let opcode = findOpcode(tokens[0], opcodeRe);
if (opcode === "G0" || opcode === "G1") {
let opx = findArg(tokens[1], opXRe);
let opy = findArg(tokens[2], opYRe);
let opz = findArg(tokens[3], opZRe);
let opf = findArg(tokens[4], opFRe);

newPosition = new Op("move", opx, opy, opz, opf);
IR.push(newPosition);
}
});
return IR;
}
Insert cell
IR2 = lowerGCode(["G0 X10.5 Y20.0 Z30.8 F300", "G1 X400.6 Y35.89 Z200 F60"])
Insert cell
IR = lowerSBP(["M3, 20, 20, 0", "M3, 50, 50, 50", "M2, 5, 6"])
Insert cell
Curve = BasicVis(IR)
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more