From 4c3af133d2ea67ff6052bdbcf17802354e36394f Mon Sep 17 00:00:00 2001 From: James Devine Date: Tue, 27 Jan 2015 15:43:59 -0800 Subject: [PATCH] --- gcodemake.pde | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 gcodemake.pde diff --git a/gcodemake.pde b/gcodemake.pde new file mode 100644 index 0000000..2d8cdf9 --- /dev/null +++ b/gcodemake.pde @@ -0,0 +1,137 @@ +/** + * Convert a JPG image to gcode for Mr Beam + * To use, put the .jpg you want to use in the /data folder of your sketch and adjust the canvas size(x,y) to suit the image, + * Use the feedrate, laserfloor and lasermax variables to determine how fast and how hard it burns. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + */ + +// The next line is needed if running in JavaScript Mode with Processing.js +/* @pjs preload="bender.jpg"; */ + +PImage img; // Declare variable "a" of type PImage + float xbed = 435; + float ybed = 300; + float feedrate = 800; + float laserfloor = 90; //highest typical value + float lasermax = 255; //laser scale factor + float scale = 20; + int plotthresh = 30; + color c; + int oldgrey = 0; + +PrintWriter output; + +void setup() { + size(299, 616); + // The image file must be in the data folder of the current sketch + // to load successfully + img = loadImage("input.jpg"); // Load the image into the program + noLoop(); + output = createWriter("output.gco"); //this is the name of the output file + +} + +void draw() { + // Displays the image at its actual size at point (0,0) + image(img, 0, 0); + + + //set up the output file + output.println("$H"); + output.println("G92X0Y0Z0"); + output.println("G90"); + output.println("M08"); + output.println("G21"); + output.print("F"); + output.println(feedrate); + + + for (int x = 0; x < width; x = x+2) { //the X axis loop + for (int y = 0; y < height; y++){ //the forward half of the y axis loop + + //y axis forward calculations + float xpos = x; + xpos = xpos/scale; + float ypos = y; + ypos = ypos/scale; + color c = get(x,y); + float red = red(c); + float green = green(c); + float blue = blue(c); + int grey = (int)(red+green+blue)/3; + color Color =color(grey,grey,grey); + set(x,y,Color); + grey = int(laserfloor-((float(grey)/256)*lasermax)); //this is where the laser scaling is done + //when considering the scale values remember 0 is black on the screen and 255 is white, so we want the reverse. + +//if the value is worth plotting, plot it, threshold is set above. Might give odd behaviour at edge cases. +if ((grey != oldgrey)&(grey>plotthresh)) { + output.print("G1 "); + output.print('X'); + output.print(nf(xpos,3,4)); + output.print(" "); + output.print('Y'); + output.print(nf(ypos,3,4)); + output.print(" "); + output.print("M03 S"); + output.println(grey); +} + + +oldgrey=grey; //pass the last pixel value back, so that the laser just runs on through pixels of the same colour + + } +for (int y = height-1; y > 0; y--){ //loop for negative Y axis, to optimise the raster scan pattern. + +//calculations for y axis negative cycle + float xpos = x+1; + xpos = xpos/scale; + float ypos = y; + ypos = ypos/scale; + color c = get(x+1,y); + float red = red(c); + float green = green(c); + float blue = blue(c); + int grey = (int)(red+green+blue)/3; + color Color =color(grey,grey,grey); + set(x+1,y,Color); + grey = int(laserfloor-((float(grey)/256)*lasermax)); + + +if ((grey != oldgrey)&(grey>plotthresh)) { //switch to decide if pixel is worth plotting + + output.print("G1 "); + output.print('X'); + output.print(nf(xpos,3,4)); + output.print(" "); + output.print('Y'); + output.print(nf(ypos,3,4)); + output.print(" "); + output.print("M03 S"); + output.println(grey); +} +oldgrey = grey; //pass back the last pixel intensity value + } + +} + +//close out the .gcode file by turning off the laser and the fan. +output.println("M05"); +output.println("M09"); +output.flush(); // Writes the remaining data to the file +output.close(); +//exit(); +} \ No newline at end of file