Skip to content

Commit 43aef3c

Browse files
committed
rudimentary auto extrusion, needs correction for g2/g3
1 parent a8bbe84 commit 43aef3c

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/gcodepy/gcode.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from typing import Callable, Tuple, Union
2+
from . import util
23

3-
# flow calculations
4+
# circumference calcs for arcs
45
# minimize feedrate output
6+
# draw absolute
57
class Gcode:
68
def __init__(
79
self,
@@ -10,13 +12,17 @@ def __init__(
1012
line_width: float = 0.4,
1113
filament_width: float = 1.75,
1214
home_position: Tuple[float, float, float] = (0, 0, 0),
15+
extrusion_length_calculator: Callable[
16+
[float, float, float, float], float
17+
] = util.calculate_extrusion_length,
1318
) -> None:
1419
self.filename = filename
1520
self.file = open(filename, "w")
1621
self.layer_height = layer_height
1722
self.line_width = line_width
1823
self.filament_width = filament_width
1924
self.home_pos = home_position
25+
self.extrusion_length_calculator = extrusion_length_calculator
2026
self.pos = [None] * 3
2127
self.e = None
2228

@@ -39,6 +45,15 @@ def get_z(self) -> float:
3945
def get_e(self) -> float:
4046
return self.e
4147

48+
def set_layer_height(self, layer_height: float):
49+
self.layer_height = layer_height
50+
51+
def set_line_width(self, line_width: float):
52+
self.line_width = line_width
53+
54+
def set_filament_width(self, filament_width: float):
55+
self.filament_width = filament_width
56+
4257
def zero_extruder(self):
4358
self.file.write("G92 E0\n")
4459
self.e = 0
@@ -114,7 +129,12 @@ def draw(
114129
self, delta: Tuple[float, float, float], e: float = None, feedrate: int = 2400
115130
):
116131
if e is None:
117-
e = 0 # REPLACE
132+
e = self.extrusion_length_calculator(
133+
util.dist(delta),
134+
self.line_width,
135+
self.layer_height,
136+
self.filament_width,
137+
)
118138
self.pos[0] += delta[0]
119139
self.pos[1] += delta[1]
120140
self.pos[2] += delta[2]
@@ -133,7 +153,12 @@ def draw_arc(
133153
feedrate: int = 2400,
134154
):
135155
if e is None:
136-
e = 0 # REPLACE
156+
e = self.extrusion_length_calculator(
157+
util.dist(delta), # REPLACE
158+
self.line_width,
159+
self.layer_height,
160+
self.filament_width,
161+
)
137162
out = "G2" if clockwise else "G3"
138163
out += f" F{feedrate}"
139164
self.pos[0] += delta[0]
@@ -158,7 +183,12 @@ def draw_arc_r(
158183
feedrate: int = 2400,
159184
):
160185
if e is None:
161-
e = 0 # REPLACE
186+
e = self.extrusion_length_calculator(
187+
util.dist(delta), # REPLACE
188+
self.line_width,
189+
self.layer_height,
190+
self.filament_width,
191+
)
162192
if delta[0] == 0.0 and delta[1] == 0.0:
163193
raise ValueError("Both x and y cannot be 0. Make sure to set at least one!")
164194
out = "G2" if clockwise else "G3"

src/gcodepy/util.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import math
2+
from typing import Tuple
3+
4+
5+
def dist(delta: Tuple[float, float, float]) -> float:
6+
return math.sqrt(sum(i ** 2 for i in delta))
7+
8+
9+
def calculate_extrusion_length(
10+
path_length: float,
11+
line_width: float,
12+
layer_height: float,
13+
filament_width: float,
14+
) -> float:
15+
# uses math from https://manual.slic3r.org/advanced/flow-math
16+
flat_area = (line_width - layer_height) * layer_height
17+
round_area = math.pi * (layer_height / 2) ** 2
18+
ext_area = flat_area + round_area
19+
ext_volume = ext_area * path_length
20+
return ext_volume / (math.pi * (filament_width / 2) ** 2)

0 commit comments

Comments
 (0)