Creating A G-Code Simulator Tool

edited February 2016 in Questions about Tools

Hi, I have a sketch of a G-code simulator that seems to be working with certain xyz value ranges stated in the sketch. If I eventually was to submit it as a tool on processing.org, can I post the sketch on the forum for faster debugging? It is quite long and I do not see an attachment button. Ryan

Answers

  • Answer ✓

    either post it here - split it over a few comments if too long

    (you can only upload images here)

    or upload it on dropbox or so

  • edited February 2016
    //This script will currently only work with...
    //  1.  positive X gcode values between 0-1200
    //  2.  positive Y gcode values that are translated to negative Z
    //      values in the processing sketch.  moves object in axis that 
    //      is imaginary through the plane of your computer screen.
    //  3.  positive Z gcode values that are 0-500
    //  4.  Only takes G01 commands of form G01 Xxxx Yxxx Zxxx   
    
    
    //To Do
    // A.  Draw tip path in 3 dimensions.s
    // B.  Implement arc arguments.
    // C.  Scale somehow for larger coordinates.  i.e. fit 1/16 microstepping
    //     axes from my steppers.  Maybe a keycode zoom.
    // D.  Change shape to tool tip.ss 
    
    float scale = 1;
    int delayy = 500;
    float a,aa;                 // Angle of rotation
    float offset = PI/24.0;  // Angle offset between boxes
    float numb=100;
    int i = 0;
    int n=1;
    int v = 5;
    File selection2;
    File inFile;
    boolean outFile, outReader;
    String ngcPath;
    String[] gcod1, gcod2;
    float dx, dy, dz;
    float ddx1,ddy1,ddz1,ddx2,ddy2,ddz2;
    int kk=0;
    int yi=0;
    PImage tex,ax;
    PShape sph;
    int x1,y1,z1,diy,dix,diz,diyTranslated_oriented;
    
    void setup() { 
      size(1200, 600, P3D);
      //noStroke();  
      outFile = false;
      //tex = loadImage("/users/rhh/desktop/bw2.jpg");
      //ax = loadImage("/users/rhh/desktop/blue.jpg");
      textureMode(NORMAL);
    
      //these are my initial tool tip positions
      dix=width/8;
      diz=0;
      diy=0;
      //This reverses the Z-axis direction.  The processing and standard
      //cartesian coordinate systems are not equivalent.  The processing 
      //positive y values are the same as the standard cartesian coordinate
      //system negative z values, and vice-versa...
      //Must compensate when reading G-code....
      ddx1=dix+200; //initial coordinates translated 200 units in +X
      ddz1=diy+200;  //initial coordinates translated 200 units in +Z
      ddy1=diz;
    
    
    } 
    
    
    void draw() {
    
      lights();
      delay(delayy);
      background(0);
      noStroke();
      //noFill();
    
      //This section places a cube with an image on the face at 
      //the desired position.  I was initially learning how to make 
      // textured surfaces or shaders in 3D images, which is why it is
      // a random image and is rotating arbitrarily.  I want to eventually 
      // replace the object with a 3D mill tip that could rotate when
      // simulator is turned on.
    
      pushMatrix();
      translate(ddx1,3*height/4-ddz1,ddy1-400);
      //rotateX(a + offset);
      rotateY(a + offset);
      scale(60);
      //TexturedCube(tex);
      //TexturedCube();
      TexturedPyramid();
      a += 10; 
      popMatrix();
    
      //This section inserts X,Y, and Z axis on the screen.
      pushMatrix();
      translate(width/8, 3*height/4, -400); 
      scale(10);
      //Xaxis(ax);
      //Yaxis(ax);
      //Zaxis(ax);
      Xaxis();
      Yaxis();
      Zaxis();
      aa += 0.1;
      popMatrix();
    
       //This section should only run after file has been loaded
       //with the Q button.
      if (outFile) {
        String lines[] = loadStrings(ngcPath);
         println(ddx1+" "+ddz1+" "+ddy1);
         println("kk= "+kk);
        if (kk==0) {
          yi=1;
          println("there are " + lines.length + " lines  "+kk);
        }
        if (yi>1){
        gcod1 = splitTokens(lines[yi-2]);
        gcod2 = splitTokens(lines[yi]);
        if (gcod1.length >= 4) {
          if (gcod1[0].equals("G01") && gcod1[0].equals("G01")) {
            //dx = round(xdiff());
            //dy = round(ydiff());
            //dz = round(zdiff());
    
            String[] xx1 = splitTokens(gcod1[1], "X");
            ddx1 = float(join(xx1, " "));
            String[] yy1 = splitTokens(gcod1[2], "Y");
            ddy1 = float(join(yy1, " "));
            String[] zz1 = splitTokens(gcod1[3], "Z");
            ddz1 = float(join(zz1, " "));
    
            String[] xx2 = splitTokens(gcod2[1], "X");
            ddx2 = float(join(xx2, " "));
            String[] yy2 = splitTokens(gcod2[2], "Y");
            ddy2 = float(join(yy2, " "));
            String[] zz2 = splitTokens(gcod2[3], "Z");
            ddz2 = float(join(zz2, " "));
    
            println("int... = "+gcod1[1]);
    
            //  Section RYRY.....  Draw line path
            strokeWeight(10);
            stroke(255);
            for (int crycry = 2; crycry < yi; crycry++)
            {
            gcod1 = splitTokens(lines[crycry-2]);
            gcod2 = splitTokens(lines[crycry]);  
    
            String[] xxx1 = splitTokens(gcod1[1], "X");
            ddx1 = float(join(xx1, " "));
            String[] yyy1 = splitTokens(gcod1[2], "Y");
            ddy1 = float(join(yy1, " "));
            String[] zzz1 = splitTokens(gcod1[3], "Z");
            ddz1 = float(join(zz1, " "));
    
            String[] xxx2 = splitTokens(gcod2[1], "X");
            ddx2 = float(join(xx2, " "));
            String[] yyy2 = splitTokens(gcod2[2], "Y");
            ddy2 = float(join(yy2, " "));
            String[] zzz2 = splitTokens(gcod2[3], "Z");
            ddz2 = float(join(zz2, " "));
    
            //line(int(ddx1)+width/8,3*height/4-int(ddz1),int(ddy1),int(ddx2+width/8),int(3*height/4-ddz2),int(ddy2));
            //vertex(ddx1+width/8,3*height/4-ddz1,ddy1);
            //vertex(ddx2+width/8,3*height/4-ddz2,ddy2);
            //End Section RYRY
            toolPath(crycry);
            }
    
            println (int(ddx1)+":"+int(ddy1)+":"+int(ddz1)+":"+int(ddx2)+":"+int(ddy2)+":"+int(ddz2));
          }
        }
        if (yi>=lines.length-1) {
          outFile = false; 
          dx=0;
          dy=0;
          dz=0;
          kk=0;
          yi=0;
        }
      }}
      //i++;
      yi++;
      if (outFile) {
      kk++;}
    } 
    
    //function to load file from finder
    void fileSelected(File selection) {
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
      } else {
        ngcPath = selection.getAbsolutePath();
        outFile = true;
        outReader = true;
        println("User selected " + selection.getAbsolutePath());
      }
    }
    
    // Function to load file when q button is pressed while 
    // the mouse cursor is position over the window created when the
    // processing script is run.
    void keyPressed() {
      if (key == 'q' || key == 'Q')
      {
        selectInput("Select a file to process:", "fileSelected");
      }
    }
    
    //Originally used to computer position vector differences between
    //G-code command lines
    
    /*
    float xdiff() {
      String[] xx1 = splitTokens(gcod1[1], "X");
      String[] xx2 = splitTokens(gcod2[1], "X");
      float xt1 = float(join(xx1, " "));
      float xt2 = float(join(xx2, " "));
      float xt = xt2-xt1;
      return xt;
    }
    
    float ydiff() {
      String[] xx1 = splitTokens(gcod1[2], "Y");
      String[] xx2 = splitTokens(gcod2[2], "Y");
      float yt1 = float(join(xx1, " "));
      float yt2 = float(join(xx2, " "));
      float yt = yt2-yt1;
      return yt;
    }
    
    float zdiff() {
      String[] xx1 = splitTokens(gcod1[3], "Z");
      String[] xx2 = splitTokens(gcod2[3], "Z");
      float zt1 = float(join(xx1, " "));
      float zt2 = float(join(xx2, " "));
      float zt = zt2-zt1;
      return zt;
    }
    */
    
    //This creates the shape of the cube with an image
    //loaded on each face
    //void TexturedCube(PImage ax) {
    void TexturedCube() {
      beginShape(QUADS);
      //texture(ax);
      fill(0,0,255);
    
      // +Z "front" face
      vertex(-1, -1, 1, 0, 0);
      vertex( 1, -1, 1, 1, 0);
      vertex( 1, 1, 1, 1, 1);
      vertex(-1, 1, 1, 0, 1);
    
      // -Z "back" face
      vertex( 1, -1, -1, 0, 0);
      vertex(-1, -1, -1, 1, 0);
      vertex(-1, 1, -1, 1, 1);
      vertex( 1, 1, -1, 0, 1);
    
      // +Y "bottom" face
      vertex(-1, 1, 1, 0, 0);
      vertex( 1, 1, 1, 1, 0);
      vertex( 1, 1, -1, 1, 1);
      vertex(-1, 1, -1, 0, 1);
    
      // -Y "top" face
      vertex(-1, -1, -1, 0, 0);
      vertex( 1, -1, -1, 1, 0);
      vertex( 1, -1, 1, 1, 1);
      vertex(-1, -1, 1, 0, 1);
    
      // +X "right" face
      vertex( 1, -1, 1, 0, 0);
      vertex( 1, -1, -1, 1, 0);
      vertex( 1, 1, -1, 1, 1);
      vertex( 1, 1, 1, 0, 1);
    
      // -X "left" face
      vertex(-1, -1, -1, 0, 0);
      vertex(-1, -1, 1, 1, 0);
      vertex(-1, 1, 1, 1, 1);
      vertex(-1, 1, -1, 0, 1);
    
    
      endShape();
    }
    
    void TexturedPyramid() {
      beginShape(TRIANGLE);
      //texture(ax);
      fill(0,0,255);
    
      vertex(-1,-1,-1);
      vertex(1,-1,-1);
      vertex(0,1,0);
    
      vertex(1,-1,-1);
      vertex(1,-1,1);
      vertex(0,1,0);
    
      vertex(1,-1,1);
      vertex(1,-1,1);
      vertex(0,1,0);
    
      vertex(-1,-1,1);
      vertex(-1,-1,-1);
      vertex(0,1,0);
    
      endShape();
    }
    
    //The reast of the functions create the axis bars...
    //void Yaxis(PImage tex) {
    void Yaxis() {
      beginShape(QUADS);
      //texture(tex);
      fill(0,255,0);
    
      // +Z "front" face
      //Zlong
      vertex(-1, -1, 100, 0, 0);
      vertex( 1, -1, 100, 1, 0);
      vertex( 1, 1, 100, 1, 1);
      vertex(-1, 1, 100, 0, 1);
    
    
      // -Z "back" face
      vertex( 1, -1, -300, 0, 0);
      vertex(-1, -1, -300, 1, 0);
      vertex(-1, 1, -300, 1, 1);
      vertex( 1, 1, -300, 0, 1);
    
      // +Y "bottom" face
      vertex(-1, 1, 100, 0, 0);
      vertex( 1, 1, 100, 1, 0);
      vertex( 1, 1, -300, 1, 1);
      vertex(-1, 1, -300, 0, 1);
    
      // -Y "top" face
      vertex(-1, -1, -300, 0, 0);
      vertex( 1, -1, -300, 1, 0);
      vertex( 1, -1, 100, 1, 1);
      vertex(-1, -1, 100, 0, 1);
    
      // +X "right" face
      vertex( 1, -1, 100, 0, 0);
      vertex( 1, -1, -300, 1, 0);
      vertex( 1, 1, -300, 1, 1);
      vertex( 1, 1, 100, 0, 1);
    
      // -X "left" face
      vertex(-1, -1, -300, 0, 0);
      vertex(-1, -1, 100, 1, 0);
      vertex(-1, 1, 100, 1, 1);
      vertex(-1, 1, -300, 0, 1);
    
      endShape();
    }
    
    //void Zaxis(PImage ax) {
    void Zaxis() {
      beginShape(QUADS);
      //texture(ax);
      tint(0,255,0);
    
      // +Z "front" face
      //Ylong
      vertex(-1, 0, 1, 0, 0);
      vertex( 1, 0, 1, 1, 0);
      vertex( 1, -100, 1, 1, 1);
      vertex(-1, -100, 1, 0, 1);
    
    
      // -Z "back" face
      vertex( 1, 0, -1, 0, 0);
      vertex(-1, 0, -1, 1, 0);
      vertex(-1, -100, -1, 1, 1);
      vertex( 1, -100, -1, 0, 1);
    
      // +Y "bottom" face
      vertex(-1, -100, 1, 0, 0);
      vertex( 1, -100, 1, 1, 0);
      vertex( 1, -100, -1, 1, 1);
      vertex(-1, -100, -1, 0, 1);
    
      // -Y "top" face
      vertex(-1, 0, -1, 0, 0);
      vertex( 1, 0, -1, 1, 0);
      vertex( 1, 0, 1, 1, 1);
      vertex(-1, 0, 1, 0, 1);
    
      // +X "right" face
      vertex( 1, 0, 1, 0, 0);
      vertex( 1, 0, -1, 1, 0);
      vertex( 1, -100, -1, 1, 1);
      vertex( 1, -100, 1, 0, 1);
    
      // -X "left" face
      vertex(-1, 0, -1, 0, 0);
      vertex(-1, 0, 1, 1, 0);
      vertex(-1, -100, 1, 1, 1);
      vertex(-1, -100, -1, 0, 1);
    
      endShape();
    }
    
    //void Xaxis(PImage ax) {
    void Xaxis() {
      beginShape(QUADS);
      //texture(ax);
      fill(0,255,0);
    
      // +Z "front" face
      //Xlong
      vertex(0, -1, 1, 0, 0);
      vertex( 300, -1, 1, 1, 0);
      vertex( 300, 1, 1, 1, 1);
      vertex(0, 1, 1, 0, 1);
    
    
      // -Z "back" face
      vertex( 300, -1, -1, 0, 0);
      vertex(0, -1, -1, 1, 0);
      vertex(0, 1, -1, 1, 1);
      vertex( 300, 1, -1, 0, 1);
    
      // +Y "bottom" face
      vertex(0, 1, 1, 0, 0);
      vertex( 300, 1, 1, 1, 0);
      vertex( 300, 1, -1, 1, 1);
      vertex(0, 1, -1, 0, 1);
    
      // -Y "top" face
      vertex(0, -1, -1, 0, 0);
      vertex( 300, -1, -1, 1, 0);
      vertex( 300, -1, 1, 1, 1);
      vertex(0, -1, 1, 0, 1);
    
      // +X "right" face
      vertex( 300, -1, 1, 0, 0);
      vertex( 300, -1, -1, 1, 0);
      vertex( 300, 1, -1, 1, 1);
      vertex( 300, 1, 1, 0, 1);
    
      // -X "left" face
      vertex(0, -1, -1, 0, 0);
      vertex(0, -1, 1, 1, 0);
      vertex(0, 1, 1, 1, 1);
      vertex(0, 1, -1, 0, 1);
    
      endShape();
    }
    
      //
    void toolPath(int yi) {
      int[] axx1 = new int[yi];
      int[] ayy1 = new int[yi];
      int[] azz1 = new int[yi];
      int[] axx2 = new int[yi];
      int[] ayy2 = new int[yi];
      int[] azz2 = new int[yi];
      String lines[] = loadStrings(ngcPath);
      for (int crycry = 2; crycry < yi; crycry++)
            {
            gcod1 = splitTokens(lines[crycry-2]);
            gcod2 = splitTokens(lines[crycry-1]);  
    
            String[] xxx1 = splitTokens(gcod1[1], "X");
            ddx1 = float(join(xxx1, " "));
            axx1[yi-2]=int(ddx1);
            println("axx1[yi-2] = .... "+axx1[yi-2]);
            String[] yyy1 = splitTokens(gcod1[2], "Y");
            ddy1 = float(join(yyy1, " "));
            ayy1[yi-2]=int(ddy1);
            String[] zzz1 = splitTokens(gcod1[3], "Z");
            ddz1 = float(join(zzz1, " "));
            azz1[yi-2]=int(ddz1);
    
            String[] xxx2 = splitTokens(gcod2[1], "X");
            ddx2 = float(join(xxx2, " "));
            axx2[yi-2]=int(ddx2);
            String[] yyy2 = splitTokens(gcod2[2], "Y");
            ddy2 = float(join(yyy2, " "));
            ayy2[yi-2]=int(ddy2);
            String[] zzz2 = splitTokens(gcod2[3], "Z");
            ddz2 = float(join(zzz2, " "));
            azz2[yi-2]=int(ddz2);
    
    
            //line(int(ddx1)+width/8,3*height/4-int(ddz1),int(ddy1),int(ddx2+width/8),int(3*height/4-ddz2),int(ddy2));
            }
          beginShape(LINES);  
         for (int crycry = 2; crycry < yi; crycry++)
            {   
              vertex(axx1[crycry-2],ayy1[crycry-2],azz1[crycry-2]);
              vertex(axx2[crycry-2],ayy2[crycry-2],azz2[crycry-2]);
            }  
         endShape();
    
    }
    
  • edited February 2016

    Hi Chrisir, I posted the sketch; however, there was some auto formatting the replaced many of the line endings perhaps from /r to /n or similarly. It also placed parts of the code in separate sections though it is all one sketch. Curpher

  • edited February 2016

    Thanks bilmor. I was able to select all and then press the code icon. This action formatted my code correctly. I actually wrote this because it was less time consuming than installing debian 7.9 Linux OS to run LinuxCNC. It was mostly examples taken from Schiffman et. al and meshed together into a single script. There are very few G-code simulators I can list. I have never seen one implemented in the processing PDE. Can anyone on the forum debug the software for proper optimization?

  • edited March 2016
    //Processing PDE G-code interpreter and simulator
    
    //"f"  load file
    //"=" is zoom in
    //"-"  is zoom out
    //"[" is rotate around Z-axis, cylindrically
    //"]" rotate opposite direction around Z-axis, cylindrically
    
    //The next two commands have bugs....
    //";" rotate in spherical coordinates
    //"'" rotate opposite direction in spherical coordinates
    
    //Not sure of the pixel range.  The largest Cartesian distance tested yet
    //is G01 X5400 Y600 Z0
    //May actually work with larger values, have not attempted.
    
    //To Do
    // A.  Debug Path offset.
    // B.  Implement arc arguments.
    // C.  Scale somehow for larger coordinates.  i.e. fit 1/16 microstepping
    //     axes from my steppers. 
    // D.  Debug camera view glitches.
    // E.  Show path generation in real-time...
    
    PShape path;float scale = 1;int delayy = 1;
    float a,aa;float offset = PI/24.0; float numb=100;
    int i = 0;int n=1;int v = 5;
    File selection2;File inFile;
    boolean outFile, outReader;
    String ngcPath;String[] gcod1, gcod2;String lines[];
    float dx, dy, dz;
    float ddx1,ddy1,ddz1,ddx2,ddy2,ddz2;
    int kk=0;int yi=0;
    int x1,y1,z1,diy,dix,diz,diyTranslated_oriented;
    float upa=1.0;float psia=1.0;float dcy=0;
    float dcr=0;float dcx=width/8;float dcz=0;
    float rads=0;int xoff,yoff,zoff;int tr=0;
    
    void setup() { 
      size(1200, 600, P3D);
      textureMode(NORMAL);
      //these are my initial tool tip positions
      dix=width/8;diz=0;diy=0;outFile = false;
      ddx1=dix; //initial coordinates translated 200 units in +X...  NOt sure why I chose 200.  haha.
      ddz1=diy+200;  //initial coordinates translated 200 units in +Z
      ddy1=diz;yoff=3*height/4;zoff=-400;xoff=0;
      path = createShape();
    } 
    
    
    void draw() {
      delay(delayy);lights();background(0);strokeWeight(5);
      stroke(255,0,0);fill(255,0,0);noStroke();
      //This sections draws the milling tip
      pushMatrix();
      int sc = 60;
      translate(width/8+ddx1,3*height/4-ddz1-sc,ddy1-400);
      rotateY(a + offset);
      scale(sc);
      TexturedPyramid();
      a += 10; 
      popMatrix();
      //This section draws the XYZ axes.
      pushMatrix();
      translate(dix, 3*height/4, -400); 
      scale(10);
      Xaxis();Yaxis();Zaxis();
      aa += 0.1;
      popMatrix();
      //This section should only run after file has been loaded
      if (outFile) {
        if (yi==0){
          path.beginShape();
          path.stroke(255,0,0);
          path.noFill();
        }
        lines = loadStrings(ngcPath);
            String[] xx1 = splitTokens(splitTokens(lines[yi])[1], "X");
            ddx1 = float(join(xx1, " "));
            String[] yy1 = splitTokens(splitTokens(lines[yi])[2], "Y");
            ddy1 = float(join(yy1, " "));
            String[] zz1 = splitTokens(splitTokens(lines[yi])[3], "Z");
            ddz1 = float(join(zz1, " "));
            path.vertex(width/8+ddx1,yoff+400-ddz1,zoff+ddy1);
            println ((xoff+ddx1)+":"+(yoff+400-ddz1)+":"+(zoff+ddy1));
        if (yi>=lines.length-1) {
          outFile = false;dx=0;dy=0;dz=0;kk=0;yi=0;tr=1;
          path.endShape();
          println("Shape Ended");
        }}
      if (outFile) {
      yi++;kk++;}
      if (tr==1){
        shape(path,float(xoff),float(zoff));
        }
    } 
    
    //function to load file from finder
    void fileSelected(File selection) {
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
      } else {
        ngcPath = selection.getAbsolutePath();
        outFile = true;outReader = true;
        println("User selected " + selection.getAbsolutePath());
        path = createShape();
      }
    }
    
    void TexturedPyramid() {
      beginShape(TRIANGLE);
      fill(0,0,255);
      vertex(-1,-1,-1);vertex(1,-1,-1);vertex(0,1,0);vertex(1,-1,-1);
      vertex(1,-1,1);vertex(0,1,0);vertex(1,-1,1);vertex(1,-1,1);
      vertex(0,1,0);vertex(-1,-1,1);vertex(-1,-1,-1);vertex(0,1,0);
      endShape();
    }
    
    void Yaxis() {
      beginShape(QUADS);
      fill(0,255,0);
      vertex(-1, -1, 100, 0, 0);vertex( 1, -1, 100, 1, 0);vertex( 1, 1, 100, 1, 1);
      vertex(-1, 1, 100, 0, 1);vertex( 1, -1, -300, 0, 0);vertex(-1, -1, -300, 1, 0);
      vertex(-1, 1, -300, 1, 1);vertex( 1, 1, -300, 0, 1);vertex(-1, 1, 100, 0, 0);
      vertex( 1, 1, 100, 1, 0);vertex( 1, 1, -300, 1, 1);vertex(-1, 1, -300, 0, 1);
      vertex(-1, -1, -300, 0, 0);vertex( 1, -1, -300, 1, 0);vertex( 1, -1, 100, 1, 1);
      vertex(-1, -1, 100, 0, 1);vertex( 1, -1, 100, 0, 0);vertex( 1, -1, -300, 1, 0);
      vertex( 1, 1, -300, 1, 1);vertex( 1, 1, 100, 0, 1);vertex(-1, -1, -300, 0, 0);
      vertex(-1, -1, 100, 1, 0);vertex(-1, 1, 100, 1, 1);vertex(-1, 1, -300, 0, 1);
      endShape();
    }
    
    void Zaxis() {
      beginShape(QUADS);
      tint(0,255,0);
      vertex(-1, 0, 1, 0, 0);vertex( 1, 0, 1, 1, 0);vertex( 1, -100, 1, 1, 1);
      vertex(-1, -100, 1, 0, 1);vertex( 1, 0, -1, 0, 0);vertex(-1, 0, -1, 1, 0);
      vertex(-1, -100, -1, 1, 1);vertex( 1, -100, -1, 0, 1);vertex(-1, -100, 1, 0, 0);
      vertex( 1, -100, 1, 1, 0);vertex( 1, -100, -1, 1, 1);vertex(-1, -100, -1, 0, 1);
      vertex(-1, 0, -1, 0, 0);vertex( 1, 0, -1, 1, 0);vertex( 1, 0, 1, 1, 1);
      vertex(-1, 0, 1, 0, 1);vertex( 1, 0, 1, 0, 0);vertex( 1, 0, -1, 1, 0);
      vertex( 1, -100, -1, 1, 1);vertex( 1, -100, 1, 0, 1);vertex(-1, 0, -1, 0, 0);
      vertex(-1, 0, 1, 1, 0);vertex(-1, -100, 1, 1, 1);vertex(-1, -100, -1, 0, 1);
      endShape();
    }
    
    void Xaxis() {
      beginShape(QUADS);
      fill(0,255,0);
      vertex(0, -1, 1, 0, 0);vertex( 300, -1, 1, 1, 0);vertex( 300, 1, 1, 1, 1);
      vertex(0, 1, 1, 0, 1);vertex( 300, -1, -1, 0, 0);vertex(0, -1, -1, 1, 0);
      vertex(0, 1, -1, 1, 1);vertex( 300, 1, -1, 0, 1);vertex(0, 1, 1, 0, 0);
      vertex( 300, 1, 1, 1, 0);vertex( 300, 1, -1, 1, 1);vertex(0, 1, -1, 0, 1);
      vertex(0, -1, -1, 0, 0);vertex( 300, -1, -1, 1, 0);vertex( 300, -1, 1, 1, 1);
      vertex(0, -1, 1, 0, 1);vertex( 300, -1, 1, 0, 0);vertex( 300, -1, -1, 1, 0);
      vertex( 300, 1, -1, 1, 1);vertex( 300, 1, 1, 0, 1);vertex(0, -1, -1, 0, 0);
      vertex(0, -1, 1, 1, 0);vertex(0, 1, 1, 1, 1);vertex(0, 1, -1, 0, 1);
      endShape();
    }
    
    void keyPressed(){
      if (key == 'f' || key == 'F')
      {
        selectInput("Select a file to process:", "fileSelected");
      }
      //Change to Adjust the Radius???
      if (key == 61)
      {
      beginCamera();
      camera(dcx, dcy, dcz, width/8, 3*height/4, -400, 0.0, 1.0, 0.0);
      endCamera();upa=dcy+30;dcy=upa;println(upa);
      }
      if (key == 45)
      {
      beginCamera();
      camera(dcx, dcy, dcz, width/8, 3*height/4, -400, 0.0, 1.0, 0.0);
      endCamera();
      upa=dcy-30;dcy=upa;println(upa);
      }
      //Adjusts cylindrical coordinates around Z-axis.  Animation Z-axis, not Processing.
      if (key == 91)
      {
      float R = height/3;
      beginCamera();
      camera(dcx, dcy, dcz, width/8, 3*height/4, -400, 0.0, 1.0, 0.0);
      endCamera();
      rads=rads+5;dcr=radians(rads);dcx=width/8+cos(dcr)*R;dcz=-400+sin(dcr)*R;
      println("degrees="+rads+" "+"radians="+dcr);
      }
      if (key == 93)
      {
      float R = height/3;
      beginCamera();
      camera(dcx, dcy, dcz, width/8, 3*height/4, -400, 0.0, 1.0, 0.0);
      endCamera();
      rads=rads-5;dcr=radians(rads);dcx=width/8+cos(dcr)*R;dcz=-400+sin(dcr)*R;
      println("degrees="+rads+" "+"radians="+dcr);
      }
      //Adjust spherical coordinates
      if (key == 59)
      {
      beginCamera();
      camera(dcx, dcy, dcz, width/8, 3*height/4, -400, 0.0, 1.0, 0.0);
      endCamera();
      float p=sqrt(dcx*dcx+dcy*dcy+dcz*dcz);psia=psia-5;
      float fact1 = sin(radians(psia))*cos(dcr);dcx=width/8+p*fact1;
      float fact2 = sin(radians(psia))*sin(dcr);
      dcy=p*fact2;dcz=-400+p*cos(dcr);
      println("degrees="+rads+" psi="+psia+" dcx="+dcx+" dcy="+dcy+" dcz="+dcz+" fact1="+fact1+" fact2="+fact2);
      }
      if (key == 39)
      {
      beginCamera();
      camera(dcx, dcy, dcz, width/8, 3*height/4, -400, 0.0, 1.0, 0.0);
      endCamera();
      float p=sqrt(dcx*dcx+dcy*dcy+dcz*dcz);
      psia=psia+5;float fact1 = sin(radians(psia))*cos(dcr);
      dcx=width/8+p*fact1;float fact2 = sin(radians(psia))*sin(dcr);
      dcy=p*fact2;dcz=-400+p*cos(dcr);
      println("degrees="+rads+" psi="+psia+" dcx="+dcx+" dcy="+dcy+" dcz="+dcz+" fact1="+fact1+" fact2="+fact2);
      }
    }
    
  • (ctrl-t in the processing editor will indent the code correctly.)

  • if (key == 'f' || key == 'F')

    this is preferable to

    if (key == 59)

  • edited March 2016

    Ok. I will try to fix that. I consolidated the code into ~220 lines instead of ~500 lines. Have you run a g-code document? It works well. There are still too many bugs, but I can still orient the camera quite easily to see the pattern. I uploaded a short video to youtube. May not work when embedded in processing blog, but works from the address bar. Why this thing not let me paste actual hyperlink address rather than an embedded youtube video?

    [video deleted, see below]

  • edited March 2016

    Embedded video does not work in my browser. Here is the hyperlink,

    .

  • (you had a trailing . - fixed)

  • edited March 2016

    Thanks. My tool path line will not show up until after my tool has completed its movement. In objective-C I would use GCD async... to update the tool path animation asynchronously. Can you implement grand central dispatch in processing? I tried using the thread() command, but I was unsuccessful.

  • the screen does not update until draw() has finished. is that the case here?

  • tried that using an example circle from here:

    https://github.com/grbl/grbl/wiki/G-Code-Examples

    G17 G20 G90 G94 G54
    G0 Z0.25
    X-0.5 Y0.
    Z0.1
    G01 Z0. F5.
    G02 X0. Y0.5 I0.5 J0. F2.5
    X0.5 Y0. I0. J-0.5
    X0. Y-0.5 I-0.5 J0.
    X-0.5 Y0. I0. J0.5
    G01 Z0.1 F5.
    G00 X0. Y0. Z0.25
    

    i got a NaN error.

    btw, have you seen PeasyCam library? it could replace all your []`-= handling code...

  • edited March 2016

    Yea. Currently only accepts Gxx Xxx Yxx Zzz; i.e. it only accepts commands for straight lines. I will try adding more commands, soon. Attached is the pseudo g-code I used for 2.54mm pitch DIP headers shown in the Youtube video.

    G01 X10 Y10 Z0
    G01 X40 Y10 Z0
    G01 X40 Y40 Z0
    G01 X10 Y40 Z0
    G01 X10 Y10 Z0
    G01 X20 Y20 Z0
    G01 X30 Y20 Z0
    G01 X30 Y30 Z0
    G01 X20 Y30 Z0
    G01 X20 Y20 Z0
    G01 X10 Y50 Z0
    G01 X40 Y50 Z0
    G01 X40 Y80 Z0
    G01 X10 Y80 Z0
    G01 X10 Y50 Z0
    G01 X20 Y60 Z0
    G01 X30 Y60 Z0
    G01 X30 Y70 Z0
    G01 X20 Y70 Z0
    G01 X20 Y60 Z0
    G01 X50 Y10 Z0
    G01 X80 Y10 Z0
    G01 X80 Y40 Z0
    G01 X50 Y40 Z0
    G01 X50 Y10 Z0
    G01 X60 Y20 Z0
    G01 X70 Y20 Z0
    G01 X70 Y30 Z0
    G01 X60 Y30 Z0
    G01 X60 Y20 Z0
    G01 X50 Y50 Z0
    G01 X80 Y50 Z0
    G01 X80 Y80 Z0
    G01 X50 Y80 Z0
    G01 X50 Y50 Z0
    G01 X60 Y60 Z0
    G01 X70 Y60 Z0
    G01 X70 Y70 Z0
    G01 X60 Y70 Z0
    G01 X60 Y60 Z0
    G01 X90 Y10 Z0
    G01 X120 Y10 Z0
    G01 X120 Y40 Z0
    G01 X90 Y40 Z0
    G01 X90 Y10 Z0
    G01 X100 Y20 Z0
    G01 X110 Y20 Z0
    G01 X110 Y30 Z0
    G01 X100 Y30 Z0
    G01 X100 Y20 Z0
    G01 X90 Y50 Z0
    G01 X120 Y50 Z0
    G01 X120 Y80 Z0
    G01 X90 Y80 Z0
    G01 X90 Y50 Z0
    G01 X100 Y60 Z0
    G01 X110 Y60 Z0
    G01 X110 Y70 Z0
    G01 X100 Y70 Z0
    G01 X100 Y60 Z0
    G01 X130 Y10 Z0
    G01 X160 Y10 Z0
    G01 X160 Y40 Z0
    G01 X130 Y40 Z0
    G01 X130 Y10 Z0
    G01 X140 Y20 Z0
    G01 X150 Y20 Z0
    G01 X150 Y30 Z0
    G01 X140 Y30 Z0
    G01 X140 Y20 Z0
    G01 X130 Y50 Z0
    G01 X160 Y50 Z0
    G01 X160 Y80 Z0
    G01 X130 Y80 Z0
    G01 X130 Y50 Z0
    G01 X140 Y60 Z0
    G01 X150 Y60 Z0
    G01 X150 Y70 Z0
    G01 X140 Y70 Z0
    G01 X140 Y60 Z0
    G01 X170 Y10 Z0
    G01 X200 Y10 Z0
    G01 X200 Y40 Z0
    G01 X170 Y40 Z0
    G01 X170 Y10 Z0
    G01 X180 Y20 Z0
    G01 X190 Y20 Z0
    G01 X190 Y30 Z0
    G01 X180 Y30 Z0
    G01 X180 Y20 Z0
    G01 X170 Y50 Z0
    G01 X200 Y50 Z0
    G01 X200 Y80 Z0
    G01 X170 Y80 Z0
    G01 X170 Y50 Z0
    G01 X180 Y60 Z0
    G01 X190 Y60 Z0
    G01 X190 Y70 Z0
    G01 X180 Y70 Z0
    G01 X180 Y60 Z0
    G01 X210 Y10 Z0
    G01 X240 Y10 Z0
    G01 X240 Y40 Z0
    G01 X210 Y40 Z0
    G01 X210 Y10 Z0
    G01 X220 Y20 Z0
    G01 X230 Y20 Z0
    G01 X230 Y30 Z0
    G01 X220 Y30 Z0
    G01 X220 Y20 Z0
    G01 X210 Y50 Z0
    G01 X240 Y50 Z0
    G01 X240 Y80 Z0
    G01 X210 Y80 Z0
    G01 X210 Y50 Z0
    G01 X220 Y60 Z0
    G01 X230 Y60 Z0
    G01 X230 Y70 Z0
    G01 X220 Y70 Z0
    G01 X220 Y60 Z0
    G01 X250 Y10 Z0
    G01 X280 Y10 Z0
    G01 X280 Y40 Z0
    G01 X250 Y40 Z0
    G01 X250 Y10 Z0
    G01 X260 Y20 Z0
    G01 X270 Y20 Z0
    G01 X270 Y30 Z0
    G01 X260 Y30 Z0
    G01 X260 Y20 Z0
    G01 X250 Y50 Z0
    G01 X280 Y50 Z0
    G01 X280 Y80 Z0
    G01 X250 Y80 Z0
    G01 X250 Y50 Z0
    G01 X260 Y60 Z0
    G01 X270 Y60 Z0
    G01 X270 Y70 Z0
    G01 X260 Y70 Z0
    G01 X260 Y60 Z0
    G01 X290 Y10 Z0
    G01 X320 Y10 Z0
    G01 X320 Y40 Z0
    G01 X290 Y40 Z0
    G01 X290 Y10 Z0
    G01 X300 Y20 Z0
    G01 X310 Y20 Z0
    G01 X310 Y30 Z0
    G01 X300 Y30 Z0
    G01 X300 Y20 Z0
    G01 X290 Y50 Z0
    G01 X320 Y50 Z0
    G01 X320 Y80 Z0
    G01 X290 Y80 Z0
    G01 X290 Y50 Z0
    G01 X300 Y60 Z0
    G01 X310 Y60 Z0
    G01 X310 Y70 Z0
    G01 X300 Y70 Z0
    G01 X300 Y60 Z0
    G01 X330 Y10 Z0
    G01 X360 Y10 Z0
    G01 X360 Y40 Z0
    G01 X330 Y40 Z0
    G01 X330 Y10 Z0
    G01 X340 Y20 Z0
    G01 X350 Y20 Z0
    G01 X350 Y30 Z0
    G01 X340 Y30 Z0
    G01 X340 Y20 Z0
    G01 X330 Y50 Z0
    G01 X360 Y50 Z0
    G01 X360 Y80 Z0
    G01 X330 Y80 Z0
    G01 X330 Y50 Z0
    G01 X340 Y60 Z0
    G01 X350 Y60 Z0
    G01 X350 Y70 Z0
    G01 X340 Y70 Z0
    G01 X340 Y60 Z0
    G01 X370 Y10 Z0
    G01 X400 Y10 Z0
    G01 X400 Y40 Z0
    G01 X370 Y40 Z0
    G01 X370 Y10 Z0
    G01 X380 Y20 Z0
    G01 X390 Y20 Z0
    G01 X390 Y30 Z0
    G01 X380 Y30 Z0
    G01 X380 Y20 Z0
    G01 X370 Y50 Z0
    G01 X400 Y50 Z0
    G01 X400 Y80 Z0
    G01 X370 Y80 Z0
    G01 X370 Y50 Z0
    G01 X380 Y60 Z0
    G01 X390 Y60 Z0
    G01 X390 Y70 Z0
    G01 X380 Y70 Z0
    G01 X380 Y60 Z0
    G01 X410 Y10 Z0
    G01 X440 Y10 Z0
    G01 X440 Y40 Z0
    G01 X410 Y40 Z0
    G01 X410 Y10 Z0
    G01 X420 Y20 Z0
    G01 X430 Y20 Z0
    G01 X430 Y30 Z0
    G01 X420 Y30 Z0
    G01 X420 Y20 Z0
    G01 X410 Y50 Z0
    G01 X440 Y50 Z0
    G01 X440 Y80 Z0
    G01 X410 Y80 Z0
    G01 X410 Y50 Z0
    G01 X420 Y60 Z0
    G01 X430 Y60 Z0
    G01 X430 Y70 Z0
    G01 X420 Y70 Z0
    G01 X420 Y60 Z0
    G01 X450 Y10 Z0
    G01 X480 Y10 Z0
    G01 X480 Y40 Z0
    G01 X450 Y40 Z0
    G01 X450 Y10 Z0
    G01 X460 Y20 Z0
    G01 X470 Y20 Z0
    G01 X470 Y30 Z0
    G01 X460 Y30 Z0
    G01 X460 Y20 Z0
    G01 X450 Y50 Z0
    G01 X480 Y50 Z0
    G01 X480 Y80 Z0
    G01 X450 Y80 Z0
    G01 X450 Y50 Z0
    G01 X460 Y60 Z0
    G01 X470 Y60 Z0
    G01 X470 Y70 Z0
    G01 X460 Y70 Z0
    G01 X460 Y60 Z0
    G01 X490 Y10 Z0
    G01 X520 Y10 Z0
    G01 X520 Y40 Z0
    G01 X490 Y40 Z0
    G01 X490 Y10 Z0
    G01 X500 Y20 Z0
    G01 X510 Y20 Z0
    G01 X510 Y30 Z0
    G01 X500 Y30 Z0
    G01 X500 Y20 Z0
    G01 X490 Y50 Z0
    G01 X520 Y50 Z0
    G01 X520 Y80 Z0
    G01 X490 Y80 Z0
    G01 X490 Y50 Z0
    G01 X500 Y60 Z0
    G01 X510 Y60 Z0
    G01 X510 Y70 Z0
    G01 X500 Y70 Z0
    G01 X500 Y60 Z0
    G01 X530 Y10 Z0
    G01 X560 Y10 Z0
    G01 X560 Y40 Z0
    G01 X530 Y40 Z0
    G01 X530 Y10 Z0
    G01 X540 Y20 Z0
    G01 X550 Y20 Z0
    G01 X550 Y30 Z0
    G01 X540 Y30 Z0
    G01 X540 Y20 Z0
    G01 X530 Y50 Z0
    G01 X560 Y50 Z0
    G01 X560 Y80 Z0
    G01 X530 Y80 Z0
    G01 X530 Y50 Z0
    G01 X540 Y60 Z0
    G01 X550 Y60 Z0
    G01 X550 Y70 Z0
    G01 X540 Y70 Z0
    G01 X540 Y60 Z0
    
  • edited March 2016

    How can I convert my G-code X values determined by ddx1 into processing commands that would be equivalent to typing the following keys followed by return..... pressing "r", pressing "u", then inputting an integer representing the ddx1 value, followed by a return keystroke?

           String[] xx1 = splitTokens(splitTokens(lines[yi])[1], "X");
           ddx1 = float(join(xx1, " "));
           String[] yy1 = splitTokens(splitTokens(lines[yi])[2], "Y");
           ddy1 = float(join(yy1, " "));
           String[] zz1 = splitTokens(splitTokens(lines[yi])[3], "Z");
           ddz1 = float(join(zz1, " "));
    
  • Nevermind. I figured it out.

                String[] xx1 = splitTokens(splitTokens(lines[yi])[1], "X");
                ddx1 = float(join(xx1, " "));
                String ddx1s = "ru"+nf(ddx1,4,0);
                String[] yy1 = splitTokens(splitTokens(lines[yi])[2], "Y");
                ddy1 = float(join(yy1, " "));
                String ddy1s = "ri"+nf(ddy1,4,0);
                String[] zz1 = splitTokens(splitTokens(lines[yi])[3], "Z");
                ddz1 = float(join(zz1, " "));
                String ddz1s = "ro"+nf(ddz1,4,0);
                println(ddx1s+":"+ddy1s+":"+ddz1s);
    
  • edited March 2016

    I fixed some the camera angles for better viewing. I have also been able to have it simulating straight lines on XYZ coordinates with random numbers between -12500 to 12500. There is a bug when determining the spherical coordinate angle away from the z-axis. It is at the very very bottom of the sketch within sections beginning with "if (key == 39)" and "if (key == 59)". Can anyone help me fix this?

     //Processing PDE G-code interpreter and simulator
    
        //"f"  load file
        //"=" is zoom in
        //"-"  is zoom out
        //"[" is rotate around Z-axis, cylindrically
        //"]" rotate opposite direction around Z-axis, cylindrically
    
        //The next two commands have bugs....
        //";" rotate in spherical coordinates
        //"'" rotate opposite direction in spherical coordinates
    
        //To Do
        // A.  Implement arc arguments. Eventually, all G-code commands.  http://reprap.org/wiki/G-code#G0_.26_G1:_Move
        // B.  Debug camera view glitches.
    
        PShape path;float scale = 1;int delayy = 0;
        float a,aa;float offset = PI/24.0; float numb=100;
        int i = 0;int n=1;int v = 5;
        File selection2;File inFile;
        boolean outFile, outReader;
        String ngcPath;String[] gcod1, gcod2;String lines[];
        float dx, dy, dz;
        float ddx1,ddy1,ddz1,ddx2,ddy2,ddz2;
        int kk=0;int yi=0;
        int x1,y1,z1,diy,dix,diz,diyTranslated_oriented;
        float upa=1.0;float psia=1.0;float dcy=0;
        float dcr=0;float dcx=width/8;float dcz=0;
        float rads=0;int xoff,yoff,zoff;int tr=0;
    
        void setup() { 
          size(1200, 600, P3D);
          textureMode(NORMAL);
          //these are my initial tool tip positions
          dix=width/8;diz=0;diy=0;outFile = false;
          ddx1=dix; //initial coordinates translated 200 units in +X...  NOt sure why I chose 200.  haha.
          ddz1=diy+200;  //initial coordinates translated 200 units in +Z
          ddy1=diz;yoff=3*height/4;zoff=-400;xoff=0;
          path = createShape();
          dcx=width/2;dcy=height/2;dcz=(height/2.0) / tan(PI*30.0 / 180.0);
        } 
    
        void draw() {
    
          delay(delayy);lights();background(0);strokeWeight(5);
          stroke(255,0,0);fill(255,0,0);noStroke();  
    
          //This section draws the XYZ axes.
          pushMatrix();
          translate(dix, 3*height/4, -400); 
          scale(10);
          Xaxis();Yaxis();Zaxis();
          aa += 0.1;
          popMatrix();
    
    
          //This sections draws the milling tip
          pushMatrix();
          int sc = 30;
          translate(width/8+ddx1,3*height/4-ddz1-sc,ddy1-400);
          rotateY(a + offset);
          scale(sc);
          TexturedPyramid();
          a += 10; 
          popMatrix();
    
          //This section should only run after file has been loaded
          if (outFile) {
            lines = loadStrings(ngcPath);
                String[] xx1 = splitTokens(splitTokens(lines[yi])[1], "X");
                ddx1 = float(join(xx1, " "));
                String ddx1s = "ru"+nf(ddx1,4,0);
                String[] yy1 = splitTokens(splitTokens(lines[yi])[2], "Y");
                ddy1 = float(join(yy1, " "));
                String ddy1s = "ri"+nf(ddy1,4,0);
                String[] zz1 = splitTokens(splitTokens(lines[yi])[3], "Z");
                ddz1 = float(join(zz1, " "));
                String ddz1s = "ro"+nf(ddz1,4,0);
                println(yi+" "+ddx1s+":"+ddy1s+":"+ddz1s);
                path.beginShape();
                path.stroke(255,0,0);
                path.noFill();
                path.vertex(width/8+ddx1,yoff+400-ddz1,zoff+ddy1);
                shape(path,float(xoff),float(zoff));
                path.endShape();
                //println ((xoff+ddx1)+":"+(yoff+400-ddz1)+":"+(zoff+ddy1));
            if (yi>=lines.length-1) {
              outFile = false;dx=0;dy=0;dz=0;kk=0;yi=0;tr=1;
              //path.endShape();
              //println("Shape Ended");
            }}
          if (outFile) {
          yi++;kk++;}
          if (tr==1){
            shape(path,float(xoff),float(zoff));
            }
        } 
    
        //function to load file from finder
        void fileSelected(File selection) {
          if (selection == null) {
            //println("Window was closed or the user hit cancel.");
          } else {
            ngcPath = selection.getAbsolutePath();
            outFile = true;outReader = true;
            //println("User selected " + selection.getAbsolutePath());
            path = createShape();
          }
        }
    
        void TexturedPyramid() {
          beginShape(TRIANGLE);
          fill(0,0,255);
          vertex(-1,-1,-1);vertex(1,-1,-1);vertex(0,1,0);vertex(1,-1,-1);
          vertex(1,-1,1);vertex(0,1,0);vertex(1,-1,1);vertex(1,-1,1);
          vertex(0,1,0);vertex(-1,-1,1);vertex(-1,-1,-1);vertex(0,1,0);
          endShape();
        }
    
        void Yaxis() {
          beginShape(QUADS);
          fill(0,255,0);
          vertex(-1, -1, 1000, 0, 0);vertex( 1, -1, 1000, 1, 0);vertex( 1, 1, 1000, 1, 1);
          vertex(-1, 1, 1000, 0, 1);vertex( 1, -1, -3000, 0, 0);vertex(-1, -1, -3000, 1, 0);
          vertex(-1, 1, -3000, 1, 1);vertex( 1, 1, -3000, 0, 1);vertex(-1, 1, 1000, 0, 0);
          vertex( 1, 1, 1000, 1, 0);vertex( 1, 1, -3000, 1, 1);vertex(-1, 1, -3000, 0, 1);
          vertex(-1, -1, -3000, 0, 0);vertex( 1, -1, -3000, 1, 0);vertex( 1, -1, 1000, 1, 1);
          vertex(-1, -1, 1000, 0, 1);vertex( 1, -1, 1000, 0, 0);vertex( 1, -1, -3000, 1, 0);
          vertex( 1, 1, -3000, 1, 1);vertex( 1, 1, 1000, 0, 1);vertex(-1, -1, -3000, 0, 0);
          vertex(-1, -1, 1000, 1, 0);vertex(-1, 1, 1000, 1, 1);vertex(-1, 1, -3000, 0, 1);
          endShape();
        }
    
        void Zaxis() {
          beginShape(QUADS);
          tint(0,255,0);
          vertex(-1, 0, 1, 0, 0);vertex( 1, 0, 1, 1, 0);vertex( 1, -1000, 1, 1, 1);
          vertex(-1, -1000, 1, 0, 1);vertex( 1, 0, -1, 0, 0);vertex(-1, 0, -1, 1, 0);
          vertex(-1, -1000, -1, 1, 1);vertex( 1, -1000, -1, 0, 1);vertex(-1, -1000, 1, 0, 0);
          vertex( 1, -1000, 1, 1, 0);vertex( 1, -1000, -1, 1, 1);vertex(-1, -1000, -1, 0, 1);
          vertex(-1, 0, -1, 0, 0);vertex( 1, 0, -1, 1, 0);vertex( 1, 0, 1, 1, 1);
          vertex(-1, 0, 1, 0, 1);vertex( 1, 0, 1, 0, 0);vertex( 1, 0, -1, 1, 0);
          vertex( 1, -1000, -1, 1, 1);vertex( 1, -1000, 1, 0, 1);vertex(-1, 0, -1, 0, 0);
          vertex(-1, 0, 1, 1, 0);vertex(-1, -1000, 1, 1, 1);vertex(-1, -1000, -1, 0, 1);
          endShape();
        }
    
        void Xaxis() {
          beginShape(QUADS);
          fill(0,255,0);
          vertex(0, -1, 1, 0, 0);vertex( 3000, -1, 1, 1, 0);vertex( 3000, 1, 1, 1, 1);
          vertex(0, 1, 1, 0, 1);vertex( 3000, -1, -1, 0, 0);vertex(0, -1, -1, 1, 0);
          vertex(0, 1, -1, 1, 1);vertex( 3000, 1, -1, 0, 1);vertex(0, 1, 1, 0, 0);
          vertex( 3000, 1, 1, 1, 0);vertex( 3000, 1, -1, 1, 1);vertex(0, 1, -1, 0, 1);
          vertex(0, -1, -1, 0, 0);vertex( 3000, -1, -1, 1, 0);vertex( 3000, -1, 1, 1, 1);
          vertex(0, -1, 1, 0, 1);vertex( 3000, -1, 1, 0, 0);vertex( 3000, -1, -1, 1, 0);
          vertex( 3000, 1, -1, 1, 1);vertex( 3000, 1, 1, 0, 1);vertex(0, -1, -1, 0, 0);
          vertex(0, -1, 1, 1, 0);vertex(0, 1, 1, 1, 1);vertex(0, 1, -1, 0, 1);
          endShape();
        }
    
        //increase spherical coordinates radius
        void x2sp(){
          float prad = sqrt(dcx*dcx+dcy*dcy+dcz*dcz);
          psia=acos(dcy/prad);
          rads=acos(dcx/prad/sin(psia));
          println("1. "+prad+":"+dcx+":"+dcy+":"+dcz+":");
          prad=prad+50;
          //psia;rads;
          dcx=prad*sin(psia)*cos(rads);
          dcz=prad*sin(psia)*sin(rads);
          dcy=dcy-prad*cos(psia);
          beginCamera();
          camera(dcx, dcy, dcz, width/8, 3*height/4, -400, 0.0, 1.0, 0.0);
          endCamera();
          println("2. "+prad+":"+dcx+":"+dcy+":"+dcz+":");
        }
    
        void x2sn(){
          float prad = sqrt(dcx*dcx+dcy*dcy+dcz*dcz);
          psia=acos(dcy/prad);
          rads=acos(dcx/prad/sin(psia));
          println("1. "+prad+":"+dcx+":"+dcy+":"+dcz+":");
          prad=prad-50;
          //psia;rads;
          dcx=prad*sin(psia)*cos(rads);
          dcz=prad*sin(psia)*sin(rads);
          dcy=dcy-prad*cos(psia);
          beginCamera();
          camera(dcx, dcy, dcz, width/8, 3*height/4, -400, 0.0, 1.0, 0.0);
          endCamera();
          println("2. "+prad+":"+dcx+":"+dcy+":"+dcz+":");
          //return dcx,dcy,dcz,rads,psia;
        }
    
        void keyPressed(){
          if (key == 'f' || key == 'F')
          {
            selectInput("Select a file to process:", "fileSelected");
          }
          //Change to Adjust the Radius???
          if (key == 61)
          {
            x2sp();
            /*
          upa=dcy+30;dcy=upa;//println(upa);
          beginCamera();
          camera(dcx, dcy, dcz, width/8, 3*height/4, -400, 0.0, 1.0, 0.0);
          endCamera();
          */
          }
          if (key == 45)
          {
            x2sn();
            /*
          upa=dcy-30;dcy=upa;//println(upa);
          beginCamera();
          camera(dcx, dcy, dcz, width/8, 3*height/4, -400, 0.0, 1.0, 0.0);
          endCamera();
          */
          }
          //Adjusts cylindrical coordinates around Z-axis.  Animation Z-axis, not Processing.
          if (key == 91)
          {
            rads=rads+PI/20;
            float prad = sqrt(dcx*dcx+dcy*dcy+dcz*dcz);
          println("1. "+prad+":"+dcx+":"+dcy+":"+dcz+":");
          dcx=prad*sin(psia)*cos(rads);
          dcz=prad*sin(psia)*sin(rads);
          dcy=dcy-prad*cos(psia);
          beginCamera();
          camera(dcx, dcy, dcz, width/8, 3*height/4, -400, 0.0, 1.0, 0.0);
          endCamera();
          //rads=rads+5;dcr=radians(rads);dcx=width/8+cos(dcr)*R;dcz=-400+sin(dcr)*R;
          println("2. "+prad+":"+dcx+":"+dcy+":"+dcz+":");
          //println("degrees="+rads+" "+"radians="+dcr);
          }
          if (key == 93)
          {
            rads=rads-PI/20;
          float prad = sqrt(dcx*dcx+dcy*dcy+dcz*dcz);
          println("1. "+prad+":"+dcx+":"+dcy+":"+dcz+":");
          dcx=prad*sin(psia)*cos(rads);
          dcz=prad*sin(psia)*sin(rads);
          dcy=dcy-prad*cos(psia);
          beginCamera();
          camera(dcx, dcy, dcz, width/8, 3*height/4, -400, 0.0, 1.0, 0.0);
          endCamera();
          //rads=rads-5;dcr=radians(rads);dcx=width/8+cos(dcr)*R;dcz=-400+sin(dcr)*R;
          println("2. "+prad+":"+dcx+":"+dcy+":"+dcz+":");
          //println("degrees="+rads+" "+"radians="+dcr);
          }
          //Adjust spherical coordinates
          if (key == 59)
          {
            rads=rads-PI/20;
    
            psia=psia+PI/20;
            float prad = sqrt(dcx*dcx+dcy*dcy+dcz*dcz);
          println("1. "+prad+":"+dcx+":"+dcy+":"+dcz+":");
          dcx=prad*sin(psia)*cos(rads);
          dcz=prad*sin(psia)*sin(rads);
          dcy=dcy-prad*cos(psia);
          beginCamera();
          camera(dcx, dcy, dcz, width/8, 3*height/4, -400, 0.0, 1.0, 0.0);
          endCamera();
          //rads=rads+5;dcr=radians(rads);dcx=width/8+cos(dcr)*R;dcz=-400+sin(dcr)*R;
          println("2. "+prad+":"+dcx+":"+dcy+":"+dcz+":");
          //println("degrees="+rads+" "+"radians="+dcr); 
    
            /*
          float p=sqrt(dcx*dcx+dcy*dcy+dcz*dcz);psia=psia-5;
          float fact1 = sin(radians(psia))*cos(dcr);dcx=width/8+p*fact1;
          float fact2 = sin(radians(psia))*sin(dcr);
          dcy=p*fact2;dcz=-400+p*cos(dcr);
          beginCamera();
          camera(dcx, dcy, dcz, width/8, 3*height/4, -400, 0.0, 1.0, 0.0);
          endCamera();
          //println("degrees="+rads+" psi="+psia+" dcx="+dcx+" dcy="+dcy+" dcz="+dcz+" fact1="+fact1+" fact2="+fact2);
          */
        }
          if (key == 39)
          {
            rads=rads+PI/20;
    
          float p=sqrt(dcx*dcx+dcy*dcy+dcz*dcz);
          psia=psia+5;float fact1 = sin(radians(psia))*cos(dcr);
          dcx=width/8+p*fact1;float fact2 = sin(radians(psia))*sin(dcr);
          dcy=p*fact2;dcz=-400+p*cos(dcr);
          beginCamera();
          camera(dcx, dcy, dcz, width/8, 3*height/4, -400, 0.0, 1.0, 0.0);
          endCamera();
          //println("degrees="+rads+" psi="+psia+" dcx="+dcx+" dcy="+dcy+" dcz="+dcz+" fact1="+fact1+" fact2="+fact2);
        }
    
        }
    
  • Attached is a bash script for generating 1000 random Gcode coordinates in a specified range. Here, I used the range from -12500 to 12500, arbitrarily.

    #!/bin/sh
    
    #  big.sh
    #  
    #
    #  Created by Ryan Holman on 3/4/16.
    #
    for i in $(seq 0 1000);
    do
    echo $i;
    xx=$(( RANDOM % (25000 - 0 + 1 ) + 0 ));
    xx=$(($xx-12500));
    yy=$(( RANDOM % (25000 - 0 + 1 ) + 0 ));
    yy=$(($yy-12500));
    zz=$(( RANDOM % (25000 - 0 + 1 ) + 0 ));
    zz=$(($zz-12500));
    printf 'G01 X' >> ~/desktop/randgen.ngc;
    printf "%d" $xx >> ~/desktop/randgen.ngc;
    printf ' Y' >> ~/desktop/randgen.ngc;
    printf "%d" $yy >> ~/desktop/randgen.ngc;
    printf ' Z' >> ~/desktop/randgen.ngc;
    printf "%d" $zz >> ~/desktop/randgen.ngc;
    printf '\n' >> ~/desktop/randgen.ngc;
    done
    
  • edited March 2016

    Here is an updated video posted on youtube....

    https://youtu.be/AuFpbSiYigU .

Sign In or Register to comment.