xxxxxxxxxx
308
ZOOM = 3;
NO_GRID = false;
Z_DOWN = 20;
Z_UP = 22;
HOME_X = 140;
HOME_Y = 30;
WAIT_FOR_PEN = 10000; //ms
PEN_OFFSET = 38;
/*
a() Absolute coords
r() Relative coords
down() Lower pen
up() Raise pen
m(x,y) Move pen to x,y
l(x,y) Draw line to x,y
c(x,y,r) Draw circle at x,y with radius r
*/
// circles
function plot(){
m(110,110);
r();
for (i= 1; i<65; i+=1.3){
c(sin(i/3)*0.5,sin(i/3)*0.5,i);
}
}
// lines from chaos
// function plot(){
// let radius = 5;
// for (y= 0; y<40; y+=1){
// a();
// m(20, 13 + 5 * y);
// r();
// for (x= 0; x<15; x+=1){
// let ang = (random()-0.5) * y/10;
// m(2+ang+radius-cos(ang)*radius, sin(ang)*radius);
// l(ang+radius+cos(ang)*radius, -sin(ang)*radius);
// }
// }
// }
// concentric squares
// function plot(){
// a();
// let x = 110;
// let y = 110;
// let p = PI/2;
// let r = 90;
// for (a = 0; a<0.96; a+=0.029){
// m(x+cos(a)*r, y+sin(a)*r);
// l(x+cos(a+p)*r, y+sin(a+p)*r);
// l(x+cos(a+p*2)*r, y+sin(a+p*2)*r);
// l(x+cos(a+p*3)*r, y+sin(a+p*3)*r);
// l(x+cos(a)*r, y+sin(a)*r);
// r-=2.7;
// }
// }
// swirly concentric squares
// function plot(){
// a();
// let x = 110;
// let y = 110;
// let p = PI/2;
// let r = 100;
// let b = -p/2;
// for (s = 0; s<16.5; s+=0.4){
// y += sin(s/2)/3;
// x += sin(s/2)/3;
// b += 0.019;
// m(x+cos(b)*r, y+sin(b)*r);
// l(x+cos(b+p)*r, y+sin(b+p)*r);
// l(x+cos(b+p*2)*r, y+sin(b+p*2)*r);
// l(x+cos(b+p*3)*r, y+sin(b+p*3)*r);
// l(x+cos(b)*r, y+sin(b)*r);
// r-=2.4;
// }
// }
// square spiral
// function plot(){
// a();
// let x = 110;
// let y = 110;
// let p = PI/2;
// let r = 100;
// let b = -p/2;
// //m(x+cos(b)*r, y+sin(b)*r);
// m(x+cos(b+p)*r, y+sin(b+p)*r);
// for (s = 0; s<5.5; s+=0.4){
// l(x+cos(b+p)*r, y+sin(b+p)*r);
// l(x+cos(b+p*2)*r, y+sin(b+p*2)*r);
// l(x+cos(b+p*3)*r, y+sin(b+p*3)*r);
// l(x+cos(b)*r, y+sin(b)*r);
// r-= 1+s*2.6;
// }
// }
///////////////////////////////////
///////////////////////////////////
///////////////////////////////////
///////////////////////////////////
///////////////////////////////////
let absolute = true;
let drawing = false;
let turtle;
let gcode= [];
W = 220;
let img;
function a(){
absolute = true;
gcode.push("G90 ;absolute");
}
function r(){
absolute = false;
gcode.push("G91 ;relative");
}
function m(x,y){
if (drawing) up();
if (absolute) x = x + PEN_OFFSET;
gcode.push("G0 X"+x+" Y"+y)
turtle.x = absolute? x: turtle.x + x;
turtle.y = absolute? y: turtle.y + y;
}
function l(x, y){
if (!drawing) down();
if (absolute) x = x + PEN_OFFSET;
let xx = absolute? x: turtle.x + x;
let yy = absolute? y: turtle.y + y;
line(
turtle.x- PEN_OFFSET,
W - turtle.y,
xx - PEN_OFFSET,
W - yy);
gcode.push("G1 X"+x+" Y"+y)
turtle.x = xx;
turtle.y = yy;
}
function down(){
if (drawing) return;
drawing = true;
let zz = absolute? Z_DOWN : "-" + (Z_UP-Z_DOWN);
gcode.push("G0 Z" + zz);
}
function up(){
if (!drawing) return;
drawing = false;
let zz = absolute? Z_UP : (Z_UP-Z_DOWN);
gcode.push("G0 Z" + zz);
}
function c(x,y,r){
if (absolute) x = x + PEN_OFFSET;
turtle.x = absolute? x: turtle.x + x;
turtle.y = absolute? y: turtle.y + y;
circle(turtle.x - PEN_OFFSET, W-turtle.y, r*2);
up();
if (absolute){
gcode.push("G0 X"+(turtle.x - r) +" Y"+turtle.y);
} else {
gcode.push("G0 X"+(x-r) +" Y"+ y);
}
down();
gcode.push("G2 I"+r);
up();
gcode.push("G0 X"+(absolute ? turtle.x : r));
}
function preload() {
//img = loadImage('ali.jpg');
}
function setup() {
turtle = createVector(0,0);
createCanvas(W*ZOOM, W*ZOOM);
background(220);
scale(ZOOM)
fill(230);
noStroke();
rect(15,15,190,190)
noFill();
strokeWeight(1/ZOOM);
if (!NO_GRID) grid();
header();
plot();
footer();
print(join(gcode, '\n'));
//image(img, 0, 0);
}
function header(){
// G28 go home
// G21 use milimeters
// G0 F4500 set rate
gcode.push("%\nG28\nG21\nG0 F4500")
// go pen home placement
a();
gcode.push("G0 Z"+Z_DOWN);
gcode.push("G0 X"+ HOME_X + " Y"+ HOME_Y);
gcode.push("G4 P"+WAIT_FOR_PEN);
// avisar inicio
gcode.push("M300 S440 P200");
gcode.push("G4 P1000");
gcode.push("M300 S440 P200");
gcode.push("G4 P1000");
gcode.push("M300 S440 P200");
gcode.push("G4 P1000");
gcode.push("G0 Z"+Z_UP);
gcode.push("; -----\n\n")
}
function footer(){
gcode.push("\n;------")
gcode.push("G0 Z"+(Z_UP*2));
//wait to finish and sing
gcode.push("M400");
gcode.push("M300 S440 P200");
gcode.push("M300 S660 P250");
gcode.push("M300 S880 P300");
gcode.push("M2\n%\n;END. feiss.be 2022")
}
function grid(){
textSize(4);
for (i=0; i <= W; i+=10)
{
noStroke()
fill(180);
text(i, i+0.5, W-0.5);
text(W-i, 0.5, i - 0.5);
stroke(215);
line(0, i, W, i);
line(i, 0, i, W);
}
fill(150)
noStroke()
text("Click to export gcode", W-39, 5);
noFill()
stroke(0)
}
function mouseClicked() {
saveStrings(gcode, "proyecto", "gcode");
}