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)
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.