Skip to content
Snippets Groups Projects
GenericGcodeDriver.java 14.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    /**
     * This file is part of LibLaserCut.
     * Copyright (C) 2011 - 2014 Thomas Oster <mail@thomas-oster.de>
     *
     * LibLaserCut is free software: you can redistribute it and/or modify
     * it under the terms of the GNU Lesser General Public License as published by
     * the Free Software Foundation, either version 3 of the License, or
     * (at your option) any later version.
     *
     * LibLaserCut 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 Lesser General Public License for more details.
     *
     * You should have received a copy of the GNU Lesser General Public License
     * along with LibLaserCut. If not, see <http://www.gnu.org/licenses/>.
     *
     **/
    package com.t_oster.liblasercut.drivers;
    
    import com.t_oster.liblasercut.*;
    import com.t_oster.liblasercut.platform.Util;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.io.UnsupportedEncodingException;
    import java.net.InetSocketAddress;
    import java.net.Socket;
    import purejavacomm.*;
    import java.util.*;
    
    /**
     * This class implements a driver for a generic GRBL GCode Lasercutter.
     * It should contain all possible options and is inteded to be the superclass
     * for e.g. the SmoothieBoard and the Lasersaur driver.
     *
     * @author Thomas Oster <thomas.oster@rwth-aachen.de>
     */
    public class GenericGcodeDriver extends LaserCutter {
    
      protected static final String SETTING_HOST = "IP/Hostname";
      protected static final String SETTING_COMPORT = "COM Port";
      protected static final String SETTING_BEDWIDTH = "Laserbed width";
      protected static final String SETTING_BEDHEIGHT = "Laserbed height";
      protected static final String SETTING_MAX_SPEED = "Max speed (in mm/min)";
      protected static final String SETTING_PRE_JOB_GCODE = "Pre-Job GCode (comma separated)";
      protected static final String SETTING_POST_JOB_GCODE = "Post-Job GCode (comma separated)";
      protected static final String SETTING_IDENTIFICATION_STRING = "Board Identification String";
      protected static final String SETTING_WAIT_FOR_OK = "Wait for OK after each line (interactive mode)";
      protected static final String LINEEND = "\r\n";
      
      protected boolean waitForOKafterEachLine = true;
    
      public boolean isWaitForOKafterEachLine()
      {
        return waitForOKafterEachLine;
      }
    
      public void setWaitForOKafterEachLine(boolean waitForOKafterEachLine)
      {
        this.waitForOKafterEachLine = waitForOKafterEachLine;
      }
    
      public String getIdentificationLine()
      {
        return identificationLine;
      }
    
      public void setIdentificationLine(String identificationLine)
      {
        this.identificationLine = identificationLine;
      }
      
      protected String preJobGcode = "G21,G90";
    
      public String getPreJobGcode()
      {
        return preJobGcode;
      }
    
      public void setPreJobGcode(String preJobGcode)
      {
        this.preJobGcode = preJobGcode;
      }
      
      protected String postJobGcode = "G0 X0 Y0";
    
      public String getPostJobGcode()
      {
        return postJobGcode;
      }
    
      public void setPostJobGcode(String postJobGcode)
      {
        this.postJobGcode = postJobGcode;
      }
      
      /**
       * What is expected to be received after serial/telnet connection
       * Used e.g. for auto-detecting the serial port.
       */
      protected String identificationLine = "Grbl";
      
      @Override
      public String getModelName() {
        return "Generic GRBL GCode Driver";
      }
      
      protected String host = "10.10.10.222";
    
      public String getHost()
      {
        return host;
      }
    
      public void setHost(String host)
      {
        this.host = host;
      }
      
      protected String comport = "auto";
    
      public String getComport()
      {
        return comport;
      }
    
      public void setComport(String comport)
      {
        this.comport = comport;
      }
      
      protected double max_speed = 20*60;
    
      public double getMax_speed()
      {
        return max_speed;
      }
    
      public void setMax_speed(double max_speed)
      {
        this.max_speed = max_speed;
      }
      
      @Override
      /**
       * We do not support Frequency atm, so we return power,speed and focus
       */
      public LaserProperty getLaserPropertyForVectorPart() {
        return new PowerSpeedFocusProperty();
      }
    
      protected void writeVectorGCode(VectorPart vp, double resolution) throws UnsupportedEncodingException, IOException {
        for (VectorCommand cmd : vp.getCommandList()) {
          switch (cmd.getType()) {
            case MOVETO:
              int x = cmd.getX();
              int y = cmd.getY();
              move(out, x, y, resolution);
              break;
            case LINETO:
              x = cmd.getX();
              y = cmd.getY();
              line(out, x, y, resolution);
              break;
            case SETPROPERTY:
              PowerSpeedFocusProperty p = (PowerSpeedFocusProperty) cmd.getProperty();
              setPower(p.getPower());
              setSpeed(p.getSpeed());
              setFocus(out, p.getFocus(), resolution);
              break;
          }
        }
      }
      private double currentPower = -1;
      private double currentSpeed = -1;
      private double nextPower = -1;
      private double nextSpeed = -1;
      private double currentFocus = 0;
    
      protected void setSpeed(double speedInPercent) {
        nextSpeed = speedInPercent;
      }
    
      protected void setPower(double powerInPercent) {
        nextPower = powerInPercent;
      }
      
      protected void setFocus(PrintStream out, double focus, double resolution) throws IOException {
        if (currentFocus != focus)
        {
          sendLine("G0 Z%f", Util.px2mm(focus, resolution));
          currentFocus = focus;
        }
      }
    
      protected void move(PrintStream out, int x, int y, double resolution) throws IOException {
        sendLine("G0 X%f Y%f", Util.px2mm(x, resolution), Util.px2mm(y, resolution));
      }
    
      protected void line(PrintStream out, int x, int y, double resolution) throws IOException {
        String append = "";
        if (nextPower != currentPower)
        {
          append += String.format(Locale.US, " S%f", nextPower);
          currentPower = nextPower;
        }
        if (nextSpeed != currentSpeed)
        {
          append += String.format(Locale.US, " F%d", (int) (max_speed*nextSpeed/100.0));
          currentSpeed = nextSpeed;
        }
        sendLine("G1 X%f Y%f"+append, Util.px2mm(x, resolution), Util.px2mm(y, resolution));
      }
    
      private void writeInitializationCode() throws IOException {
        if (preJobGcode != null)
        {
          for (String line : preJobGcode.split(","))
          {
            sendLine(line);
          }
        }
      }
    
      private void writeShutdownCode() throws IOException {
        if (postJobGcode != null)
        {
          for (String line : postJobGcode.split(","))
          {
            sendLine(line);
          }
        }
      }
    
      private BufferedReader in;
      private PrintStream out;
      private Socket socket;
      private CommPort port;
      private CommPortIdentifier portIdentifier;
      
      protected void sendLine(String text, Object... parameters) throws IOException
      {
        out.format(text+LINEEND, parameters);
        if (isWaitForOKafterEachLine())
        {
          if (!"ok".equals(in.readLine()))
          {
            throw new IOException("Smoothieboard did not respond 'ok'");
          }
        }
      }
      
      protected String connect_serial(CommPortIdentifier i, ProgressListener pl) throws PortInUseException, IOException
      {
        pl.taskChanged(this, "opening '"+i.getName()+"'");
        if (i.getPortType() == CommPortIdentifier.PORT_SERIAL)
        {
          try
          {
            port = i.open("VisiCut", 1000);
            out = new PrintStream(port.getOutputStream(), true, "US-ASCII");
            in = new BufferedReader(new InputStreamReader(port.getInputStream()));
            if (getIdentificationLine() != null && getIdentificationLine().length() > 0)
            {
              String line = in.readLine();
              if (!getIdentificationLine().equals(line))
              {
                in.close();
                out.close();
                port.close();
                return ("Does not seem to be a smoothieboard on "+i.getName());
              }
            }
            portIdentifier = i;
            return null;
          }
          catch (PortInUseException e)
          {
            return "Port in use "+i.getName();
          }
          catch (IOException e)
          {
            return "IO Error "+i.getName();
          }
          catch (PureJavaIllegalStateException e)
          {
            return "Could not open "+i.getName();
          }
        }
        else
        {
          return "Not a serial Port "+comport;
        }
      }
      
      protected void connect(ProgressListener pl) throws IOException, PortInUseException, NoSuchPortException
      {
        if (getComport() != null && !getComport().equals(""))
        {
          String error = "No serial port found";
          if (portIdentifier == null && !getComport().equals("auto"))
          {
            portIdentifier = CommPortIdentifier.getPortIdentifier(getComport());
          }
          
          if (portIdentifier != null)
          {//use port identifier we had last time
            error = connect_serial(portIdentifier, pl);
          }
          else
          {
            Enumeration<CommPortIdentifier> e = CommPortIdentifier.getPortIdentifiers();
            while (e.hasMoreElements())
            {
              CommPortIdentifier i = e.nextElement();
              if (i.getPortType() == CommPortIdentifier.PORT_SERIAL)
              {
                error = connect_serial(i, pl);
                if (error == null)
                {
                  break;
                }
              }
            }
          }
          if (error != null)
          {
            throw new IOException(error);
          }
        }
        else
        {
          socket = new Socket();
          socket.connect(new InetSocketAddress(getHost(), 23), 1000);
          in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
          out = new PrintStream(socket.getOutputStream(), true, "US-ASCII");
        }
      }
      
      protected void disconnect() throws IOException
      {
        in.close();
        out.close();
        if (this.socket != null)
        {
          socket.close();
          socket = null;
        }
        else if (this.port != null)
        {
          this.port.close();
          this.port = null;
        }
      }
      
      @Override
      public void sendJob(LaserJob job, ProgressListener pl, List<String> warnings) throws IllegalJobException, Exception {
        pl.progressChanged(this, 0);
        this.currentPower = -1;
        this.currentSpeed = -1;
    
        pl.taskChanged(this, "checking job");
        checkJob(job);
        job.applyStartPoint();
        pl.taskChanged(this, "connecting...");
        connect(pl);
        pl.taskChanged(this, "sending");
        writeInitializationCode();
        pl.progressChanged(this, 20);
        int i = 0;
        int max = job.getParts().size();
        for (JobPart p : job.getParts())
        {
          if (p instanceof RasterPart)
          {
            RasterPart rp = (RasterPart) p;
            LaserProperty black = rp.getLaserProperty();
            LaserProperty white = black.clone();
            white.setProperty("power", 0);
            p = convertRasterToVectorPart((RasterPart) p, black, white,  p.getDPI(), false);
          }
          if (p instanceof VectorPart)
          {
            //TODO: in direct mode use progress listener to indicate progress 
            //of individual job
            writeVectorGCode((VectorPart) p, p.getDPI());
          }
          i++;
          pl.progressChanged(this, 20 + (int) (i*(double) 60/max));
        }
        writeShutdownCode();
        disconnect();
        pl.taskChanged(this, "sent.");
        pl.progressChanged(this, 100);
      }
      private List<Double> resolutions;
    
      @Override
      public List<Double> getResolutions() {
        if (resolutions == null) {
          resolutions = Arrays.asList(new Double[]{
                    500d
                  });
        }
        return resolutions;
      }
      protected double bedWidth = 250;
    
      /**
       * Get the value of bedWidth
       *
       * @return the value of bedWidth
       */
      @Override
      public double getBedWidth() {
        return bedWidth;
      }
    
      /**
       * Set the value of bedWidth
       *
       * @param bedWidth new value of bedWidth
       */
      public void setBedWidth(double bedWidth) {
        this.bedWidth = bedWidth;
      }
      protected double bedHeight = 280;
    
      /**
       * Get the value of bedHeight
       *
       * @return the value of bedHeight
       */
      @Override
      public double getBedHeight() {
        return bedHeight;
      }
    
      /**
       * Set the value of bedHeight
       *
       * @param bedHeight new value of bedHeight
       */
      public void setBedHeight(double bedHeight) {
        this.bedHeight = bedHeight;
      }
      
      private static String[] settingAttributes = new String[]{
        SETTING_BEDWIDTH,
        SETTING_BEDHEIGHT,
        SETTING_COMPORT,
        SETTING_HOST,
        SETTING_IDENTIFICATION_STRING,
        SETTING_MAX_SPEED,
        SETTING_PRE_JOB_GCODE,
        SETTING_POST_JOB_GCODE,
        SETTING_WAIT_FOR_OK
      };
    
      @Override
      public String[] getPropertyKeys() {
        return settingAttributes;
      }
    
      @Override
      public Object getProperty(String attribute) {
        if (SETTING_HOST.equals(attribute)) {
          return this.getHost();
        } else if (SETTING_BEDWIDTH.equals(attribute)) {
          return this.getBedWidth();
        } else if (SETTING_BEDHEIGHT.equals(attribute)) {
          return this.getBedHeight();
        } else if (SETTING_COMPORT.equals(attribute)) {
          return this.getComport();
        } else if (SETTING_HOST.equals(attribute)) {
          return this.getHost();
        } else if (SETTING_IDENTIFICATION_STRING.equals(attribute)) {
          return this.getIdentificationLine();
        } else if (SETTING_MAX_SPEED.equals(attribute)) {
          return this.getMax_speed();
        } else if (SETTING_PRE_JOB_GCODE.equals(attribute)) {
          return this.getPreJobGcode();
        } else if (SETTING_POST_JOB_GCODE.equals(attribute)) {
          return this.getPostJobGcode();
        } else if (SETTING_WAIT_FOR_OK.equals(attribute)) {
          return this.isWaitForOKafterEachLine();
        }
        
        return null;
      }
    
      @Override
      public void setProperty(String attribute, Object value) {
        if (SETTING_HOST.equals(attribute)) {
          this.setHost((String) value);
        } else if (SETTING_BEDWIDTH.equals(attribute)) {
          this.setBedWidth((Double) value);
        } else if (SETTING_BEDHEIGHT.equals(attribute)) {
          this.setBedHeight((Double) value);
        } else if (SETTING_COMPORT.equals(attribute)) {
          this.setComport((String) value);
        } else if (SETTING_HOST.equals(attribute)) {
          this.setHost((String) value);
        } else if (SETTING_IDENTIFICATION_STRING.equals(attribute)) {
          this.setIdentificationLine((String) value);
        } else if (SETTING_MAX_SPEED.equals(attribute)) {
          this.setMax_speed((Double) max_speed);
        } else if (SETTING_PRE_JOB_GCODE.equals(attribute)) {
          this.setPreJobGcode((String) value);
        } else if (SETTING_POST_JOB_GCODE.equals(attribute)) {
          this.setPostJobGcode((String) value);
        } else if (SETTING_WAIT_FOR_OK.equals(attribute)) {
          this.setWaitForOKafterEachLine((Boolean) value);
        }
      }
    
      @Override
      public GenericGcodeDriver clone() {
        GenericGcodeDriver clone = new GenericGcodeDriver();
        clone.copyProperties(this);
        return clone;
      }
    }