砂3D打印机切片问题

时间:2015-08-06 16:45:27

标签: interface arrays indexing

对于我的博士论文,我正在建立一个基于特温特大学松散的3D打印机:

http://pwdr.github.io/

到目前为止,一切都相当顺利。硬件部分花费的时间比预期的要长,但电子设备让我感到有些害怕。我可以成功地对所有电机进行微动,并且机械地完成所有应该做的事情。

但是,现在我正在软件方面工作,我感到头痛。

Pwder人员编写了一个代码,该代码使用Processing来获取.STL文件并将其分层。运行代码后,将打开Processing GUI,我可以在其中加载模型。模型加载很好(我正在使用犹他州茶壶),并表明它将需要149层。

在点击“转换”时,程序应该获取.STL文件并将其分层,然后写入一个文本文件,然后我可以上传到SD卡。然后打印机将直接从SD卡打印。

然而,当我点击“转换”时,我得到一个“数组索引超出界限”错误。我不太清楚这意味着什么......任何人都可以启发我吗?

下面可以找到代码,以及错误图片。

谢谢。

enter image description here

    // Convert the graphical output of the sliced STL into a printable         binary format. 
    // The bytes are read by the Arduino firmware

    PrintWriter output, outputUpper;

    int loc;
    int LTR = 0;
    int lowernozzles = 8;
    int uppernozzles = 4;
    int nozzles = lowernozzles+uppernozzles;

    int printXcoordinate = 120+280;  // Left margin 120
    int printYcoordinate = 30+190;  // Top margin 30
    int printWidth = 120;        // Total image width 650
    int printHeight = 120;      // Total image height 480

    int layer_size = printWidth * printHeight/nozzles * 2;

    void convertModel() {

      // Create config file for the printer, trailing comma for convenience  
      output = createWriter("PWDR/PWDRCONF.TXT");        output.print(printWidth+","+printHeight/nozzles+","+maxSlices+","+inkSaturation+ ",");
      output.flush();
      output.close();

      int index = 0;
      byte[] print_data = new byte[layer_size * 2];

      // Steps of 12 nozzles in Y direction
      for (int y = printYcoordinate; y < printYcoordinate+printHeight;  y=y+nozzles ) {
        // Set a variable to know wheter we're moving LTR of RTL
        LTR++;  
        // Step in X direction  
        for (int x = 0; x < printWidth; x++) {

          // Clear the temp strings
          String[] LowerStr = {""};
          String LowerStr2 = "";
          String[] UpperStr = {""};
          String UpperStr2 = "";

          // For every step in Y direction, sample the 12 nozzles
          for ( int i=0; i<nozzles; i++) {
            // Calculate the location in the pixel array, use total window  width!
            // Use the LTR to determine the direction

            if (LTR % 2 == 1){
              loc = printXcoordinate + printWidth - x + (y+i) * width;
            } else {
              loc = printXcoordinate + x + (y+i) * width;
            }

            if (brightness(pixels[loc]) < 100) {

              // Write a zero when the pixel is white (or should be white, as the preview is inverted)
              if (i<uppernozzles) {
                UpperStr = append(UpperStr, "0");
              } else {
                LowerStr = append(LowerStr, "0");
              }
            } else {
              // Write a one when the pixel is black     
              if (i<uppernozzles) {
                UpperStr = append(UpperStr, "1");                  
              } else {
                LowerStr = append(LowerStr, "1");
              }
            }
          } 

          LowerStr2 = join(LowerStr, "");
          print_data[index] = byte(unbinary(LowerStr2));
          index++;

          UpperStr2 = join(UpperStr, "");
          print_data[index] = byte(unbinary(UpperStr2));
          index++;
        }
      }

      if (sliceNumber >= 1 && sliceNumber < 10){
        String DEST_FILE = "PWDR/PWDR000"+sliceNumber+".DAT";
        File dataFile = sketchFile(DEST_FILE);
        if (dataFile.exists()){
          dataFile.delete();
        }
        saveBytes(DEST_FILE, print_data); // Savebytes directly causes bug  under Windows
      } else if (sliceNumber >= 10 && sliceNumber < 100){
        String DEST_FILE = "PWDR/PWDR00"+sliceNumber+".DAT";
        File dataFile = sketchFile(DEST_FILE);
        if (dataFile.exists()){
          dataFile.delete();
        }
        saveBytes(DEST_FILE, print_data); // Savebytes directly causes bug   under Windows
      } else if (sliceNumber >= 100 && sliceNumber < 1000){
        String DEST_FILE = "PWDR/PWDR0"+sliceNumber+".DAT";
        File dataFile = sketchFile(DEST_FILE);
        if (dataFile.exists()){
          dataFile.delete();
        }
        saveBytes(DEST_FILE, print_data); // Savebytes directly causes bug  under Windows
      } else if (sliceNumber >= 1000) {
        String DEST_FILE = "PWDR/PWDR"+sliceNumber+".DAT";
        File dataFile = sketchFile(DEST_FILE);
        if (dataFile.exists()){
          dataFile.delete();
        }
        saveBytes(DEST_FILE, print_data); // Savebytes directly causes bug  under Windows
      }

      sliceNumber++;
      println(sliceNumber);
    }

1 个答案:

答案 0 :(得分:0)

正在发生的事情是print_data小于索引。 (例如,如果index是123,但print_data只有122个元素。)

print_data的大小是layer_size * 2或printWidth * printHeight / nozzle * 4或4800

索引的最大尺寸为printHeight / nozzle * 2 * printWidth或20 * 120或2400。

这似乎没问题,所以我可能错过了一些东西,它似乎将数据放在元素4800中,这很奇怪。我建议使用一堆print语句来获取print_data和索引的大小。