2012-10-17 18:19:51 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# This file is copied from GCoder.
|
|
|
|
#
|
|
|
|
# GCoder is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# GCoder is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Printrun. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-10-17 17:46:58 +00:00
|
|
|
import sys
|
|
|
|
import re
|
2012-11-08 22:06:59 +00:00
|
|
|
import math
|
2013-05-15 13:28:09 +00:00
|
|
|
import datetime
|
2012-11-08 22:06:59 +00:00
|
|
|
|
2013-05-15 20:44:59 +00:00
|
|
|
gcode_parsed_args = ["x", "y", "e", "f", "z", "p", "i", "j"]
|
2013-05-18 20:09:56 +00:00
|
|
|
gcode_exp = re.compile("\([^\(\)]*\)|[/\*].*\n|[a-z][-+]?[0-9]*\.?[0-9]*")
|
2013-05-18 20:39:35 +00:00
|
|
|
m114_exp = re.compile("\([^\(\)]*\)|[/\*].*\n|[A-Z]:?[-+]?[0-9]*\.?[0-9]*")
|
2013-05-18 19:47:27 +00:00
|
|
|
move_gcodes = ["G0", "G1", "G2", "G3"]
|
2013-05-15 12:45:17 +00:00
|
|
|
|
2013-05-15 17:43:26 +00:00
|
|
|
class Line(object):
|
2013-05-15 12:45:17 +00:00
|
|
|
|
2013-05-15 17:43:26 +00:00
|
|
|
x = None
|
|
|
|
y = None
|
|
|
|
z = None
|
|
|
|
e = None
|
|
|
|
f = None
|
2013-05-15 20:44:59 +00:00
|
|
|
i = None
|
|
|
|
j = None
|
2013-05-15 17:43:26 +00:00
|
|
|
|
|
|
|
relative = False
|
|
|
|
relative_e = False
|
2013-05-15 12:45:17 +00:00
|
|
|
|
2013-05-15 17:43:26 +00:00
|
|
|
raw = None
|
|
|
|
split_raw = None
|
2013-05-15 12:45:17 +00:00
|
|
|
|
2013-05-15 17:43:26 +00:00
|
|
|
command = None
|
|
|
|
is_move = False
|
2013-05-15 12:45:17 +00:00
|
|
|
|
2013-05-15 19:49:12 +00:00
|
|
|
duration = None
|
|
|
|
|
2013-05-15 17:43:26 +00:00
|
|
|
def __init__(self, l):
|
|
|
|
self.raw = l.lower()
|
|
|
|
if ";" in self.raw:
|
|
|
|
self.raw = self.raw.split(";")[0].rstrip()
|
2013-05-18 19:16:09 +00:00
|
|
|
self.split_raw = gcode_exp.findall(self.raw)
|
2013-05-16 17:16:13 +00:00
|
|
|
self.command = self.split_raw[0].upper() if not self.split_raw[0].startswith("n") else self.split_raw[1]
|
2013-05-18 19:47:27 +00:00
|
|
|
self.is_move = self.command in move_gcodes
|
2013-05-15 17:43:26 +00:00
|
|
|
|
|
|
|
def parse_coordinates(self, imperial):
|
2013-05-16 18:32:50 +00:00
|
|
|
# Not a G-line, we don't want to parse its arguments
|
|
|
|
if not self.command[0] == "G":
|
|
|
|
return
|
2013-05-15 17:43:26 +00:00
|
|
|
if imperial:
|
|
|
|
for bit in self.split_raw:
|
|
|
|
code = bit[0]
|
|
|
|
if code in gcode_parsed_args and len(bit) > 1:
|
|
|
|
setattr(self, code, 25.4*float(bit[1:]))
|
|
|
|
else:
|
|
|
|
for bit in self.split_raw:
|
|
|
|
code = bit[0]
|
|
|
|
if code in gcode_parsed_args and len(bit) > 1:
|
|
|
|
setattr(self, code, float(bit[1:]))
|
|
|
|
|
2013-05-15 19:49:12 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return self.raw.upper()
|
2013-05-15 12:45:17 +00:00
|
|
|
|
2012-11-08 22:06:59 +00:00
|
|
|
class Layer(object):
|
2013-05-15 19:49:12 +00:00
|
|
|
|
|
|
|
lines = None
|
|
|
|
duration = None
|
|
|
|
|
|
|
|
def __init__(self, lines):
|
2013-05-15 12:45:17 +00:00
|
|
|
self.lines = lines
|
2013-05-15 19:49:12 +00:00
|
|
|
|
2013-05-15 12:45:17 +00:00
|
|
|
def measure(self):
|
2013-05-15 13:18:36 +00:00
|
|
|
xmin = float("inf")
|
|
|
|
ymin = float("inf")
|
2013-05-15 12:45:17 +00:00
|
|
|
zmin = 0
|
2013-05-15 13:18:36 +00:00
|
|
|
xmax = float("-inf")
|
|
|
|
ymax = float("-inf")
|
|
|
|
zmax = float("-inf")
|
2013-05-15 12:45:17 +00:00
|
|
|
relative = False
|
|
|
|
relative_e = False
|
|
|
|
|
|
|
|
current_x = 0
|
|
|
|
current_y = 0
|
|
|
|
current_z = 0
|
|
|
|
|
|
|
|
for line in self.lines:
|
2013-05-15 17:43:26 +00:00
|
|
|
if line.is_move:
|
2013-05-15 12:45:17 +00:00
|
|
|
x = line.x
|
|
|
|
y = line.y
|
|
|
|
z = line.z
|
|
|
|
|
|
|
|
if line.relative:
|
|
|
|
x = current_x + (x or 0)
|
|
|
|
y = current_y + (y or 0)
|
|
|
|
z = current_z + (z or 0)
|
|
|
|
|
2013-05-15 13:35:48 +00:00
|
|
|
if line.e:
|
|
|
|
if x:
|
|
|
|
xmin = min(xmin, x)
|
|
|
|
xmax = max(xmax, x)
|
|
|
|
if y:
|
|
|
|
ymin = min(ymin, y)
|
|
|
|
ymax = max(ymax, y)
|
2013-05-15 12:45:17 +00:00
|
|
|
if z:
|
2013-05-15 13:35:48 +00:00
|
|
|
zmin = min(zmin, z)
|
|
|
|
zmax = max(zmax, z)
|
2013-05-15 12:45:17 +00:00
|
|
|
|
|
|
|
current_x = x or current_x
|
|
|
|
current_y = y or current_y
|
|
|
|
current_z = z or current_z
|
|
|
|
|
2013-05-18 19:47:27 +00:00
|
|
|
elif line.command == "G92":
|
|
|
|
current_x = line.x or current_x
|
|
|
|
current_y = line.y or current_y
|
|
|
|
current_z = line.z or current_z
|
|
|
|
|
2013-05-15 13:35:48 +00:00
|
|
|
return (xmin, xmax), (ymin, ymax), (zmin, zmax)
|
2012-11-08 22:06:59 +00:00
|
|
|
|
|
|
|
class GCode(object):
|
2013-05-15 19:49:12 +00:00
|
|
|
|
|
|
|
lines = None
|
|
|
|
layers = None
|
2013-05-18 20:23:28 +00:00
|
|
|
all_layers = None
|
2013-05-18 21:07:53 +00:00
|
|
|
idxs = None
|
2013-05-15 19:49:12 +00:00
|
|
|
|
2013-05-15 12:45:17 +00:00
|
|
|
def __init__(self,data):
|
2013-05-15 17:43:26 +00:00
|
|
|
self.lines = [Line(l2) for l2 in
|
|
|
|
(l.strip() for l in data)
|
2013-05-18 20:23:28 +00:00
|
|
|
if l2 and not l2[0] == ";"]
|
2013-05-15 12:45:17 +00:00
|
|
|
self._preprocess()
|
|
|
|
self._create_layers()
|
|
|
|
|
|
|
|
def _preprocess(self):
|
2013-05-15 19:49:12 +00:00
|
|
|
"""Checks for G20, G21, G90 and G91, sets imperial and relative flags"""
|
2013-05-15 12:45:17 +00:00
|
|
|
imperial = False
|
|
|
|
relative = False
|
|
|
|
relative_e = False
|
|
|
|
for line in self.lines:
|
2013-05-18 19:47:27 +00:00
|
|
|
if line.is_move:
|
|
|
|
line.relative = relative
|
|
|
|
line.relative_e = relative_e
|
|
|
|
elif line.command == "G20":
|
2013-05-15 12:45:17 +00:00
|
|
|
imperial = True
|
2013-05-15 17:43:26 +00:00
|
|
|
elif line.command == "G21":
|
2013-05-15 12:45:17 +00:00
|
|
|
imperial = False
|
2013-05-15 17:43:26 +00:00
|
|
|
elif line.command == "G90":
|
2013-05-15 12:45:17 +00:00
|
|
|
relative = False
|
|
|
|
relative_e = False
|
2013-05-15 17:43:26 +00:00
|
|
|
elif line.command == "G91":
|
2013-05-15 12:45:17 +00:00
|
|
|
relative = True
|
|
|
|
relative_e = True
|
2013-05-15 17:43:26 +00:00
|
|
|
elif line.command == "M82":
|
2013-05-15 12:45:17 +00:00
|
|
|
relative_e = False
|
2013-05-15 17:43:26 +00:00
|
|
|
elif line.command == "M83":
|
2013-05-15 12:45:17 +00:00
|
|
|
relative_e = True
|
2013-05-18 19:47:27 +00:00
|
|
|
if line.command[0] == "G":
|
|
|
|
line.parse_coordinates(imperial)
|
2013-05-15 19:49:12 +00:00
|
|
|
|
|
|
|
# FIXME : looks like this needs to be tested with list Z on move
|
2013-05-15 12:45:17 +00:00
|
|
|
def _create_layers(self):
|
2013-05-15 19:49:12 +00:00
|
|
|
layers = {}
|
2013-05-18 20:23:28 +00:00
|
|
|
all_layers = []
|
2013-05-18 21:07:53 +00:00
|
|
|
idxs = []
|
|
|
|
|
|
|
|
layer_id = 0
|
|
|
|
layer_line = 0
|
2013-05-15 12:45:17 +00:00
|
|
|
|
|
|
|
prev_z = None
|
|
|
|
cur_z = 0
|
|
|
|
cur_lines = []
|
|
|
|
for line in self.lines:
|
2013-05-15 17:43:26 +00:00
|
|
|
if line.command == "G92" and line.z != None:
|
2013-05-15 12:45:17 +00:00
|
|
|
cur_z = line.z
|
2013-05-15 17:43:26 +00:00
|
|
|
elif line.is_move:
|
2013-05-15 12:45:17 +00:00
|
|
|
if line.z != None:
|
|
|
|
if line.relative:
|
|
|
|
cur_z += line.z
|
|
|
|
else:
|
|
|
|
cur_z = line.z
|
2013-05-15 19:49:12 +00:00
|
|
|
|
2013-05-15 12:45:17 +00:00
|
|
|
if cur_z != prev_z:
|
2013-05-18 20:23:28 +00:00
|
|
|
all_layers.append(Layer(cur_lines))
|
2013-05-15 19:49:12 +00:00
|
|
|
old_lines = layers.get(prev_z, [])
|
2013-05-15 12:45:17 +00:00
|
|
|
old_lines += cur_lines
|
2013-05-15 19:49:12 +00:00
|
|
|
layers[prev_z] = old_lines
|
2013-05-15 12:45:17 +00:00
|
|
|
cur_lines = []
|
2013-05-18 21:07:53 +00:00
|
|
|
layer_id += 1
|
|
|
|
layer_line = 0
|
2013-05-15 19:49:12 +00:00
|
|
|
|
2013-05-15 12:45:17 +00:00
|
|
|
cur_lines.append(line)
|
2013-05-18 21:07:53 +00:00
|
|
|
idxs.append((layer_id, layer_line))
|
|
|
|
layer_line += 1
|
2013-05-15 12:45:17 +00:00
|
|
|
prev_z = cur_z
|
2013-05-15 19:49:12 +00:00
|
|
|
|
2013-05-18 20:23:28 +00:00
|
|
|
if cur_lines:
|
|
|
|
all_layers.append(Layer(cur_lines))
|
|
|
|
old_lines = layers.pop(prev_z, [])
|
|
|
|
old_lines += cur_lines
|
|
|
|
layers[prev_z] = old_lines
|
2013-05-15 12:45:17 +00:00
|
|
|
|
2013-05-15 19:49:12 +00:00
|
|
|
for idx in layers.keys():
|
|
|
|
cur_lines = layers[idx]
|
2013-05-15 12:45:17 +00:00
|
|
|
has_movement = False
|
2013-05-15 19:49:12 +00:00
|
|
|
for l in layers[idx]:
|
2013-05-15 17:43:26 +00:00
|
|
|
if l.is_move and l.e != None:
|
2013-05-15 12:45:17 +00:00
|
|
|
has_movement = True
|
|
|
|
break
|
|
|
|
if has_movement:
|
2013-05-18 20:23:28 +00:00
|
|
|
layers[idx] = Layer(cur_lines)
|
2013-05-15 19:49:12 +00:00
|
|
|
else:
|
|
|
|
del layers[idx]
|
|
|
|
|
2013-05-18 20:23:28 +00:00
|
|
|
self.all_layers = all_layers
|
2013-05-15 19:49:12 +00:00
|
|
|
self.layers = layers
|
2013-05-18 21:07:53 +00:00
|
|
|
self.idxs = idxs
|
2013-05-15 12:45:17 +00:00
|
|
|
|
|
|
|
def num_layers(self):
|
|
|
|
return len(self.layers)
|
|
|
|
|
|
|
|
def measure(self):
|
2013-05-15 13:18:36 +00:00
|
|
|
xmin = float("inf")
|
|
|
|
ymin = float("inf")
|
2013-05-15 12:45:17 +00:00
|
|
|
zmin = 0
|
2013-05-15 13:18:36 +00:00
|
|
|
xmax = float("-inf")
|
|
|
|
ymax = float("-inf")
|
|
|
|
zmax = float("-inf")
|
2013-05-15 12:45:17 +00:00
|
|
|
|
2013-05-15 19:49:12 +00:00
|
|
|
for l in self.layers.values():
|
2013-05-15 17:43:26 +00:00
|
|
|
(xm, xM), (ym, yM), (zm, zM) = l.measure()
|
|
|
|
xmin = min(xm, xmin)
|
|
|
|
xmax = max(xM, xmax)
|
|
|
|
ymin = min(ym, ymin)
|
|
|
|
ymax = max(yM, ymax)
|
|
|
|
zmin = min(zm, zmin)
|
|
|
|
zmax = max(zM, zmax)
|
2013-05-15 12:45:17 +00:00
|
|
|
|
|
|
|
self.xmin = xmin
|
|
|
|
self.xmax = xmax
|
|
|
|
self.ymin = ymin
|
|
|
|
self.ymax = ymax
|
|
|
|
self.zmin = zmin
|
|
|
|
self.zmax = zmax
|
|
|
|
self.width = xmax - xmin
|
|
|
|
self.depth = ymax - ymin
|
|
|
|
self.height = zmax - zmin
|
|
|
|
|
|
|
|
def filament_length(self):
|
2013-05-18 18:20:53 +00:00
|
|
|
total_e = 0
|
|
|
|
max_e = 0
|
2013-05-15 12:45:17 +00:00
|
|
|
cur_e = 0
|
|
|
|
|
|
|
|
for line in self.lines:
|
2013-05-15 13:24:33 +00:00
|
|
|
if line.e == None:
|
2013-05-15 13:19:23 +00:00
|
|
|
continue
|
2013-05-18 19:47:27 +00:00
|
|
|
if line.is_move:
|
2013-05-15 12:45:17 +00:00
|
|
|
if line.relative_e:
|
2013-05-15 13:19:23 +00:00
|
|
|
total_e += line.e
|
2013-05-15 12:45:17 +00:00
|
|
|
else:
|
2013-05-15 13:19:23 +00:00
|
|
|
total_e += line.e - cur_e
|
2013-05-15 12:45:17 +00:00
|
|
|
cur_e = line.e
|
2013-05-18 18:20:53 +00:00
|
|
|
max_e = max(max_e, total_e)
|
2013-05-18 19:47:27 +00:00
|
|
|
elif line.command == "G92":
|
|
|
|
cur_e = line.e
|
2013-05-15 13:19:23 +00:00
|
|
|
|
2013-05-18 18:20:53 +00:00
|
|
|
return max_e
|
2012-10-17 17:46:58 +00:00
|
|
|
|
2013-05-15 19:49:12 +00:00
|
|
|
def estimate_duration(self):
|
2013-05-15 13:23:46 +00:00
|
|
|
lastx = lasty = lastz = laste = lastf = 0.0
|
|
|
|
x = y = z = e = f = 0.0
|
|
|
|
currenttravel = 0.0
|
|
|
|
totaltravel = 0.0
|
|
|
|
moveduration = 0.0
|
|
|
|
totalduration = 0.0
|
|
|
|
acceleration = 1500.0 #mm/s/s ASSUMING THE DEFAULT FROM SPRINTER !!!!
|
|
|
|
layerduration = 0.0
|
|
|
|
layerbeginduration = 0.0
|
|
|
|
layercount = 0
|
|
|
|
#TODO:
|
|
|
|
# get device caps from firmware: max speed, acceleration/axis (including extruder)
|
|
|
|
# calculate the maximum move duration accounting for above ;)
|
2013-05-15 19:49:12 +00:00
|
|
|
zs = self.layers.keys()
|
|
|
|
zs.sort()
|
|
|
|
for z in zs:
|
|
|
|
layer = self.layers[z]
|
|
|
|
for line in layer.lines:
|
2013-05-18 19:47:27 +00:00
|
|
|
if line.command not in ["G1", "G0", "G4"]:
|
2013-05-15 19:49:12 +00:00
|
|
|
continue
|
|
|
|
if line.command == "G4":
|
|
|
|
moveduration = line.p
|
|
|
|
if not moveduration:
|
2013-05-15 13:23:46 +00:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
moveduration /= 1000.0
|
2013-05-18 19:47:27 +00:00
|
|
|
else:
|
2013-05-15 19:49:12 +00:00
|
|
|
x = line.x if line.x != None else lastx
|
|
|
|
y = line.y if line.y != None else lasty
|
|
|
|
e = line.e if line.e != None else laste
|
2013-05-15 20:44:59 +00:00
|
|
|
f = line.f / 60.0 if line.f != None else lastf # mm/s vs mm/m => divide by 60
|
2013-05-15 19:49:12 +00:00
|
|
|
|
2013-05-15 13:23:46 +00:00
|
|
|
# given last feedrate and current feedrate calculate the distance needed to achieve current feedrate.
|
|
|
|
# if travel is longer than req'd distance, then subtract distance to achieve full speed, and add the time it took to get there.
|
|
|
|
# then calculate the time taken to complete the remaining distance
|
|
|
|
|
2013-05-15 19:49:12 +00:00
|
|
|
currenttravel = math.hypot(x - lastx, y - lasty)
|
|
|
|
# FIXME: review this better
|
|
|
|
# this looks wrong : there's little chance that the feedrate we'll decelerate to is the previous feedrate
|
|
|
|
# shouldn't we instead look at three consecutive moves ?
|
|
|
|
distance = 2 * abs(((lastf + f) * (f - lastf) * 0.5) / acceleration) # multiply by 2 because we have to accelerate and decelerate
|
|
|
|
if distance <= currenttravel and lastf + f != 0 and f != 0:
|
|
|
|
# Unsure about this formula -- iXce reviewing this code
|
|
|
|
moveduration = 2 * distance / (lastf + f)
|
2013-05-15 13:23:46 +00:00
|
|
|
currenttravel -= distance
|
|
|
|
moveduration += currenttravel/f
|
|
|
|
else:
|
2013-05-15 19:49:12 +00:00
|
|
|
moveduration = math.sqrt(2 * distance / acceleration) # probably buggy : not taking actual travel into account
|
2013-05-15 13:23:46 +00:00
|
|
|
|
|
|
|
totalduration += moveduration
|
|
|
|
|
|
|
|
lastx = x
|
|
|
|
lasty = y
|
|
|
|
laste = e
|
|
|
|
lastf = f
|
|
|
|
|
2013-05-15 19:49:12 +00:00
|
|
|
layer.duration = totalduration - layerbeginduration
|
|
|
|
layerbeginduration = totalduration
|
|
|
|
|
|
|
|
return "%d layers, %s" % (len(self.layers), str(datetime.timedelta(seconds = int(totalduration))))
|
2012-10-17 17:46:58 +00:00
|
|
|
|
|
|
|
def main():
|
2013-05-15 12:45:17 +00:00
|
|
|
if len(sys.argv) < 2:
|
|
|
|
print "usage: %s filename.gcode" % sys.argv[0]
|
|
|
|
return
|
2013-05-18 19:47:27 +00:00
|
|
|
|
2013-05-15 19:49:12 +00:00
|
|
|
gcode = GCode(open(sys.argv[1]))
|
2013-05-15 17:43:26 +00:00
|
|
|
|
2013-05-15 12:45:17 +00:00
|
|
|
gcode.measure()
|
2012-10-17 17:46:58 +00:00
|
|
|
|
2013-05-15 12:45:17 +00:00
|
|
|
print "Dimensions:"
|
|
|
|
print "\tX: %0.02f - %0.02f (%0.02f)" % (gcode.xmin,gcode.xmax,gcode.width)
|
|
|
|
print "\tY: %0.02f - %0.02f (%0.02f)" % (gcode.ymin,gcode.ymax,gcode.depth)
|
|
|
|
print "\tZ: %0.02f - %0.02f (%0.02f)" % (gcode.zmin,gcode.zmax,gcode.height)
|
|
|
|
print "Filament used: %0.02fmm" % gcode.filament_length()
|
|
|
|
print "Number of layers: %d" % gcode.num_layers()
|
2013-05-18 17:52:05 +00:00
|
|
|
print "Estimated duration: %s" % gcode.estimate_duration()
|
2012-11-08 22:06:59 +00:00
|
|
|
|
2012-10-17 17:46:58 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2013-05-15 12:45:17 +00:00
|
|
|
main()
|