3D printers are prone to slipping when running at high speeds. The problem occurs when the travel distance is too short, however, the slicing software has a very limited ability to adjust the speed according to the travel distance.
So I wrote a program that automatically adjusts the speed based on the travel distance.
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 = 0with 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 inenumerate(file):
maxline = num
line = line.rstrip()
words = line.split()
if words:
if words[0] == 'G1':
glist.append("G1")
gpos.append(num+1)
for i inrange(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 inrange(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 inrange(maxline):
checkline = j
if checkline == j:
for i inrange(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 inrange(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 inrange(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 inrange(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 inrange(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 inrange(len(array_line)):
if array_line[0] == 'G0' or array_line[0] == 'G1':
for i inrange(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...
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.
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.