Close

​ #3 Project log

A project log for Create a program to speed up 3D printing

Create a program to speed up 3D printing for FDM 3D printer

tranvinhquangTRAN.VINH.QUANG 11/02/2024 at 16:280 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 in array_line]

              lines2[checkline-1] = ' '.join(res)+"\n"

with open("test-gcode.gcode", "w") as f:

  f.writelines(lines2)

And now I will proceed to test run...

Discussions