Commit eba337cf authored by Jayant Khatkar's avatar Jayant Khatkar
Browse files

Merge branch 'master' into feature/layer-interface

parents 639a5572 1f90b55d
......@@ -15,6 +15,7 @@ class GCode:
self.end_script_file = "" # empty string uses default values
self.current_layer = 0
self.extr_dist_tot = 0.0
self.cumulative_extr_dist_tot = 0.0
self.pl_arr = pl_arr
self.config_extrusion_modifier = settings.extrusion_modifier # kept constance for reference
self.extrusion_modifier = settings.extrusion_modifier # can be changed
......@@ -82,6 +83,14 @@ class GCode:
self.curr_path_layer_h = gn.closest_dist(self.previous_layer_proximity_obj, points)
self.historical_curr_layer_h = np.append(self.historical_curr_layer_h, self.curr_path_layer_h)
def reset_extr_dist(self):
"""
Resets the current extrusion distance internally, and appends G92 E0
"""
self.cumulative_extr_dist_tot += self.extr_dist_tot
self.extr_dist_tot = 0.0
self.gcode_str += "G92 E0 \n"
def change_layer(self):
"""
Performs necessary actions on layer change, keeps track of current layer
......@@ -97,6 +106,7 @@ class GCode:
self.historical_layer_h.setdefault(self.current_layer)
self.historical_layer_h[self.current_layer] = self.historical_curr_layer_h.tolist()
self.historical_curr_layer_h = np.empty(0, dtype=object)
self.reset_extr_dist()
def append_layer_change_indicator(self):
"""
......@@ -314,7 +324,7 @@ class GCode:
Returns the filament used and estimated weight based on the current value of total extrusion distance
"""
filament_density = 1.24 / 1000 # 1.24g/cm3 for PLA
filament_used = round(self.extr_dist_tot, 1)
filament_used = round(self.cumulative_extr_dist_tot, 1)
filament_volume = ((self.filament_diam / 2) ** 2) * np.pi * filament_used
est_weight = round(filament_density * filament_volume, 3)
return filament_used, est_weight
......
......@@ -4,3 +4,4 @@ from .ParsedSettings import ParsedSettings
from .GCode import GCode
from .Infill import Infill
from .Constants import prec
from . import accOptimiser
import numpy as np
class GCodeReader:
X = 0
Y = 0
Z = 0
E = 0
F = 0
def __init__(self, file):
"""
"""
self.file = file
self.line = GCodeLine(self)
def parse_file(self, callback):
"""
"""
with open(self.file, 'r') as f:
lines = f.readlines()
for line in lines:
self.parse_line(line, callback)
def parse_line(self, line, callback):
"""
"""
stripped_line = line.split(';')[0] # strip comments
self.line = GCodeLine(self)
self.line.raw_line = stripped_line
args = self.line.raw_line.split(" ")
self.line.code = args[0]
args.pop(0)
for i, arg in enumerate(args):
if len(arg) < 2:
continue
self.line.line_args[arg[0]] = float(arg[1:])
callback(self.line)
if self.line.code == 'G0' or self.line.code == 'G1':
self.X = self.line.new_x()
self.Y = self.line.new_y()
self.Z = self.line.new_z()
self.E = self.line.new_e()
self.F = self.line.new_f()
class GCodeLine:
raw_line = None
line_args = dict()
code = str()
def __init__(self, reader):
"""
"""
self.reader = reader
def line_has(self, arg):
"""
"""
return True if arg in self.line_args else False
def new_x(self):
"""
"""
return self.line_args['X'] if self.line_has('X') else self.reader.X
def new_y(self):
"""
"""
return self.line_args['Y'] if self.line_has('Y') else self.reader.Y
def new_z(self):
"""
"""
return self.line_args['Z'] if self.line_has('Z') else self.reader.Z
def new_e(self):
"""
"""
return self.line_args['E'] if self.line_has('E') else self.reader.E
def new_f(self):
"""
"""
return self.line_args['F'] if self.line_has('F') else self.reader.F
def dist_x(self):
"""
"""
return self.new_x() - self.reader.X
def dist_y(self):
"""
"""
return self.new_y() - self.reader.Y
def dist_z(self):
"""
"""
return self.new_z() - self.reader.Z
def dist_e(self):
"""
"""
return self.new_e() - self.reader.E
def dist_xy(self):
"""
"""
x = self.dist_x()
y = self.dist_y()
return np.sqrt(x**2 + y**2)
def dist_xyz(self):
"""
"""
x = self.dist_x()
y = self.dist_y()
z = self.dist_z()
return np.sqrt(x**2 + y**2 + z**2)
from . import GCodeReader
class GCodeTimeEstimator(GCodeReader):
def __init__(self, file):
"""
Estimate print time by parsing the given GCode
"""
super().__init__(file)
self.time = 0
self.acceleration_xy = 500
self.acceleration_e = 25
self.acceleration_z = 5
def parse(self):
"""
"""
super().parse_file(lambda line: self._parser(line))
print(self.time/60)
def _parser(self, line):
"""
Parse GCode file and calculate time estimates line-by-line
"""
if line.code == 'G1' or line.code == 'G0':
dist = line.dist_xy()
vel_f = line.new_f()/60
if dist > 0:
self.time += self._acc_move(dist, vel_f, self.acceleration_xy)
else:
self.time += self._acc_move(abs(line.dist_e()), vel_f, self.acceleration_e)
self.time += self._acc_move(abs(line.dist_z()), vel_f, self.acceleration_z)
def _acc_move(self, dist, vel, acc):
"""
Calculate the time taken for a move
"""
half_dist = dist/2.0
t_i = vel/acc
dx_i = 0.5*vel*t_i
t = 0.0
if half_dist >= dx_i:
half_dist -= 0.5*vel*t_i
t += t_i
t += half_dist/vel
return t*2.0
from .GCodeReader import GCodeReader
from .GCodeTimeEstimator import GCodeTimeEstimator
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment