-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacklash_compensation.py
71 lines (63 loc) · 2.48 KB
/
backlash_compensation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import re
input_file_path = 'input.gcode'
output_file_path = 'output.gcode'
# Initialize variables
currentX = 0
currentY = 0
targetX = 0
targetY = 0
Xdirection = 0
Ydirection = 0
previousXdirection = 0
previousYdirection = 0
Xbacklash = 0.1 # Example value for x-axis backlash compensation
Ybacklash = 0.1 # Example value for y-axis backlash compensation
comp = False
with open(input_file_path, 'r') as input_file, open(output_file_path, 'w') as output_file:
for line in input_file:
# Assuming G-code commands are in the format "G0 X12.5 Y7.3" or similar
if line.startswith('G0 ') or line.startswith('G1 '):
try:
targetX = float(re.findall(r'X(\d+\.\d+|\d+)', line)[0])
except IndexError:
targetX = currentX
try:
targetY = float(re.findall(r'Y(\d+\.\d+|\d+)', line)[0])
except IndexError:
targetY = currentY
# Check direction changes
Xdirection = 1 if targetX > currentX else -1 if targetX < currentX else 0
Ydirection = 1 if targetY > currentY else -1 if targetY < currentY else 0
if Xdirection > previousXdirection and Xdirection!= 0:
compX = round(currentX + Xbacklash, 3)
comp = True
elif Xdirection < previousXdirection and Xdirection!= 0:
compX = round(currentX - Xbacklash, 3)
comp = True
else:
compX = currentX
if Ydirection > previousYdirection and Ydirection!= 0:
compY = round(currentY + Ybacklash, 3)
comp = True
elif Ydirection < previousYdirection and Ydirection!= 0:
compY = round(currentY - Ybacklash, 3)
comp = True
else:
compY = currentY
if comp:
compLines = [f"G0 x{compX} y{compY} ; Compensation line", f"G92 x{currentX} y{currentY} ; Compensation line"]
newLine = f"{compLines[0]}\n{compLines[1]}\n{line}"
else:
newLine = line
currentX = targetX
currentY = targetY
previousXdirection = Xdirection
previousYdirection = Ydirection
comp = False
else:
newLine = line
# Write the modified line to the output file
output_file.write(newLine)
# Close the files
input_file.close()
output_file.close()