/* * geomerative example, * updated by ricard marxer * * fjenett 20080417 * fjenett 20081203 - updated to geomerative 19 */ // Key 1 = 0,0 // Key 2 = width,0 // Key 3 = width,height // Key 4 = 0,height // Key S = stop // Key G = go import processing.serial.*; // serial communication import java.io.*; //Java import geomerative.*; RFont font; Bot machine; PrintWriter writer; //--------------------------------------------------- // physical setup float MOTORSEP = 96.0;//72.0; // how far apart are the two steppers float PAGEW = 24.0;// float PAGEH = 18.0; float XMARGIN = 2.0; float YMARGIN = 5.0; // page location relative to motors float PAGE0X = (MOTORSEP-PAGEW)/2.0-0.5; // upper left float PAGE0Y = 32.0;//36.0; //float PAGE0Y = 17.0;//3.0;//12.0;//6.0-PAGEH; // top of paper //12.0;//40.0; // home position of pen carriage. where it is when the program starts float HOMEA = 86.5;//81.0;//82.0;// 58.5225650924496; // 108 float HOMEB = 86.5;//81.0;//82.0;//56.02580320709378; // 108 // scale image to screen for display int windowH = 900; float onScreenScale=windowH/PAGEH; int PPI = 90; int windowW = int(PAGEW*onScreenScale); // feed rates and gcode stuff float SLOWFEED = 150.0; static int PEN_UP = 0; static int PEN_DOWN = 1; // state variables boolean On = false;// USED SO THAT THE KEY 'S' AND 'G' START AND STOP THE DRAWING SEQUENCE, starts off to calibrate boolean doJump = false; boolean atPoint = true; // if the arduino returns that it is at the point, turn true. boolean atHome=true; // configuration boolean serialOn = true; boolean writeToFile = true; boolean printToConsole = true; // drawing state int i=1; int linenum=-1; float y=0; int penState=PEN_UP; RPoint[] pnts; String[] lines = { // "with pencil, draw 1000 random straight lines", // "10 inches long, each day for 10 days,", // "in a 10 x 10 foot square" // wall drawing 138 // "Grid and arcs from four corners. (ACG 103)" // wall drawing 154 //"A black outlined square with a red", //"horizontal line from the midpoint", //"of the left side toward the middle", //"of the right side." // wall drawing 915 "Arcs, circle, and irregular bands." }; boolean linedone=false; void setup() { size(windowW, windowH); smooth(); RG.init(this); font = new RFont( "5thgradecursive.ttf", 48, RFont.LEFT); // arduino g-code interface machine = new Bot(this); machine.waitForStart(); writer = createWriter("test.nc"); machine.setupWriting(writer); background(255); if (printToConsole) println("(HOME: "+HOMEA + " " +HOMEB+")"); machine.storeHome(); machine.penUp(); noCursor(); frameRate( 20 ); nextline(); machine.penUp(); penState=PEN_UP; // println(lines.length); } void nextline() { // line is done linenum++; if(linenum>=lines.length) { linenum=0; exit(); } y=linenum*1.0; RGroup grp = font.toGroup(lines[linenum]); //RCommand.setSegmentator(RCommand.ADAPTATIVE); pnts = grp.getPoints(); i=1; machine.penUp(); penState=PEN_UP; } void draw() { //background(255); //translate(100,100);//2.0/onScreenScale, 2.0/onScreenScale);//width/2, height/2); //translate(width/2, height/2); //ellipse(pnts[0].x, pnts[0].y, 5, 5); if (On) { i++; if (i>=pnts.length) { nextline(); } if (i==1 && serialOn && penState==PEN_UP) { machine.penDown(); penState=PEN_DOWN; } float xcoord = XMARGIN+(pnts[i].x/PPI);//+PAGEW/2.0; float ycoord = y+YMARGIN+(pnts[i].y/PPI);//+PAGEH/2.0; line( (XMARGIN+(pnts[i-1].x/PPI))*onScreenScale, (YMARGIN+y+(pnts[i-1].y/PPI))*onScreenScale, xcoord*onScreenScale, ycoord*onScreenScale ); //doMessage("("+xcoord+","+ycoord+")"); if(serialOn) { machine.sendVal(xcoord, ycoord, PEN_DOWN); machine.waitForOk(); } //ellipse(pnts[i].x, pnts[i].y, 5, 5); } } void stop() { machine.penUp(); //machine.DirectCommand("G1 X"+HOMEA+" Y"+HOMEB+" Z0.0 F"+FASTFEED); machine.DirectCommand("G0 X"+HOMEA+" Y"+HOMEB+" Z0.0"); machine.stop(); }; // keyboard interface void keyPressed() { if (key == 'g') { // start movement On = true; if (printToConsole)println("(GO!)"); machine.penUp(); doJump=true; } if (key == 's') { // stop movement On = false; if (printToConsole)println("(STOP)"); } if (key=='h') { machine.goHome(); } if (!On) { switch(key) { case '1': // top left //machine.sendVal(0, 0, 0); machine.sendVal(XMARGIN, YMARGIN, 0); break; case '2': // top right //machine.sendVal(PAGEW, 0, 0); machine.sendVal(PAGEW-XMARGIN, YMARGIN, 0); break; case '3': // lower right //machine.sendVal(PAGEW, PAGEH, 0); machine.sendVal(PAGEW-XMARGIN, PAGEH-YMARGIN, 0); break; case '4': // lower left //machine.sendVal(0, PAGEH, 0); machine.sendVal(XMARGIN, PAGEH-YMARGIN, 0); break; case '0': machine.penUp(); if (printToConsole)println("(going to stored home position)"); //machine.DirectCommand("G1 X"+HOMEA+" Y"+HOMEB+" Z0.0 F"+FASTFEED); machine.DirectCommand("G0 X"+HOMEA+" Y"+HOMEB+" Z0"); break; case 'd': machine.penDown(); break; case 'u': machine.penUp(); break; default: break; } } } // -------- terminal output and logging -------- // void doMessage(String message) { if (printToConsole) println(message); if (writeToFile) { writer.write(message+"\n"); writer.flush(); } }