• #4 Project log

    TRAN.VINH.QUANG11/07/2024 at 11:12 0 comments

    After making some changes, I have created a python program that can automatically adjust the speed based on the distance. The program will create a new gcode file based on the old gcode file, can customize the distance to change at the variable distv1 (mm) and the speed to decrease at the variable speedv1. The next step is to create an executable file so that it can be used more friendly without having to open visual studio anymore.

    And here is the code of the program:

    """
    
    * The MIT License (MIT)
    
    * Copyright (c) 2024 by TRAN VINH QUANG
    
    *
    
    * Permission is hereby granted, free of charge, to any person obtaining a copy
    
    * of this software and associated documentation files (the "Software"), to deal
    
    * in the Software without restriction, including without limitation the rights
    
    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    
    * copies of the Software, and to permit persons to whom the Software is
    
    * furnished to do so, subject to the following conditions:
    
    * The above copyright notice and this permission notice shall be included in all
    
    * copies or substantial portions of the Software.
    
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    
    * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    
    * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    
    * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    
    * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    
    * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    
    * SOFTWARE.
    
    """
    
    import numpy as np
    
    with open('test-gcode.gcode','r') as file, open("new-gcode.gcode", "w+") as newgcode:
    
    firstpoint = [0,0,0,0,0,"G0"]
    
    secondpoint = [0,0,0,0,0,"G0"]
    
    line = file.readlines()
    
    ptr1 = 1
    
    ptr2 = 1
    
    ptr3 = 1
    
    ptr4 = 1
    
    ptr5 = 1
    
    ptr6 = 1
    
    ptr7 = 1
    
    speedv1 = 2.0
    
    distv1 = 3.0
    
    for i in range(len(line)):
    
    words2 = line[i].split()
    
    firstpoint[0] = secondpoint[0]
    
    firstpoint[1] = secondpoint[1]
    
    firstpoint[2] = secondpoint[2]
    
    firstpoint[3] = secondpoint[3]
    
    firstpoint[4] = secondpoint[4]
    
    if words2:
    
    ptr1 += 1
    
    if words2[0] == 'G1' or words2[0] == 'G0':
    
    ptr2 += 1
    
    for j in range(len(words2)):
    
    if words2[j][0] == 'X':
    
    secondpoint[0] = float(words2[j].replace('X',''))
    
    if words2[j][0] == 'Y':
    
    secondpoint[1] = float(words2[j].replace('Y',''))
    
    if words2[j][0] == 'Z':
    
    secondpoint[2] = float(words2[j].replace('Z',''))
    
    if words2[j][0] == 'F':
    
    secondpoint[3] = float(words2[j].replace('F',''))
    
    if words2[j][0] == 'E':
    
    secondpoint[4] = float(words2[j].replace('E',''))
    
    secondpoint[5] = words2[0]
    
    p1 = np.array([firstpoint[0],firstpoint[1],firstpoint[2]])
    
    p2 = np.array([secondpoint[0],secondpoint[1],secondpoint[2]])
    
    squared_dist = np.sum((p1-p2)**2, axis=0)
    
    dist = np.sqrt(squared_dist)
    
    if words2[j][0] == 'F':
    
    if dist <= distv1:
    
    res = [sub.replace(words2[j], "dF"+str(float(words2[j].replace("F",""))/speedv1)) for sub in words2]
    
    line[i] = ' '.join(res)+"\n"
    
    ptr3 += 1
    
    else:
    
    ptr4 += 1
    
    else:
    
    ptr5 += 1
    
    if dist <= distv1:
    
    if words2[j][0] == 'F':
    
    res = [sub.replace(words2[0], words2[0]+" F"+str(float(words2[0].replace("F",""))/speedv1)) for sub in words2]
    
    line[i] = ' '.join(res)+"\n"
    
    else:
    
    if words2[1][0] == 'F':
    
    res = [sub.replace(words2[1], "F"+str(secondpoint[3]/speedv1) ) for sub in words2]
    
    line[i] = ' '.join(res)+"\n"
    
    else:
    
    res = [sub.replace(words2[1], "F"+str(secondpoint[3]/speedv1) +" "+ words2[1]) for sub in words2]
    
    line[i] = ' '.join(res)+"\n"
    
    else:
    
    if words2[j][0] == 'F':
    
    res = [sub.replace(words2[0], words2[0]+" F600") for sub in words2]
    
    line[i] = ' '.join(res)+"\n"
    
    else:
    
    if words2[1][0] == 'F':
    
    res = [sub.replace(words2[1], "F600" ) for sub in words2]
    
    line[i] = ' '.join(res)+"\n"
    
    else:
    
    res = [sub.replace(words2[1], "F600 "+ words2[1]) for sub in words2]
    
    line[i] = ' '.join(res)+"\n"
    
    else:
    
    ptr6 += 1
    
    else:
    
    ptr7 += 1
    
    newgcode.writelines(line)
    
    

  • ​ #3 Project log

    TRAN.VINH.QUANG11/06/2024 at 02:37 0 comments

    From the first try, there were so many errors and it was so slow, um… I don't want this, I have to try another way.

  • ​ #3 Project log

    TRAN.VINH.QUANG11/02/2024 at 16:28 0 comments

    I tested the first solution. I scanned all the lines in the sample gcode file named test-gcode.gcode. Each line will be separated into separate elements, those elements can be X or Y or Z coordinates or F speed and G1 or G0 code.

    Then load them into arrays containing the coordinates and position of that element.

    I continued to scan the lines in the gcode file, corresponding to each line I scanned the arrays obtained in the previous step to determine the coordinates, speed, G format of the point corresponding to the line being considered and the previous point.

    From there, calculate the distance between these two points, if it is smaller than the predetermined distance, for example 5mm, then proceed to reduce the speed. The value will be assigned to the current line and finally write all the scanned and changed lines to that gcode file.

    Finally, I have a new gcode file with the speed adjusted according to the distance. 

    import numpy as np
    
    lineCounter = 0
    
    with open('test-gcode.gcode','r') as file:  
    
      xlist=[0]
    
      xpos=[0]
    
      ylist=[0]
    
      ypos=[0]
    
      zlist=[0]
    
      zpos=[0]
    
      flist=[0]
    
      fpos=[0]
    
      glist=[0]
    
      gpos=[0]
    
      maxline = 0  
    
      firstpoint = [0,0,0,0,"G0"]          
    
      secondpoint = [0,0,0,0,"G0"]
    
      for num,line in enumerate(file):
    
        maxline = num
    
        line = line.rstrip()
    
        words = line.split()
    
        if words:
    
          if words[0] == 'G1':
    
            glist.append("G1")
    
            gpos.append(num+1)
    
            for i in range(len(words)):
    
              if words[i][0] == 'X':
    
                xlist.append(float(words[i].replace('X','')))
    
                xpos.append(num+1)
    
              if words[i][0] == 'Y':
    
                ylist.append(float(words[i].replace('Y','')))
    
                ypos.append(num+1)
    
              if words[i][0] == 'Z':
    
                zlist.append(float(words[i].replace('Z','')))
    
                zpos.append(num+1)
    
              if words[i][0] == 'F':
    
                flist.append(float(words[i].replace('F','')))
    
                fpos.append(num+1)
    
          if words[0] == 'G0':
    
            glist.append("G0")
    
            gpos.append(num+1)
    
            for i in range(len(words)):
    
              if words[i][0] == 'X':
    
                xlist.append(float(words[i].replace('X','')))
    
                xpos.append(num+1)
    
              if words[i][0] == 'Y':
    
                ylist.append(float(words[i].replace('Y','')))
    
                ypos.append(num+1)
    
              if words[i][0] == 'Z':
    
                zlist.append(float(words[i].replace('Z','')))
    
                zpos.append(num+1)
    
              if words[i][0] == 'F':
    
                flist.append(float(words[i].replace('F','')))
    
                fpos.append(num+1)  
    
    with open('test-gcode.gcode','r') as file2:
    
      lines2 = file2.readlines()  
    
    for j in range(maxline):  
    
      checkline = j
    
      if checkline == j:
    
        for i in range(len(xpos)):
    
          if xpos[i-1] < checkline and xpos[i] > checkline:
    
            firstpoint[0]=xlist[i-1]
    
            secondpoint[0]=xlist[i]
    
          elif xpos[i] == checkline:
    
            firstpoint[0]=xlist[i-1]
    
            secondpoint[0]=xlist[i]
    
        for i in range(len(ypos)):
    
          if ypos[i-1] < checkline and ypos[i] > checkline:
    
            firstpoint[1]=ylist[i-1]
    
            secondpoint[1]=ylist[i]
    
          elif ypos[i] == checkline:
    
            firstpoint[1]=ylist[i-1]
    
            secondpoint[1]=ylist[i]
    
        for i in range(len(zpos)):
    
          if zpos[i-1] < checkline and zpos[i] > checkline:
    
            firstpoint[2]=zlist[i-1]
    
            secondpoint[2]=zlist[i-1]
    
          elif zpos[i] == checkline:
    
            firstpoint[2]=zlist[i]
    
            secondpoint[2]=zlist[i]          
    
        for i in range(len(fpos)):
    
          if fpos[i-1] < checkline and fpos[i] > checkline:
    
            firstpoint[3]=flist[i-1]
    
            secondpoint[3]=flist[i]
    
          elif fpos[i] == checkline:
    
            firstpoint[3]=flist[i-1]
    
            secondpoint[3]=flist[i]
    
        for i in range(len(gpos)):
    
          if gpos[i-1] < checkline and gpos[i] > checkline:
    
            firstpoint[4]=glist[i-1]
    
            secondpoint[4]=glist[i]
    
          elif gpos[i] == checkline:
    
            firstpoint[4]=glist[i-1]
    
            secondpoint[4]=glist[i]
    
        p1 = np.array([firstpoint[0],firstpoint[1],firstpoint[2]])
    
        p2 = np.array([secondpoint[0],secondpoint[1],secondpoint[2]])
    
        squared_dist = np.sum((p1-p2)**2, axis=0)
    
        dist = np.sqrt(squared_dist)
    
        array_line = lines2[checkline-1].split()
    
        for i127 in range(len(array_line)):
    
          if array_line[0] == 'G0' or array_line[0] == 'G1':
    
            for i in range(len(array_line)):
    
              if array_line[i][0] == 'F':
    
                if dist < 5:
    
                  res = [sub.replace(array_line[i], "F"+str(float(array_line[i].replace("F",""))/2.5)) for sub...
    Read more »

  • #2 Project log

    TRAN.VINH.QUANG11/01/2024 at 04:33 0 comments

    I created a gcode file from the previous 3d model and used visual studio to program the python program. Of course the first thing is to read the gcode file with the python program.

  • #1 Project log

    TRAN.VINH.QUANG10/24/2024 at 09:07 0 comments

    3d printers are prone to slipping when running at high speeds. The problem occurs when the travel distance is too short, however, slicing software has very limited speed customization.

    So I wrote a program that automatically adjusts the speed based on the distance traveled. I used python and started creating a 3d file to test.